Purpose

menu.cpp
1/*
2 SPDX-FileCopyrightText: 2015 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "menu.h"
8#include <KLocalizedQmlContext>
9#include <QDebug>
10#include <QPointer>
11#include <QQmlApplicationEngine>
12#include <QQmlContext>
13#include <purpose/alternativesmodel.h>
14#include <purpose/configuration.h>
15#include <qqml.h>
16
17using namespace Purpose;
18
19class Purpose::MenuPrivate : public QObject
20{
22public:
23 MenuPrivate(Menu *qq)
24 : QObject(qq)
25 , m_model(new AlternativesModel(qq))
26 , q(qq)
27 {
28 }
29
30 ~MenuPrivate() override
31 {
32 if (m_engine) {
33 m_engine->deleteLater();
34 }
35 }
36
37 void trigger(int row)
38 {
39 if (!m_engine) {
40 m_engine = new QQmlApplicationEngine;
42 m_engine->load(QUrl(QStringLiteral("qrc:/org.kde.purpose/JobDialog.qml")));
43 }
44
45 Q_ASSERT(!m_engine->rootObjects().isEmpty());
46 QObject *o = m_engine->rootObjects().at(0);
47
48 if (!o) {
49 qWarning() << Q_FUNC_INFO << "object is NULL at m_engine" << m_engine << "rootObjects=" << m_engine->rootObjects();
50 return;
51 }
52
53 o->setProperty("model", QVariant::fromValue(m_model.data()));
54 o->setProperty("index", row);
55 o->setProperty("visible", true);
57 o->setParent(q);
58
59 QMetaObject::invokeMethod(o, "start");
60 }
61
62public:
63 QQmlApplicationEngine *m_engine = nullptr;
65 Purpose::Menu *q;
66};
67
68Menu::Menu(QWidget *parent)
69 : QMenu(parent)
70 , d_ptr(new MenuPrivate(this))
71{
72 qmlRegisterUncreatableType<Menu>("org.kde.purpose.private.widgets", 1, 0, "Menu", QString());
73
74 connect(d_ptr->m_model.data(), &AlternativesModel::inputDataChanged, this, &Menu::reload);
75 connect(this, &QMenu::triggered, this, [this](QAction *action) {
76 Q_D(Menu);
77 const int row = action->property("row").toInt();
78 Q_EMIT aboutToShare();
79 d->trigger(row);
80 });
81}
82
83void Menu::reload()
84{
85 Q_D(Menu);
86 clear();
87 for (int i = 0, c = d->m_model->rowCount(); i != c; ++i) {
88 QModelIndex idx = d->m_model->index(i);
89 QAction *a = addAction(idx.data(AlternativesModel::ActionDisplayRole).toString());
92 a->setProperty("pluginId", idx.data(AlternativesModel::PluginIdRole));
93 a->setProperty("row", i);
94 }
95
96 setEnabled(!isEmpty());
97}
98
99AlternativesModel *Menu::model() const
100{
101 Q_D(const Menu);
102 return d->m_model.data();
103}
104
105#include "menu.moc"
106#include "moc_menu.cpp"
Interface for client applications to share data.
void setIcon(const QIcon &icon)
void setToolTip(const QString &tip)
void triggered(QAction *action)
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QVariant data(int role) const const
Q_OBJECTQ_OBJECT
void deleteLater()
QVariant property(const char *name) const const
void setParent(QObject *parent)
bool setProperty(const char *name, QVariant &&value)
void load(const QString &filePath)
QList< QObject * > rootObjects() const const
void setContextObject(QObject *object)
QQmlContext * rootContext() const const
ToolTipRole
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
QString toString() const const
T value() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:03:06 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.