Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
QmitkHotkeyLineEdit.cpp
Go to the documentation of this file.
1 /*===================================================================
2 
3 The Medical Imaging Interaction Toolkit (MITK)
4 
5 Copyright (c) German Cancer Research Center,
6 Division of Medical and Biological Informatics.
7 All rights reserved.
8 
9 This software is distributed WITHOUT ANY WARRANTY; without
10 even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 A PARTICULAR PURPOSE.
12 
13 See LICENSE.txt or http://www.mitk.org for details.
14 
15 ===================================================================*/
16 
17 #include "QmitkHotkeyLineEdit.h"
18 
19 #include <QGridLayout>
20 #include <QKeyEvent>
21 #include <QLabel>
22 #include <QPushButton>
23 
24 const std::string QmitkHotkeyLineEdit::TOOLTIP = "Press any key (combination)";
25 
26 QmitkHotkeyLineEdit::QmitkHotkeyLineEdit(QWidget *parent) : QLineEdit(parent)
27 {
28  this->Init();
29 }
30 
31 QmitkHotkeyLineEdit::QmitkHotkeyLineEdit(const QKeySequence &_QKeySequence, QWidget *parent) : QLineEdit(parent)
32 {
33  this->Init();
34  this->SetKeySequence(_QKeySequence);
35 }
36 
37 QmitkHotkeyLineEdit::QmitkHotkeyLineEdit(const QString &_QKeySequenceAsString, QWidget *parent) : QLineEdit(parent)
38 {
39  this->Init();
40  this->SetKeySequence(_QKeySequenceAsString);
41 }
42 
44 {
45  this->setToolTip(QString::fromStdString(QmitkHotkeyLineEdit::TOOLTIP));
46  this->setReadOnly(true);
47  connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(LineEditTextChanged(const QString &)));
48 }
49 
50 void QmitkHotkeyLineEdit::keyPressEvent(QKeyEvent *event)
51 {
52  if (event->key() == Qt::Key_unknown)
53  return;
54  else if (event->key() == Qt::Key_Escape)
55  m_KeySequence = QKeySequence();
56 
57  else
58  {
59  m_KeySequence = QKeySequence(event->modifiers(), event->key());
60  // if no modifier was pressed the sequence is now empty
61  if (event->modifiers() == Qt::NoModifier)
62  m_KeySequence = QKeySequence(event->key());
63  }
64 
66 }
67 
68 void QmitkHotkeyLineEdit::SetKeySequence(const QKeySequence &_QKeySequence)
69 {
70  this->setText(_QKeySequence.toString());
71 }
72 
73 void QmitkHotkeyLineEdit::SetKeySequence(const QString &_QKeySequenceAsString)
74 {
75  this->SetKeySequence(QKeySequence(_QKeySequenceAsString));
76 }
77 
79 {
80  return m_KeySequence;
81 }
82 
84 {
85  return m_KeySequence.toString();
86 }
87 
88 bool QmitkHotkeyLineEdit::Matches(QKeyEvent *event)
89 {
90  QKeySequence _KeySequence = QKeySequence(event->modifiers(), event->key());
91  // if no modifier was pressed the sequence is now empty
92  if (event->modifiers() == Qt::NoModifier)
93  _KeySequence = QKeySequence(event->key());
94 
95  return _KeySequence == m_KeySequence;
96 }
97 
99 {
100  m_KeySequence = QKeySequence(text.toUpper());
101 }
bool Matches(QKeyEvent *event)
virtual QString GetKeySequenceAsString()
virtual void keyPressEvent(QKeyEvent *event) override
static const std::string TOOLTIP
virtual QKeySequence GetKeySequence()
QmitkHotkeyLineEdit(QWidget *parent=nullptr)
void LineEditTextChanged(const QString &)
virtual void SetKeySequence(const QKeySequence &_QKeySequence)