00001 /*========================================================================= 00002 00003 Program: Medical Imaging & Interaction Toolkit 00004 Language: C++ 00005 Date: $Date: 2009-07-07 16:57:15 +0200 (Di, 07 Jul 2009) $ 00006 Version: $Revision: 18019 $ 00007 00008 Copyright (c) German Cancer Research Center, Division of Medical and 00009 Biological Informatics. All rights reserved. 00010 See MITKCopyright.txt or https://www.mitk.org/copyright.html for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 00018 #include "QmitkHotkeyLineEdit.h" 00019 00020 #include <QLabel> 00021 #include <QPushButton> 00022 #include <QGridLayout> 00023 #include <QKeyEvent> 00024 00025 const std::string QmitkHotkeyLineEdit::TOOLTIP = "Press any key (combination)"; 00026 00027 QmitkHotkeyLineEdit::QmitkHotkeyLineEdit( QWidget* parent ) 00028 : QLineEdit(parent) 00029 { 00030 this->Init(); 00031 //this->setReadOnly(true); 00032 } 00033 00034 QmitkHotkeyLineEdit::QmitkHotkeyLineEdit( const QKeySequence& _QKeySequence, QWidget* parent) 00035 : QLineEdit(parent) 00036 { 00037 this->Init(); 00038 //this->setReadOnly(true); 00039 this->SetKeySequence(_QKeySequence); 00040 } 00041 00042 QmitkHotkeyLineEdit::QmitkHotkeyLineEdit( const QString& _QKeySequenceAsString, QWidget* parent) 00043 : QLineEdit(parent) 00044 { 00045 this->Init(); 00046 //this->setReadOnly(true); 00047 this->SetKeySequence(_QKeySequenceAsString); 00048 } 00049 00050 void QmitkHotkeyLineEdit::Init() 00051 { 00052 this->setToolTip(QString::fromStdString(QmitkHotkeyLineEdit::TOOLTIP)); 00053 this->setReadOnly(true); 00054 connect( this, SIGNAL( textChanged(const QString &) ), this, 00055 SLOT( LineEditTextChanged(const QString &) ) ); 00056 } 00057 00058 void QmitkHotkeyLineEdit::keyPressEvent( QKeyEvent * event ) 00059 { 00060 if(event->key() == Qt::Key_unknown) 00061 return; 00062 00063 m_KeySequence = QKeySequence(event->modifiers(), event->key()); 00064 // if no modifier was pressed the sequence is now empty 00065 if(event->modifiers() == Qt::NoModifier) 00066 m_KeySequence = QKeySequence(event->key()); 00067 00068 this->SetKeySequence(m_KeySequence); 00069 } 00070 00071 void QmitkHotkeyLineEdit::SetKeySequence( const QKeySequence& _QKeySequence) 00072 { 00073 this->setText(_QKeySequence.toString()); 00074 } 00075 00076 void QmitkHotkeyLineEdit::SetKeySequence( const QString& _QKeySequenceAsString ) 00077 { 00078 this->SetKeySequence(QKeySequence(_QKeySequenceAsString)); 00079 } 00080 00081 QKeySequence QmitkHotkeyLineEdit::GetKeySequence() 00082 { 00083 return m_KeySequence; 00084 } 00085 00086 QString QmitkHotkeyLineEdit::GetKeySequenceAsString() 00087 { 00088 return m_KeySequence.toString(); 00089 } 00090 00091 bool QmitkHotkeyLineEdit::Matches( QKeyEvent * event ) 00092 { 00093 QKeySequence _KeySequence = QKeySequence(event->modifiers(), event->key()); 00094 // if no modifier was pressed the sequence is now empty 00095 if(event->modifiers() == Qt::NoModifier) 00096 _KeySequence = QKeySequence(event->key()); 00097 00098 return _KeySequence == m_KeySequence; 00099 } 00100 00101 void QmitkHotkeyLineEdit::LineEditTextChanged(const QString & text) 00102 { 00103 m_KeySequence = QKeySequence(text.toUpper()); 00104 }