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