diff --git a/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp b/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp index e1b082470..e80514860 100644 --- a/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp +++ b/src/lib/plugins/qml/api/sidebar/qmlsidebar.cpp @@ -22,9 +22,16 @@ #include QmlSideBar::QmlSideBar(QObject *parent) - : SideBarInterface(parent) + : QObject(parent) , m_item(nullptr) { + m_sideBarHelper = new QmlSideBarHelper(this); + + connect(this, &QmlSideBar::titleChanged, m_sideBarHelper, &QmlSideBarHelper::setTitle); + connect(this, &QmlSideBar::iconChanged, m_sideBarHelper, &QmlSideBarHelper::setIcon); + connect(this, &QmlSideBar::shortcutChanged, m_sideBarHelper, &QmlSideBarHelper::setShortcut); + connect(this, &QmlSideBar::checkableChanged, m_sideBarHelper, &QmlSideBarHelper::setCheckable); + connect(this, &QmlSideBar::itemChanged, m_sideBarHelper, &QmlSideBarHelper::setItem); } QString QmlSideBar::name() const @@ -45,6 +52,7 @@ QString QmlSideBar::title() const void QmlSideBar::setTitle(const QString &title) { m_title = title; + emit titleChanged(title); } QString QmlSideBar::icon() const @@ -55,6 +63,7 @@ QString QmlSideBar::icon() const void QmlSideBar::setIcon(const QString &icon) { m_iconUrl = icon; + emit iconChanged(m_iconUrl); } QString QmlSideBar::shortcut() const @@ -65,6 +74,7 @@ QString QmlSideBar::shortcut() const void QmlSideBar::setShortcut(const QString &shortcut) { m_shortcut = shortcut; + emit shortcutChanged(m_shortcut); } bool QmlSideBar::checkable() @@ -75,6 +85,7 @@ bool QmlSideBar::checkable() void QmlSideBar::setCheckable(bool checkable) { m_checkable = checkable; + emit checkableChanged(m_checkable); } QQmlComponent *QmlSideBar::item() const @@ -85,20 +96,35 @@ QQmlComponent *QmlSideBar::item() const void QmlSideBar::setItem(QQmlComponent *item) { m_item = item; + emit itemChanged(m_item); } -QAction *QmlSideBar::createMenuAction() +SideBarInterface *QmlSideBar::sideBar() const { - QAction *action = new QAction; - action->setText(m_title); - action->setCheckable(m_checkable); + return m_sideBarHelper; +} + +QmlSideBarHelper::QmlSideBarHelper(QObject *parent) + : SideBarInterface(parent) + , m_item(nullptr) +{ +} + +QString QmlSideBarHelper::title() const +{ + return m_title; +} + +QAction *QmlSideBarHelper::createMenuAction() +{ + QAction *action = new QAction(m_title); action->setShortcut(QKeySequence(m_shortcut)); - QString iconPath = QzTools::getPathFromUrl(QUrl(m_iconUrl)); - action->setIcon(QIcon(iconPath)); + action->setIcon(QIcon(QzTools::getPathFromUrl(QUrl(m_iconUrl)))); + action->setCheckable(m_checkable); return action; } -QWidget *QmlSideBar::createSideBarWidget(BrowserWindow *mainWindow) +QWidget *QmlSideBarHelper::createSideBarWidget(BrowserWindow *mainWindow) { Q_UNUSED(mainWindow) @@ -110,3 +136,28 @@ QWidget *QmlSideBar::createSideBarWidget(BrowserWindow *mainWindow) return QWidget::createWindowContainer(window); } + +void QmlSideBarHelper::setTitle(const QString &title) +{ + m_title = title; +} + +void QmlSideBarHelper::setIcon(const QString &icon) +{ + m_iconUrl = icon; +} + +void QmlSideBarHelper::setShortcut(const QString &shortcut) +{ + m_shortcut = shortcut; +} + +void QmlSideBarHelper::setCheckable(bool checkable) +{ + m_checkable = checkable; +} + +void QmlSideBarHelper::setItem(QQmlComponent *item) +{ + m_item = item; +} diff --git a/src/lib/plugins/qml/api/sidebar/qmlsidebar.h b/src/lib/plugins/qml/api/sidebar/qmlsidebar.h index 4262a0d08..8a2f63eae 100644 --- a/src/lib/plugins/qml/api/sidebar/qmlsidebar.h +++ b/src/lib/plugins/qml/api/sidebar/qmlsidebar.h @@ -19,19 +19,21 @@ #include "sidebarinterface.h" #include -class QmlSideBar : public SideBarInterface +class QmlSideBarHelper; + +class QmlSideBar : public QObject { Q_OBJECT - Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(QString title READ title WRITE setTitle) - Q_PROPERTY(QString icon READ icon WRITE setIcon) - Q_PROPERTY(QString shortcut READ shortcut WRITE setShortcut) - Q_PROPERTY(bool checkable READ checkable WRITE setCheckable) - Q_PROPERTY(QQmlComponent* item READ item WRITE setItem) + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) + Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) + Q_PROPERTY(QString icon READ icon WRITE setIcon NOTIFY iconChanged) + Q_PROPERTY(QString shortcut READ shortcut WRITE setShortcut NOTIFY shortcutChanged) + Q_PROPERTY(bool checkable READ checkable WRITE setCheckable NOTIFY checkableChanged) + Q_PROPERTY(QQmlComponent* item READ item WRITE setItem NOTIFY itemChanged) Q_CLASSINFO("DefaultProperty", "item") public: - QmlSideBar(QObject *parent = nullptr); + explicit QmlSideBar(QObject *parent = nullptr); QString name() const; void setName(const QString &name); QString title() const; @@ -45,8 +47,15 @@ public: QQmlComponent *item() const; void setItem(QQmlComponent *item); - QAction *createMenuAction(); - QWidget *createSideBarWidget(BrowserWindow *mainWindow); + SideBarInterface *sideBar() const; + +Q_SIGNALS: + void nameChanged(const QString &name); + void titleChanged(const QString &title); + void iconChanged(const QString &icon); + void shortcutChanged(const QString &shortcut); + void checkableChanged(bool checkable); + void itemChanged(QQmlComponent *item); private: QString m_name; @@ -55,4 +64,29 @@ private: QString m_shortcut; bool m_checkable; QQmlComponent *m_item; + + QmlSideBarHelper *m_sideBarHelper; +}; + +class QmlSideBarHelper : public SideBarInterface +{ + Q_OBJECT +public: + explicit QmlSideBarHelper(QObject *parent = nullptr); + QString title() const; + QAction *createMenuAction(); + QWidget *createSideBarWidget(BrowserWindow *mainWindow); + + void setTitle(const QString &title); + void setIcon(const QString &icon); + void setShortcut(const QString &shortcut); + void setCheckable(bool checkable); + void setItem(QQmlComponent *item); + +private: + QString m_title; + QString m_iconUrl; + QString m_shortcut; + bool m_checkable; + QQmlComponent *m_item; }; diff --git a/src/lib/plugins/qml/qmlplugininterface.cpp b/src/lib/plugins/qml/qmlplugininterface.cpp index 5cd0a6294..1f3520118 100644 --- a/src/lib/plugins/qml/qmlplugininterface.cpp +++ b/src/lib/plugins/qml/qmlplugininterface.cpp @@ -51,7 +51,7 @@ void QmlPluginInterface::init(InitState state, const QString &settingsPath) } if (m_sideBar) { - SideBarManager::addSidebar(m_sideBar->name(), m_sideBar); + SideBarManager::addSidebar(m_sideBar->name(), m_sideBar->sideBar()); } } @@ -78,7 +78,7 @@ void QmlPluginInterface::unload() } if (m_sideBar) { - SideBarManager::removeSidebar(m_sideBar); + SideBarManager::removeSidebar(m_sideBar->sideBar()); } emit qmlPluginUnloaded();