15 #include <QAbstractItemView>
19 #include <QHBoxLayout>
20 #include <QHeaderView>
21 #include <QKeySequence>
24 #include <QPushButton>
25 #include <QRadioButton>
26 #include <QSizePolicy>
27 #include <QSpacerItem>
30 #include <QTableWidget>
31 #include <QTableWidgetItem>
32 #include <QVBoxLayout>
34 #include "../displaywindow/btactioncollection.h"
35 #include "../../util/btconnect.h"
42 auto i = input.indexOf(QLatin1Char(
'&'));
46 auto out = input.mid(0, i);
47 decltype(i) nextFragmentStart;
49 nextFragmentStart = i + 1;
50 i = input.indexOf(QLatin1Char(
'&'), nextFragmentStart + 1);
51 out.append(input.mid(nextFragmentStart, i - nextFragmentStart));
63 QKeySequence defaultKeys)
65 , m_defaultKeys(std::move(defaultKeys))
68 setIcon(action->icon());
69 setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
73 m_newFirstHotkey = list.at(0);
75 m_newSecondHotkey = list.at(1);
82 if (!m_newFirstHotkey.isEmpty())
83 list << m_newFirstHotkey;
84 if (!m_newSecondHotkey.isEmpty())
85 list << m_newSecondHotkey;
87 m_action->setShortcuts(list);
92 template <
typename ... Args>
94 { m_newFirstHotkey = QKeySequence(std::forward<Args>(args)...); }
96 template <
typename ... Args>
98 { m_newSecondHotkey = QKeySequence(std::forward<Args>(args)...); }
101 m_newFirstHotkey = QKeySequence();
102 m_newSecondHotkey = QKeySequence();
123 :
QWidget(parent), m_dlg(new
BtShortcutsDialog(this)), m_table(nullptr), m_shortcutChooser(nullptr), m_noneButton(nullptr), m_defaultButton(nullptr),
124 m_customButton(nullptr), m_defaultLabelValue(nullptr), m_currentRow(-1) {
126 QVBoxLayout *
const vBox =
new QVBoxLayout(
this);
130 m_table =
new QTableWidget{
this};
132 m_table->setAlternatingRowColors(
true);
133 m_table->setSelectionBehavior(QAbstractItemView::SelectRows);
134 m_table->setSelectionMode(QAbstractItemView::SingleSelection);
135 m_table->setHorizontalHeaderLabels({tr(
"Action"),
138 m_table->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive);
139 m_table->horizontalHeader()->resizeSection(0, 180);
140 m_table->horizontalHeader()->resizeSection(1, 100);
141 m_table->horizontalHeader()->setStretchLastSection(
true);
142 m_table->verticalHeader()->setVisible(
false);
150 new QGroupBox(tr(
"Shortcut for selected action name"),
this);
155 QHBoxLayout *
const hLayout =
new QHBoxLayout();
156 vLayout->addLayout(hLayout);
181 hLayout->addItem(
new QSpacerItem(1,
183 QSizePolicy::Expanding,
184 QSizePolicy::Minimum));
187 QHBoxLayout *
const hLayout =
new QHBoxLayout();
188 vLayout->addLayout(hLayout);
195 hLayout->addItem(
new QSpacerItem(1,
197 QSizePolicy::Expanding,
198 QSizePolicy::Minimum));
202 for (
auto *
const action : collection->
actions()) {
203 int const count =
m_table->rowCount();
208 new BtShortcutsEditorItem(
213 m_table->setItem(count, 0, item);
225 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
226 item->setToolTip(tr(
"Select to change key"));
227 if (keys.count() > 0)
228 item->setText(keys[0].toString());
229 m_table->setItem(count, 1, item);
239 item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
240 item->setToolTip(tr(
"Select to change key"));
241 if (keys.count() > 1)
242 item->setText(keys[1].toString());
243 m_table->setItem(count, 2, item);
254 [
this](QKeySequence
const & keys) {
263 int const rows =
m_table->rowCount();
264 for (
int row = 0; row < rows; row++)
266 btItem->commitChanges();
273 auto const defaultKeys = item.getDefaultKeys();
278 QString shortcut = item1->text();
281 QString alternate = item2->text();
283 QString bothKeys = shortcut;
284 if (!alternate.isEmpty())
285 bothKeys = bothKeys + QStringLiteral(
"; ") + alternate;
288 if ( bothKeys == defaultKeys.toString())
290 else if (bothKeys.isEmpty())
304 item.deleteHotkeys();
305 item.setFirstHotkey();
317 auto const defaultKeys = item.getDefaultKeys();
318 item.deleteHotkeys();
319 item.setFirstHotkey(defaultKeys);
336 int code =
m_dlg->exec();
337 if (code == QDialog::Accepted) {
340 if (newPriKeys == newAltKeys)
341 newAltKeys = QString();
343 item.setFirstHotkey(newPriKeys);
344 item.setSecondHotkey(newAltKeys);
357 auto const keyString(keys.toString());
358 for (
int row = 0; row <
m_table->rowCount(); row++) {
360 if (
m_table->item(row, 1)->text() == keyString) {
361 m_table->item(row, 1)->setText(QString());
362 item.setFirstHotkey();
364 if (
m_table->item(row, 2)->text() == keyString) {
365 m_table->item(row, 2)->setText(QString());
366 item.setSecondHotkey();
374 auto const keyString(keys.toString());
375 for (
int i = 0; i <
m_table->rowCount(); i++)
376 if ((
m_table->item(i, 1)->text() == keyString)
377 || (
m_table->item(i, 2)->text() == keyString))
378 return m_table->item(i, 0)->text();
QMap< QString, QAction * > const & actions() const noexcept
static QKeySequence getDefaultShortcut(QAction &action)
void changeSelectedShortcut(QKeySequence const &keys)
void keyChangeRequest(QKeySequence const &keys)
void setFirstKeys(const QString &keys)
void setSecondKeys(const QString &keys)
BtShortcutsEditor(BtActionCollection *collection, QWidget *parent)
void customButtonClicked(bool checked)
void clearConflictWithKeys(QKeySequence const &keys)
void keyChangeRequest(QString const &actionName, QKeySequence const &keys)
void defaultButtonClicked(bool checked)
QRadioButton * m_noneButton
QString findConflictWithKeys(QKeySequence const &keys) const
QRadioButton * m_customButton
void changeShortcutInDialog(QKeySequence const &keys)
QPushButton * m_customPushButton
QRadioButton * m_defaultButton
void slotSelectionChanged()
QGroupBox * m_shortcutChooser
QLabel * m_defaultLabelValue
void noneButtonClicked(bool checked)
BtShortcutsDialog * m_dlg
BtShortcutsEditorItem(QAction *action, QKeySequence defaultKeys)
void setFirstHotkey(Args &&... args)
void setSecondHotkey(Args &&... args)
void deleteHotkeys() noexcept
QKeySequence m_defaultKeys
QKeySequence m_newFirstHotkey
QKeySequence getDefaultKeys() const
QKeySequence m_newSecondHotkey
QString removeMnemonics(QString input)
auto getShortcutsEditor(QTableWidget const &tableWidget, int const row)