Seamly2D
Code documentation
vshortcutpropertyeditor.cpp
Go to the documentation of this file.
1 /************************************************************************
2  **
3  ** @file vshortcutpropertyeditor.cpp
4  ** @author hedgeware <internal(at)hedgeware.net>
5  ** @date
6  **
7  ** @brief
8  ** @copyright
9  ** All rights reserved. This program and the accompanying materials
10  ** are made available under the terms of the GNU Lesser General Public License
11  ** (LGPL) version 2.1 which accompanies this distribution, and is available at
12  ** http://www.gnu.org/licenses/lgpl-2.1.html
13  **
14  ** This library is distributed in the hope that it will be useful,
15  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  ** Lesser General Public License for more details.
18  **
19  *************************************************************************/
20 
22 
23 #include <QEvent>
24 #include <QHBoxLayout>
25 #include <QKeyEvent>
26 #include <QLineEdit>
27 #include <QSizePolicy>
28 #include <Qt>
29 
31  : QWidget(parent), CurrentKeySequence(), LineEdit(nullptr)
32 {
33  // Create the line edit widget
34  LineEdit = new QLineEdit(this);
35  LineEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
36  LineEdit->clear();
37  LineEdit->installEventFilter(this);
38  setFocusProxy(LineEdit);
39  connect(LineEdit, &QLineEdit::textEdited, this, &VShortcutEditWidget::onTextEdited);
40 
41  // The layout (a horizontal layout)
42  QHBoxLayout* layout = new QHBoxLayout(this);
43  layout->setSpacing(0);
44  layout->setMargin(0);
45  layout->addWidget(LineEdit);
46 }
47 
48 
50 {
51  // nothing needs to be done here
52 }
53 
54 bool VPE::VShortcutEditWidget::eventFilter(QObject *obj, QEvent *event)
55 {
56  if (obj == LineEdit)
57  {
58  if (event->type() == QEvent::KeyPress)
59  {
60  QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
61 
62  int keys = keyEvent->key();
63 
64  if (keys != Qt::Key_Shift &&
65  keys != Qt::Key_Control &&
66  keys != Qt::Key_Meta &&
67  keys != Qt::Key_AltGr &&
68  keys != Qt::Key_Alt)
69  {
70  keys += keyEvent->modifiers();
71  setShortcut(QKeySequence(keys), true);
72  return true;
73  }
74  }
75  }
76 
77  return QWidget::eventFilter(obj, event);
78 }
79 
81 {
82  return CurrentKeySequence.toString();
83 }
84 
85 // cppcheck-suppress unusedFunction
87 {
88  return CurrentKeySequence;
89 }
90 
91 void VPE::VShortcutEditWidget::setShortcut(const QString &shortcut, bool emit_signal)
92 {
93  setShortcut(QKeySequence::fromString(shortcut), emit_signal);
94 }
95 
96 void VPE::VShortcutEditWidget::setShortcut(const QKeySequence &shortcut, bool emit_signal)
97 {
98  if (shortcut != CurrentKeySequence)
99  {
100  CurrentKeySequence = shortcut;
101  LineEdit->setText(CurrentKeySequence.toString());
102  if (emit_signal)
103  {
104  emit dataChangedByUser(CurrentKeySequence, this);
105  }
106  }
107 }
108 
109 void VPE::VShortcutEditWidget::onTextEdited(const QString &text)
110 {
111  setShortcut(text, true);
112 }
void setShortcut(const QString &shortcut, bool emit_signal)
Sets the shortcut.
QLineEdit * LineEdit
The line to display and edit the key sequence.
void onTextEdited(const QString &text)
This slot is called when the user edits the line edit (e.g. by removing or pasting text using the mou...
virtual ~VShortcutEditWidget() Q_DECL_OVERRIDE
virtual bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE
Needed for proper event handling.
QKeySequence getShortcut()
Returns the currently set shortcut.
QString getShortcutAsString() const
Returns the currently set shortcut.