]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/toolbar.cpp
qt: toolbar_editor: fix preview bar ordering
[vlc] / modules / gui / qt4 / dialogs / toolbar.cpp
1 /*****************************************************************************
2  * toolbar.cpp : ToolbarEdit dialog
3  ****************************************************************************
4  * Copyright (C) 2008-2009 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "dialogs/toolbar.hpp"
29
30 /* Widgets */
31 #include "util/input_slider.hpp"
32 #include "util/customwidgets.hpp"
33 #include "components/interface_widgets.hpp"
34 #include "util/buttons/DeckButtonsLayout.hpp"
35 #include "util/buttons/BrowseButton.hpp"
36 #include "util/buttons/RoundButton.hpp"
37
38 #include "qt4.hpp"
39 #include "input_manager.hpp"
40 #include <vlc_vout.h>                       /* vout_thread_t for aspect ratio combobox */
41
42 #include <QGroupBox>
43 #include <QLabel>
44 #include <QComboBox>
45 #include <QListWidget>
46 #include <QSpinBox>
47 #include <QRubberBand>
48 #include <QDrag>
49 #include <QDragEnterEvent>
50 #include <QDialogButtonBox>
51 #include <QInputDialog>
52 #include <QMimeData>
53 #include <QFormLayout>
54 #include <QVBoxLayout>
55 #include <QTabWidget>
56 #include <QSignalMapper>
57
58 #include <assert.h>
59
60 ToolbarEditDialog::ToolbarEditDialog( QWidget *_w, intf_thread_t *_p_intf)
61                   : QVLCDialog( _w,  _p_intf )
62 {
63     setWindowTitle( qtr( "Toolbars Editor" ) );
64     setWindowRole( "vlc-toolbars-editor" );
65     QGridLayout *mainLayout = new QGridLayout( this );
66     setMinimumWidth( 600 );
67     setAttribute( Qt::WA_DeleteOnClose );
68
69     /* main GroupBox */
70     QGroupBox *widgetBox = new QGroupBox( qtr( "Toolbar Elements") , this );
71     widgetBox->setSizePolicy( QSizePolicy::Preferred,
72                               QSizePolicy::MinimumExpanding );
73     QGridLayout *boxLayout = new QGridLayout( widgetBox );
74
75     flatBox = new QCheckBox( qtr( "Flat Button" ) );
76     flatBox->setToolTip( qtr( "Next widget style" ) );
77     bigBox = new QCheckBox( qtr( "Big Button" ) );
78     bigBox->setToolTip( flatBox->toolTip() );
79     shinyBox = new QCheckBox( qtr( "Native Slider" ) );
80     shinyBox->setToolTip( flatBox->toolTip() );
81
82     boxLayout->addWidget( new WidgetListing( p_intf, this ), 1, 0, 1, -1 );
83     boxLayout->addWidget( flatBox, 0, 0 );
84     boxLayout->addWidget( bigBox, 0, 1 );
85     boxLayout->addWidget( shinyBox, 0, 2 );
86     mainLayout->addWidget( widgetBox, 5, 0, 3, 6 );
87
88     QTabWidget *tabWidget = new QTabWidget();
89     mainLayout->addWidget( tabWidget, 1, 0, 4, 9 );
90
91     /* Main ToolBar */
92     QWidget *mainToolbarBox = new QWidget();
93     tabWidget->addTab( mainToolbarBox, qtr( "Main Toolbar" ) );
94     QFormLayout *mainTboxLayout = new QFormLayout( mainToolbarBox );
95
96     positionCheckbox = new QCheckBox( qtr( "Above the Video" ) );
97     positionCheckbox->setChecked(
98                 getSettings()->value( "MainWindow/ToolbarPos", 0 ).toInt() );
99     mainTboxLayout->addRow( new QLabel( qtr( "Toolbar position:" ) ),
100                             positionCheckbox );
101
102     QString line1 = getSettings()->value( "MainWindow/MainToolbar1",
103                                           MAIN_TB1_DEFAULT ).toString();
104     controller1 = new DroppingController( p_intf, line1, this );
105     mainTboxLayout->addRow( new QLabel( qtr("Line 1:") ), controller1 );
106
107     QString line2 = getSettings()->value( "MainWindow/MainToolbar2",
108                                           MAIN_TB2_DEFAULT ).toString();
109     controller2 = new DroppingController( p_intf, line2, this );
110     mainTboxLayout->addRow( new QLabel( qtr("Line 2:") ), controller2 );
111
112     /* TimeToolBar */
113     QString line = getSettings()->value( "MainWindow/InputToolbar",
114                                          INPT_TB_DEFAULT ).toString();
115     controller = new DroppingController( p_intf, line, this );
116     QWidget *timeToolbarBox = new QWidget();
117     timeToolbarBox->setLayout( new QVBoxLayout() );
118     timeToolbarBox->layout()->addWidget( controller );
119     tabWidget->addTab( timeToolbarBox, qtr( "Time Toolbar" ) );
120
121     /* Advanced ToolBar */
122     QString lineA = getSettings()->value( "MainWindow/AdvToolbar",
123                                           ADV_TB_DEFAULT ).toString();
124     controllerA = new DroppingController( p_intf, lineA, this );
125     QWidget *advToolbarBox = new QWidget();
126     advToolbarBox->setLayout( new QVBoxLayout() );
127     advToolbarBox->layout()->addWidget( controllerA );
128     tabWidget->addTab( advToolbarBox, qtr( "Advanced Widget" ) );
129
130     /* FSCToolBar */
131     QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar",
132                                             FSC_TB_DEFAULT ).toString();
133     controllerFSC = new DroppingController( p_intf, lineFSC, this );
134     QWidget *FSCToolbarBox = new QWidget();
135     FSCToolbarBox->setLayout( new QVBoxLayout() );
136     FSCToolbarBox->layout()->addWidget( controllerFSC );
137     tabWidget->addTab( FSCToolbarBox, qtr( "Fullscreen Controller" ) );
138
139     /* Profile */
140     QGridLayout *profileBoxLayout = new QGridLayout();
141
142     profileCombo = new QComboBox;
143
144     QToolButton *newButton = new QToolButton;
145     newButton->setIcon( QIcon( ":/new" ) );
146     newButton->setToolTip( qtr("New profile") );
147     QToolButton *deleteButton = new QToolButton;
148     deleteButton->setIcon( QIcon( ":/toolbar/clear" ) );
149     deleteButton->setToolTip( qtr( "Delete the current profile" ) );
150
151     profileBoxLayout->addWidget( new QLabel( qtr( "Select profile:" ) ), 0, 0 );
152     profileBoxLayout->addWidget( profileCombo, 0, 1 );
153     profileBoxLayout->addWidget( newButton, 0, 2 );
154     profileBoxLayout->addWidget( deleteButton, 0, 3 );
155
156     mainLayout->addLayout( profileBoxLayout, 0, 0, 1, 9 );
157
158     /* Fill combos */
159     int i_size = getSettings()->beginReadArray( "ToolbarProfiles" );
160     for( int i = 0; i < i_size; i++ )
161     {
162         getSettings()->setArrayIndex(i);
163         profileCombo->addItem( getSettings()->value( "ProfileName" ).toString(),
164                                getSettings()->value( "Value" ).toString() );
165     }
166     getSettings()->endArray();
167
168     /* Load defaults ones if we have no combos */
169     /* We could decide that we load defaults on first launch of the dialog
170        or when the combo is back to 0. I choose the second solution, because some clueless
171        user might hit on delete a bit too much, but discussion is opened. -- jb */
172     if( i_size == 0 )
173     {
174         profileCombo->addItem( PROFILE_NAME_6, QString( VALUE_6 ) );
175         profileCombo->addItem( PROFILE_NAME_1, QString( VALUE_1 ) );
176         profileCombo->addItem( PROFILE_NAME_2, QString( VALUE_2 ) );
177         profileCombo->addItem( PROFILE_NAME_3, QString( VALUE_3 ) );
178         profileCombo->addItem( PROFILE_NAME_4, QString( VALUE_4 ) );
179         profileCombo->addItem( PROFILE_NAME_5, QString( VALUE_5 ) );
180     }
181     profileCombo->setCurrentIndex( -1 );
182
183     /* Build and prepare our preview */
184     PreviewWidget *previewWidget = new PreviewWidget( controller, controller1, controller2 );
185     QGroupBox *previewBox = new QGroupBox( qtr("Preview"), this );
186     previewBox->setLayout( new QVBoxLayout() );
187     previewBox->layout()->addWidget( previewWidget );
188     mainLayout->addWidget( previewBox, 5, 6, 3, 3 );
189     CONNECT( positionCheckbox, stateChanged(int),
190              previewWidget, setBarsTopPosition(int) );
191
192     /* Buttons */
193     QDialogButtonBox *okCancel = new QDialogButtonBox;
194     QPushButton *okButton = new QPushButton( qtr( "Cl&ose" ), this );
195     okButton->setDefault( true );
196     QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ), this );
197     okCancel->addButton( okButton, QDialogButtonBox::AcceptRole );
198     okCancel->addButton( cancelButton, QDialogButtonBox::RejectRole );
199
200     BUTTONACT( deleteButton, deleteProfile() );
201     BUTTONACT( newButton, newProfile() );
202     CONNECT( profileCombo, currentIndexChanged( int ), this, changeProfile( int ) );
203     BUTTONACT( okButton, close() );
204     BUTTONACT( cancelButton, cancel() );
205     mainLayout->addWidget( okCancel, 8, 0, 1, 9 );
206 }
207
208
209 ToolbarEditDialog::~ToolbarEditDialog()
210 {
211     getSettings()->beginWriteArray( "ToolbarProfiles" );
212     for( int i = 0; i < profileCombo->count(); i++ )
213     {
214         getSettings()->setArrayIndex(i);
215         getSettings()->setValue( "ProfileName", profileCombo->itemText( i ) );
216         getSettings()->setValue( "Value", profileCombo->itemData( i ) );
217     }
218     getSettings()->endArray();
219 }
220
221 void ToolbarEditDialog::newProfile()
222 {
223     bool ok;
224     QString name =  QInputDialog::getText( this, qtr( "Profile Name" ),
225                  qtr( "Please enter the new profile name." ), QLineEdit::Normal, 0, &ok );
226     if( !ok ) return;
227
228     QString temp = QString::number( !!positionCheckbox->isChecked() );
229     temp += "|" + controller1->getValue();
230     temp += "|" + controller2->getValue();
231     temp += "|" + controllerA->getValue();
232     temp += "|" + controller->getValue();
233     temp += "|" + controllerFSC->getValue();
234
235     profileCombo->addItem( name, temp );
236     profileCombo->setCurrentIndex( profileCombo->count() - 1 );
237 }
238
239 void ToolbarEditDialog::deleteProfile()
240 {
241     profileCombo->removeItem( profileCombo->currentIndex() );
242 }
243
244 void ToolbarEditDialog::changeProfile( int i )
245 {
246     QStringList qs_list = profileCombo->itemData( i ).toString().split( "|" );
247     if( qs_list.count() < 6 )
248         return;
249
250     positionCheckbox->setChecked( qs_list[0].toInt() );
251     controller1->resetLine( qs_list[1] );
252     controller2->resetLine( qs_list[2] );
253     controllerA->resetLine( qs_list[3] );
254     controller->resetLine( qs_list[4] );
255     controllerFSC->resetLine( qs_list[5] );
256 }
257
258 void ToolbarEditDialog::close()
259 {
260     getSettings()->setValue( "MainWindow/ToolbarPos", !!positionCheckbox->isChecked() );
261     getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
262     getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
263     getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
264     getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
265     getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
266     getSettings()->sync();
267     accept();
268 }
269
270 void ToolbarEditDialog::cancel()
271 {
272     reject();
273 }
274
275
276 PreviewWidget::PreviewWidget( QWidget *a, QWidget *b, QWidget *c )
277                : QWidget( a )
278 {
279     bars[0] = a;
280     bars[1] = b;
281     bars[2] = c;
282     for ( int i=0; i<3; i++ ) bars[i]->installEventFilter( this );
283     setAutoFillBackground( true );
284     setBarsTopPosition( false );
285 }
286
287 void PreviewWidget::setBarsTopPosition( int b )
288 {
289     b_top = b;
290     repaint();
291 }
292
293 void PreviewWidget::paintEvent( QPaintEvent * )
294 {
295     int i_total = 0, i_offset = 0, i;
296     QPainter painter( this );
297     QPixmap pixmaps[3];
298     for( int i=0; i<3; i++ )
299     {
300         pixmaps[i] = QPixmap::grabWidget( bars[i], bars[i]->contentsRect() );
301         for( int j=0; j < bars[i]->layout()->count(); j++ )
302         {
303             QLayoutItem *item = bars[i]->layout()->itemAt( j );
304             if ( !strcmp( item->widget()->metaObject()->className(), "QLabel" ) )
305             {
306                 QPainter eraser( &pixmaps[i] );
307                 eraser.fillRect( item->geometry(), palette().background() );
308                 eraser.end();
309             }
310         }
311         pixmaps[i] = pixmaps[i].scaled( size(), Qt::KeepAspectRatio );
312     }
313
314     for( i=0; i<3; i++ )
315         i_total += pixmaps[i].size().height();
316
317     /* Draw top bars */
318     i = ( b_top ) ? 1 : 3;
319     for( ; i<3; i++ )
320     {
321         painter.drawPixmap( pixmaps[i].rect().translated( 0, i_offset ), pixmaps[i] );
322         i_offset += pixmaps[i].rect().height();
323     }
324
325     /* Draw central area */
326     QRect conearea( 0, i_offset, size().width(), size().height() - i_total );
327     painter.fillRect( conearea, Qt::black );
328     QPixmap cone = QPixmap( ":/logo/vlc128.png" );
329     if ( ( conearea.size() - QSize(10, 10) - cone.size() ).isEmpty() )
330         cone = cone.scaled( conearea.size() - QSize(10, 10), Qt::KeepAspectRatio );
331     if ( cone.size().isValid() )
332     {
333         painter.drawPixmap( ((conearea.size() - cone.size()) / 2).width(),
334                             i_offset + ((conearea.size() - cone.size()) / 2).height(),
335                             cone );
336     }
337
338     /* Draw bottom bars */
339     i_offset += conearea.height();
340     for( i = 0 ; i< ((b_top) ? 1 : 3); i++ )
341     {
342         painter.drawPixmap( pixmaps[i].rect().translated( 0, i_offset ), pixmaps[i] );
343         i_offset += pixmaps[i].rect().height();
344     }
345
346     /* Draw overlay */
347     painter.fillRect( rect(), QColor( 255, 255, 255, 128 ) );
348
349     painter.end();
350 }
351
352 bool PreviewWidget::eventFilter( QObject *obj, QEvent *event )
353 {
354     if ( obj == this )
355         return QWidget::eventFilter( obj, event );
356
357     if ( event->type() == QEvent::LayoutRequest )
358         repaint();
359     return false;
360 }
361
362 /************************************************
363  *  Widget Listing:
364  * Creation of the list of drawed lovely buttons
365  ************************************************/
366 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
367               : QListWidget( _parent )
368 {
369     /* We need the parent to know the options checked */
370     parent = qobject_cast<ToolbarEditDialog *>(_parent);
371     assert( parent );
372
373     /* Normal options */
374     setViewMode( QListView::ListMode );
375     setTextElideMode( Qt::ElideNone );
376     setDragEnabled( true );
377     setIconSize( QSize( 64, 32 ) );
378
379     /* All the buttons do not need a special rendering */
380     for( int i = 0; i < BUTTON_MAX; i++ )
381     {
382         QListWidgetItem *widgetItem = new QListWidgetItem( this );
383         widgetItem->setText( qtr( nameL[i] ) );
384         widgetItem->setSizeHint( QSize( widgetItem->sizeHint().width(), 32 ) );
385         QPixmap pix( iconL[i] );
386         widgetItem->setIcon( pix.scaled( 16, 16, Qt::KeepAspectRatio, Qt::SmoothTransformation ) );
387         widgetItem->setData( Qt::UserRole, QVariant( i ) );
388         widgetItem->setToolTip( widgetItem->text() );
389         addItem( widgetItem );
390     }
391
392     /* Spacers are yet again a different thing */
393     QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/toolbar/space" ),
394             qtr( "Spacer" ), this );
395     widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
396     widgetItem->setToolTip( widgetItem->text() );
397     widgetItem->setSizeHint( QSize( widgetItem->sizeHint().width(), 32 ) );
398     addItem( widgetItem );
399
400     widgetItem = new QListWidgetItem( QIcon( ":/toolbar/space" ),
401             qtr( "Expanding Spacer" ), this );
402     widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
403     widgetItem->setToolTip( widgetItem->text() );
404     widgetItem->setSizeHint( QSize( widgetItem->sizeHint().width(), 32 ) );
405     addItem( widgetItem );
406
407     /**
408      * For all other widgets, we create then, do a pseudo rendering in
409      * a pixmaps for the view, and delete the object
410      *
411      * A lot of code is retaken from the Abstract, but not exactly...
412      * So, rewrite.
413      * They are better ways to deal with this, but I doubt that this is
414      * necessary. If you feel like you have the time, be my guest.
415      * --
416      * jb
417      **/
418     for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
419     {
420         QWidget *widget = NULL;
421         QListWidgetItem *widgetItem = new QListWidgetItem( this );
422         widgetItem->setSizeHint( QSize( widgetItem->sizeHint().width(), 32 ) );
423         switch( i )
424         {
425         case SPLITTER:
426             {
427                 QFrame *line = new QFrame( this );
428                 line->setFrameShape( QFrame::VLine );
429                 line->setFrameShadow( QFrame::Raised );
430                 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
431                 widget = line;
432             }
433             widgetItem->setText( qtr("Splitter") );
434             break;
435         case INPUT_SLIDER:
436             {
437                 SeekSlider *slider = new SeekSlider( Qt::Horizontal, this );
438                 widget = slider;
439             }
440             widgetItem->setText( qtr("Time Slider") );
441             break;
442         case VOLUME:
443             {
444                 SoundWidget *snd = new SoundWidget( this, p_intf,
445                         parent->getOptions() & WIDGET_SHINY );
446                 widget = snd;
447             }
448             widgetItem->setText( qtr("Volume") );
449             break;
450         case VOLUME_SPECIAL:
451             {
452                 QListWidgetItem *widgetItem = new QListWidgetItem( this );
453                 widgetItem->setText( qtr("Small Volume") );
454                 widgetItem->setIcon( QIcon( ":/toolbar/volume-medium" ) );
455                 widgetItem->setData( Qt::UserRole, QVariant( i ) );
456                 addItem( widgetItem );
457             }
458             continue;
459         case TIME_LABEL:
460             {
461                 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
462                 widget = timeLabel;
463             }
464             widgetItem->setText( qtr("Time") );
465             break;
466         case MENU_BUTTONS:
467             {
468                 QWidget *discFrame = new QWidget( this );
469                 //discFrame->setLineWidth( 1 );
470                 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
471                 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
472
473                 QToolButton *prevSectionButton = new QToolButton( discFrame );
474                 prevSectionButton->setIcon( QIcon( ":/toolbar/dvd_prev" ) );
475                 prevSectionButton->setToolTip( qtr("Previous chapter") );
476                 discLayout->addWidget( prevSectionButton );
477
478                 QToolButton *menuButton = new QToolButton( discFrame );
479                 menuButton->setIcon( QIcon( ":/toolbar/dvd_menu" ) );
480                 menuButton->setToolTip( qtr("Go to the DVD menu") );
481                 discLayout->addWidget( menuButton );
482
483                 QToolButton *nextButton = new QToolButton( discFrame );
484                 nextButton->setIcon( QIcon( ":/toolbar/dvd_next" ) );
485                 nextButton->setToolTip( qtr("Next chapter") );
486                 discLayout->addWidget( nextButton );
487
488                 widget = discFrame;
489             }
490             widgetItem->setText( qtr("DVD menus") );
491             break;
492         case TELETEXT_BUTTONS:
493             {
494                 QWidget *telexFrame = new QWidget( this );
495                 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
496                 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
497
498                 QToolButton *telexOn = new QToolButton( telexFrame );
499                 telexOn->setIcon( QIcon( ":/toolbar/tv" ) );
500                 telexLayout->addWidget( telexOn );
501
502                 QToolButton *telexTransparent = new QToolButton;
503                 telexTransparent->setIcon( QIcon( ":/toolbar/tvtelx" ) );
504                 telexTransparent->setToolTip( qtr("Teletext transparency") );
505                 telexLayout->addWidget( telexTransparent );
506
507                 QSpinBox *telexPage = new QSpinBox;
508                 telexLayout->addWidget( telexPage );
509
510                 widget = telexFrame;
511             }
512             widgetItem->setText( qtr("Teletext") );
513             break;
514         case ADVANCED_CONTROLLER:
515             {
516                 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
517                 widget = advControls;
518             }
519             widgetItem->setText( qtr("Advanced Buttons") );
520             break;
521         case PLAYBACK_BUTTONS:
522             {
523                 widget = new QWidget;
524                 DeckButtonsLayout *layout = new DeckButtonsLayout( widget );
525                 BrowseButton *prev = new BrowseButton( widget, BrowseButton::Backward );
526                 BrowseButton *next = new BrowseButton( widget );
527                 RoundButton *play = new RoundButton( widget );
528                 layout->setBackwardButton( prev );
529                 layout->setForwardButton( next );
530                 layout->setRoundButton( play );
531             }
532             widgetItem->setText( qtr("Playback Buttons") );
533             break;
534         case ASPECT_RATIO_COMBOBOX:
535             widget = new AspectRatioComboBox( p_intf );
536             widgetItem->setText( qtr("Aspect ratio selector") );
537             break;
538         case SPEED_LABEL:
539             widget = new SpeedLabel( p_intf, this );
540             widgetItem->setText( qtr("Speed selector") );
541             break;
542         case TIME_LABEL_ELAPSED:
543             widget = new QLabel( "2:42", this );
544             widgetItem->setText( qtr("Elapsed time") );
545             break;
546         case TIME_LABEL_REMAINING:
547             widget = new QLabel( "-2:42", this );
548             widgetItem->setText( qtr("Total/Remaining time") );
549             break;
550         default:
551             msg_Warn( p_intf, "This should not happen %i", i );
552             break;
553         }
554
555         if( widget == NULL ) continue;
556
557
558         widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
559         widgetItem->setToolTip( widgetItem->text() );
560         widget->hide();
561         widgetItem->setData( Qt::UserRole, QVariant( i ) );
562
563         addItem( widgetItem );
564         delete widget;
565     }
566 }
567
568 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
569 {
570     QListWidgetItem *item = currentItem();
571
572     QByteArray itemData;
573     QDataStream dataStream( &itemData, QIODevice::WriteOnly );
574
575     int i_type = item->data( Qt::UserRole ).toInt();
576     int i_option = parent->getOptions();
577     dataStream << i_type << i_option;
578
579     /* Create a new dragging event */
580     QDrag *drag = new QDrag( this );
581
582     /* With correct mimedata */
583     QMimeData *mimeData = new QMimeData;
584     mimeData->setData( "vlc/button-bar", itemData );
585     drag->setMimeData( mimeData );
586
587     /* And correct pixmap */
588     QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
589     drag->setPixmap( aPixmap );
590     drag->setHotSpot( QPoint( 20, 20 ) );
591
592     /* We want to keep a copy */
593     drag->exec( Qt::CopyAction | Qt::MoveAction );
594 }
595
596 /*
597  * The special controller with drag'n drop abilities.
598  * We don't do this in the main controller, since we don't want the OverHead
599  * to propagate there too
600  */
601 DroppingController::DroppingController( intf_thread_t *_p_intf,
602                                         const QString& line,
603                                         QWidget *_parent )
604                    : AbstractController( _p_intf, _parent )
605 {
606     RTL_UNAFFECTED_WIDGET
607     rubberband = NULL;
608     b_draging = false;
609     setAcceptDrops( true );
610     controlLayout = new QHBoxLayout( this );
611     controlLayout->setSpacing( 5 );
612     controlLayout->setMargin( 0 );
613     setFrameShape( QFrame::StyledPanel );
614     setFrameShadow( QFrame::Raised );
615     setMinimumHeight( 20 );
616
617     parseAndCreate( line, controlLayout );
618 }
619
620 void DroppingController::resetLine( const QString& line )
621 {
622     hide();
623     QLayoutItem *child;
624     while( (child = controlLayout->takeAt( 0 ) ) != 0 )
625     {
626         child->widget()->hide();
627         delete child;
628     }
629
630     parseAndCreate( line, controlLayout );
631     show();
632 }
633
634 /* Overloading the AbstractController one, because we don't manage the
635    Spacing items in the same ways */
636 void DroppingController::createAndAddWidget( QBoxLayout *newControlLayout,
637                                              int i_index,
638                                              buttonType_e i_type,
639                                              int i_option )
640 {
641     /* Special case for SPACERS, who aren't QWidgets */
642     if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
643     {
644         QLabel *label = new QLabel( this );
645         label->setPixmap( QPixmap( ":/toolbar/space" ) );
646         if( i_type == WIDGET_SPACER_EXTEND )
647         {
648             label->setSizePolicy( QSizePolicy::MinimumExpanding,
649                     QSizePolicy::Preferred );
650
651             /* Create a box around it */
652             label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
653             label->setLineWidth ( 1 );
654             label->setAlignment( Qt::AlignCenter );
655         }
656         else
657             label->setSizePolicy( QSizePolicy::Fixed,
658                     QSizePolicy::Preferred );
659
660         /* Install event Filter for drag'n drop */
661         label->installEventFilter( this );
662         newControlLayout->insertWidget( i_index, label );
663     }
664
665     /* Normal Widgets */
666     else
667     {
668         QWidget *widg = createWidget( i_type, i_option );
669         if( !widg ) return;
670
671         /* Install the Event Filter in order to catch the drag */
672         widg->setParent( this );
673         widg->installEventFilter( this );
674
675         /* We are in a complex widget, we need to stop events on children too */
676         if( i_type >= VOLUME && i_type < SPECIAL_MAX )
677         {
678             QList<QObject *>children = widg->children();
679
680             QObject *child;
681             foreach( child, children )
682             {
683                 QWidget *childWidg;
684                 if( ( childWidg = qobject_cast<QWidget *>( child ) ) )
685                 {
686                     child->installEventFilter( this );
687                     childWidg->setEnabled( true );
688                 }
689             }
690
691             /* Decorating the frames when possible */
692             QFrame *frame;
693             if( i_type >= MENU_BUTTONS  /* Don't bother to check for volume */
694                 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
695             {
696                 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
697                 frame->setLineWidth ( 1 );
698             }
699         }
700
701         /* Some Widgets are deactivated at creation */
702         widg->setEnabled( true );
703         widg->show();
704         newControlLayout->insertWidget( i_index, widg );
705     }
706
707     /* QList and QBoxLayout don't act the same with insert() */
708     if( i_index < 0 ) i_index = newControlLayout->count() - 1;
709
710     doubleInt *value = new doubleInt;
711     value->i_type = i_type;
712     value->i_option = i_option;
713
714     widgetList.insert( i_index, value );
715 }
716
717 DroppingController::~DroppingController()
718 {
719     qDeleteAll( widgetList );
720     widgetList.clear();
721 }
722
723 QString DroppingController::getValue()
724 {
725     QString qs = "";
726
727     for( int i = 0; i < controlLayout->count(); i++ )
728     {
729         doubleInt *dI = widgetList.at( i );
730         assert( dI );
731
732         qs.append( QString::number( dI->i_type ) );
733         if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
734         qs.append( ';' );
735     }
736     return qs;
737 }
738
739 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
740 {
741     if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
742         event->accept();
743     else
744         event->ignore();
745 }
746
747 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
748 {
749     QPoint origin = event->pos();
750
751     int i_pos = getParentPosInLayout( origin );
752     bool b_end = false;
753
754     /* Both sides of the frame */
755     if( i_pos == -1 )
756     {
757         if( rubberband ) rubberband->hide();
758         return;
759     }
760
761     /* Last item is special because of underlying items */
762     if( i_pos >= controlLayout->count() )
763     {
764         i_pos--;
765         b_end = true;
766     }
767
768     /* Query the underlying item for size && middles */
769     QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
770     QWidget *temp = tempItem->widget(); assert( temp );
771
772     /* Position assignment */
773     origin.ry() = 0;
774     origin.rx() = temp->x() - 2;
775
776     if( b_end ) origin.rx() += temp->width();
777
778     if( !rubberband )
779         rubberband = new QRubberBand( QRubberBand::Line, this );
780     rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
781     rubberband->show();
782 }
783
784 inline int DroppingController::getParentPosInLayout( QPoint point )
785 {
786     point.ry() = height() / 2 ;
787     QPoint origin = mapToGlobal ( point );
788
789     QWidget *tempWidg = QApplication::widgetAt( origin );
790     if( tempWidg == NULL )
791         return -1;
792
793     int i = controlLayout->indexOf( tempWidg );
794     if( i == -1 )
795     {
796         i = controlLayout->indexOf( tempWidg->parentWidget() );
797         tempWidg = tempWidg->parentWidget();
798     }
799
800     /* Return the nearest position */
801     if( ( point.x() - tempWidg->x()  > tempWidg->width() / 2 ) && i != -1 )
802         i++;
803
804     //    msg_Dbg( p_intf, "%i", i);
805     return i;
806 }
807
808 void DroppingController::dropEvent( QDropEvent *event )
809 {
810     int i = getParentPosInLayout( event->pos() );
811
812     QByteArray data = event->mimeData()->data( "vlc/button-bar" );
813     QDataStream dataStream(&data, QIODevice::ReadOnly);
814
815     int i_option = 0, i_type = 0;
816     dataStream >> i_type >> i_option;
817
818     createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
819
820     /* Hide by precaution, you don't exactly know what could have happened in
821        between */
822     if( rubberband ) rubberband->hide();
823 }
824
825 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
826 {
827     if( rubberband ) rubberband->hide();
828     event->accept();
829 }
830
831 /**
832  * Overloading doAction to block any action
833  **/
834 void DroppingController::doAction( int i )
835 {
836     VLC_UNUSED( i );
837 }
838
839 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
840 {
841     switch( event->type() )
842     {
843         case QEvent::MouseButtonPress:
844             b_draging = true;
845             return true;
846         case QEvent::MouseButtonRelease:
847             b_draging = false;
848             return true;
849         case QEvent::MouseMove:
850             {
851             if( !b_draging ) return true;
852             QWidget *widg = static_cast<QWidget*>(obj);
853
854             QByteArray itemData;
855             QDataStream dataStream( &itemData, QIODevice::WriteOnly );
856
857             int i = -1;
858             i = controlLayout->indexOf( widg );
859             if( i == -1 )
860             {
861                 i = controlLayout->indexOf( widg->parentWidget() );
862                 widg = widg->parentWidget();
863                 /* NOTE: be extra-careful Now with widg access */
864             }
865
866             if( i == -1 ) return true;
867             i_dragIndex = i;
868
869             doubleInt *dI = widgetList.at( i );
870
871             int i_type = dI->i_type;
872             int i_option = dI->i_option;
873             dataStream << i_type << i_option;
874
875             /* With correct mimedata */
876             QMimeData *mimeData = new QMimeData;
877             mimeData->setData( "vlc/button-bar", itemData );
878
879             QDrag *drag = new QDrag( widg );
880             drag->setMimeData( mimeData );
881
882             /* Remove before the drag to not mess DropEvent,
883                that will createAndAddWidget */
884             widgetList.removeAt( i );
885             controlLayout->removeWidget( widg );
886             widg->hide();
887
888             /* Start the effective drag */
889             drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
890             b_draging = false;
891             }
892             return true;
893
894         case QEvent::MouseButtonDblClick:
895         case QEvent::EnabledChange:
896         case QEvent::Hide:
897         case QEvent::HideToParent:
898         case QEvent::Move:
899         case QEvent::ZOrderChange:
900             return true;
901         default:
902             return false;
903     }
904 }