swift
Loading...
Searching...
No Matches
settingshotkeycomponent.cpp
1// SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
6#include <QAbstractItemModel>
7#include <QFlags>
8#include <QItemSelectionModel>
9#include <QList>
10#include <QMessageBox>
11#include <QModelIndex>
12#include <QModelIndexList>
13#include <QPushButton>
14#include <QString>
15#include <QTableView>
16#include <QVariant>
17#include <QtGlobal>
18
19#include "ui_settingshotkeycomponent.h"
20
23#include "core/inputmanager.h"
26#include "gui/guiapplication.h"
28#include "misc/metadatautils.h"
29
30using namespace swift::misc;
31using namespace swift::misc::input;
32using namespace swift::gui::models;
33using namespace swift::core;
34using namespace swift::core::context;
35
37{
40 {
41 Q_ASSERT_X(sGui, Q_FUNC_INFO, "Missing sGui");
42 ui->setupUi(this);
43 ui->tv_Hotkeys->setModel(&m_model);
44
45 connect(ui->pb_AddHotkey, &QPushButton::clicked, this, &CSettingsHotkeyComponent::addEntry);
46 connect(ui->pb_EditHotkey, &QPushButton::clicked, this, &CSettingsHotkeyComponent::editEntry);
47 connect(ui->pb_RemoveHotkey, &QPushButton::clicked, this, &CSettingsHotkeyComponent::removeEntry);
49
51 ui->tv_Hotkeys->selectRow(0);
52 }
53
55
57 {
58 const CStatusMessage msg = m_actionHotkeys.save();
60 }
61
63 {
64 Q_ASSERT_X(sApp && sApp->getInputManager(), Q_FUNC_INFO, "Missing input manager");
65 sApp->getInputManager()->registerAction(pttHotkeyAction(), pttHotkeyIcon());
66 }
67
68 void CSettingsHotkeyComponent::addEntry()
69 {
70 const CActionHotkey selectedActionHotkey =
71 CHotkeyDialog::getActionHotkey(CActionHotkey(), getAllIdentifiers(), this);
72 if (selectedActionHotkey.isValid() && checkAndConfirmConflicts(selectedActionHotkey))
73 {
74 addHotkeyToSettings(selectedActionHotkey);
75 const int position = m_model.rowCount();
76 m_model.insertRows(position, 1, QModelIndex());
77 const QModelIndex index = m_model.index(position, 0, QModelIndex());
78 m_model.setData(index, QVariant::fromValue(selectedActionHotkey), CActionHotkeyListModel::ActionHotkeyRole);
79
80 // T784, further info about the "key"/button
81 CLogMessage(this).debug(u"%1, added key: '%2'")
82 << classNameShort(this) << selectedActionHotkey.toQString(true);
83 }
84 this->resizeView();
85 }
86
87 void CSettingsHotkeyComponent::editEntry()
88 {
89 const auto index = ui->tv_Hotkeys->selectionModel()->currentIndex();
90 if (!index.isValid())
91 {
92 return;
93 }
94
95 const auto model = ui->tv_Hotkeys->model();
96 const QModelIndex indexHotkey = model->index(index.row(), 0, QModelIndex());
97 Q_ASSERT_X(indexHotkey.data(CActionHotkeyListModel::ActionHotkeyRole).canConvert<CActionHotkey>(), Q_FUNC_INFO,
98 "No action hotkey");
99 const auto actionHotkey = indexHotkey.data(CActionHotkeyListModel::ActionHotkeyRole).value<CActionHotkey>();
100 const CActionHotkey selectedActionHotkey =
101 CHotkeyDialog::getActionHotkey(actionHotkey, getAllIdentifiers(), this);
102 if (selectedActionHotkey.isValid() && checkAndConfirmConflicts(selectedActionHotkey, { actionHotkey }))
103 {
104 updateHotkeyInSettings(actionHotkey, selectedActionHotkey);
105 m_model.setData(indexHotkey, QVariant::fromValue(selectedActionHotkey),
106 CActionHotkeyListModel::ActionHotkeyRole);
107
108 // T784, further info about the "key"/button
109 CLogMessage(this).debug(u"%1, edited key: '%2'")
110 << classNameShort(this) << selectedActionHotkey.toQString(true);
111 }
112 this->resizeView();
113 }
114
115 void CSettingsHotkeyComponent::removeEntry()
116 {
117 const QModelIndexList indexes = ui->tv_Hotkeys->selectionModel()->selectedRows();
118 for (const auto &index : indexes)
119 {
120 const auto actionHotkey = index.data(CActionHotkeyListModel::ActionHotkeyRole).value<CActionHotkey>();
121 removeHotkeyFromSettings(actionHotkey);
122 m_model.removeRows(index.row(), 1, QModelIndex());
123 }
124 this->resizeView();
125 }
126
127 void CSettingsHotkeyComponent::addHotkeyToSettings(const CActionHotkey &actionHotkey)
128 {
129 CActionHotkeyList actionHotkeyList(m_actionHotkeys.getThreadLocal());
130 actionHotkeyList.push_back(actionHotkey);
131 m_actionHotkeys.set(actionHotkeyList);
132 }
133
134 void CSettingsHotkeyComponent::updateHotkeyInSettings(const CActionHotkey &oldValue, const CActionHotkey &newValue)
135 {
137 this->removeHotkeyFromSettings(oldValue);
138 this->addHotkeyToSettings(newValue);
139
140 // CActionHotkeyList actionHotkeyList(m_actionHotkeys.getThreadLocal());
141 // actionHotkeyList.replace(oldValue, newValue);
142 // m_actionHotkeys.set(actionHotkeyList);
143 }
144
145 void CSettingsHotkeyComponent::removeHotkeyFromSettings(const CActionHotkey &actionHotkey)
146 {
147 CActionHotkeyList actionHotkeyList(m_actionHotkeys.getThreadLocal());
148 actionHotkeyList.remove(actionHotkey);
149 m_actionHotkeys.set(actionHotkeyList);
150 }
151
152 bool CSettingsHotkeyComponent::checkAndConfirmConflicts(const CActionHotkey &actionHotkey,
153 const CActionHotkeyList &ignore)
154 {
155 // check the hotkeys of the same machine only
156 // and avoid duplicates (replace or add)
157 const CActionHotkeyList configuredHotkeysSameMachine =
158 m_actionHotkeys.getThreadLocal().findBySameMachine(actionHotkey);
159 CActionHotkeyList conflicts = configuredHotkeysSameMachine.findSupersetsOf(actionHotkey);
160 conflicts.replaceOrAdd(configuredHotkeysSameMachine.findSubsetsOf(actionHotkey));
161 conflicts.removeIfIn(ignore.findBySameMachine(actionHotkey));
162
163 if (!conflicts.isEmpty())
164 {
165 QString message =
166 QStringLiteral("The selected combination conflicts with the following %1 combination(s):\n\n")
167 .arg(conflicts.size());
168 for (const CActionHotkey &conflict : conflicts)
169 {
170 message += conflict.toQString();
171 message += "\n";
172 }
173 message += "\n Do you want to use it anway?";
174 const auto reply = QMessageBox::warning(this, "SettingsHotkeyComponent", message,
176 if (reply == QMessageBox::No)
177 {
178 return false;
179 }
180 }
181 return true;
182 }
183
185 {
186 const CActionHotkeyList hotkeys = m_actionHotkeys.getThreadLocal();
187 m_model.clear();
188
189 // list of all defined hotkeys (not the dialog)
190 for (const CActionHotkey &hotkey : hotkeys)
191 {
192 const int position = m_model.rowCount();
193 m_model.insertRows(position, 1, QModelIndex());
194 const QModelIndex index = m_model.index(position, 0, QModelIndex());
195 m_model.setData(index, QVariant::fromValue(hotkey), CActionHotkeyListModel::ActionHotkeyRole);
196 }
197 this->resizeView();
198 }
199
200 CIdentifierList CSettingsHotkeyComponent::getAllIdentifiers() const
201 {
202 CIdentifierList identifiers;
203 if (!sGui || !sGui->getIContextApplication())
204 {
205 return identifiers;
206 }
207 if (sGui->getIContextApplication())
208 {
209 identifiers = sGui->getIContextApplication()->getRegisteredApplications();
210 }
211
212 // add local application
213 identifiers.push_back(CIdentifier("local identifer for hotkeys"));
214 return identifiers;
215 }
216
217 void CSettingsHotkeyComponent::resizeView() { ui->tv_Hotkeys->resizeRowsToContents(); }
218
219 void CSettingsHotkeyComponent::hotkeySlot(bool keyDown)
220 {
221 if (keyDown)
222 {
223 auto *msgBox = new QMessageBox(this);
224 msgBox->setAttribute(Qt::WA_DeleteOnClose);
225 msgBox->setStandardButtons(QMessageBox::Ok);
226 msgBox->setWindowTitle("Test");
227 msgBox->setText("Hotkey test");
228 msgBox->setIcon(QMessageBox::Information);
229 msgBox->setModal(false);
230 msgBox->open();
231 }
232 }
233
235 {
236 Q_ASSERT_X(m_config, Q_FUNC_INFO, "Missing configuration");
238 {
239 return true;
240 }
241 m_config->saveSettings();
242 return true;
243 }
244
246 {
247 Q_ASSERT_X(m_config, Q_FUNC_INFO, "Missing configuration");
248 m_config->reloadHotkeysFromSettings();
249 }
250} // namespace swift::gui::components
static bool lastWizardStepSkipped(const QWizard *standardWizard)
Static version of CConfigurationWizard::lastStepSkipped.
static swift::misc::input::CActionHotkey getActionHotkey(const swift::misc::input::CActionHotkey &initial, const swift::misc::CIdentifierList &identifiers, QWidget *parent=nullptr)
Runs the hotkey dialog and returns the result.
void registerDummyPttEntry()
Create dummy/emtpy Ptt entry for wizard.
CSettingsHotkeyComponent(QWidget *parent=nullptr)
Constructor.
int rowCount(const QModelIndex &parent=QModelIndex()) const
bool setData(const QModelIndex &index, const QVariant &var, int role)
bool insertRows(int position, int rows, const QModelIndex &index)
Value object encapsulating information identifying a component of a modular distributed swift process...
Definition identifier.h:29
Value object encapsulating a list of object identifiers.
Class for emitting a log message.
Definition logmessage.h:27
static void preformatted(const CStatusMessage &statusMessage)
Sends a verbatim, preformatted message to the log.
Derived & debug()
Set the severity to debug.
size_type size() const
Returns number of elements in the sequence.
Definition sequence.h:273
void replaceOrAdd(const T &original, const T &replacement)
Replace elements matching the given element. If there is no match, push the new element on the end.
Definition sequence.h:533
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
void clear()
Removes all elements in the sequence.
Definition sequence.h:288
int removeIfIn(const CSequence &other)
Remove all elements if they are in other.
Definition sequence.h:476
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
Streamable status message, e.g.
Value object encapsulating a action hotkey.
bool isValid() const
Is hotkey valid?
Value object encapsulating a list of hotkeys.
CActionHotkeyList findBySameMachine(const CActionHotkey &key) const
Find hotkeys for the same machine.
CActionHotkeyList findSupersetsOf(const CActionHotkey &other) const
Returns true if this list has a hotkey with a combination for which other is a superset of other Exam...
CActionHotkeyList findSubsetsOf(const CActionHotkey &other) const
Returns true if this list has a action hotkey with a combination which is a subset of other Example: ...
QString toQString(bool i18n=false) const
Cast as QString.
Definition mixinstring.h:74
Backend services of the swift project, like dealing with the network or the simulators.
Definition actionbind.cpp:7
High level reusable GUI components.
Models to be used with views, mainly QTableView.
Free functions in swift::misc.
QString classNameShort(const QObject *object)
Class name as from QMetaObject::className without namespace.
void clicked(bool checked)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
QFrame(QWidget *parent, Qt::WindowFlags f)
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton)
QVariant data(int role) const const
bool isValid() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
WA_DeleteOnClose
bool canConvert() const const
QVariant fromValue(T &&value)
T value() const &const
QWidget(QWidget *parent, Qt::WindowFlags f)
QWizard * wizard() const const