]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/toolbar.cpp
Qt: no need to restart VLC to get the new toolbars.
[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     getSettings()->setValue( "MainWindow/ToolbarPos",
251             positionCombo->itemData( positionCombo->currentIndex() ).toInt() );
252     getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
253     getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
254     getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
255     getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
256     getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
257     getSettings()->sync();
258     accept();
259 }
260
261 void ToolbarEditDialog::cancel()
262 {
263     reject();
264 }
265
266 /************************************************
267  *  Widget Listing:
268  * Creation of the list of drawed lovely buttons
269  ************************************************/
270 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
271               : QListWidget( _parent )
272 {
273     /* We need the parent to know the options checked */
274     parent = qobject_cast<ToolbarEditDialog *>(_parent);
275     assert( parent );
276
277     /* Normal options */
278     setViewMode( QListView::IconMode );
279     setSpacing( 20 );
280     setDragEnabled( true );
281     setMinimumHeight( 250 );
282
283     /* All the buttons do not need a special rendering */
284     for( int i = 0; i < BUTTON_MAX; i++ )
285     {
286         QListWidgetItem *widgetItem = new QListWidgetItem( this );
287         widgetItem->setText( qtr( nameL[i] ) );
288         widgetItem->setIcon( QIcon( iconL[i] ) );
289         widgetItem->setData( Qt::UserRole, QVariant( i ) );
290         addItem( widgetItem );
291     }
292
293     /* Spacers are yet again a different thing */
294     QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
295             qtr( "Spacer" ), this );
296     widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
297     addItem( widgetItem );
298
299     widgetItem = new QListWidgetItem( QIcon( ":/space" ),
300             qtr( "Expanding Spacer" ), this );
301     widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
302     addItem( widgetItem );
303
304     /**
305      * For all other widgets, we create then, do a pseudo rendering in
306      * a pixmaps for the view, and delete the object
307      *
308      * A lot of code is retaken from the Abstract, but not exactly...
309      * So, rewrite.
310      * They are better ways to deal with this, but I doubt that this is
311      * necessary. If you feel like you have the time, be my guest.
312      * --
313      * jb
314      **/
315     for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
316     {
317         QWidget *widget = NULL;
318         QListWidgetItem *widgetItem = new QListWidgetItem( this );
319         switch( i )
320         {
321         case SPLITTER:
322             {
323                 QFrame *line = new QFrame( this );
324                 line->setFrameShape( QFrame::VLine );
325                 line->setFrameShadow( QFrame::Raised );
326                 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
327                 widget = line;
328             }
329             widgetItem->setText( qtr("Splitter") );
330             break;
331         case INPUT_SLIDER:
332             {
333                 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
334                 widget = slider;
335             }
336             widgetItem->setText( qtr("Time Slider") );
337             break;
338         case VOLUME:
339             {
340                 SoundWidget *snd = new SoundWidget( this, p_intf,
341                         parent->getOptions() & WIDGET_SHINY );
342                 widget = snd;
343             }
344             widgetItem->setText( qtr("Volume") );
345             break;
346         case VOLUME_SPECIAL:
347             {
348                 QListWidgetItem *widgetItem = new QListWidgetItem( this );
349                 widgetItem->setText( qtr("Small Volume") );
350                 widgetItem->setIcon( QIcon( ":/volume-medium" ) );
351                 widgetItem->setData( Qt::UserRole, QVariant( i ) );
352                 addItem( widgetItem );
353             }
354             continue;
355         case TIME_LABEL:
356             {
357                 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
358                 widget = timeLabel;
359             }
360             widgetItem->setText( qtr("Time") );
361             break;
362         case MENU_BUTTONS:
363             {
364                 QWidget *discFrame = new QWidget( this );
365                 //discFrame->setLineWidth( 1 );
366                 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
367                 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
368
369                 QToolButton *prevSectionButton = new QToolButton( discFrame );
370                 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
371                 discLayout->addWidget( prevSectionButton );
372
373                 QToolButton *menuButton = new QToolButton( discFrame );
374                 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
375                 discLayout->addWidget( menuButton );
376
377                 QToolButton *nextButton = new QToolButton( discFrame );
378                 nextButton->setIcon( QIcon( ":/dvd_next" ) );
379                 discLayout->addWidget( nextButton );
380
381                 widget = discFrame;
382             }
383             widgetItem->setText( qtr("DVD menus") );
384             break;
385         case TELETEXT_BUTTONS:
386             {
387                 QWidget *telexFrame = new QWidget( this );
388                 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
389                 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
390
391                 QToolButton *telexOn = new QToolButton( telexFrame );
392                 telexOn->setIcon( QIcon( ":/tv" ) );
393                 telexLayout->addWidget( telexOn );
394
395                 QToolButton *telexTransparent = new QToolButton;
396                 telexOn->setIcon( QIcon( ":/tvtelx" ) );
397                 telexLayout->addWidget( telexTransparent );
398
399                 QSpinBox *telexPage = new QSpinBox;
400                 telexLayout->addWidget( telexPage );
401
402                 widget = telexFrame;
403             }
404             widgetItem->setText( qtr("Teletext") );
405             break;
406         case ADVANCED_CONTROLLER:
407             {
408                 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
409                 widget = advControls;
410             }
411             widgetItem->setText( qtr("Advanced Buttons") );
412             break;
413         default:
414             msg_Warn( p_intf, "This should not happen %i", i );
415             break;
416         }
417
418         if( widget == NULL ) continue;
419
420
421         widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
422         widget->hide();
423         widgetItem->setData( Qt::UserRole, QVariant( i ) );
424
425         addItem( widgetItem );
426         delete widget;
427     }
428 }
429
430 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
431 {
432     QListWidgetItem *item = currentItem();
433
434     QByteArray itemData;
435     QDataStream dataStream( &itemData, QIODevice::WriteOnly );
436
437     int i_type = item->data( Qt::UserRole ).toInt();
438     int i_option = parent->getOptions();
439     dataStream << i_type << i_option;
440
441     /* Create a new dragging event */
442     QDrag *drag = new QDrag( this );
443
444     /* With correct mimedata */
445     QMimeData *mimeData = new QMimeData;
446     mimeData->setData( "vlc/button-bar", itemData );
447     drag->setMimeData( mimeData );
448
449     /* And correct pixmap */
450     QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
451     drag->setPixmap( aPixmap );
452     drag->setHotSpot( QPoint( 20, 20 ) );
453
454     /* We want to keep a copy */
455     drag->exec( Qt::CopyAction | Qt::MoveAction );
456 }
457
458 /*
459  * The special controller with drag'n drop abilities.
460  * We don't do this in the main controller, since we don't want the OverHead
461  * to propagate there too
462  */
463 DroppingController::DroppingController( intf_thread_t *_p_intf,
464                                         const QString& line,
465                                         QWidget *_parent )
466                    : AbstractController( _p_intf, _parent )
467 {
468     rubberband = NULL;
469     b_draging = false;
470     setAcceptDrops( true );
471     controlLayout = new QHBoxLayout( this );
472     controlLayout->setSpacing( 5 );
473     controlLayout->setMargin( 0 );
474     setFrameShape( QFrame::StyledPanel );
475     setFrameShadow( QFrame::Raised );
476
477     parseAndCreate( line, controlLayout );
478 }
479
480 void DroppingController::resetLine( const QString& line )
481 {
482     hide();
483     QLayoutItem *child;
484     int i =0;
485     while( (child = controlLayout->takeAt( 0 ) ) != 0 )
486     {
487         child->widget()->hide();
488         delete child;
489     }
490
491     parseAndCreate( line, controlLayout );
492     show();
493 }
494
495 /* Overloading the AbstractController one, because we don't manage the
496    Spacing items in the same ways */
497 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
498                                              int i_index,
499                                              buttonType_e i_type,
500                                              int i_option )
501 {
502     /* Special case for SPACERS, who aren't QWidgets */
503     if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
504     {
505         QLabel *label = new QLabel( this );
506         label->setPixmap( QPixmap( ":/space" ) );
507         if( i_type == WIDGET_SPACER_EXTEND )
508         {
509             label->setSizePolicy( QSizePolicy::MinimumExpanding,
510                     QSizePolicy::Preferred );
511
512             /* Create a box around it */
513             label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
514             label->setLineWidth ( 1 );
515             label->setAlignment( Qt::AlignCenter );
516         }
517         else
518             label->setSizePolicy( QSizePolicy::Fixed,
519                     QSizePolicy::Preferred );
520
521         /* Install event Filter for drag'n drop */
522         label->installEventFilter( this );
523         controlLayout->insertWidget( i_index, label );
524     }
525
526     /* Normal Widgets */
527     else
528     {
529         QWidget *widg = createWidget( i_type, i_option );
530         if( !widg ) return;
531
532         /* Install the Event Filter in order to catch the drag */
533         widg->setParent( this );
534         widg->installEventFilter( this );
535
536         /* We are in a complex widget, we need to stop events on children too */
537         if( i_type >= VOLUME && i_type < SPECIAL_MAX )
538         {
539             QList<QObject *>children = widg->children();
540
541             QObject *child;
542             foreach( child, children )
543             {
544                 QWidget *childWidg;
545                 if( childWidg = qobject_cast<QWidget *>( child ) )
546                 {
547                     child->installEventFilter( this );
548                     childWidg->setEnabled( true );
549                 }
550             }
551
552             /* Decorating the frames when possible */
553             QFrame *frame;
554             if( i_type >= MENU_BUTTONS  /* Don't bother to check for volume */
555                 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
556             {
557                 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
558                 frame->setLineWidth ( 1 );
559             }
560         }
561
562         /* Some Widgets are deactivated at creation */
563         widg->setEnabled( true );
564         widg->show();
565         controlLayout->insertWidget( i_index, widg );
566     }
567
568     /* QList and QBoxLayout don't act the same with insert() */
569     if( i_index < 0 ) i_index = controlLayout->count() - 1;
570
571     /* Insert in the value listing */
572     doubleInt *value = new doubleInt;
573     value->i_type = i_type;
574     value->i_option = i_option;
575     widgetList.insert( i_index, value );
576 }
577
578 DroppingController::~DroppingController()
579 {
580     qDeleteAll( widgetList );
581     widgetList.clear();
582 }
583
584 QString DroppingController::getValue()
585 {
586     QString qs = "";
587
588     for( int i = 0; i < controlLayout->count(); i++ )
589     {
590         doubleInt *dI = widgetList.at( i );
591         assert( dI );
592
593         qs.append( QString::number( dI->i_type ) );
594         if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
595         qs.append( ';' );
596     }
597     return qs;
598 }
599
600 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
601 {
602     if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
603         event->accept();
604     else
605         event->ignore();
606 }
607
608 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
609 {
610     QPoint origin = event->pos();
611
612     int i_pos = getParentPosInLayout( origin );
613     bool b_end = false;
614
615     /* Both sides of the frame */
616     if( i_pos == -1 )
617     {
618         if( rubberband ) rubberband->hide();
619         return;
620     }
621
622     /* Last item is special because of underlying items */
623     if( i_pos >= controlLayout->count() )
624     {
625         i_pos--;
626         b_end = true;
627     }
628
629     /* Query the underlying item for size && middles */
630     QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
631     QWidget *temp = tempItem->widget(); assert( temp );
632
633     /* Position assignment */
634     origin.ry() = 0;
635     origin.rx() = temp->x() - 2;
636
637     if( b_end ) origin.rx() += temp->width();
638
639     if( !rubberband )
640         rubberband = new QRubberBand( QRubberBand::Line, this );
641     rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
642     rubberband->show();
643 }
644
645 inline int DroppingController::getParentPosInLayout( QPoint point )
646 {
647     point.ry() = height() / 2 ;
648     QPoint origin = mapToGlobal ( point );
649
650     QWidget *tempWidg = QApplication::widgetAt( origin );
651
652     int i = -1;
653     if( tempWidg != NULL)
654     {
655         i = controlLayout->indexOf( tempWidg );
656         if( i == -1 )
657         {
658             i = controlLayout->indexOf( tempWidg->parentWidget() );
659             tempWidg = tempWidg->parentWidget();
660         }
661     }
662
663     /* Return the nearest position */
664     if( ( point.x() - tempWidg->x()  > tempWidg->width() / 2 ) && i != -1 )
665         i++;
666
667     //    msg_Dbg( p_intf, "%i", i);
668     return i;
669 }
670
671 void DroppingController::dropEvent( QDropEvent *event )
672 {
673     int i = getParentPosInLayout( event->pos() );
674
675     QByteArray data = event->mimeData()->data( "vlc/button-bar" );
676     QDataStream dataStream(&data, QIODevice::ReadOnly);
677
678     int i_option = 0, i_type = 0;
679     dataStream >> i_type >> i_option;
680
681     createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
682
683     /* Hide by precaution, you don't exactly know what could have happened in
684        between */
685     if( rubberband ) rubberband->hide();
686 }
687
688 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
689 {
690     if( rubberband ) rubberband->hide();
691     event->accept();
692 }
693
694 /**
695  * Overloading doAction to block any action
696  **/
697 void DroppingController::doAction( int i )
698 {
699     VLC_UNUSED( i );
700 }
701
702 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
703 {
704     switch( event->type() )
705     {
706         case QEvent::MouseButtonPress:
707             b_draging = true;
708             return true;
709         case QEvent::MouseButtonRelease:
710             b_draging = false;
711             return true;
712         case QEvent::MouseMove:
713             {
714             if( !b_draging ) return true;
715             QWidget *widg = static_cast<QWidget*>(obj);
716
717             QByteArray itemData;
718             QDataStream dataStream( &itemData, QIODevice::WriteOnly );
719
720             int i = -1;
721             i = controlLayout->indexOf( widg );
722             if( i == -1 )
723             {
724                 i = controlLayout->indexOf( widg->parentWidget() );
725                 widg = widg->parentWidget();
726                 /* NOTE: be extra-careful Now with widg access */
727             }
728
729             if( i == -1 ) return true;
730             doubleInt *dI = widgetList.at( i );
731
732             int i_type = dI->i_type;
733             int i_option = dI->i_option;
734             dataStream << i_type << i_option;
735
736             /* With correct mimedata */
737             QMimeData *mimeData = new QMimeData;
738             mimeData->setData( "vlc/button-bar", itemData );
739
740             QDrag *drag = new QDrag( widg );
741             drag->setMimeData( mimeData );
742
743             /* Start the effective drag */
744             drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
745
746             widgetList.removeAt( i );
747             controlLayout->removeWidget( widg );
748             widg->hide();
749             b_draging = false;
750             }
751             return true;
752
753         case QEvent::MouseButtonDblClick:
754         case QEvent::EnabledChange:
755         case QEvent::Hide:
756         case QEvent::HideToParent:
757         case QEvent::Move:
758         case QEvent::ZOrderChange:
759             return true;
760         default:
761             return false;
762     }
763 }
764