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