Debian Codegraph @

src/librssguard/dynamic-shortcuts/dynamicshortcutswidget.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 #include "dynamic-shortcuts/dynamicshortcutswidget.h"
4
5 #include "definitions/definitions.h"
6 #include "dynamic-shortcuts/shortcutcatcher.h"
7 #include "gui/messagebox.h"
8
9 #include <QAction>
10 #include <QGridLayout>
11 #include <QLabel>
12
13 DynamicShortcutsWidget::DynamicShortcutsWidget(QWidget* parent) : QWidget(parent) {
14 // Create layout for this control and set is as active.
15 m_layout = new QGridLayout(this);
16 m_layout->setContentsMargins({});
17
18 setLayout(m_layout);
19 }
20
21 DynamicShortcutsWidget::~DynamicShortcutsWidget() {
22 delete m_layout;
23 }
24
25 bool DynamicShortcutsWidget::areShortcutsUnique() const {
26 QList<QKeySequence> all_shortcuts;
27
28 // Obtain all shortcuts.
29 for (ShortcutCatcher* catcher : m_actionBindings) {
30 const QKeySequence new_shortcut = catcher->shortcut();
31
32 if (!new_shortcut.isEmpty() && all_shortcuts.contains(new_shortcut)) {
33 // Problem, two identical non-empty shortcuts found.
34 return false;
35 }
36 else {
37 all_shortcuts.append(catcher->shortcut());
38 }
39 }
40
41 return true;
42 }
43
44 void DynamicShortcutsWidget::updateShortcuts() {
45 for (ShortcutCatcher* catcher : std::as_const(m_actionBindings)) {
46 catcher->action()->setShortcut(catcher->shortcut());
47 }
48 }
49
50 void DynamicShortcutsWidget::populate(QList<QAction*> actions) {
51 m_actionBindings.clear();
52 std::sort(actions.begin(), actions.end(), [](QAction* lhs, QAction* rhs) {
53 return QString::localeAwareCompare(lhs->text().replace(QL1S("&"), QString()),
54 rhs->text().replace(QL1S("&"), QString())) < 0;
55 });
56 int row_id = 0;
57
58 for (QAction* action : actions) {
59 // Create shortcut catcher for this action and set default shortcut.
60 auto* catcher = new ShortcutCatcher(this);
61
62 catcher->setAction(action);
63 catcher->setDefaultShortcut(action->shortcut());
64
65 if (!action->shortcut().isEmpty()) {
66 m_assignedShortcuts.insert(action->shortcut(), catcher);
67 }
68
69 m_actionBindings.append(catcher);
70
71 // Add new catcher to our control.
72 auto* action_label = new QLabel(this);
73 auto act_text = action->text().remove(QSL("&"));
74 auto act_toolt = action->toolTip();
75
76 if (act_toolt.isEmpty() || act_text == act_toolt) {
77 action_label->setText(act_text);
78 }
79 else {
80 action_label->setText(act_toolt);
81 // action_label->setText(QSL("%1 (%2)").arg(act_text, act_toolt));
82 }
83
84 action_label->setToolTip(action->toolTip());
85 action_label->setWordWrap(true);
86
87 // action_label->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
88
89 auto* action_icon = new QLabel(this);
90
91 action_icon->setPixmap(action->icon().pixmap(ICON_SIZE_SETTINGS, ICON_SIZE_SETTINGS));
92 action_icon->setToolTip(action->toolTip());
93
94 m_layout->addWidget(action_icon, row_id, 0);
95 m_layout->addWidget(action_label, row_id, 1);
96 m_layout->addWidget(catcher, row_id, 2);
97
98 row_id++;
99 connect(catcher, &ShortcutCatcher::shortcutChanged, this, &DynamicShortcutsWidget::onShortcutChanged);
100 }
101
102 // Make sure that "spacer" is added.
103 m_layout->setRowStretch(row_id, 1);
104 m_layout->setColumnStretch(1, 1);
105 }
106
107 void DynamicShortcutsWidget::onShortcutChanged(const QKeySequence& sequence) {
108 ShortcutCatcher* catcher = qobject_cast<ShortcutCatcher*>(sender());
109 QKeySequence assigned_sequence = m_assignedShortcuts.key(catcher);
110
111 qDebugNN << catcher->action()->text();
112
113 // We remove currently assigned sequence to this catcher.
114 m_assignedShortcuts.remove(assigned_sequence);
115
116 if (!sequence.isEmpty()) {
117 // Now we check if any other catcher has assigned the same sequence.
118 ShortcutCatcher* conflicting_catcher = m_assignedShortcuts.value(sequence);
119
120 if (conflicting_catcher != nullptr) {
121 // We found conflicting catcher.
122 catcher->blockSignals(true);
123
124 QMessageBox::StandardButton decision =
125 MsgBox::show(this,
126 QMessageBox::Icon::Critical,
127 tr("Duplicate shortcut"),
128 tr("There is another action which has the same shortcut assigned."),
129 tr("Do you want to keep the new shortcut assignment and clear the previous shortcut?"),
130 conflicting_catcher->action()->text().remove(QSL("&")),
131 QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No,
132 QMessageBox::StandardButton::Yes);
133
134 catcher->blockSignals(false);
135
136 if (decision == QMessageBox::StandardButton::Yes) {
137 // We keep the new shortcut and reset old conflicting action.
138 m_assignedShortcuts.insert(sequence, catcher);
139 conflicting_catcher->clearShortcut();
140 }
141 else {
142 // We discard new shortcut.
143 catcher->clearShortcut();
144 }
145 }
146 else {
147 m_assignedShortcuts.insert(sequence, catcher);
148 }
149 }
150
151 emit setupChanged();
152 }