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