From be4cca987ba9de8ae3dd33243ef863263d7723d7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Cl=C3=A9ment=20Stenac?= Date: Sun, 8 Oct 2006 19:28:49 +0000 Subject: [PATCH] Hotkey selector widget. Save not implemented yet --- .../qt4/components/preferences_widgets.cpp | 95 ++++++++++++++++++- .../qt4/components/preferences_widgets.hpp | 19 ++++ modules/gui/qt4/main_interface.cpp | 50 +--------- modules/gui/qt4/util/customwidgets.cpp | 81 ++++++++++++++++ modules/gui/qt4/util/customwidgets.hpp | 5 + 5 files changed, 200 insertions(+), 50 deletions(-) diff --git a/modules/gui/qt4/components/preferences_widgets.cpp b/modules/gui/qt4/components/preferences_widgets.cpp index 477c7ddd78..bd17adb1c4 100644 --- a/modules/gui/qt4/components/preferences_widgets.cpp +++ b/modules/gui/qt4/components/preferences_widgets.cpp @@ -32,7 +32,9 @@ */ #include "components/preferences_widgets.hpp" +#include "util/customwidgets.hpp" #include "qt4.hpp" + #include #include #include @@ -40,6 +42,9 @@ #include #include #include +#include + +#include ConfigControl *ConfigControl::createControl( vlc_object_t *p_this, module_config_t *p_item, @@ -761,14 +766,100 @@ void KeySelectorControl::finish() { QTreeWidgetItem *treeItem = new QTreeWidgetItem(); treeItem->setText( 0, qfu( p_item->psz_text ) ); - treeItem->setText( 1, p_item->psz_value ); - treeItem->setData( 0, Qt::UserRole, QVariant( p_item->psz_name ) ); + treeItem->setText( 1, VLCKeyToString( p_item->i_value ) ); + treeItem->setData( 0, Qt::UserRole, + QVariant::fromValue( (void*)p_item ) ); + values += p_item; table->addTopLevelItem( treeItem ); } } while( p_item->i_type != CONFIG_HINT_END && p_item++ ); + table->resizeColumnToContents( 0 ); + + CONNECT( table, itemDoubleClicked( QTreeWidgetItem *, int ), + this, selectKey( QTreeWidgetItem * ) ); +} + +void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem ) +{ + module_config_t *p_keyItem = static_cast + (keyItem->data( 0, Qt::UserRole ).value()); + + KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text ); + d->exec(); + if( d->result() == QDialog::Accepted ) + { + p_keyItem->i_value = d->keyValue; + if( d->conflicts ) + { + for( int i = 0; i < table->topLevelItemCount() ; i++ ) + { + QTreeWidgetItem *it = table->topLevelItem(i); + module_config_t *p_item = static_cast + (it->data( 0, Qt::UserRole ).value()); + it->setText( 1, VLCKeyToString( p_item->i_value ) ); + } + } + else + keyItem->setText( 1, VLCKeyToString( p_keyItem->i_value ) ); + } + delete d; } void KeySelectorControl::doApply() { +} +KeyInputDialog::KeyInputDialog( QList& _values, + char * _keyToChange ) : + QDialog(0), keyValue(0) +{ + setModal( true ); + values = _values; + conflicts = false; + keyToChange = _keyToChange; + setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange) ); + + QVBoxLayout *l = new QVBoxLayout( this ); + selected = new QLabel( qtr("Press the new keys for ") + qfu(keyToChange) ); + warning = new QLabel(); + l->addWidget( selected , Qt::AlignCenter ); + l->addWidget( warning, Qt::AlignCenter ); + + QHBoxLayout *l2 = new QHBoxLayout(); + QPushButton *ok = new QPushButton( qtr("OK") ); + l2->addWidget( ok ); + QPushButton *cancel = new QPushButton( qtr("Cancel") ); + l2->addWidget( cancel ); + + BUTTONACT( ok, accept() ); + BUTTONACT( cancel, reject() ); + + l->addLayout( l2 ); +} + +void KeyInputDialog::keyPressEvent( QKeyEvent *e ) +{ + if( e->key() == Qt::Key_Tab ) return; + int i_vlck = qtEventToVLCKey( e ); + selected->setText( VLCKeyToString( i_vlck ) ); + conflicts = false; + module_config_t *p_current = NULL; + foreach( p_current, values ) + { + if( p_current->i_value == i_vlck && strcmp( p_current->psz_text, + keyToChange ) ) + { + p_current->i_value = 0; + conflicts = true; + break; + } + } + if( conflicts ) + { + warning->setText( + qtr("Warning: the key is already assigned to \"") + + QString( p_current->psz_text ) + "\"" ); + } + else warning->setText( "" ); + keyValue = i_vlck; } diff --git a/modules/gui/qt4/components/preferences_widgets.hpp b/modules/gui/qt4/components/preferences_widgets.hpp index e5c7f4f082..11e3877c8f 100644 --- a/modules/gui/qt4/components/preferences_widgets.hpp +++ b/modules/gui/qt4/components/preferences_widgets.hpp @@ -33,6 +33,8 @@ #include #include #include +#include + #include "ui/input_stats.h" #include "qt4.hpp" #include @@ -310,6 +312,20 @@ private slot: /********************************************************************** * Key selector widget **********************************************************************/ +class KeyInputDialog : public QDialog +{ +public: + KeyInputDialog( QList &, char * ); + int keyValue; + bool conflicts; +private: + void keyPressEvent( QKeyEvent *); + QLabel *selected; + QLabel *warning; + char * keyToChange; + QList values; +}; + class KeySelectorControl : public ConfigControl { Q_OBJECT; @@ -325,6 +341,9 @@ private: void finish(); QLabel *label; QTreeWidget *table; + QList values; +private slots: + void selectKey( QTreeWidgetItem *); }; #endif diff --git a/modules/gui/qt4/main_interface.cpp b/modules/gui/qt4/main_interface.cpp index 7251d2e593..d39f2a2943 100644 --- a/modules/gui/qt4/main_interface.cpp +++ b/modules/gui/qt4/main_interface.cpp @@ -25,6 +25,7 @@ #include "input_manager.hpp" #include "util/input_slider.hpp" #include "util/qvlcframe.hpp" +#include "util/customwidgets.hpp" #include "dialogs_provider.hpp" #include "components/interface_widgets.hpp" #include "dialogs/playlist.hpp" @@ -498,54 +499,7 @@ void MainInterface::customEvent( QEvent *event ) ************************************************************************/ void MainInterface::keyPressEvent( QKeyEvent *e ) { - int i_vlck = 0; - /* Handle modifiers */ - if( e->modifiers()& Qt::ShiftModifier ) i_vlck |= KEY_MODIFIER_SHIFT; - if( e->modifiers()& Qt::AltModifier ) i_vlck |= KEY_MODIFIER_ALT; - if( e->modifiers()& Qt::ControlModifier ) i_vlck |= KEY_MODIFIER_CTRL; - if( e->modifiers()& Qt::MetaModifier ) i_vlck |= KEY_MODIFIER_META; - - bool found = false; - /* Look for some special keys */ -#define HANDLE( qt, vk ) case Qt::qt : i_vlck |= vk; found = true;break - switch( e->key() ) - { - HANDLE( Key_Left, KEY_LEFT ); - HANDLE( Key_Right, KEY_RIGHT ); - HANDLE( Key_Up, KEY_UP ); - HANDLE( Key_Down, KEY_DOWN ); - HANDLE( Key_Space, KEY_SPACE ); - HANDLE( Key_Escape, KEY_ESC ); - HANDLE( Key_Enter, KEY_ENTER ); - HANDLE( Key_F1, KEY_F1 ); - HANDLE( Key_F2, KEY_F2 ); - HANDLE( Key_F3, KEY_F3 ); - HANDLE( Key_F4, KEY_F4 ); - HANDLE( Key_F5, KEY_F5 ); - HANDLE( Key_F6, KEY_F6 ); - HANDLE( Key_F7, KEY_F7 ); - HANDLE( Key_F8, KEY_F8 ); - HANDLE( Key_F9, KEY_F9 ); - HANDLE( Key_F10, KEY_F10 ); - HANDLE( Key_F11, KEY_F11 ); - HANDLE( Key_F12, KEY_F12 ); - HANDLE( Key_PageUp, KEY_PAGEUP ); - HANDLE( Key_PageDown, KEY_PAGEDOWN ); - HANDLE( Key_Home, KEY_HOME ); - HANDLE( Key_End, KEY_END ); - HANDLE( Key_Insert, KEY_INSERT ); - HANDLE( Key_Delete, KEY_DELETE ); - - } - if( !found ) - { - /* Force lowercase */ - if( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z ) - i_vlck += e->key() + 32; - /* Rest of the ascii range */ - else if( e->key() >= Qt::Key_Space && e->key() <= Qt::Key_AsciiTilde ) - i_vlck += e->key(); - } + int i_vlck = qtEventToVLCKey( e ); if( i_vlck >= 0 ) { var_SetInteger( p_intf->p_libvlc, "key-pressed", i_vlck ); diff --git a/modules/gui/qt4/util/customwidgets.cpp b/modules/gui/qt4/util/customwidgets.cpp index b59ac6c971..10c37c617a 100644 --- a/modules/gui/qt4/util/customwidgets.cpp +++ b/modules/gui/qt4/util/customwidgets.cpp @@ -29,6 +29,9 @@ #include #include #include +#include + +#include ClickLineEdit::ClickLineEdit( const QString &msg, QWidget *parent) : QLineEdit( parent ) { @@ -90,3 +93,81 @@ void ClickLineEdit::focusOutEvent( QFocusEvent *ev ) } QLineEdit::focusOutEvent( ev ); } + +/*************************************************************************** + * Hotkeys converters + ***************************************************************************/ + +int qtEventToVLCKey( QKeyEvent *e ) +{ + int i_vlck = 0; + /* Handle modifiers */ + if( e->modifiers()& Qt::ShiftModifier ) i_vlck |= KEY_MODIFIER_SHIFT; + if( e->modifiers()& Qt::AltModifier ) i_vlck |= KEY_MODIFIER_ALT; + if( e->modifiers()& Qt::ControlModifier ) i_vlck |= KEY_MODIFIER_CTRL; + if( e->modifiers()& Qt::MetaModifier ) i_vlck |= KEY_MODIFIER_META; + + bool found = false; + /* Look for some special keys */ +#define HANDLE( qt, vk ) case Qt::qt : i_vlck |= vk; found = true;break + switch( e->key() ) + { + HANDLE( Key_Left, KEY_LEFT ); + HANDLE( Key_Right, KEY_RIGHT ); + HANDLE( Key_Up, KEY_UP ); + HANDLE( Key_Down, KEY_DOWN ); + HANDLE( Key_Space, KEY_SPACE ); + HANDLE( Key_Escape, KEY_ESC ); + HANDLE( Key_Enter, KEY_ENTER ); + HANDLE( Key_F1, KEY_F1 ); + HANDLE( Key_F2, KEY_F2 ); + HANDLE( Key_F3, KEY_F3 ); + HANDLE( Key_F4, KEY_F4 ); + HANDLE( Key_F5, KEY_F5 ); + HANDLE( Key_F6, KEY_F6 ); + HANDLE( Key_F7, KEY_F7 ); + HANDLE( Key_F8, KEY_F8 ); + HANDLE( Key_F9, KEY_F9 ); + HANDLE( Key_F10, KEY_F10 ); + HANDLE( Key_F11, KEY_F11 ); + HANDLE( Key_F12, KEY_F12 ); + HANDLE( Key_PageUp, KEY_PAGEUP ); + HANDLE( Key_PageDown, KEY_PAGEDOWN ); + HANDLE( Key_Home, KEY_HOME ); + HANDLE( Key_End, KEY_END ); + HANDLE( Key_Insert, KEY_INSERT ); + HANDLE( Key_Delete, KEY_DELETE ); + + } + if( !found ) + { + /* Force lowercase */ + if( e->key() >= Qt::Key_A && e->key() <= Qt::Key_Z ) + i_vlck += e->key() + 32; + /* Rest of the ascii range */ + else if( e->key() >= Qt::Key_Space && e->key() <= Qt::Key_AsciiTilde ) + i_vlck += e->key(); + } + return i_vlck; +} + +QString VLCKeyToString( int val ) +{ + QString r = ""; + if( val & KEY_MODIFIER_CTRL ) + r+= "Ctrl+"; + if( val & KEY_MODIFIER_ALT ) + r+= "Alt+"; + if( val & KEY_MODIFIER_SHIFT ) + r+= "Shift+"; + + unsigned int i_keys = sizeof(vlc_keys)/sizeof(key_descriptor_t); + for( unsigned int i = 0; i< i_keys; i++ ) + { + if( vlc_keys[i].i_key_code == (val& ~KEY_MODIFIER) ) + { + r+= vlc_keys[i].psz_key_string; + } + } + return r; +} diff --git a/modules/gui/qt4/util/customwidgets.hpp b/modules/gui/qt4/util/customwidgets.hpp index a9a7b32248..9a4d94b018 100644 --- a/modules/gui/qt4/util/customwidgets.hpp +++ b/modules/gui/qt4/util/customwidgets.hpp @@ -84,4 +84,9 @@ public: signals: void rightClicked( QModelIndex, QPoint ); }; + +class QKeyEvent; +int qtEventToVLCKey( QKeyEvent *e ); +QString VLCKeyToString( int val ); + #endif -- 2.39.2