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 <KLocalizedContext> |
9 | #include <QDebug> |
10 | #include <QPointer> |
11 | #include <QQmlApplicationEngine> |
12 | #include <QQmlContext> |
13 | #include <purpose/alternativesmodel.h> |
14 | #include <purpose/configuration.h> |
15 | |
16 | using namespace Purpose; |
17 | |
18 | class Purpose:: : public QObject |
19 | { |
20 | Q_OBJECT |
21 | public: |
22 | (Menu *qq) |
23 | : QObject(qq) |
24 | , m_model(new AlternativesModel(qq)) |
25 | , q(qq) |
26 | { |
27 | } |
28 | |
29 | () override |
30 | { |
31 | if (m_engine) { |
32 | m_engine->deleteLater(); |
33 | } |
34 | } |
35 | |
36 | void (int row) |
37 | { |
38 | if (!m_engine) { |
39 | m_engine = new QQmlApplicationEngine; |
40 | m_engine->rootContext()->setContextObject(new KLocalizedContext(this)); |
41 | m_engine->load(url: QUrl(QStringLiteral("qrc:/JobDialog.qml" ))); |
42 | } |
43 | |
44 | Q_ASSERT(!m_engine->rootObjects().isEmpty()); |
45 | QObject *o = m_engine->rootObjects().at(i: 0); |
46 | |
47 | if (!o) { |
48 | qWarning() << Q_FUNC_INFO << "object is NULL at m_engine" << m_engine << "rootObjects=" << m_engine->rootObjects(); |
49 | return; |
50 | } |
51 | |
52 | o->setProperty(name: "model" , value: QVariant::fromValue(value: m_model.data())); |
53 | o->setProperty(name: "index" , value: row); |
54 | o->setProperty(name: "visible" , value: true); |
55 | o->setProperty(name: "q" , value: QVariant::fromValue<QObject *>(value: q)); |
56 | o->setParent(q); |
57 | |
58 | QMetaObject::invokeMethod(obj: o, member: "start" ); |
59 | } |
60 | |
61 | public: |
62 | QQmlApplicationEngine * = nullptr; |
63 | QPointer<AlternativesModel> ; |
64 | Purpose::Menu *; |
65 | }; |
66 | |
67 | Menu::(QWidget *parent) |
68 | : QMenu(parent) |
69 | , d_ptr(new MenuPrivate(this)) |
70 | { |
71 | connect(sender: d_ptr->m_model.data(), signal: &AlternativesModel::inputDataChanged, context: this, slot: &Menu::reload); |
72 | connect(sender: this, signal: &QMenu::triggered, context: this, slot: [this](QAction *action) { |
73 | Q_D(Menu); |
74 | const int row = action->property(name: "row" ).toInt(); |
75 | Q_EMIT aboutToShare(); |
76 | d->trigger(row); |
77 | }); |
78 | } |
79 | |
80 | void Menu::() |
81 | { |
82 | Q_D(Menu); |
83 | clear(); |
84 | for (int i = 0, c = d->m_model->rowCount(); i != c; ++i) { |
85 | QModelIndex idx = d->m_model->index(row: i); |
86 | QAction *a = addAction(text: idx.data(arole: AlternativesModel::ActionDisplayRole).toString()); |
87 | a->setToolTip(idx.data(arole: Qt::ToolTipRole).toString()); |
88 | a->setIcon(idx.data(arole: Qt::DecorationRole).value<QIcon>()); |
89 | a->setProperty(name: "pluginId" , value: idx.data(arole: AlternativesModel::PluginIdRole)); |
90 | a->setProperty(name: "row" , value: i); |
91 | } |
92 | |
93 | setEnabled(!isEmpty()); |
94 | } |
95 | |
96 | AlternativesModel *Menu::() const |
97 | { |
98 | Q_D(const Menu); |
99 | return d->m_model.data(); |
100 | } |
101 | |
102 | #include "menu.moc" |
103 | #include "moc_menu.cpp" |
104 | |