Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix focusing the first/last item in a qtquickwidget #11715

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/gui/accountsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ AccountSettings::AccountSettings(const AccountStatePtr &accountState, QWidget *p

_sortModel = weightedModel;

ui->quickWidget->rootContext()->setContextProperty(QStringLiteral("ctx"), this);
ui->quickWidget->engine()->addImageProvider(QStringLiteral("space"), new Spaces::SpaceImageProvider(_accountState->account()));
QmlUtils::initQuickWidget(ui->quickWidget, QUrl(QStringLiteral("qrc:/qt/qml/org/ownCloud/gui/qml/FolderDelegate.qml")));
QmlUtils::initQuickWidget(ui->quickWidget, QUrl(QStringLiteral("qrc:/qt/qml/org/ownCloud/gui/qml/FolderDelegate.qml")), this);

connect(FolderMan::instance(), &FolderMan::folderListChanged, _model, &FolderStatusModel::resetFolders);
ui->connectLabel->clear();
Expand Down
8 changes: 7 additions & 1 deletion src/gui/accountsettings.ui
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
</item>
</layout>
</widget>
<widget class="QQuickWidget" name="quickWidget"/>
<widget class="OCC::QmlUtils::OCQuickWidget" name="quickWidget"/>
</widget>
</item>
</layout>
Expand All @@ -156,6 +156,12 @@
<header>QProgressIndicator.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>OCC::QmlUtils::OCQuickWidget</class>
<extends>QWidget</extends>
<header>gui/qmlutils.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources>
<include location="../resources/client.qrc"/>
Expand Down
17 changes: 17 additions & 0 deletions src/gui/qml/AccountBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,25 @@ import org.ownCloud.libsync 1.0

Pane {
id: bar
readonly property SettingsDialog settingsDialog: ocContext
Accessible.name: qsTr("Navigation bar")

Connections {
target: settingsDialog

function onFocusFirst() {
if (accountButtons.count === 0) {
addAccountButton.forceActiveFocus(Qt.TabFocusReason);
} else {
accountButtons.itemAt(0).forceActiveFocus(Qt.TabFocusReason);
}
}

function onFocusLast() {
quitButton.forceActiveFocus(Qt.TabFocusReason);
}
}

RowLayout {
anchors.fill: parent

Expand Down
18 changes: 17 additions & 1 deletion src/gui/qml/FolderDelegate.qml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,27 @@ Pane {
id: folderSyncPanel
// TODO: not cool
readonly property real normalSize: 170
readonly property AccountSettings accountSettings: ctx
readonly property AccountSettings accountSettings: ocContext

Accessible.role: Accessible.List
Accessible.name: qsTr("Folder Sync")

Connections {
target: accountSettings

function onFocusFirst() {
listView.currentIndex = 0;
}

function onFocusLast() {
if (addSyncButton.enabled) {
addSyncButton.forceActiveFocus(Qt.TabFocusReason);
} else {
listView.currentIndex = listView.count - 1;
}
}
}

ColumnLayout {
anchors.fill: parent
ScrollView {
Expand Down
23 changes: 22 additions & 1 deletion src/gui/qmlutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#include "resources/resources.h"

#include <QMessageBox>
#include <QQmlContext>
#include <QQuickWidget>

void OCC::QmlUtils::initQuickWidget(QQuickWidget *widget, const QUrl &src)
void OCC::QmlUtils::initQuickWidget(OCQuickWidget *widget, const QUrl &src, QObject *ocContext)
{
widget->rootContext()->setContextProperty(QStringLiteral("ocContext"), ocContext);
widget->engine()->addImageProvider(QStringLiteral("ownCloud"), new OCC::Resources::CoreImageProvider());
widget->setResizeMode(QQuickWidget::SizeRootObjectToView);
widget->setSource(src);
Expand All @@ -30,4 +32,23 @@ void OCC::QmlUtils::initQuickWidget(QQuickWidget *widget, const QUrl &src)
box->exec();
qFatal("A qml error occured %s", qPrintable(QDebug::toString(widget->errors())));
}

// string based connects as they are provided by OC_DECLARE_WIDGET_FOCUS and not inherited
QObject::connect(widget, SIGNAL(focusFirst()), ocContext, SIGNAL(focusFirst()));
QObject::connect(widget, SIGNAL(focusLast()), ocContext, SIGNAL(focusLast()));
}

void OCC::QmlUtils::OCQuickWidget::focusInEvent(QFocusEvent *event)
{
switch (event->reason()) {
case Qt::TabFocusReason:
Q_EMIT focusFirst();
break;
case Qt::BacktabFocusReason:
Q_EMIT focusLast();
break;
default:
break;
}
QQuickWidget::focusInEvent(event);
}
22 changes: 19 additions & 3 deletions src/gui/qmlutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
*/

#pragma once
#include <QtQuickWidgets/QQuickWidget>


class QQuickWidget;
class QUrl;

#define OC_DECLARE_WIDGET_FOCUS \
Expand All @@ -29,9 +28,26 @@ public:
focusPreviousChild(); \
} \
\
Q_SIGNALS: \
void focusFirst(); \
void focusLast(); \
\
private:

namespace OCC::QmlUtils {
class OCQuickWidget : public QQuickWidget
{
Q_OBJECT
public:
using QQuickWidget::QQuickWidget;

Q_SIGNALS:
void focusFirst();
void focusLast();

protected:
void focusInEvent(QFocusEvent *event) override;
};

void initQuickWidget(QQuickWidget *widget, const QUrl &src);
void initQuickWidget(OCQuickWidget *widget, const QUrl &src, QObject *ocContext);
}
3 changes: 1 addition & 2 deletions src/gui/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,9 @@ SettingsDialog::SettingsDialog(ownCloudGui *gui, QWidget *parent)
// People perceive this as a Window, so also make Ctrl+W work
addAction(tr("Hide"), Qt::CTRL | Qt::Key_W, this, &SettingsDialog::hide);

_ui->quickWidget->rootContext()->setContextProperty(QStringLiteral("settingsDialog"), this);
// TODO: fix sizing
_ui->quickWidget->setFixedHeight(minimumHeight() * 0.15);
QmlUtils::initQuickWidget(_ui->quickWidget, QUrl(QStringLiteral("qrc:/qt/qml/org/ownCloud/gui/qml/AccountBar.qml")));
QmlUtils::initQuickWidget(_ui->quickWidget, QUrl(QStringLiteral("qrc:/qt/qml/org/ownCloud/gui/qml/AccountBar.qml")), this);
connect(
_ui->quickWidget->engine(), &QQmlEngine::quit, QApplication::instance(),
[this] {
Expand Down
14 changes: 4 additions & 10 deletions src/gui/settingsdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,7 @@
<number>0</number>
</property>
<item>
<widget class="QQuickWidget" name="quickWidget">
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
</widget>
<widget class="OCC::QmlUtils::OCQuickWidget" name="quickWidget" native="true"/>
</item>
<item>
<widget class="QStackedWidget" name="stack"/>
Expand All @@ -65,9 +58,10 @@
</widget>
<customwidgets>
<customwidget>
<class>QQuickWidget</class>
<class>OCC::QmlUtils::OCQuickWidget</class>
<extends>QWidget</extends>
<header location="global">QtQuickWidgets/QQuickWidget</header>
<header>gui/qmlutils.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
Expand Down
3 changes: 2 additions & 1 deletion src/gui/spaces/qml/SpacesView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import QtQuick.Layouts

import org.ownCloud.gui 1.0
import org.ownCloud.libsync 1.0
import org.ownCloud.gui.spaces 1.0

Pane {
// TODO: not cool
readonly property real normalSize: 170

//property SpacesBrowser spacesBrowser
readonly property SpacesBrowser spacesBrowser: ocContext

ScrollView {
id: scrollView
Expand Down
3 changes: 1 addition & 2 deletions src/gui/spaces/spacesbrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ SpacesBrowser::SpacesBrowser(QWidget *parent)
_sortModel->setSortRole(static_cast<int>(SpacesModel::Roles::Priority));
_sortModel->sort(0, Qt::DescendingOrder);

ui->quickWidget->rootContext()->setContextProperty(QStringLiteral("spacesBrowser"), this);
QmlUtils::initQuickWidget(ui->quickWidget, QUrl(QStringLiteral("qrc:/qt/qml/org/ownCloud/gui/spaces/qml/SpacesView.qml")));
QmlUtils::initQuickWidget(ui->quickWidget, QUrl(QStringLiteral("qrc:/qt/qml/org/ownCloud/gui/spaces/qml/SpacesView.qml")), this);
}

SpacesBrowser::~SpacesBrowser()
Expand Down
11 changes: 4 additions & 7 deletions src/gui/spaces/spacesbrowser.ui
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,16 @@
<number>0</number>
</property>
<item>
<widget class="QQuickWidget" name="quickWidget">
<property name="resizeMode">
<enum>QQuickWidget::SizeRootObjectToView</enum>
</property>
</widget>
<widget class="OCC::QmlUtils::OCQuickWidget" name="quickWidget" native="true"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QQuickWidget</class>
<class>OCC::QmlUtils::OCQuickWidget</class>
<extends>QWidget</extends>
<header location="global">QtQuickWidgets/QQuickWidget</header>
<header>gui/qmlutils.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
Expand Down
Loading