Changeset View
Changeset View
Standalone View
Standalone View
plugins/runcommand/runcommand_config.cpp
Show All 18 Lines | |||||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "runcommand_config.h" | 21 | #include "runcommand_config.h" | ||
22 | 22 | | |||
23 | #include <QStandardPaths> | 23 | #include <QStandardPaths> | ||
24 | #include <QTableView> | 24 | #include <QTableView> | ||
25 | #include <QHBoxLayout> | 25 | #include <QHBoxLayout> | ||
26 | #include <QHeaderView> | 26 | #include <QHeaderView> | ||
27 | #include <QPushButton> | ||||
28 | #include <QMenu> | ||||
27 | #include <QStandardItemModel> | 29 | #include <QStandardItemModel> | ||
28 | #include <QDebug> | 30 | #include <QDebug> | ||
29 | #include <QUuid> | 31 | #include <QUuid> | ||
30 | #include <QJsonDocument> | 32 | #include <QJsonDocument> | ||
31 | 33 | | |||
32 | #include <KLocalizedString> | 34 | #include <KLocalizedString> | ||
33 | 35 | | |||
34 | #include <KPluginFactory> | 36 | #include <KPluginFactory> | ||
35 | 37 | | |||
36 | K_PLUGIN_FACTORY(ShareConfigFactory, registerPlugin<RunCommandConfig>();) | 38 | K_PLUGIN_FACTORY(ShareConfigFactory, registerPlugin<RunCommandConfig>();) | ||
37 | 39 | | |||
40 | | ||||
38 | RunCommandConfig::RunCommandConfig(QWidget* parent, const QVariantList& args) | 41 | RunCommandConfig::RunCommandConfig(QWidget* parent, const QVariantList& args) | ||
39 | : KdeConnectPluginKcm(parent, args, QStringLiteral("kdeconnect_runcommand_config")) | 42 | : KdeConnectPluginKcm(parent, args, QStringLiteral("kdeconnect_runcommand_config")) | ||
40 | { | 43 | { | ||
44 | QMenu* defaultMenu = new QMenu(this); | ||||
45 | addSuggestedCommand(defaultMenu, i18n("Suspend"), QStringLiteral("systemctl suspend")); | ||||
46 | addSuggestedCommand(defaultMenu, i18n("Maximum Brightness"), QStringLiteral("qdbus org.kde.Solid.PowerManagement /org/kde/Solid/PowerManagement/Actions/BrightnessControl org.kde.Solid.PowerManagement.Actions.BrightnessControl.setBrightness `qdbus org.kde.Solid.PowerManagement /org/kde/Solid/PowerManagement/Actions/BrightnessControl org.kde.Solid.PowerManagement.Actions.BrightnessControl.brightnessMax`")); | ||||
47 | | ||||
41 | QTableView* table = new QTableView(this); | 48 | QTableView* table = new QTableView(this); | ||
42 | table->horizontalHeader()->setStretchLastSection(true); | 49 | table->horizontalHeader()->setStretchLastSection(true); | ||
43 | table->verticalHeader()->setVisible(false); | 50 | table->verticalHeader()->setVisible(false); | ||
44 | QVBoxLayout* layout = new QVBoxLayout(this); | 51 | QVBoxLayout* layout = new QVBoxLayout(this); | ||
45 | layout->addWidget(table); | 52 | layout->addWidget(table); | ||
53 | QPushButton* button = new QPushButton(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Sample commands"), this); | ||||
54 | button->setMenu(defaultMenu); | ||||
55 | layout->addWidget(button); | ||||
56 | | ||||
46 | setLayout(layout); | 57 | setLayout(layout); | ||
47 | 58 | | |||
48 | m_entriesModel = new QStandardItemModel(this); | 59 | m_entriesModel = new QStandardItemModel(this); | ||
49 | table->setModel(m_entriesModel); | 60 | table->setModel(m_entriesModel); | ||
50 | 61 | | |||
51 | m_entriesModel->setHorizontalHeaderLabels(QStringList() << i18n("Name") << i18n("Command")); | 62 | m_entriesModel->setHorizontalHeaderLabels(QStringList() << i18n("Name") << i18n("Command")); | ||
52 | 63 | | |||
53 | } | 64 | } | ||
54 | 65 | | |||
55 | RunCommandConfig::~RunCommandConfig() | 66 | RunCommandConfig::~RunCommandConfig() | ||
56 | { | 67 | { | ||
57 | } | 68 | } | ||
58 | 69 | | |||
70 | void RunCommandConfig::addSuggestedCommand(QMenu* menu, const QString &name, const QString &command) | ||||
71 | { | ||||
72 | auto action = new QAction(name); | ||||
73 | connect(action, &QAction::triggered, action, [this, name, command]() { | ||||
74 | insertRow(0, name, command); | ||||
75 | Q_EMIT changed(true); | ||||
76 | }); | ||||
77 | menu->addAction(action); | ||||
78 | } | ||||
79 | | ||||
59 | void RunCommandConfig::defaults() | 80 | void RunCommandConfig::defaults() | ||
60 | { | 81 | { | ||
61 | KCModule::defaults(); | 82 | KCModule::defaults(); | ||
62 | m_entriesModel->clear(); | 83 | m_entriesModel->clear(); | ||
63 | 84 | | |||
64 | Q_EMIT changed(true); | 85 | Q_EMIT changed(true); | ||
65 | } | 86 | } | ||
66 | 87 | | |||
▲ Show 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 118 | { | |||
119 | 140 | | |||
120 | KCModule::save(); | 141 | KCModule::save(); | ||
121 | 142 | | |||
122 | Q_EMIT changed(false); | 143 | Q_EMIT changed(false); | ||
123 | } | 144 | } | ||
124 | 145 | | |||
125 | void RunCommandConfig::insertEmptyRow() | 146 | void RunCommandConfig::insertEmptyRow() | ||
126 | { | 147 | { | ||
127 | QStandardItem* newName = new QStandardItem; | 148 | insertRow(m_entriesModel->rowCount(), {}, {}); | ||
149 | } | ||||
150 | | ||||
151 | void RunCommandConfig::insertRow(int i, const QString& name, const QString& command) | ||||
152 | { | ||||
153 | QStandardItem* newName = new QStandardItem(name); | ||||
128 | newName->setEditable(true); | 154 | newName->setEditable(true); | ||
129 | QStandardItem* newCommand = new QStandardItem; | 155 | QStandardItem* newCommand = new QStandardItem(command); | ||
130 | newName->setEditable(true); | 156 | newName->setEditable(true); | ||
131 | 157 | | |||
132 | m_entriesModel->appendRow(QList<QStandardItem*>() << newName << newCommand); | 158 | m_entriesModel->insertRow(i, QList<QStandardItem*>() << newName << newCommand); | ||
133 | } | 159 | } | ||
134 | 160 | | |||
135 | void RunCommandConfig::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) | 161 | void RunCommandConfig::onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) | ||
136 | { | 162 | { | ||
137 | changed(true); | 163 | changed(true); | ||
138 | Q_UNUSED(topLeft); | 164 | Q_UNUSED(topLeft); | ||
139 | if (bottomRight.row() == m_entriesModel->rowCount() - 1) { | 165 | if (bottomRight.row() == m_entriesModel->rowCount() - 1) { | ||
140 | //TODO check both entries are still empty | 166 | //TODO check both entries are still empty | ||
141 | insertEmptyRow(); | 167 | insertEmptyRow(); | ||
142 | } | 168 | } | ||
143 | } | 169 | } | ||
144 | 170 | | |||
145 | 171 | | |||
146 | #include "runcommand_config.moc" | 172 | #include "runcommand_config.moc" |