]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/toolbar.cpp
Qt: make the customize dialog a true Dialog.
[vlc] / modules / gui / qt4 / dialogs / toolbar.cpp
1 /*****************************************************************************
2  * ToolbarEdit.cpp : ToolbarEdit and About dialogs
3  ****************************************************************************
4  * Copyright (C) 2008 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 #include "util/input_slider.hpp"
31 #include "util/customwidgets.hpp"
32 #include "components/interface_widgets.hpp"
33
34 #include <QScrollArea>
35 #include <QGroupBox>
36 #include <QLabel>
37 #include <QComboBox>
38 #include <QListWidget>
39
40 #include <QDragEnterEvent>
41 #include <QDialogButtonBox>
42 #include <QInputDialog>
43
44 ToolbarEditDialog::ToolbarEditDialog( QWidget *_w, intf_thread_t *_p_intf)
45                   : QVLCDialog( _w,  _p_intf )
46 {
47     setWindowTitle( qtr( "Toolbars Editor" ) );
48     QGridLayout *mainLayout = new QGridLayout( this );
49     setMinimumWidth( 600 );
50     setAttribute( Qt::WA_DeleteOnClose );
51
52     /* main GroupBox */
53     QGroupBox *widgetBox = new QGroupBox( qtr( "Toolbar Elements") , this );
54     widgetBox->setSizePolicy( QSizePolicy::Preferred,
55                               QSizePolicy::MinimumExpanding );
56     QGridLayout *boxLayout = new QGridLayout( widgetBox );
57
58     QLabel *styleLabel = new QLabel( qtr( "Next widget style:" ) );
59     flatBox = new QCheckBox( qtr( "Flat Button" ) );
60     bigBox = new QCheckBox( qtr( "Big Button" ) );
61     shinyBox = new QCheckBox( qtr( "Native Slider" ) );
62
63     boxLayout->addWidget( new WidgetListing( p_intf, this ), 0, 0, 1, -1);
64     boxLayout->addWidget( styleLabel, 1, 0 );
65     boxLayout->addWidget( flatBox, 1, 1 );
66     boxLayout->addWidget( bigBox, 1, 2 );
67     boxLayout->addWidget( shinyBox, 1, 3 );
68     mainLayout->addWidget( widgetBox, 0, 0, 1, -1 );
69
70
71     /* Main ToolBar */
72     QGroupBox *mainToolbarBox = new QGroupBox( qtr( "Main Toolbar" ), this );
73     QGridLayout *mainTboxLayout = new QGridLayout( mainToolbarBox );
74
75     QLabel *label = new QLabel( qtr( "Toolbar position:" ) );
76     mainTboxLayout->addWidget(label, 0, 0, 1, 2);
77
78     positionCombo = new QComboBox;
79     positionCombo->addItem( qtr( "Under the Video" ), QVariant( 0 ) );
80     positionCombo->addItem( qtr( "Above the Video" ), QVariant( 1 ) );
81     positionCombo->setCurrentIndex( positionCombo->findData(
82                 getSettings()->value( "MainWindow/ToolbarPos", 0 ).toInt() ) );
83     mainTboxLayout->addWidget( positionCombo, 0, 2, 1, 1 );
84
85     QLabel *line1Label = new QLabel( "Line 1:" );
86     QString line1 = getSettings()->value( "MainWindow/MainToolbar1",
87                                           MAIN_TB1_DEFAULT ).toString();
88     controller1 = new DroppingController( p_intf, line1,
89             this );
90     mainTboxLayout->addWidget( line1Label, 1, 0, 1, 1 );
91     mainTboxLayout->addWidget( controller1, 1, 1, 1, 2 );
92
93     QLabel *line2Label = new QLabel( "Line 2:" );
94     QString line2 = getSettings()->value( "MainWindow/MainToolbar2",
95                                           MAIN_TB2_DEFAULT ).toString();
96     controller2 = new DroppingController( p_intf, line2,
97             this );
98     mainTboxLayout->addWidget( line2Label, 2, 0, 1, 1 );
99     mainTboxLayout->addWidget( controller2, 2, 1, 1, 2);
100
101     /* Advanced ToolBar */
102     QLabel *advLabel = new QLabel( qtr( "Advanced Widget toolbar:" ) );
103     QString lineA = getSettings()->value( "MainWindow/AdvToolbar",
104                                           ADV_TB_DEFAULT ).toString();
105     controllerA = new DroppingController( p_intf, lineA,
106             this );
107     mainTboxLayout->addWidget( advLabel, 3, 0, 1, 2 );
108     mainTboxLayout->addWidget( controllerA, 3, 2, 1, 1 );
109
110     mainLayout->addWidget( mainToolbarBox, 1, 0, 1, -1 );
111
112     /* TimeToolBar */
113     QGroupBox *timeToolbarBox = new QGroupBox( qtr( "Time Toolbar" ) , this );
114     QGridLayout *timeTboxLayout = new QGridLayout( timeToolbarBox );
115
116     QString line = getSettings()->value( "MainWindow/InputToolbar",
117                                          INPT_TB_DEFAULT ).toString();
118     controller = new DroppingController( p_intf, line,
119             this );
120     timeTboxLayout->addWidget( controller, 0, 0, 1, -1 );
121
122     mainLayout->addWidget( timeToolbarBox, 2, 0, 1, -1 );
123
124     /* FSCToolBar */
125     QGroupBox *FSCToolbarBox = new QGroupBox( qtr( "Fullscreen Controller" ),
126                                               this );
127     QGridLayout *FSCTboxLayout = new QGridLayout( FSCToolbarBox );
128
129     QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar",
130                                             FSC_TB_DEFAULT ).toString();
131     controllerFSC = new DroppingController( p_intf,
132             lineFSC, this );
133     FSCTboxLayout->addWidget( controllerFSC, 0, 0, 1, -1 );
134
135     mainLayout->addWidget( FSCToolbarBox, 3, 0, 1, -1 );
136
137     /* Profile */
138     QGroupBox *profileBox = new QGroupBox( qtr( "Profile" ), this );
139     QGridLayout *profileBoxLayout = new QGridLayout( profileBox );
140
141     profileCombo = new QComboBox;
142     QLabel *profileLabel = new QLabel( qtr( "Select profile:" ), this );
143
144     QToolButton *newButton = new QToolButton;
145     newButton->setIcon( QIcon( ":/new" ) );
146     QToolButton *deleteButton = new QToolButton;
147     deleteButton->setIcon( QIcon( ":/clear" ) );
148     deleteButton->setToolTip( qtr( "Delete the current profile" ) );
149
150     profileBoxLayout->addWidget( profileLabel, 0, 0 );
151     profileBoxLayout->addWidget( profileCombo, 0, 1 );
152     profileBoxLayout->addWidget( newButton, 0, 2 );
153     profileBoxLayout->addWidget( deleteButton, 0, 3 );
154
155     mainLayout->addWidget( profileBox, 4, 0, 1, -1 );
156
157     /* Fill combos */
158     int i_size = getSettings()->beginReadArray( "ToolbarProfiles" );
159     for( int i = 0; i < i_size; i++ )
160     {
161         getSettings()->setArrayIndex(i);
162         profileCombo->addItem( getSettings()->value( "ProfileName" ).toString(),
163                                getSettings()->value( "Value" ).toString() );
164     }
165     getSettings()->endArray();
166
167     /* Load defaults ones if we have no combos */
168     /* We could decide that we load defaults on first launch of the dialog
169        or when the combo is back to 0. I choose the second solution, because some clueless
170        user might hit on delete a bit too much, but discussion is opened. -- jb */
171     if( i_size == 0 )
172     {
173         profileCombo->addItem( PROFILE_NAME_1, QString( VALUE_1 ) );
174         profileCombo->addItem( PROFILE_NAME_2, QString( VALUE_2 ) );
175         profileCombo->addItem( PROFILE_NAME_3, QString( VALUE_3 ) );
176         profileCombo->addItem( PROFILE_NAME_4, QString( VALUE_4 ) );
177         profileCombo->addItem( PROFILE_NAME_5, QString( VALUE_5 ) );
178     }
179     profileCombo->setCurrentIndex( -1 );
180
181     /* Buttons */
182     QDialogButtonBox *okCancel = new QDialogButtonBox;
183     QPushButton *okButton = new QPushButton( qtr( "Cl&ose" ), this );
184     okButton->setDefault( true );
185     QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ), this );
186     okCancel->addButton( okButton, QDialogButtonBox::AcceptRole );
187     okCancel->addButton( cancelButton, QDialogButtonBox::RejectRole );
188
189     BUTTONACT( deleteButton, deleteProfile() );
190     BUTTONACT( newButton, newProfile() );
191     CONNECT( profileCombo, currentIndexChanged( int ), this, changeProfile( int ) );
192     BUTTONACT( okButton, close() );
193     BUTTONACT( cancelButton, cancel() );
194     mainLayout->addWidget( okCancel, 5, 2 );
195 }
196
197
198 ToolbarEditDialog::~ToolbarEditDialog()
199 {
200     getSettings()->beginWriteArray( "ToolbarProfiles" );
201     for( int i = 0; i < profileCombo->count(); i++ )
202     {
203         getSettings()->setArrayIndex(i);
204         getSettings()->setValue( "ProfileName", profileCombo->itemText( i ) );
205         getSettings()->setValue( "Value", profileCombo->itemData( i ) );
206     }
207     getSettings()->endArray();
208 }
209
210 void ToolbarEditDialog::newProfile()
211 {
212     bool ok;
213     QString name =  QInputDialog::getText( this, qtr( "Profile Name" ),
214                  qtr( "Please enter the new profile name." ), QLineEdit::Normal, 0, &ok );
215     if( !ok ) return;
216
217     QString temp = QString::number( positionCombo->currentIndex() );
218     temp += "|" + controller1->getValue();
219     temp += "|" + controller2->getValue();
220     temp += "|" + controllerA->getValue();
221     temp += "|" + controller->getValue();
222     temp += "|" + controllerFSC->getValue();
223
224     profileCombo->addItem( name, temp );
225     profileCombo->setCurrentIndex( profileCombo->count() - 1 );
226 }
227
228 void ToolbarEditDialog::deleteProfile()
229 {
230     profileCombo->removeItem( profileCombo->currentIndex() );
231 }
232
233 void ToolbarEditDialog::changeProfile( int i )
234 {
235     QStringList qs_list = profileCombo->itemData( i ).toString().split( "|" );
236     if( qs_list.count() < 6 )
237         return;
238
239     positionCombo->setCurrentIndex( positionCombo->findData( qs_list[0].toInt() ) );
240     controller1->resetLine( qs_list[1] );
241     controller2->resetLine( qs_list[2] );
242     controllerA->resetLine( qs_list[3] );
243     controller->resetLine( qs_list[4] );
244     controllerFSC->resetLine( qs_list[5] );
245 }
246
247 void ToolbarEditDialog::close()
248 {
249     msg_Dbg( p_intf, "Close and save" );
250     hide();
251     getSettings()->setValue( "MainWindow/ToolbarPos",
252             positionCombo->itemData( positionCombo->currentIndex() ).toInt() );
253     getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
254     getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
255     getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
256     getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
257     getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
258 }
259
260 void ToolbarEditDialog::cancel()
261 {
262     hide();
263 }
264
265 /************************************************
266  *  Widget Listing:
267  * Creation of the list of drawed lovely buttons
268  ************************************************/
269 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
270               : QListWidget( _parent )
271 {
272     /* We need the parent to know the options checked */
273     parent = qobject_cast<ToolbarEditDialog *>(_parent);
274     assert( parent );
275
276     /* Normal options */
277     setViewMode( QListView::IconMode );
278     setSpacing( 20 );
279     setDragEnabled( true );
280     setMinimumHeight( 250 );
281
282     /* All the buttons do not need a special rendering */
283     for( int i = 0; i < BUTTON_MAX; i++ )
284     {
285         QListWidgetItem *widgetItem = new QListWidgetItem( this );
286         widgetItem->setText( qtr( nameL[i] ) );
287         widgetItem->setIcon( QIcon( iconL[i] ) );
288         widgetItem->setData( Qt::UserRole, QVariant( i ) );
289         addItem( widgetItem );
290     }
291
292     /* Spacers are yet again a different thing */
293     QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
294             qtr( "Spacer" ), this );
295     widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
296     addItem( widgetItem );
297
298     widgetItem = new QListWidgetItem( QIcon( ":/space" ),
299             qtr( "Expanding Spacer" ), this );
300     widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
301     addItem( widgetItem );
302
303     /**
304      * For all other widgets, we create then, do a pseudo rendering in
305      * a pixmaps for the view, and delete the object
306      *
307      * A lot of code is retaken from the Abstract, but not exactly...
308      * So, rewrite.
309      * They are better ways to deal with this, but I doubt that this is
310      * necessary. If you feel like you have the time, be my guest.
311      * --
312      * jb
313      **/
314     for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
315     {
316         QWidget *widget = NULL;
317         QListWidgetItem *widgetItem = new QListWidgetItem( this );
318         switch( i )
319         {
320         case SPLITTER:
321             {
322                 QFrame *line = new QFrame( this );
323                 line->setFrameShape( QFrame::VLine );
324                 line->setFrameShadow( QFrame::Raised );
325                 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
326                 widget = line;
327             }
328             widgetItem->setText( qtr("Splitter") );
329             break;
330         case INPUT_SLIDER:
331             {
332                 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
333                 widget = slider;
334             }
335             widgetItem->setText( qtr("Time Slider") );
336             break;
337         case VOLUME:
338             {
339                 SoundWidget *snd = new SoundWidget( this, p_intf,
340                         parent->getOptions() & WIDGET_SHINY );
341                 widget = snd;
342             }
343             widgetItem->setText( qtr("Volume") );
344             break;
345         case VOLUME_SPECIAL:
346             {
347                 QListWidgetItem *widgetItem = new QListWidgetItem( this );
348                 widgetItem->setText( qtr("Small Volume") );
349                 widgetItem->setIcon( QIcon( ":/volume-medium" ) );
350                 widgetItem->setData( Qt::UserRole, QVariant( i ) );
351                 addItem( widgetItem );
352             }
353             continue;
354         case TIME_LABEL:
355             {
356                 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
357                 widget = timeLabel;
358             }
359             widgetItem->setText( qtr("Time") );
360             break;
361         case MENU_BUTTONS:
362             {
363                 QWidget *discFrame = new QWidget( this );
364                 //discFrame->setLineWidth( 1 );
365                 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
366                 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
367
368                 QToolButton *prevSectionButton = new QToolButton( discFrame );
369                 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
370                 discLayout->addWidget( prevSectionButton );
371
372                 QToolButton *menuButton = new QToolButton( discFrame );
373                 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
374                 discLayout->addWidget( menuButton );
375
376                 QToolButton *nextButton = new QToolButton( discFrame );
377                 nextButton->setIcon( QIcon( ":/dvd_next" ) );
378                 discLayout->addWidget( nextButton );
379
380                 widget = discFrame;
381             }
382             widgetItem->setText( qtr("DVD menus") );
383             break;
384         case TELETEXT_BUTTONS:
385             {
386                 QWidget *telexFrame = new QWidget( this );
387                 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
388                 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
389
390                 QToolButton *telexOn = new QToolButton( telexFrame );
391                 telexOn->setIcon( QIcon( ":/tv" ) );
392                 telexLayout->addWidget( telexOn );
393
394                 QToolButton *telexTransparent = new QToolButton;
395                 telexOn->setIcon( QIcon( ":/tvtelx" ) );
396                 telexLayout->addWidget( telexTransparent );
397
398                 QSpinBox *telexPage = new QSpinBox;
399                 telexLayout->addWidget( telexPage );
400
401                 widget = telexFrame;
402             }
403             widgetItem->setText( qtr("Teletext") );
404             break;
405         case ADVANCED_CONTROLLER:
406             {
407                 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
408                 widget = advControls;
409             }
410             widgetItem->setText( qtr("Advanced Buttons") );
411             break;
412         default:
413             msg_Warn( p_intf, "This should not happen %i", i );
414             break;
415         }
416
417         if( widget == NULL ) continue;
418
419
420         widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
421         widget->hide();
422         widgetItem->setData( Qt::UserRole, QVariant( i ) );
423
424         addItem( widgetItem );
425         delete widget;
426     }
427 }
428
429 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
430 {
431     QListWidgetItem *item = currentItem();
432
433     QByteArray itemData;
434     QDataStream dataStream( &itemData, QIODevice::WriteOnly );
435
436     int i_type = item->data( Qt::UserRole ).toInt();
437     int i_option = parent->getOptions();
438     dataStream << i_type << i_option;
439
440     /* Create a new dragging event */
441     QDrag *drag = new QDrag( this );
442
443     /* With correct mimedata */
444     QMimeData *mimeData = new QMimeData;
445     mimeData->setData( "vlc/button-bar", itemData );
446     drag->setMimeData( mimeData );
447
448     /* And correct pixmap */
449     QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
450     drag->setPixmap( aPixmap );
451     drag->setHotSpot( QPoint( 20, 20 ) );
452
453     /* We want to keep a copy */
454     drag->exec( Qt::CopyAction | Qt::MoveAction );
455 }
456
457 /*
458  * The special controller with drag'n drop abilities.
459  * We don't do this in the main controller, since we don't want the OverHead
460  * to propagate there too
461  */
462 DroppingController::DroppingController( intf_thread_t *_p_intf,
463                                         const QString& line,
464                                         QWidget *_parent )
465                    : AbstractController( _p_intf, _parent )
466 {
467     rubberband = NULL;
468     b_draging = false;
469     setAcceptDrops( true );
470     controlLayout = new QHBoxLayout( this );
471     controlLayout->setSpacing( 5 );
472     controlLayout->setMargin( 0 );
473     setFrameShape( QFrame::StyledPanel );
474     setFrameShadow( QFrame::Raised );
475
476     parseAndCreate( line, controlLayout );
477 }
478
479 void DroppingController::resetLine( const QString& line )
480 {
481     hide();
482     QLayoutItem *child;
483     int i =0;
484     while( (child = controlLayout->takeAt( 0 ) ) != 0 )
485     {
486         child->widget()->hide();
487         delete child;
488     }
489
490     parseAndCreate( line, controlLayout );
491     show();
492 }
493
494 /* Overloading the AbstractController one, because we don't manage the
495    Spacing items in the same ways */
496 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
497                                              int i_index,
498                                              buttonType_e i_type,
499                                              int i_option )
500 {
501     /* Special case for SPACERS, who aren't QWidgets */
502     if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
503     {
504         QLabel *label = new QLabel( this );
505         label->setPixmap( QPixmap( ":/space" ) );
506         if( i_type == WIDGET_SPACER_EXTEND )
507         {
508             label->setSizePolicy( QSizePolicy::MinimumExpanding,
509                     QSizePolicy::Preferred );
510
511             /* Create a box around it */
512             label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
513             label->setLineWidth ( 1 );
514             label->setAlignment( Qt::AlignCenter );
515         }
516         else
517             label->setSizePolicy( QSizePolicy::Fixed,
518                     QSizePolicy::Preferred );
519
520         /* Install event Filter for drag'n drop */
521         label->installEventFilter( this );
522         controlLayout->insertWidget( i_index, label );
523     }
524
525     /* Normal Widgets */
526     else
527     {
528         QWidget *widg = createWidget( i_type, i_option );
529         if( !widg ) return;
530
531         /* Install the Event Filter in order to catch the drag */
532         widg->setParent( this );
533         widg->installEventFilter( this );
534
535         /* We are in a complex widget, we need to stop events on children too */
536         if( i_type >= VOLUME && i_type < SPECIAL_MAX )
537         {
538             QList<QObject *>children = widg->children();
539
540             QObject *child;
541             foreach( child, children )
542             {
543                 QWidget *childWidg;
544                 if( childWidg = qobject_cast<QWidget *>( child ) )
545                 {
546                     child->installEventFilter( this );
547                     childWidg->setEnabled( true );
548                 }
549             }
550
551             /* Decorating the frames when possible */
552             QFrame *frame;
553             if( i_type >= MENU_BUTTONS  /* Don't bother to check for volume */
554                 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
555             {
556                 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
557                 frame->setLineWidth ( 1 );
558             }
559         }
560
561         /* Some Widgets are deactivated at creation */
562         widg->setEnabled( true );
563         widg->show();
564         controlLayout->insertWidget( i_index, widg );
565     }
566
567     /* QList and QBoxLayout don't act the same with insert() */
568     if( i_index < 0 ) i_index = controlLayout->count() - 1;
569
570     /* Insert in the value listing */
571     doubleInt *value = new doubleInt;
572     value->i_type = i_type;
573     value->i_option = i_option;
574     widgetList.insert( i_index, value );
575 }
576
577 DroppingController::~DroppingController()
578 {
579     qDeleteAll( widgetList );
580     widgetList.clear();
581 }
582
583 QString DroppingController::getValue()
584 {
585     QString qs = "";
586
587     for( int i = 0; i < controlLayout->count(); i++ )
588     {
589         doubleInt *dI = widgetList.at( i );
590         assert( dI );
591
592         qs.append( QString::number( dI->i_type ) );
593         if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
594         qs.append( ';' );
595     }
596     return qs;
597 }
598
599 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
600 {
601     if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
602         event->accept();
603     else
604         event->ignore();
605 }
606
607 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
608 {
609     QPoint origin = event->pos();
610
611     int i_pos = getParentPosInLayout( origin );
612     bool b_end = false;
613
614     /* Both sides of the frame */
615     if( i_pos == -1 )
616     {
617         if( rubberband ) rubberband->hide();
618         return;
619     }
620
621     /* Last item is special because of underlying items */
622     if( i_pos >= controlLayout->count() )
623     {
624         i_pos--;
625         b_end = true;
626     }
627
628     /* Query the underlying item for size && middles */
629     QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
630     QWidget *temp = tempItem->widget(); assert( temp );
631
632     /* Position assignment */
633     origin.ry() = 0;
634     origin.rx() = temp->x() - 2;
635
636     if( b_end ) origin.rx() += temp->width();
637
638     if( !rubberband )
639         rubberband = new QRubberBand( QRubberBand::Line, this );
640     rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
641     rubberband->show();
642 }
643
644 inline int DroppingController::getParentPosInLayout( QPoint point )
645 {
646     point.ry() = height() / 2 ;
647     QPoint origin = mapToGlobal ( point );
648
649     QWidget *tempWidg = QApplication::widgetAt( origin );
650
651     int i = -1;
652     if( tempWidg != NULL)
653     {
654         i = controlLayout->indexOf( tempWidg );
655         if( i == -1 )
656         {
657             i = controlLayout->indexOf( tempWidg->parentWidget() );
658             tempWidg = tempWidg->parentWidget();
659         }
660     }
661
662     /* Return the nearest position */
663     if( ( point.x() - tempWidg->x()  > tempWidg->width() / 2 ) && i != -1 )
664         i++;
665
666     //    msg_Dbg( p_intf, "%i", i);
667     return i;
668 }
669
670 void DroppingController::dropEvent( QDropEvent *event )
671 {
672     int i = getParentPosInLayout( event->pos() );
673
674     QByteArray data = event->mimeData()->data( "vlc/button-bar" );
675     QDataStream dataStream(&data, QIODevice::ReadOnly);
676
677     int i_option = 0, i_type = 0;
678     dataStream >> i_type >> i_option;
679
680     createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
681
682     /* Hide by precaution, you don't exactly know what could have happened in
683        between */
684     if( rubberband ) rubberband->hide();
685 }
686
687 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
688 {
689     if( rubberband ) rubberband->hide();
690     event->accept();
691 }
692
693 /**
694  * Overloading doAction to block any action
695  **/
696 void DroppingController::doAction( int i )
697 {
698     VLC_UNUSED( i );
699 }
700
701 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
702 {
703     switch( event->type() )
704     {
705         case QEvent::MouseButtonPress:
706             b_draging = true;
707             return true;
708         case QEvent::MouseButtonRelease:
709             b_draging = false;
710             return true;
711         case QEvent::MouseMove:
712             {
713             if( !b_draging ) return true;
714             QWidget *widg = static_cast<QWidget*>(obj);
715
716             QByteArray itemData;
717             QDataStream dataStream( &itemData, QIODevice::WriteOnly );
718
719             int i = -1;
720             i = controlLayout->indexOf( widg );
721             if( i == -1 )
722             {
723                 i = controlLayout->indexOf( widg->parentWidget() );
724                 widg = widg->parentWidget();
725                 /* NOTE: be extra-careful Now with widg access */
726             }
727
728             if( i == -1 ) return true;
729             doubleInt *dI = widgetList.at( i );
730
731             int i_type = dI->i_type;
732             int i_option = dI->i_option;
733             dataStream << i_type << i_option;
734
735             /* With correct mimedata */
736             QMimeData *mimeData = new QMimeData;
737             mimeData->setData( "vlc/button-bar", itemData );
738
739             QDrag *drag = new QDrag( widg );
740             drag->setMimeData( mimeData );
741
742             /* Start the effective drag */
743             drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
744
745             widgetList.removeAt( i );
746             controlLayout->removeWidget( widg );
747             widg->hide();
748             b_draging = false;
749             }
750             return true;
751
752         case QEvent::MouseButtonDblClick:
753         case QEvent::EnabledChange:
754         case QEvent::Hide:
755         case QEvent::HideToParent:
756         case QEvent::Move:
757         case QEvent::ZOrderChange:
758             return true;
759         default:
760             return false;
761     }
762 }
763