KXmlGui

kshortcuteditwidget.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 1998 Mark Donohoe <[email protected]>
4  SPDX-FileCopyrightText: 1997 Nicolas Hadacek <[email protected]>
5  SPDX-FileCopyrightText: 1998 Matthias Ettrich <[email protected]>
6  SPDX-FileCopyrightText: 2001 Ellis Whitehead <[email protected]>
7  SPDX-FileCopyrightText: 2006 Hamish Rodda <[email protected]>
8  SPDX-FileCopyrightText: 2007 Roberto Raggi <[email protected]>
9  SPDX-FileCopyrightText: 2007 Andreas Hartmetz <[email protected]>
10 
11  SPDX-License-Identifier: LGPL-2.0-or-later
12 */
13 
14 #include "config-xmlgui.h"
15 #include "kshortcutsdialog_p.h"
16 
17 #include <QAction>
18 #include <QPainter>
19 #include <QPen>
20 #include <QGridLayout>
21 #include <QRadioButton>
22 #include <QLabel>
23 
24 #include <klocalizedstring.h>
25 #if HAVE_GLOBALACCEL
26 # include <kglobalaccel.h>
27 #endif
28 
29 #include "kkeysequencewidget.h"
30 
31 void TabConnectedWidget::paintEvent(QPaintEvent *e)
32 {
34  QPainter p(this);
35  QPen pen(QPalette().highlight().color());
36  pen.setWidth(6);
37  p.setPen(pen);
38  p.drawLine(0, 0, width(), 0);
39  if (qApp->isLeftToRight()) {
40  p.drawLine(0, 0, 0, height());
41  } else {
42  p.drawLine(width(), 0, width(), height());
43  }
44 }
45 
46 ShortcutEditWidget::ShortcutEditWidget(QWidget *viewport, const QKeySequence &defaultSeq,
47  const QKeySequence &activeSeq, bool allowLetterShortcuts)
48  : TabConnectedWidget(viewport),
49  m_defaultKeySequence(defaultSeq),
50  m_isUpdating(false),
51  m_action(nullptr),
52  m_noneText(i18nc("No shortcut defined", "None"))
53 {
54  QGridLayout *layout = new QGridLayout(this);
55 
56  m_defaultRadio = new QRadioButton(i18nc("@option:radio", "Default:"), this);
57  m_defaultLabel = new QLabel(m_noneText, this);
58  const QString defaultText = defaultSeq.toString(QKeySequence::NativeText);
59  if (!defaultText.isEmpty()) {
60  m_defaultLabel->setText(defaultText);
61  }
62 
63  m_customRadio = new QRadioButton(i18nc("@option:radio", "Custom:"), this);
64  m_customEditor = new KKeySequenceWidget(this);
65  m_customEditor->setModifierlessAllowed(allowLetterShortcuts);
66 
67  layout->addWidget(m_defaultRadio, 0, 0);
68  layout->addWidget(m_defaultLabel, 0, 1);
69  layout->addWidget(m_customRadio, 1, 0);
70  layout->addWidget(m_customEditor, 1, 1);
71  layout->setColumnStretch(2, 1);
72 
73  setKeySequence(activeSeq);
74 
75  connect(m_defaultRadio, &QRadioButton::toggled,
76  this, &ShortcutEditWidget::defaultToggled);
77  connect(m_customEditor, &KKeySequenceWidget::keySequenceChanged,
78  this, &ShortcutEditWidget::setCustom);
79  connect(m_customEditor, &KKeySequenceWidget::stealShortcut,
80  this, &ShortcutEditWidget::stealShortcut);
81 #if HAVE_GLOBALACCEL
83  this, [this](QAction *action, const QKeySequence &seq) {
84  if (action != m_action) {
85  return;
86  }
87  setKeySequence(seq);
88  }
89  );
90 #endif
91 }
92 
93 KKeySequenceWidget::ShortcutTypes ShortcutEditWidget::checkForConflictsAgainst() const
94 {
95  return m_customEditor->checkForConflictsAgainst();
96 }
97 
98 //slot
99 void ShortcutEditWidget::defaultToggled(bool checked)
100 {
101  if (m_isUpdating) {
102  return;
103  }
104 
105  m_isUpdating = true;
106  if (checked) {
107  // The default key sequence should be activated. We check first if this is
108  // possible.
109  if (m_customEditor->isKeySequenceAvailable(m_defaultKeySequence)) {
110  // Clear the customs widget
111  m_customEditor->clearKeySequence();
112  emit keySequenceChanged(m_defaultKeySequence);
113  } else {
114  // We tried to switch to the default key sequence and failed. Go
115  // back.
116  m_customRadio->setChecked(true);
117  }
118  } else {
119  // The empty key sequence is always valid
120  emit keySequenceChanged(QKeySequence());
121  }
122  m_isUpdating = false;
123 }
124 
125 void ShortcutEditWidget::setCheckActionCollections(
126  const QList<KActionCollection *> &checkActionCollections)
127 {
128  // We just forward them to out KKeySequenceWidget.
129  m_customEditor->setCheckActionCollections(checkActionCollections);
130 }
131 
132 void ShortcutEditWidget::setCheckForConflictsAgainst(KKeySequenceWidget::ShortcutTypes types)
133 {
134  m_customEditor->setCheckForConflictsAgainst(types);
135 }
136 
137 void ShortcutEditWidget::setComponentName(const QString &componentName)
138 {
139  m_customEditor->setComponentName(componentName);
140 }
141 
142 void ShortcutEditWidget::setMultiKeyShortcutsAllowed(bool allowed)
143 {
144  // We just forward them to out KKeySequenceWidget.
145  m_customEditor->setMultiKeyShortcutsAllowed(allowed);
146 }
147 
148 bool ShortcutEditWidget::multiKeyShortcutsAllowed() const
149 {
150  return m_customEditor->multiKeyShortcutsAllowed();
151 }
152 
153 void ShortcutEditWidget::setAction(QObject *action)
154 {
155  m_action = action;
156 }
157 
158 //slot
159 void ShortcutEditWidget::setCustom(const QKeySequence &seq)
160 {
161  if (m_isUpdating) {
162  return;
163  }
164 
165  // seq is a const reference to a private variable of KKeySequenceWidget.
166  // Somewhere below we possible change that one. But we want to emit seq
167  // whatever happens. So we make a copy.
168  QKeySequence original = seq;
169 
170  m_isUpdating = true;
171 
172  // Check if the user typed in the default sequence into the custom field.
173  // We do this by calling setKeySequence which will do the right thing.
174  setKeySequence(original);
175 
176  emit keySequenceChanged(original);
177  m_isUpdating = false;
178 }
179 
180 void ShortcutEditWidget::setKeySequence(const QKeySequence &activeSeq)
181 {
182  const QString seqString = activeSeq.isEmpty() ? m_noneText
183  : activeSeq.toString(QKeySequence::NativeText);
184  if (seqString == m_defaultLabel->text()) {
185  m_defaultRadio->setChecked(true);
186  m_customEditor->clearKeySequence();
187  } else {
188  m_customRadio->setChecked(true);
189  // m_customEditor->setKeySequence does some stuff we only want to
190  // execute when the sequence really changes.
191  if (activeSeq != m_customEditor->keySequence()) {
192  m_customEditor->setKeySequence(activeSeq);
193  }
194  }
195 }
196 
A widget to input a QKeySequence.
void globalShortcutChanged(QAction *action, const QKeySequence &seq)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void stealShortcut(const QKeySequence &seq, QAction *action)
This signal is emitted after the user agreed to steal a shortcut from an action.
virtual void paintEvent(QPaintEvent *event)
void keySequenceChanged(const QKeySequence &seq)
This signal is emitted when the current key sequence has changed, be it by user input or programmatic...
QString i18nc(const char *context, const char *text, const TYPE &arg...)
bool isEmpty() const const
bool isEmpty() const const
static KGlobalAccel * self()
QString toString(QKeySequence::SequenceFormat format) const const
void setColumnStretch(int column, int stretch)
void toggled(bool checked)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Wed Aug 12 2020 22:50:46 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.