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