]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/toolbar.cpp
Unused variable
[vlc] / modules / gui / qt4 / dialogs / toolbar.cpp
1 /*****************************************************************************
2  * ToolbarEdit.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 #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, 2, 1 );
68     mainLayout->addWidget( widgetBox, 0, 0, 5, 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, 0, 1, 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, 1, 1, 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, 2, 1, 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( ":/toolbar/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, 3, 1, 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
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( ":/toolbar/space" ),
294             qtr( "Spacer" ), this );
295     widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
296     addItem( widgetItem );
297
298     widgetItem = new QListWidgetItem( QIcon( ":/toolbar/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( ":/toolbar/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( ":/toolbar/dvd_prev" ) );
370                 discLayout->addWidget( prevSectionButton );
371
372                 QToolButton *menuButton = new QToolButton( discFrame );
373                 menuButton->setIcon( QIcon( ":/toolbar/dvd_menu" ) );
374                 discLayout->addWidget( menuButton );
375
376                 QToolButton *nextButton = new QToolButton( discFrame );
377                 nextButton->setIcon( QIcon( ":/toolbar/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( ":/toolbar/tv" ) );
392                 telexLayout->addWidget( telexOn );
393
394                 QToolButton *telexTransparent = new QToolButton;
395                 telexOn->setIcon( QIcon( ":/toolbar/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     while( (child = controlLayout->takeAt( 0 ) ) != 0 )
484     {
485         child->widget()->hide();
486         delete child;
487     }
488
489     parseAndCreate( line, controlLayout );
490     show();
491 }
492
493 /* Overloading the AbstractController one, because we don't manage the
494    Spacing items in the same ways */
495 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
496                                              int i_index,
497                                              buttonType_e i_type,
498                                              int i_option )
499 {
500     doubleInt *value = new doubleInt;
501     value->i_type = i_type;
502     value->i_option = i_option;
503
504     /* Special case for SPACERS, who aren't QWidgets */
505     if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
506     {
507         QLabel *label = new QLabel( this );
508         label->setPixmap( QPixmap( ":/toolbar/space" ) );
509         if( i_type == WIDGET_SPACER_EXTEND )
510         {
511             label->setSizePolicy( QSizePolicy::MinimumExpanding,
512                     QSizePolicy::Preferred );
513
514             /* Create a box around it */
515             label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
516             label->setLineWidth ( 1 );
517             label->setAlignment( Qt::AlignCenter );
518         }
519         else
520             label->setSizePolicy( QSizePolicy::Fixed,
521                     QSizePolicy::Preferred );
522
523         /* Install event Filter for drag'n drop */
524         label->installEventFilter( this );
525         controlLayout->insertWidget( i_index, label );
526     }
527
528     /* Normal Widgets */
529     else
530     {
531         QWidget *widg = createWidget( i_type, i_option );
532         if( !widg ) return;
533
534         /* Install the Event Filter in order to catch the drag */
535         widg->setParent( this );
536         widg->installEventFilter( this );
537
538         /* We are in a complex widget, we need to stop events on children too */
539         if( i_type >= VOLUME && i_type < SPECIAL_MAX )
540         {
541             QList<QObject *>children = widg->children();
542
543             QObject *child;
544             foreach( child, children )
545             {
546                 QWidget *childWidg;
547                 if( childWidg = qobject_cast<QWidget *>( child ) )
548                 {
549                     child->installEventFilter( this );
550                     childWidg->setEnabled( true );
551                 }
552             }
553
554             /* Decorating the frames when possible */
555             QFrame *frame;
556             if( i_type >= MENU_BUTTONS  /* Don't bother to check for volume */
557                 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
558             {
559                 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
560                 frame->setLineWidth ( 1 );
561             }
562         }
563
564         /* Some Widgets are deactivated at creation */
565         widg->setEnabled( true );
566         widg->show();
567         controlLayout->insertWidget( i_index, widg );
568     }
569
570     /* QList and QBoxLayout don't act the same with insert() */
571     if( i_index < 0 ) i_index = controlLayout->count() - 1;
572
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     if( tempWidg == NULL )
650         return -1;
651
652     int i = controlLayout->indexOf( tempWidg );
653     if( i == -1 )
654     {
655         i = controlLayout->indexOf( tempWidg->parentWidget() );
656         tempWidg = tempWidg->parentWidget();
657     }
658
659     /* Return the nearest position */
660     if( ( point.x() - tempWidg->x()  > tempWidg->width() / 2 ) && i != -1 )
661         i++;
662
663     //    msg_Dbg( p_intf, "%i", i);
664     return i;
665 }
666
667 void DroppingController::dropEvent( QDropEvent *event )
668 {
669     int i = getParentPosInLayout( event->pos() );
670
671     QByteArray data = event->mimeData()->data( "vlc/button-bar" );
672     QDataStream dataStream(&data, QIODevice::ReadOnly);
673
674     int i_option = 0, i_type = 0;
675     dataStream >> i_type >> i_option;
676
677     createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
678
679     /* Hide by precaution, you don't exactly know what could have happened in
680        between */
681     if( rubberband ) rubberband->hide();
682 }
683
684 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
685 {
686     if( rubberband ) rubberband->hide();
687     event->accept();
688 }
689
690 /**
691  * Overloading doAction to block any action
692  **/
693 void DroppingController::doAction( int i )
694 {
695     VLC_UNUSED( i );
696 }
697
698 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
699 {
700     switch( event->type() )
701     {
702         case QEvent::MouseButtonPress:
703             b_draging = true;
704             return true;
705         case QEvent::MouseButtonRelease:
706             b_draging = false;
707             return true;
708         case QEvent::MouseMove:
709             {
710             if( !b_draging ) return true;
711             QWidget *widg = static_cast<QWidget*>(obj);
712
713             QByteArray itemData;
714             QDataStream dataStream( &itemData, QIODevice::WriteOnly );
715
716             int i = -1;
717             i = controlLayout->indexOf( widg );
718             if( i == -1 )
719             {
720                 i = controlLayout->indexOf( widg->parentWidget() );
721                 widg = widg->parentWidget();
722                 /* NOTE: be extra-careful Now with widg access */
723             }
724
725             if( i == -1 ) return true;
726             doubleInt *dI = widgetList.at( i );
727
728             int i_type = dI->i_type;
729             int i_option = dI->i_option;
730             dataStream << i_type << i_option;
731
732             /* With correct mimedata */
733             QMimeData *mimeData = new QMimeData;
734             mimeData->setData( "vlc/button-bar", itemData );
735
736             QDrag *drag = new QDrag( widg );
737             drag->setMimeData( mimeData );
738
739             /* Remove before the drag to not mess DropEvent,
740                that will createAndAddWidget */
741             widgetList.removeAt( i );
742
743             /* Start the effective drag */
744             drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
745             widg->hide();
746             controlLayout->removeWidget( widg );
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