]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/toolbar.cpp
Qt: toolbar-profile, provide 4 profiles, one being close to 0.8.6 interface, the...
[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::instance = NULL;
45
46 ToolbarEditDialog::ToolbarEditDialog( intf_thread_t *_p_intf)
47                   : QVLCFrame(  _p_intf )
48 {
49     setWindowTitle( qtr( "Toolbars Editor" ) );
50     QGridLayout *mainLayout = new QGridLayout( this );
51     setMinimumWidth( 600 );
52
53     /* main GroupBox */
54     QGroupBox *widgetBox = new QGroupBox( qtr( "Toolbar Elements") , this );
55     widgetBox->setSizePolicy( QSizePolicy::Preferred,
56                               QSizePolicy::MinimumExpanding );
57     QGridLayout *boxLayout = new QGridLayout( widgetBox );
58
59     QLabel *styleLabel = new QLabel( qtr( "Next widget style:" ) );
60     flatBox = new QCheckBox( qtr( "Flat Button" ) );
61     bigBox = new QCheckBox( qtr( "Big Button" ) );
62     shinyBox = new QCheckBox( qtr( "Native Slider" ) );
63
64     boxLayout->addWidget( new WidgetListing( p_intf, this ), 0, 0, 1, -1);
65     boxLayout->addWidget( styleLabel, 1, 0 );
66     boxLayout->addWidget( flatBox, 1, 1 );
67     boxLayout->addWidget( bigBox, 1, 2 );
68     boxLayout->addWidget( shinyBox, 1, 3 );
69     mainLayout->addWidget( widgetBox, 0, 0, 1, -1 );
70
71
72     /* Main ToolBar */
73     QGroupBox *mainToolbarBox = new QGroupBox( qtr( "Main Toolbar" ), this );
74     QGridLayout *mainTboxLayout = new QGridLayout( mainToolbarBox );
75
76     QLabel *label = new QLabel( qtr( "Toolbar position:" ) );
77     mainTboxLayout->addWidget(label, 0, 0, 1, 2);
78
79     positionCombo = new QComboBox;
80     positionCombo->addItem( qtr( "Under the Video" ), QVariant( 0 ) );
81     positionCombo->addItem( qtr( "Above the Video" ), QVariant( 1 ) );
82     positionCombo->setCurrentIndex( positionCombo->findData(
83                 getSettings()->value( "MainWindow/ToolbarPos", 0 ).toInt() ) );
84     mainTboxLayout->addWidget( positionCombo, 0, 2, 1, 1 );
85
86     QLabel *line1Label = new QLabel( "Line 1:" );
87     QString line1 = getSettings()->value( "MainWindow/MainToolbar1",
88                                           MAIN_TB1_DEFAULT ).toString();
89     controller1 = new DroppingController( p_intf, line1,
90             this );
91     mainTboxLayout->addWidget( line1Label, 1, 0, 1, 1 );
92     mainTboxLayout->addWidget( controller1, 1, 1, 1, 2 );
93
94     QLabel *line2Label = new QLabel( "Line 2:" );
95     QString line2 = getSettings()->value( "MainWindow/MainToolbar2",
96                                           MAIN_TB2_DEFAULT ).toString();
97     controller2 = new DroppingController( p_intf, line2,
98             this );
99     mainTboxLayout->addWidget( line2Label, 2, 0, 1, 1 );
100     mainTboxLayout->addWidget( controller2, 2, 1, 1, 2);
101
102     /* Advanced ToolBar */
103     QLabel *advLabel = new QLabel( qtr( "Advanced Widget toolbar:" ) );
104     QString lineA = getSettings()->value( "MainWindow/AdvToolbar",
105                                           ADV_TB_DEFAULT ).toString();
106     controllerA = new DroppingController( p_intf, lineA,
107             this );
108     mainTboxLayout->addWidget( advLabel, 3, 0, 1, 2 );
109     mainTboxLayout->addWidget( controllerA, 3, 2, 1, 1 );
110
111     mainLayout->addWidget( mainToolbarBox, 1, 0, 1, -1 );
112
113     /* TimeToolBar */
114     QGroupBox *timeToolbarBox = new QGroupBox( qtr( "Time Toolbar" ) , this );
115     QGridLayout *timeTboxLayout = new QGridLayout( timeToolbarBox );
116
117     QString line = getSettings()->value( "MainWindow/InputToolbar",
118                                          INPT_TB_DEFAULT ).toString();
119     controller = new DroppingController( p_intf, line,
120             this );
121     timeTboxLayout->addWidget( controller, 0, 0, 1, -1 );
122
123     mainLayout->addWidget( timeToolbarBox, 2, 0, 1, -1 );
124
125     /* FSCToolBar */
126     QGroupBox *FSCToolbarBox = new QGroupBox( qtr( "Fullscreen Controller" ),
127                                               this );
128     QGridLayout *FSCTboxLayout = new QGridLayout( FSCToolbarBox );
129
130     QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar",
131                                             FSC_TB_DEFAULT ).toString();
132     controllerFSC = new DroppingController( p_intf,
133             lineFSC, this );
134     FSCTboxLayout->addWidget( controllerFSC, 0, 0, 1, -1 );
135
136     mainLayout->addWidget( FSCToolbarBox, 3, 0, 1, -1 );
137
138     /* Profile */
139     QGroupBox *profileBox = new QGroupBox( qtr( "Profile" ), this );
140     QGridLayout *profileBoxLayout = new QGridLayout( profileBox );
141
142     profileCombo = new QComboBox;
143     QLabel *profileLabel = new QLabel( qtr( "Select profile:" ), this );
144
145     QToolButton *newButton = new QToolButton;
146     newButton->setIcon( QIcon( ":/new" ) );
147     QToolButton *deleteButton = new QToolButton;
148     deleteButton->setIcon( QIcon( ":/clear" ) );
149     deleteButton->setToolTip( qtr( "Delete the current profile" ) );
150
151     profileBoxLayout->addWidget( profileLabel, 0, 0 );
152     profileBoxLayout->addWidget( profileCombo, 0, 1 );
153     profileBoxLayout->addWidget( newButton, 0, 2 );
154     profileBoxLayout->addWidget( deleteButton, 0, 3 );
155
156     mainLayout->addWidget( profileBox, 4, 0, 1, -1 );
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_1, QString( VALUE_1 ) );
175         profileCombo->addItem( PROFILE_NAME_2, QString( VALUE_2 ) );
176         profileCombo->addItem( PROFILE_NAME_3, QString( VALUE_3 ) );
177         profileCombo->addItem( PROFILE_NAME_4, QString( VALUE_4 ) );
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( 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( "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                                         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( 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;
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->installEventFilter( this );
533
534         /* We are in a complex widget, we need to stop events on children too */
535         if( i_type >= VOLUME && i_type < SPECIAL_MAX )
536         {
537             QList<QObject *>children = widg->children();
538
539             QObject *child;
540             foreach( child, children )
541             {
542                 QWidget *childWidg;
543                 if( childWidg = qobject_cast<QWidget *>( child ) )
544                 {
545                     child->installEventFilter( this );
546                     childWidg->setEnabled( true );
547                 }
548             }
549
550             /* Decorating the frames when possible */
551             QFrame *frame;
552             if( i_type >= MENU_BUTTONS  /* Don't bother to check for volume */
553                 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
554             {
555                 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
556                 frame->setLineWidth ( 1 );
557             }
558         }
559
560         /* Some Widgets are deactivated at creation */
561         widg->setEnabled( true );
562         widg->show();
563         controlLayout->insertWidget( i_index, widg );
564     }
565
566     /* QList and QBoxLayout don't act the same with insert() */
567     if( i_index < 0 ) i_index = controlLayout->count() - 1;
568
569     /* Insert in the value listing */
570     doubleInt *value = new doubleInt;
571     value->i_type = i_type;
572     value->i_option = i_option;
573     widgetList.insert( i_index, value );
574 }
575
576 DroppingController::~DroppingController()
577 {
578     qDeleteAll( widgetList );
579     widgetList.clear();
580 }
581
582 QString DroppingController::getValue()
583 {
584     QString qs = "";
585
586     for( int i = 0; i < controlLayout->count(); i++ )
587     {
588         doubleInt *dI = widgetList.at( i );
589         assert( dI );
590
591         qs.append( QString::number( dI->i_type ) );
592         if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
593         qs.append( ';' );
594     }
595     return qs;
596 }
597
598 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
599 {
600     if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
601         event->accept();
602     else
603         event->ignore();
604 }
605
606 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
607 {
608     QPoint origin = event->pos();
609
610     int i_pos = getParentPosInLayout( origin );
611     bool b_end = false;
612
613     /* Both sides of the frame */
614     if( i_pos == -1 )
615     {
616         if( rubberband ) rubberband->hide();
617         return;
618     }
619
620     /* Last item is special because of underlying items */
621     if( i_pos >= controlLayout->count() )
622     {
623         i_pos--;
624         b_end = true;
625     }
626
627     /* Query the underlying item for size && middles */
628     QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
629     QWidget *temp = tempItem->widget(); assert( temp );
630
631     /* Position assignment */
632     origin.ry() = 0;
633     origin.rx() = temp->x() - 2;
634
635     if( b_end ) origin.rx() += temp->width();
636
637     if( !rubberband )
638         rubberband = new QRubberBand( QRubberBand::Line, this );
639     rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
640     rubberband->show();
641 }
642
643 inline int DroppingController::getParentPosInLayout( QPoint point )
644 {
645     point.ry() = height() / 2 ;
646     QPoint origin = mapToGlobal ( point );
647
648     QWidget *tempWidg = QApplication::widgetAt( origin );
649
650     int i = -1;
651     if( tempWidg != NULL)
652     {
653         i = controlLayout->indexOf( tempWidg );
654         if( i == -1 )
655         {
656             i = controlLayout->indexOf( tempWidg->parentWidget() );
657             tempWidg = tempWidg->parentWidget();
658         }
659     }
660
661     /* Return the nearest position */
662     if( ( point.x() - tempWidg->x()  > tempWidg->width() / 2 ) && i != -1 )
663         i++;
664
665     //    msg_Dbg( p_intf, "%i", i);
666     return i;
667 }
668
669 void DroppingController::dropEvent( QDropEvent *event )
670 {
671     int i = getParentPosInLayout( event->pos() );
672
673     QByteArray data = event->mimeData()->data( "vlc/button-bar" );
674     QDataStream dataStream(&data, QIODevice::ReadOnly);
675
676     int i_option = 0, i_type = 0;
677     dataStream >> i_type >> i_option;
678
679     createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
680
681     /* Hide by precaution, you don't exactly know what could have happened in
682        between */
683     if( rubberband ) rubberband->hide();
684 }
685
686 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
687 {
688     if( rubberband ) rubberband->hide();
689     event->accept();
690 }
691
692 /**
693  * Overloading doAction to block any action
694  **/
695 void DroppingController::doAction( int i )
696 {
697     VLC_UNUSED( i );
698 }
699
700 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
701 {
702     switch( event->type() )
703     {
704         case QEvent::MouseButtonPress:
705             b_draging = true;
706             return true;
707         case QEvent::MouseButtonRelease:
708             b_draging = false;
709             return true;
710         case QEvent::MouseMove:
711             {
712             if( !b_draging ) return true;
713             QWidget *widg = static_cast<QWidget*>(obj);
714
715             QByteArray itemData;
716             QDataStream dataStream( &itemData, QIODevice::WriteOnly );
717
718             int i = -1;
719             i = controlLayout->indexOf( widg );
720             if( i == -1 )
721             {
722                 i = controlLayout->indexOf( widg->parentWidget() );
723                 widg = widg->parentWidget();
724                 /* NOTE: be extra-careful Now with widg access */
725             }
726
727             if( i == -1 ) return true;
728             doubleInt *dI = widgetList.at( i );
729
730             int i_type = dI->i_type;
731             int i_option = dI->i_option;
732             dataStream << i_type << i_option;
733
734             /* With correct mimedata */
735             QMimeData *mimeData = new QMimeData;
736             mimeData->setData( "vlc/button-bar", itemData );
737
738             QDrag *drag = new QDrag( widg );
739             drag->setMimeData( mimeData );
740
741             /* Start the effective drag */
742             drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
743
744             widgetList.removeAt( i );
745             controlLayout->removeWidget( widg );
746             widg->hide();
747             b_draging = false;
748             }
749             return true;
750
751         case QEvent::MouseButtonDblClick:
752         case QEvent::EnabledChange:
753         case QEvent::Hide:
754         case QEvent::HideToParent:
755         case QEvent::Move:
756         case QEvent::ZOrderChange:
757             return true;
758         default:
759             return false;
760     }
761 }
762