Skip to content

Commit

Permalink
qtquick: Introduce QML singletons
Browse files Browse the repository at this point in the history
This will make QML tooling happier.
  • Loading branch information
iamsergio committed Jan 27, 2025
1 parent f629131 commit 6291deb
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/qtquick/customtabbar/MyTabBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ KDDW.TabBarBase {
onPressed: {
/// Do not use random uuids if you're planing to use layout save/restore!
/// Using them here for convenience of the exercise
var uniqueName = _kddwHelpers.generateUuid();
var uniqueName = Singletons.helpers.generateUuid();

var code = `import com.kdab.dockwidgets 2.0 as KDDW;
import QtQuick 2.6;
Expand Down
2 changes: 1 addition & 1 deletion examples/qtquick/dockwidgets/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ ApplicationWindow {
onTriggered: {
/// Do not use random uuids if you're planing to use layout save/restore!
/// Using them here for convenience of the exercise
var uniqueName = _kddwHelpers.generateUuid();
var uniqueName = KDDW.Singletons.helpers.generateUuid();

var code = `import com.kdab.dockwidgets 2.0 as KDDW;
import QtQuick 2.6;
Expand Down
7 changes: 7 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,13 @@ if(KDDW_FRONTEND_QTQUICK)
set(QT_QML_GENERATE_QMLLS_INI ON)

set(KDDW_MODULE_QML_ELEMENTS
qtquick/Singletons.cpp
qtquick/Singletons_p.h
qtquick/Helpers.cpp
qtquick/Helpers_p.h
qtquick/ViewFactory.cpp
qtquick/ViewFactory.h
core/ViewFactory.h
qtquick/MainWindowInstantiator.cpp
qtquick/MainWindowInstantiator.h
qtquick/LayoutSaverInstantiator.cpp
Expand Down
5 changes: 3 additions & 2 deletions src/core/DockRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class MainWindowMDI;
class DOCKS_EXPORT DockRegistry : public Core::Object, public Core::EventFilterInterface
{
Q_OBJECT

QML_ELEMENT
QML_UNCREATABLE("Created by the framework only.")
public:
enum class DockByNameFlag {
None = 0,
Expand All @@ -61,7 +62,7 @@ class DOCKS_EXPORT DockRegistry : public Core::Object, public Core::EventFilterI
static DockRegistry *self();
static bool isInitialized();

~DockRegistry();
~DockRegistry() override;
void registerDockWidget(Core::DockWidget *);
void unregisterDockWidget(Core::DockWidget *);

Expand Down
2 changes: 2 additions & 0 deletions src/core/ViewFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class TitleBar;
class DOCKS_EXPORT ViewFactory : public Core::Object
{
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("Created by the framework only.")
public:
ViewFactory() = default;

Expand Down
4 changes: 3 additions & 1 deletion src/qtquick/Helpers_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#ifndef KD_QTQUICK_HELPERS_P_H
#define KD_QTQUICK_HELPERS_P_H

#include <QObject>
#include <QQmlEngine>

#include <kdbindings/signal.h>

Expand All @@ -26,6 +26,8 @@ namespace KDDockWidgets {
class QtQuickHelpers : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_UNCREATABLE("Created by the framework only.")
Q_PROPERTY(
QObject *groupViewInMDIResize READ groupViewInMDIResize NOTIFY groupInMDIResizeChanged)

Expand Down
5 changes: 5 additions & 0 deletions src/qtquick/Platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,8 @@ Core::DockWidget *Platform::dockWidgetForItem(QQuickItem *item)

return nullptr;
}

QtQuickHelpers *Platform::helpers() const
{
return m_qquickHelpers;
}
2 changes: 2 additions & 0 deletions src/qtquick/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class DOCKS_EXPORT Platform : public QtCommon::Platform_qt
void ungrabMouse() override;
void onViewFactoryChanged() override;

QtQuickHelpers *helpers() const;

static Platform *instance();

static Core::DockWidget *dockWidgetForItem(QQuickItem *);
Expand Down
5 changes: 5 additions & 0 deletions src/qtquick/QmlTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "MainWindowInstantiator.h"
#include "MainWindowMDIInstantiator.h"
#include "LayoutSaverInstantiator.h"
#include "Singletons_p.h"

#include "views/Group.h"
#include "views/TitleBar.h"
Expand Down Expand Up @@ -50,4 +51,8 @@ void KDDockWidgets::registerQmlTypes()

qmlRegisterUncreatableType<QtQuick::DockWidgetModel>("com.kdab.dockwidgets", 2, 0,
"DockWidgetModel", QStringLiteral("Internal usage only"));

qmlRegisterSingletonType<QtQuick::Singletons>("com.kdab.dockwidgets", 2, 0, "Singletons", [](QQmlEngine *, QJSEngine *) -> QObject * {
return new QtQuick::Singletons();
});
}
3 changes: 3 additions & 0 deletions src/qtquick/QmlTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
#define KD_QMLTYPES_H

namespace KDDockWidgets {
// This is only called for:
// Qt5
// Qt6 if KDDockWidgets_QML_MODULE=OFF
void registerQmlTypes();
}

Expand Down
40 changes: 40 additions & 0 deletions src/qtquick/Singletons.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
This file is part of KDDockWidgets.
SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
Author: Sérgio Martins <[email protected]>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
Contact KDAB at <[email protected]> for commercial licensing options.
*/

#include "Singletons_p.h"
#include "ViewFactory.h"
#include "core/DockRegistry.h"
#include "core/DockWidget.h"
#include "Platform.h"
#include "Helpers_p.h"

using namespace KDDockWidgets::QtQuick;

Singletons::~Singletons() = default;

KDDockWidgets::QtQuick::ViewFactory *Singletons::widgetFactory() const
{
return QtQuick::ViewFactory::self();
}

KDDockWidgets::DockRegistry *Singletons::dockRegistry() const
{
return DockRegistry::self();
}

KDDockWidgets::QtQuickHelpers *Singletons::helpers() const
{
if (auto plat = QtQuick::Platform::instance()) {
return plat->helpers();
}

return nullptr;
}
37 changes: 37 additions & 0 deletions src/qtquick/Singletons_p.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
This file is part of KDDockWidgets.
SPDX-FileCopyrightText: 2025 Klarälvdalens Datakonsult AB, a KDAB Group company <[email protected]>
Author: Sérgio Martins <[email protected]>
SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
Contact KDAB at <[email protected]> for commercial licensing options.
*/

#pragma once

#include "Helpers_p.h"
#include "ViewFactory.h"
#include "core/DockRegistry.h"

#include <QQmlEngine>

namespace KDDockWidgets::QtQuick {

class Singletons : public QObject
{
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
Q_PROPERTY(KDDockWidgets::QtQuick::ViewFactory *widgetFactory READ widgetFactory CONSTANT)
Q_PROPERTY(KDDockWidgets::DockRegistry *dockRegistry READ dockRegistry CONSTANT)
Q_PROPERTY(KDDockWidgets::QtQuickHelpers *helpers READ helpers CONSTANT)
public:
~Singletons() override;
KDDockWidgets::QtQuick::ViewFactory *widgetFactory() const;
KDDockWidgets::DockRegistry *dockRegistry() const;
KDDockWidgets::QtQuickHelpers *helpers() const;
};

}
3 changes: 3 additions & 0 deletions src/qtquick/ViewFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "kddockwidgets/core/ViewFactory.h"

#include <QQmlEngine>

// clazy:excludeall=ctor-missing-parent-argument

/**
Expand Down Expand Up @@ -49,6 +51,7 @@ namespace QtQuick {
class DOCKS_EXPORT ViewFactory : public Core::ViewFactory
{
Q_OBJECT
QML_ELEMENT
public:
ViewFactory() = default;
~ViewFactory() override;
Expand Down
2 changes: 1 addition & 1 deletion src/qtquick/views/qml/FloatingWindow.qml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Rectangle {
id: titleBar
readonly property TitleBarView titleBarCpp: root.titleBarCpp
readonly property int heightWhenVisible: item.heightWhenVisible // qmllint disable missing-property
source: _kddw_widgetFactory.titleBarFilename()
source: Singletons.widgetFactory.titleBarFilename()

anchors {
top: parent ? parent.top : undefined
Expand Down
6 changes: 3 additions & 3 deletions src/qtquick/views/qml/Group.qml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Rectangle {
property int titleBarContentsMargin: 1
property int mouseResizeMargin: 8
readonly property bool isMDI: groupCpp && groupCpp.isMDI
readonly property bool resizeAllowed: root.isMDI && !_kddwHelpers.isDragging && _kddwDockRegistry && (!_kddwHelpers.groupViewInMDIResize || _kddwHelpers.groupViewInMDIResize === groupCpp)
readonly property bool resizeAllowed: root.isMDI && !Singletons.helpers.isDragging && Singletons.dockRegistry && (!Singletons.helpers.groupViewInMDIResize || Singletons.helpers.groupViewInMDIResize === groupCpp)
property alias tabBarHeight: tabbar.height
readonly property bool hasCustomMouseEventRedirector: false
readonly property bool isFixedHeight: groupCpp && groupCpp.isFixedHeight
Expand Down Expand Up @@ -176,7 +176,7 @@ Rectangle {
Loader {
id: titleBar
readonly property QtObject titleBarCpp: root.titleBarCpp
source: groupCpp ? _kddw_widgetFactory.titleBarFilename() : ""
source: groupCpp ? Singletons.widgetFactory.titleBarFilename() : ""

anchors {
top: parent ? parent.top : undefined
Expand All @@ -193,7 +193,7 @@ Rectangle {
readonly property GroupView groupCpp: root.groupCpp
readonly property bool hasCustomMouseEventRedirector: root.hasCustomMouseEventRedirector

source: groupCpp ? _kddw_widgetFactory.tabbarFilename() : ""
source: groupCpp ? Singletons.widgetFactory.tabbarFilename() : ""

function topAnchor() {
if (root.tabsAtTop) {
Expand Down

0 comments on commit 6291deb

Please sign in to comment.