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