Kirigami-addons

abstractkirigamiapplication.cpp
1// SPDX-FileCopyrightText: 2023 Carl Schwan <carlschwan@kde.org>
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#include "abstractkirigamiapplication.h"
5#include "commandbarfiltermodel_p.h"
6#include "actionsmodel_p.h"
7#include "shortcutsmodel_p.h"
8#include <KAboutData>
9#include <KAuthorized>
10#include <KConfigGroup>
11#include <KLocalizedString>
12#include <KSharedConfig>
13#include <QGuiApplication>
14
15using namespace std::chrono_literals;
16using namespace Qt::StringLiterals;
17
18class AbstractKirigamiApplication::Private
19{
20public:
21 KCommandBarModel *actionModel = nullptr;
22 QSortFilterProxyModel *proxyModel = nullptr;
23 KirigamiActionCollection *collection = nullptr;
24 ShortcutsModel *shortcutsModel = nullptr;
25 QObject *configurationView = nullptr;
26 QAction *openConfigurationViewAction = nullptr;
27};
28
30 : QObject(parent)
31 , d(std::make_unique<Private>())
32{
33 d->collection = new KirigamiActionCollection(parent);
34}
35
37{
38 if (d->actionModel) {
39 auto lastUsedActions = d->actionModel->lastUsedActions();
40 auto cfg = KSharedConfig::openConfig();
41 KConfigGroup cg(cfg, QStringLiteral("General"));
42 cg.writeEntry("CommandBarLastUsedActions", lastUsedActions);
43 }
44}
45
46/**
47 * A helper function that takes a list of KActionCollection* and converts it
48 * to KCommandBar::ActionGroup
49 */
50static QList<KCommandBarModel::ActionGroup> actionCollectionToActionGroup(const QList<KirigamiActionCollection *> &actionCollections)
51{
52 using ActionGroup = KCommandBarModel::ActionGroup;
53
54 QList<ActionGroup> actionList;
55 actionList.reserve(actionCollections.size());
56
57 for (const auto collection : actionCollections) {
58 const auto collectionActions = collection->actions();
59 const auto componentName = collection->componentDisplayName();
60
61 ActionGroup ag;
62 ag.name = componentName;
63 ag.actions.reserve(collection->count());
64 for (const auto action : collectionActions) {
65 if (action && !action->text().isEmpty()) {
66 ag.actions.append(action);
67 }
68 }
69 actionList.append(ag);
70 }
71 return actionList;
72}
73
75{
76 const auto collections = actionCollections();
77 for (const auto collection : collections) {
78 collection->readSettings();
79 }
80}
81
83{
84 if (!d->proxyModel) {
85 d->actionModel = new KCommandBarModel(this);
86 d->proxyModel = new CommandBarFilterModel(this);
87 d->proxyModel->setSortRole(KCommandBarModel::Score);
88 d->proxyModel->setFilterRole(Qt::DisplayRole);
89 d->proxyModel->setSourceModel(d->actionModel);
90 }
91
92 // setLastUsedActions
93 auto cfg = KSharedConfig::openConfig();
94 KConfigGroup cg(cfg, QStringLiteral("General"));
95
96 const auto actionNames = cg.readEntry(QStringLiteral("CommandBarLastUsedActions"), QStringList());
97
98 d->actionModel->setLastUsedActions(actionNames);
99 d->actionModel->refresh(actionCollectionToActionGroup(actionCollections()));
100 return d->proxyModel;
101}
102
104{
105 if (!d->shortcutsModel) {
106 d->shortcutsModel = new ShortcutsModel(this);
107 }
108
109 d->shortcutsModel->refresh(actionCollections());
110 return d->shortcutsModel;
111}
112
114{
115 const auto collections = actionCollections();
116 for (const auto collection : collections) {
117 auto resultAction = collection->action(name);
118 if (resultAction) {
119 return resultAction;
120 }
121 }
122
123 qWarning() << "Not found action for name" << name;
124
125 return nullptr;
126}
127
129{
130 return QList{
131 d->collection,
132 };
133}
134
139
141{
142 auto actionName = QLatin1StringView("open_kcommand_bar");
143 if (KAuthorized::authorizeAction(actionName)) {
144 auto openKCommandBarAction = d->collection->addAction(actionName, this, &AbstractKirigamiApplication::openKCommandBarAction);
145 openKCommandBarAction->setText(i18n("Open Command Bar"));
146 openKCommandBarAction->setIcon(QIcon::fromTheme(QStringLiteral("new-command-alarm")));
147
148 d->collection->addAction(openKCommandBarAction->objectName(), openKCommandBarAction);
149 d->collection->setDefaultShortcut(openKCommandBarAction, QKeySequence(Qt::CTRL | Qt::ALT | Qt::Key_I));
150 }
151
152 actionName = QLatin1StringView("file_quit");
153 if (KAuthorized::authorizeAction(actionName)) {
154 auto action = KStandardActions::quit(this, &AbstractKirigamiApplication::quit, this);
155 d->collection->addAction(action->objectName(), action);
156 }
157
158 actionName = QLatin1StringView("options_configure_keybinding");
159 if (KAuthorized::authorizeAction(actionName)) {
160 auto keyBindingsAction = KStandardActions::keyBindings(this, &AbstractKirigamiApplication::shortcutsEditorAction, this);
161 d->collection->addAction(keyBindingsAction->objectName(), keyBindingsAction);
162 }
163
164 actionName = QLatin1StringView("open_about_page");
165 if (KAuthorized::authorizeAction(actionName)) {
166 auto action = d->collection->addAction(actionName, this, &AbstractKirigamiApplication::openAboutPage);
167 action->setText(i18n("About %1", KAboutData::applicationData().displayName()));
168 action->setIcon(QIcon::fromTheme(QStringLiteral("help-about")));
169 }
170
171 actionName = QLatin1StringView("open_about_kde_page");
172 if (KAuthorized::authorizeAction(actionName)) {
173 auto action = d->collection->addAction(actionName, this, &AbstractKirigamiApplication::openAboutKDEPage);
174 action->setText(i18n("About KDE"));
175 action->setIcon(QIcon::fromTheme(QStringLiteral("kde")));
176
177 if (!KAboutData::applicationData().desktopFileName().startsWith(u"org.kde."_s)) {
178 action->setVisible(false);
179 }
180 }
181}
182
183void AbstractKirigamiApplication::quit()
184{
185 qGuiApp->exit();
186}
187
189{
190 return d->configurationView;
191}
192
194{
195 if (d->configurationView == configurationView) {
196 return;
197 }
198
199 if (d->configurationView) {
200 d->openConfigurationViewAction->setVisible(false);
201 }
202
203 d->configurationView = configurationView;
205
206 if (d->configurationView) {
207 if (!d->openConfigurationViewAction) {
208 // TODO also expose individual ConfigurationModule as action
209 d->openConfigurationViewAction = KStandardActions::preferences(this, [this]() {
210 QMetaObject::invokeMethod(d->configurationView, "open", Qt::QueuedConnection, QVariant());
211 }, this);
212 mainCollection()->addAction(d->openConfigurationViewAction->objectName(), d->openConfigurationViewAction);
213 }
214 d->openConfigurationViewAction->setVisible(true);
215
217 }
218}
219
220#include "moc_abstractkirigamiapplication.cpp"
void configurationViewChanged()
Changed signal for the configurationView property.
void setConfigurationView(QObject *configurationView)
Setter for the configurationView property.
void readSettings()
Read the configured settings for the action.
KirigamiActionCollection * mainCollection() const
Return the main action collection.
Q_INVOKABLE QAction * action(const QString &actionName)
Get the named action.
QML_ELEMENTQSortFilterProxyModel * actionsModel
QObject * configurationView
This property holds the configurationView of the application.
virtual QList< KirigamiActionCollection * > actionCollections() const
Return the list of KirigamiActionCollection setup in your application.
virtual void setupActions()
Entry points to declare your actions.
virtual ~AbstractKirigamiApplication()
Default destructor of AbstractKirigamiApplication.
AbstractKirigamiApplication(QObject *parent=nullptr)
Default constructor of AbstractKirigamiApplication.
static KAboutData applicationData()
static Q_INVOKABLE bool authorizeAction(const QString &action)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
static KSharedConfig::Ptr openConfig(const QString &fileName=QString(), OpenFlags mode=FullConfig, QStandardPaths::StandardLocation type=QStandardPaths::GenericConfigLocation)
A container for a set of QAction objects.
Q_INVOKABLE QAction * addAction(const QString &name, QAction *action)
Add an action under the given name to the collection.
void readSettings(KConfigGroup *config=nullptr)
Read all key associations from config.
QString i18n(const char *text, const TYPE &arg...)
void setIcon(const QIcon &icon)
void setText(const QString &text)
void setVisible(bool)
QIcon fromTheme(const QString &name)
void append(QList< T > &&value)
void reserve(qsizetype size)
qsizetype size() const const
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
Q_EMITQ_EMIT
QObject * parent() const const
QueuedConnection
DisplayRole
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:33:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.