From b7f65dc1263c9033f9b5e3c513b1fa2cb83b3c72 Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Tue, 21 Apr 2009 18:29:33 +0000 Subject: [PATCH] +! Those were meant to be commited as a part of the previous commit. --- src/hotkeyedit.cc | 118 ++++++++++++++++++++++++++++++++++++++++++++++ src/hotkeyedit.hh | 37 +++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 src/hotkeyedit.cc create mode 100644 src/hotkeyedit.hh diff --git a/src/hotkeyedit.cc b/src/hotkeyedit.cc new file mode 100644 index 00000000..d20fc37d --- /dev/null +++ b/src/hotkeyedit.cc @@ -0,0 +1,118 @@ +/* This file is (c) 2008-2009 Konstantin Isakov + * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ + +#include "hotkeyedit.hh" +#include + +HotKeyEdit::HotKeyEdit( QWidget * parent ): + QLineEdit( parent ), + currentModifiers( 0 ), currentKey1( 0 ), currentKey2( 0 ), + continuingCombo( false ) +{ + renderCurrentValue(); +} + +void HotKeyEdit::setHotKey( Config::HotKey const & hk ) +{ + currentModifiers = hk.modifiers; + currentKey1 = hk.key1; + currentKey2 = hk.key2; + + renderCurrentValue(); +} + +Config::HotKey HotKeyEdit::getHotKey() const +{ + Config::HotKey hk; + + hk.modifiers = currentModifiers; + hk.key1 = currentKey1; + hk.key2 = currentKey2; + + return hk; +} + +void HotKeyEdit::renderCurrentValue() +{ + QString result; + + if ( currentKey1 ) + { + result = QKeySequence( currentKey1 | currentModifiers ).toString( QKeySequence::PortableText ); + + if ( currentKey2 ) + result += "+" + QKeySequence( currentKey2 ).toString( QKeySequence::PortableText ); + } + + setText( result ); +} + +void HotKeyEdit::keyPressEvent( QKeyEvent * event ) +{ + int key = event->key(); + + switch( key ) + { + case 0: + case Qt::Key_unknown: + case Qt::Key_Shift: + case Qt::Key_Control: + case Qt::Key_Meta: + case Qt::Key_Alt: + case Qt::Key_AltGr: + continuingCombo = false; + QLineEdit::keyPressEvent( event ); + break; + + default: + { + if ( !event->modifiers() && + ( ( key == Qt::Key_Backspace ) || ( key == Qt::Key_Delete ) ) ) + { + // Delete current combo + currentKey1 = 0; + currentKey2 = 0; + currentModifiers = 0; + continuingCombo = false; + } + else + if ( !continuingCombo ) + { + if ( event->modifiers() || event->text().isEmpty() ) // Don't allow plain letters + { + currentKey2 = 0; + currentKey1 = key; + currentModifiers = event->modifiers(); + continuingCombo = true; + } + } + else + { + currentKey2 = key; + continuingCombo = false; + } + + renderCurrentValue(); + } + break; + } +} + +void HotKeyEdit::keyReleaseEvent( QKeyEvent * event ) +{ + switch( event->key() ) + { + case 0: + case Qt::Key_unknown: + case Qt::Key_Shift: + case Qt::Key_Control: + case Qt::Key_Meta: + case Qt::Key_Alt: + case Qt::Key_AltGr: + continuingCombo = false; + break; + } + + QLineEdit::keyReleaseEvent( event ); +} + diff --git a/src/hotkeyedit.hh b/src/hotkeyedit.hh new file mode 100644 index 00000000..dc3fb370 --- /dev/null +++ b/src/hotkeyedit.hh @@ -0,0 +1,37 @@ +/* This file is (c) 2008-2009 Konstantin Isakov + * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ + +#ifndef __HOTKEYEDIT_HH_INCLUDED__ +#define __HOTKEYEDIT_HH_INCLUDED__ + +#include "config.hh" +#include + +// This widget allows grabbing a hotkey +class HotKeyEdit: public QLineEdit +{ + Q_OBJECT + + Qt::KeyboardModifiers currentModifiers; + int currentKey1, currentKey2; + + bool continuingCombo; + +public: + + HotKeyEdit( QWidget * parent = 0 ); + + void setHotKey( Config::HotKey const & ); + Config::HotKey getHotKey() const; + +protected: + + void keyPressEvent( QKeyEvent * event ); + void keyReleaseEvent( QKeyEvent * event ); + +private: + + void renderCurrentValue(); +}; + +#endif