From fe5d2d7a634e89f43ecaa93a56e0afa8f3975675 Mon Sep 17 00:00:00 2001 From: Michail Vourlakos Date: Mon, 1 Mar 2021 19:04:21 +0200 Subject: [PATCH] context menu:provide Add view through templates --- app/dbus/org.kde.LatteDock.xml | 7 +++ app/lattecorona.cpp | 23 ++++++++ app/lattecorona.h | 2 + containmentactions/contextmenu/menu.cpp | 53 +++++++++++++++++-- containmentactions/contextmenu/menu.h | 6 +++ .../configuration/LatteDockConfiguration.qml | 8 +-- 6 files changed, 92 insertions(+), 7 deletions(-) diff --git a/app/dbus/org.kde.LatteDock.xml b/app/dbus/org.kde.LatteDock.xml index 447ae8840..0f24294f5 100644 --- a/app/dbus/org.kde.LatteDock.xml +++ b/app/dbus/org.kde.LatteDock.xml @@ -18,6 +18,10 @@ + + + + @@ -35,6 +39,9 @@ + + + diff --git a/app/lattecorona.cpp b/app/lattecorona.cpp index 7cf978430..1a538d481 100644 --- a/app/lattecorona.cpp +++ b/app/lattecorona.cpp @@ -26,6 +26,7 @@ #include "apptypes.h" #include "lattedockadaptor.h" #include "screenpool.h" +#include "data/generictable.h" #include "declarativeimports/interfaces.h" #include "indicator/factory.h" #include "layout/abstractlayout.h" @@ -1163,6 +1164,28 @@ QStringList Corona::contextMenuData(const uint &containmentId) return data; } +QStringList Corona::viewTemplatesData() +{ + QStringList data; + + Latte::Data::GenericTable viewtemplates = m_templatesManager->viewTemplates(); + + for(int i=0; isynchronizer()->viewForContainment((int)containmentId); + if (view && view->layout() && !templateId.isEmpty()) { + view->layout()->newView(templateId); + } +} + void Corona::duplicateView(const uint &containmentId) { auto view = m_layoutsManager->synchronizer()->viewForContainment((int)containmentId); diff --git a/app/lattecorona.h b/app/lattecorona.h index de5271674..348c1fb18 100644 --- a/app/lattecorona.h +++ b/app/lattecorona.h @@ -160,12 +160,14 @@ public: void showSettingsWindow(int page); QStringList contextMenuData(const uint &containmentId); + QStringList viewTemplatesData(); public slots: void aboutApplication(); void activateLauncherMenu(); void loadDefaultLayout() override; + void addView(const uint &containmentId, const QString &templateId); void duplicateView(const uint &containmentId); void exportViewTemplate(const uint &containmentId); void moveViewToLayout(const uint &containmentId, const QString &layoutName); diff --git a/containmentactions/contextmenu/menu.cpp b/containmentactions/contextmenu/menu.cpp index d891c5798..1aba78015 100644 --- a/containmentactions/contextmenu/menu.cpp +++ b/containmentactions/contextmenu/menu.cpp @@ -43,6 +43,7 @@ const int LAYOUTMENUINDEX = 3; const int VIEWTYPEINDEX = 4; const int VIEWLAYOUTINDEX = 5; +const char ADDVIEWNAME[] = "add view"; const char LAYOUTSNAME[] = "layouts"; const char PREFERENCESNAME[] = "preferences"; const char QUITLATTENAME[] = "quit latte"; @@ -83,6 +84,7 @@ Menu::~Menu() m_separator->deleteLater(); //! sub-menus + m_addViewMenu->deleteLater(); m_switchLayoutsMenu->deleteLater(); m_moveToLayoutMenu->deleteLater(); @@ -148,6 +150,17 @@ void Menu::makeActions() connect(m_switchLayoutsMenu, &QMenu::aboutToShow, this, &Menu::populateLayouts); connect(m_switchLayoutsMenu, &QMenu::triggered, this, &Menu::switchToLayout); + //! Add View submenu + m_addViewMenu = new QMenu; + m_addViewAction = m_addViewMenu->menuAction(); + m_addViewAction->setText(i18n("&Add Dock/Panel")); + m_addViewAction->setIcon(QIcon::fromTheme("list-add")); + m_addViewAction->setStatusTip(i18n("Add dock or panel based on specific template")); + this->containment()->actions()->addAction(ADDVIEWNAME, m_addViewAction); + + connect(m_addViewMenu, &QMenu::aboutToShow, this, &Menu::populateViewTemplates); + connect(m_addViewMenu, &QMenu::triggered, this, &Menu::addView); + //! Move submenu m_moveToLayoutMenu = new QMenu; m_moveAction = m_moveToLayoutMenu->menuAction(); @@ -237,6 +250,7 @@ QList Menu::contextualActions() actions << m_separator; actions << m_addWidgetsAction; + actions << m_addViewAction; actions << m_duplicateAction; actions << m_moveAction; actions << m_exportViewAction; @@ -244,12 +258,15 @@ QList Menu::contextualActions() actions << m_removeAction; m_data.clear(); + m_viewTemplates.clear(); QDBusInterface iface("org.kde.lattedock", "/Latte", "", QDBusConnection::sessionBus()); if (iface.isValid()) { - QDBusReply replyData = iface.call("contextMenuData", containment()->id()); + QDBusReply contextData = iface.call("contextMenuData", containment()->id()); + m_data = contextData.value(); - m_data = replyData.value(); + QDBusReply templatesData = iface.call("viewTemplatesData"); + m_viewTemplates = templatesData.value(); } ViewType viewType{static_cast((m_data[VIEWTYPEINDEX]).toInt())}; @@ -280,7 +297,9 @@ QList Menu::contextualActions() QAction *Menu::action(const QString &name) { - if (name == ADDWIDGETSNAME) { + if (name == ADDVIEWNAME) { + return m_addViewAction; + } else if (name == ADDWIDGETSNAME) { return m_addWidgetsAction; } else if (name == DUPLICATEVIEWNAME) { return m_duplicateAction; @@ -430,6 +449,34 @@ void Menu::populateMoveToLayouts() } } +void Menu::populateViewTemplates() +{ + m_addViewMenu->clear(); + + for(int i=0; iaddAction(m_viewTemplates[i]); + templateAction->setData(m_viewTemplates[i+1]); + } +} + +void Menu::addView(QAction *action) +{ + const QString templateId = action->data().toString(); + + QTimer::singleShot(400, [this, templateId]() { + QDBusInterface iface("org.kde.lattedock", "/Latte", "", QDBusConnection::sessionBus()); + + if (iface.isValid()) { + iface.call("addView", containment()->id(), templateId); + } + }); +} + void Menu::moveToLayout(QAction *action) { const QString layoutName = action->data().toString(); diff --git a/containmentactions/contextmenu/menu.h b/containmentactions/contextmenu/menu.h index 8cafd9f92..48a8b5f65 100644 --- a/containmentactions/contextmenu/menu.h +++ b/containmentactions/contextmenu/menu.h @@ -45,15 +45,18 @@ private Q_SLOTS: void onUserConfiguringChanged(const bool &configuring); void populateLayouts(); void populateMoveToLayouts(); + void populateViewTemplates(); void quitApplication(); void requestConfiguration(); void requestWidgetExplorer(); + void addView(QAction *action); void moveToLayout(QAction *action); void switchToLayout(QAction *action); private: QStringList m_data; + QStringList m_viewTemplates; QListm_actions; @@ -69,6 +72,9 @@ private: QAction *m_removeAction{nullptr}; QAction *m_quitApplication{nullptr}; + QAction *m_addViewAction{nullptr}; + QMenu *m_addViewMenu{nullptr}; + QAction *m_layoutsAction{nullptr}; QMenu *m_switchLayoutsMenu{nullptr}; diff --git a/shell/package/contents/configuration/LatteDockConfiguration.qml b/shell/package/contents/configuration/LatteDockConfiguration.qml index fa4208252..5d5de4787 100644 --- a/shell/package/contents/configuration/LatteDockConfiguration.qml +++ b/shell/package/contents/configuration/LatteDockConfiguration.qml @@ -519,7 +519,7 @@ Loader { buttonEnabled: true buttonIsTriggeringMenu: true - buttonText: i18n("New...") + buttonText: i18n("Add...") buttonIconSource: "list-add" buttonToolTip: i18n("Add new docks and panels from various templates") @@ -549,7 +549,7 @@ Loader { onActivated: { var item = actionsModel.get(index); - if (item && item.actionId === "new:") { + if (item && item.actionId === "add:") { latteView.layout.newView(item.templateId); } @@ -587,10 +587,10 @@ Loader { for(var i=viewTemplateIds.length-1; i>=0; --i) { //! add view templates on reverse var viewtemplate = { - actionId: 'new:', + actionId: 'add:', enabled: true, templateId: viewTemplateIds[i], - name: i18n("New %0").arg(viewTemplateNames[i]), + name: i18n("Add %0").arg(viewTemplateNames[i]), icon: 'list-add' }; actionsModel.append(viewtemplate);