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