HAL
keybind_edit.cpp
Go to the documentation of this file.
4 
5 #include <QEvent>
6 #include <QKeyEvent>
7 #include <QStyle>
8 
9 namespace hal
10 {
12  : QKeySequenceEdit(parent), mItem(nullptr),
13  mSkipValidate(false), mValidated(true), mGrab(false)
14  {;}
15 
16  void KeybindEdit::setHasGrab(bool isgrab)
17  {
18  mGrab = isgrab;
19 
20  QStyle* s = style();
21  s->unpolish(this);
22  s->polish(this);
23 
24  }
25 
26  void KeybindEdit::setValidated(bool valid)
27  {
28  mValidated = valid;
29  }
30 
32  {
33  return mGrab;
34  }
35 
37  {
38  return mValidated;
39  }
40 
42  {
43  if (mSkipValidate) return true;
44  QKeySequence current = keySequence();
46  bool ok = (item == nullptr || item == mItem);
47 
48  setValidated(ok);
49  if (ok)
50  {
51  AssignedKeybindMap::instance()->assign(current,mItem,mOldSequence);
52  mOldSequence = current;
54  }
55  else
56  {
57  // revert
59  QString("<%1> is already assigned:\n<%2>")
60  .arg(current.toString())
61  .arg(item->label())));
62  restoreOldSequence();
63  }
64  return ok;
65  }
66 
68  {
69  QKeySequence previousSequence = keySequence();
70  mOldSequence = seq;
71  mItem = item;
73  AssignedKeybindMap::instance()->assign(seq, item, previousSequence);
74  }
75 
76  void KeybindEdit::restoreOldSequence()
77  {
78  mSkipValidate = true;
79  setKeySequence(mOldSequence);
80  mSkipValidate = false;
81  }
82 
84  {
85  bool recognized = true;
86  switch(e->type())
87  {
88  /*
89  * Make sure we have the keyboard exclusively.
90  * This suppresses defined keyboard shortcuts
91  * and lets us react properly if the user
92  * attempts to configure stupid keybinds like
93  * Alt-F4.
94  */
95  case QEvent::FocusIn:
96  grabKeyboard();
97  setHasGrab(true);
98  break;
99  case QEvent::FocusOut:
100  // FIXME this messes with the API. Better define a Validator that
101  // can actually handle QVariants and thus QKeySequences.
102  doValidate();
103  setHasGrab(false);
104  releaseKeyboard();
105  break;
106  case QEvent::KeyRelease:
107  // FIXME this messes with the API. Better define a Validator that
108  // can actually handle QVariants and thus QKeySequences.
109  doValidate();
110  break;
111  default:
112  recognized = false;
113  break;
114  }
115  recognized |= QKeySequenceEdit::event(e);
116  return recognized;
117  }
118 }
void assign(const QKeySequence &newkey, SettingsItemKeybind *setting, const QKeySequence &oldkey=QKeySequence())
SettingsItemKeybind * currentAssignment(const QKeySequence &needle) const
static AssignedKeybindMap * instance()
KeybindEdit(QWidget *parent=nullptr)
void setValidated(bool valid)
bool event(QEvent *e) override
void editRejected(QString errmsg)
void setHasGrab(bool isgrab)
void load(const QKeySequence &seq, SettingsItemKeybind *item)
virtual QString label() const
A SettingsItem to modify keybinds.
QEvent::Type type() const const
QString toString(QKeySequence::SequenceFormat format) const const
virtual bool event(QEvent *e) override
Q_EMITQ_EMIT
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
virtual void polish(QWidget *widget)
virtual void unpolish(QWidget *widget)
void grabKeyboard()
void releaseKeyboard()
QStyle * style() const const