Commit e512b8bd authored by Jean-Baptiste Kempf's avatar Jean-Baptiste Kempf

Qt4 - Preferences: avoid a few segfaults in the hotkeys selector. Avoid using...

Qt4 - Preferences: avoid a few segfaults in the hotkeys selector. Avoid using QList of module_config_t and attack it from different classes. Avoid using some useless casts...

parent f0e55f52
...@@ -1016,6 +1016,7 @@ KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this, ...@@ -1016,6 +1016,7 @@ KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
QPushButton *clearButton = new QPushButton( qtr( "Clear" ) ); QPushButton *clearButton = new QPushButton( qtr( "Clear" ) );
QPushButton *setButton = new QPushButton( qtr( "Set" ) ); QPushButton *setButton = new QPushButton( qtr( "Set" ) );
setButton->setDefault( true );
finish(); finish();
gLayout->addWidget( label, 0, 0, 1, 4 ); gLayout->addWidget( label, 0, 0, 1, 4 );
...@@ -1036,15 +1037,17 @@ KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this, ...@@ -1036,15 +1037,17 @@ KeySelectorControl::KeySelectorControl( vlc_object_t *_p_this,
void KeySelectorControl::finish() void KeySelectorControl::finish()
{ {
if( label ) if( label )
label->setToolTip( formatTooltip(qtr(p_item->psz_longtext)) ); label->setToolTip( formatTooltip( qtr( p_item->psz_longtext ) ) );
/* Fill the table */ /* Fill the table */
table->setColumnCount( 2 ); table->setColumnCount( 2 );
table->setAlternatingRowColors( true ); table->setAlternatingRowColors( true );
/* Get the main Module */
module_t *p_main = module_Find( p_this, "main" ); module_t *p_main = module_Find( p_this, "main" );
assert( p_main ); assert( p_main );
/* Access to the module_config_t */
unsigned confsize; unsigned confsize;
module_config_t *p_config; module_config_t *p_config;
...@@ -1054,15 +1057,24 @@ void KeySelectorControl::finish() ...@@ -1054,15 +1057,24 @@ void KeySelectorControl::finish()
{ {
module_config_t *p_item = p_config + i; module_config_t *p_item = p_config + i;
if( p_item->i_type & CONFIG_ITEM && p_item->psz_name && /* If we are a key option not empty */
strstr( p_item->psz_name , "key-" ) && !EMPTY_STR( p_item->psz_text ) ) if( p_item->i_type & CONFIG_ITEM && p_item->psz_name
&& strstr( p_item->psz_name , "key-" )
&& !EMPTY_STR( p_item->psz_text ) )
{ {
/*
Each tree item has:
- QString text in column 0
- QString name in data of column 0
- KeyValue in String in column 1
- KeyValue in int in column 1
*/
QTreeWidgetItem *treeItem = new QTreeWidgetItem(); QTreeWidgetItem *treeItem = new QTreeWidgetItem();
treeItem->setText( 0, qtr( p_item->psz_text ) ); treeItem->setText( 0, qtr( p_item->psz_text ) );
treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
treeItem->setData( 0, Qt::UserRole, treeItem->setData( 0, Qt::UserRole,
QVariant::fromValue( (void*)p_item ) ); QVariant( qfu( p_item->psz_name ) ) );
values += p_item; treeItem->setText( 1, VLCKeyToString( p_item->value.i ) );
treeItem->setData( 1, Qt::UserRole, QVariant( p_item->value.i ) );
table->addTopLevelItem( treeItem ); table->addTopLevelItem( treeItem );
} }
} }
...@@ -1078,9 +1090,11 @@ void KeySelectorControl::finish() ...@@ -1078,9 +1090,11 @@ void KeySelectorControl::finish()
CONNECT( shortcutValue, pressed(), this, selectKey() ); CONNECT( shortcutValue, pressed(), this, selectKey() );
} }
/* Show the key selected from the table in the keySelector */
void KeySelectorControl::select1Key( QTreeWidgetItem *keyItem ) void KeySelectorControl::select1Key( QTreeWidgetItem *keyItem )
{ {
shortcutValue->setText( keyItem->text( 1 ) ); shortcutValue->setText( keyItem->text( 1 ) );
shortcutValue->setValue( keyItem->data( 1, Qt::UserRole ).toInt() );
} }
void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem ) void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
...@@ -1092,28 +1106,32 @@ void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem ) ...@@ -1092,28 +1106,32 @@ void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
and the shortcutValue is clicked */ and the shortcutValue is clicked */
if( !keyItem ) return; if( !keyItem ) return;
module_config_t *p_keyItem = static_cast<module_config_t*> /* Launch a small dialog to ask for a new key */
(keyItem->data( 0, Qt::UserRole ).value<void*>()); KeyInputDialog *d = new KeyInputDialog( table, keyItem->text( 0 ), widget );
KeyInputDialog *d = new KeyInputDialog( values, p_keyItem->psz_text, widget );
d->exec(); d->exec();
if( d->result() == QDialog::Accepted ) if( d->result() == QDialog::Accepted )
{ {
p_keyItem->value.i = d->keyValue; int newValue = d->keyValue;
shortcutValue->setText( VLCKeyToString( newValue ) );
shortcutValue->setValue( newValue );
if( d->conflicts ) if( d->conflicts )
{ {
QTreeWidgetItem *it;
for( int i = 0; i < table->topLevelItemCount() ; i++ ) for( int i = 0; i < table->topLevelItemCount() ; i++ )
{ {
QTreeWidgetItem *it = table->topLevelItem(i); it = table->topLevelItem(i);
module_config_t *p_item = static_cast<module_config_t*> if( ( keyItem != it )
(it->data( 0, Qt::UserRole ).value<void*>()); && ( it->data( 1, Qt::UserRole ).toInt() == newValue ) )
if( p_keyItem != p_item && p_item->value.i == d->keyValue ) {
p_item->value.i = 0; it->setData( 1, Qt::UserRole, QVariant( -1 ) );
shortcutValue->setText( VLCKeyToString( p_item->value.i ) ); it->setText( 1, qtr( "Unset" ) );
}
} }
/* We already made an OK once. */
setTheKey();
} }
else
shortcutValue->setText( VLCKeyToString( p_keyItem->value.i ) );
} }
delete d; delete d;
} }
...@@ -1121,30 +1139,35 @@ void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem ) ...@@ -1121,30 +1139,35 @@ void KeySelectorControl::selectKey( QTreeWidgetItem *keyItem )
void KeySelectorControl::setTheKey() void KeySelectorControl::setTheKey()
{ {
table->currentItem()->setText( 1, shortcutValue->text() ); table->currentItem()->setText( 1, shortcutValue->text() );
table->currentItem()->setData( 1, Qt::UserRole, shortcutValue->getValue() );
} }
void KeySelectorControl::doApply() void KeySelectorControl::doApply()
{ {
foreach( module_config_t *p_current, values ) QTreeWidgetItem *it;
for( int i = 0; i < table->topLevelItemCount() ; i++ )
{ {
config_PutInt( p_this, p_current->psz_name, p_current->value.i ); it = table->topLevelItem(i);
if( it->data( 1, Qt::UserRole ).toInt() >= 0 )
config_PutInt( p_this,
qtu( it->data( 0, Qt::UserRole ).toString() ),
it->data( 1, Qt::UserRole ).toInt() );
} }
} }
KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values, KeyInputDialog::KeyInputDialog( QTreeWidget *_table,
const char * _keyToChange, QString keyToChange,
QWidget *_parent ) : QWidget *_parent ) :
QDialog( _parent ), keyValue(0) QDialog( _parent ), keyValue(0)
{ {
setModal( true ); setModal( true );
values = _values;
conflicts = false; conflicts = false;
keyToChange = _keyToChange;
setWindowTitle( qtr( "Hotkey for " ) + qfu( keyToChange) ); table = _table;
setWindowTitle( qtr( "Hotkey for " ) + keyToChange );
vLayout = new QVBoxLayout( this ); vLayout = new QVBoxLayout( this );
selected = new QLabel( qtr("Press the new keys for ") + qfu( keyToChange ) ); selected = new QLabel( qtr( "Press the new keys for " ) + keyToChange );
vLayout->addWidget( selected , Qt::AlignCenter ); vLayout->addWidget( selected , Qt::AlignCenter );
buttonBox = new QDialogButtonBox; buttonBox = new QDialogButtonBox;
...@@ -1152,6 +1175,7 @@ KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values, ...@@ -1152,6 +1175,7 @@ KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
QPushButton *cancel = new QPushButton( qtr("Cancel") ); QPushButton *cancel = new QPushButton( qtr("Cancel") );
buttonBox->addButton( ok, QDialogButtonBox::AcceptRole ); buttonBox->addButton( ok, QDialogButtonBox::AcceptRole );
buttonBox->addButton( cancel, QDialogButtonBox::RejectRole ); buttonBox->addButton( cancel, QDialogButtonBox::RejectRole );
ok->setDefault( true );
vLayout->addWidget( buttonBox ); vLayout->addWidget( buttonBox );
buttonBox->hide(); buttonBox->hide();
...@@ -1162,27 +1186,18 @@ KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values, ...@@ -1162,27 +1186,18 @@ KeyInputDialog::KeyInputDialog( QList<module_config_t*>& _values,
void KeyInputDialog::checkForConflicts( int i_vlckey ) void KeyInputDialog::checkForConflicts( int i_vlckey )
{ {
conflicts = false; QList<QTreeWidgetItem *> conflictList =
module_config_t *p_current = NULL; table->findItems( VLCKeyToString( i_vlckey ), Qt::MatchExactly, 1 );
/* Search for conflicts */
foreach( p_current, values )
{
if( p_current->value.i == i_vlckey && strcmp( p_current->psz_text,
keyToChange ) )
{
conflicts = true;
break;
}
}
if( conflicts ) if( conflictList.size() )
{ {
QLabel *warning = new QLabel( QLabel *warning = new QLabel(
qtr("Warning: the key is already assigned to \"") + qtr("Warning: the key is already assigned to \"") +
qfu( p_current->psz_text ) + "\"" ); conflictList[0]->text( 0 ) + "\"" );
warning->setWordWrap( true );
vLayout->insertWidget( 1, warning ); vLayout->insertWidget( 1, warning );
buttonBox->show(); buttonBox->show();
conflicts = true;
} }
else accept(); else accept();
} }
...@@ -1191,7 +1206,7 @@ void KeyInputDialog::keyPressEvent( QKeyEvent *e ) ...@@ -1191,7 +1206,7 @@ void KeyInputDialog::keyPressEvent( QKeyEvent *e )
{ {
if( e->key() == Qt::Key_Tab ) return; if( e->key() == Qt::Key_Tab ) return;
int i_vlck = qtEventToVLCKey( e ); int i_vlck = qtEventToVLCKey( e );
selected->setText( VLCKeyToString( i_vlck ) ); selected->setText( qtr( "Key: " ) + VLCKeyToString( i_vlck ) );
checkForConflicts( i_vlck ); checkForConflicts( i_vlck );
keyValue = i_vlck; keyValue = i_vlck;
} }
...@@ -1199,7 +1214,7 @@ void KeyInputDialog::keyPressEvent( QKeyEvent *e ) ...@@ -1199,7 +1214,7 @@ void KeyInputDialog::keyPressEvent( QKeyEvent *e )
void KeyInputDialog::wheelEvent( QWheelEvent *e ) void KeyInputDialog::wheelEvent( QWheelEvent *e )
{ {
int i_vlck = qtWheelEventToVLCKey( e ); int i_vlck = qtWheelEventToVLCKey( e );
selected->setText( VLCKeyToString( i_vlck ) ); selected->setText( qtr( "Key: " ) + VLCKeyToString( i_vlck ) );
checkForConflicts( i_vlck ); checkForConflicts( i_vlck );
keyValue = i_vlck; keyValue = i_vlck;
} }
...@@ -1208,3 +1223,4 @@ void KeyShortcutEdit::mousePressEvent( QMouseEvent *) ...@@ -1208,3 +1223,4 @@ void KeyShortcutEdit::mousePressEvent( QMouseEvent *)
{ {
emit pressed(); emit pressed();
} }
...@@ -412,27 +412,14 @@ private slot: ...@@ -412,27 +412,14 @@ private slot:
/********************************************************************** /**********************************************************************
* Key selector widget * Key selector widget
**********************************************************************/ **********************************************************************/
class KeyInputDialog : public QDialog
{
public:
KeyInputDialog( QList<module_config_t *> &, const char *, QWidget * );
int keyValue;
bool conflicts;
private:
void checkForConflicts( int i_vlckey );
void keyPressEvent( QKeyEvent *);
void wheelEvent( QWheelEvent *);
QLabel *selected;
QVBoxLayout *vLayout;
const char *keyToChange;
QList<module_config_t*> values;
QDialogButtonBox *buttonBox;
};
class KeyShortcutEdit: public QLineEdit class KeyShortcutEdit: public QLineEdit
{ {
Q_OBJECT Q_OBJECT
public:
void setValue( int _value ){ value = _value; }
int getValue(){ return value; }
private: private:
int value;
virtual void mousePressEvent( QMouseEvent *event ); virtual void mousePressEvent( QMouseEvent *event );
signals: signals:
void pressed(); void pressed();
...@@ -461,4 +448,19 @@ private slots: ...@@ -461,4 +448,19 @@ private slots:
void select1Key( QTreeWidgetItem *); void select1Key( QTreeWidgetItem *);
}; };
class KeyInputDialog : public QDialog
{
public:
KeyInputDialog( QTreeWidget *, QString, QWidget * );
int keyValue;
bool conflicts;
private:
QTreeWidget *table;
void checkForConflicts( int i_vlckey );
void keyPressEvent( QKeyEvent *);
void wheelEvent( QWheelEvent *);
QLabel *selected;
QVBoxLayout *vLayout;
QDialogButtonBox *buttonBox;
};
#endif #endif
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment