]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/toolbar.cpp
Qt: Teletext handling simplifications and change of the icons to more clear ones.
[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 TIME_LABEL:
252             {
253                 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
254                 widget = timeLabel;
255             }
256             widgetItem->setText( qtr("Time") );
257             break;
258         case MENU_BUTTONS:
259             {
260                 QWidget *discFrame = new QWidget( this );
261                 //discFrame->setLineWidth( 1 );
262                 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
263                 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
264
265                 QToolButton *prevSectionButton = new QToolButton( discFrame );
266                 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
267                 discLayout->addWidget( prevSectionButton );
268
269                 QToolButton *menuButton = new QToolButton( discFrame );
270                 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
271                 discLayout->addWidget( menuButton );
272
273                 QToolButton *nextButton = new QToolButton( discFrame );
274                 nextButton->setIcon( QIcon( ":/dvd_next" ) );
275                 discLayout->addWidget( nextButton );
276
277                 widget = discFrame;
278             }
279             widgetItem->setText( qtr("DVD menus") );
280             break;
281         case TELETEXT_BUTTONS:
282             {
283                 QWidget *telexFrame = new QWidget( this );
284                 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
285                 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
286
287                 QToolButton *telexOn = new QToolButton( telexFrame );
288                 telexOn->setIcon( QIcon( ":/tv" ) );
289                 telexLayout->addWidget( telexOn );
290
291                 QToolButton *telexTransparent = new QToolButton;
292                 telexOn->setIcon( QIcon( ":/tvtelx" ) );
293                 telexLayout->addWidget( telexTransparent );
294
295                 QSpinBox *telexPage = new QSpinBox;
296                 telexLayout->addWidget( telexPage );
297
298                 widget = telexFrame;
299             }
300             widgetItem->setText( qtr("Teletext") );
301             break;
302         case ADVANCED_CONTROLLER:
303             {
304                 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
305                 widget = advControls;
306             }
307             widgetItem->setText( qtr("Advanced Buttons") );
308             break;
309         default:
310             msg_Warn( p_intf, "This should not happen %i", i );
311             break;
312         }
313
314         if( widget == NULL ) continue;
315
316
317         widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
318         widget->hide();
319         widgetItem->setData( Qt::UserRole, QVariant( i ) );
320
321         addItem( widgetItem );
322         delete widget;
323     }
324 }
325
326 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
327 {
328     QListWidgetItem *item = currentItem();
329
330     QByteArray itemData;
331     QDataStream dataStream( &itemData, QIODevice::WriteOnly );
332
333     int i_type = item->data( Qt::UserRole ).toInt();
334     int i_option = parent->getOptions();
335     dataStream << i_type << i_option;
336
337     /* Create a new dragging event */
338     QDrag *drag = new QDrag( this );
339
340     /* With correct mimedata */
341     QMimeData *mimeData = new QMimeData;
342     mimeData->setData( "vlc/button-bar", itemData );
343     drag->setMimeData( mimeData );
344
345     /* And correct pixmap */
346     QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
347     drag->setPixmap( aPixmap );
348     drag->setHotSpot( QPoint( 20, 20 ) );
349
350     /* We want to keep a copy */
351     drag->exec( Qt::CopyAction | Qt::MoveAction );
352 }
353
354 /*
355  * The special controller with drag'n drop abilities.
356  * We don't do this in the main controller, since we don't want the OverHead
357  * to propagate there too
358  */
359 DroppingController::DroppingController( intf_thread_t *_p_intf,
360                                         QString line,
361                                         QWidget *_parent )
362                    : AbstractController( _p_intf, _parent )
363 {
364     rubberband = NULL;
365     b_draging = false;
366     setAcceptDrops( true );
367     controlLayout = new QHBoxLayout( this );
368     controlLayout->setSpacing( 5 );
369     controlLayout->setMargin( 0 );
370     setFrameShape( QFrame::StyledPanel );
371     setFrameShadow( QFrame::Raised );
372
373     parseAndCreate( line, controlLayout );
374 }
375
376 /* Overloading the AbstractController one, because we don't manage the
377    Spacing items in the same ways */
378 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
379                                              int i_index,
380                                              buttonType_e i_type,
381                                              int i_option )
382 {
383     /* Special case for SPACERS, who aren't QWidgets */
384     if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
385     {
386         QLabel *label = new QLabel;
387         label->setPixmap( QPixmap( ":/space" ) );
388         if( i_type == WIDGET_SPACER_EXTEND )
389         {
390             label->setSizePolicy( QSizePolicy::MinimumExpanding,
391                     QSizePolicy::Preferred );
392
393             /* Create a box around it */
394             label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
395             label->setLineWidth ( 1 );
396             label->setAlignment( Qt::AlignCenter );
397         }
398         else
399             label->setSizePolicy( QSizePolicy::Fixed,
400                     QSizePolicy::Preferred );
401
402         /* Install event Filter for drag'n drop */
403         label->installEventFilter( this );
404         controlLayout->insertWidget( i_index, label );
405     }
406
407     /* Normal Widgets */
408     else
409     {
410         QWidget *widg = createWidget( i_type, i_option );
411         if( !widg ) return;
412
413         /* Install the Event Filter in order to catch the drag */
414         widg->installEventFilter( this );
415
416         /* We are in a complex widget, we need to stop events on children too */
417         if( i_type >= VOLUME && i_type < SPECIAL_MAX )
418         {
419             QList<QObject *>children = widg->children();
420
421             QObject *child;
422             foreach( child, children )
423             {
424                 QWidget *childWidg;
425                 if( childWidg = qobject_cast<QWidget *>( child ) )
426                 {
427                     child->installEventFilter( this );
428                     childWidg->setEnabled( true );
429                 }
430             }
431
432             /* Decorating the frames when possible */
433             QFrame *frame;
434             if( i_type >= MENU_BUTTONS  /* Don't bother to check for volume */
435                 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
436             {
437                 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
438                 frame->setLineWidth ( 1 );
439             }
440         }
441
442         /* Some Widgets are deactivated at creation */
443         widg->setEnabled( true );
444         widg->show();
445         controlLayout->insertWidget( i_index, widg );
446     }
447
448     /* QList and QBoxLayout don't act the same with insert() */
449     if( i_index < 0 ) i_index = controlLayout->count() - 1;
450
451     /* Insert in the value listing */
452     doubleInt *value = new doubleInt;
453     value->i_type = i_type;
454     value->i_option = i_option;
455     widgetList.insert( i_index, value );
456 }
457
458 DroppingController::~DroppingController()
459 {
460     qDeleteAll( widgetList );
461     widgetList.clear();
462 }
463
464 QString DroppingController::getValue()
465 {
466     QString qs = "";
467
468     for( int i = 0; i < controlLayout->count(); i++ )
469     {
470         doubleInt *dI = widgetList.at( i );
471         assert( dI );
472
473         qs.append( QString::number( dI->i_type ) );
474         if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
475         qs.append( ';' );
476     }
477     return qs;
478 }
479
480 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
481 {
482     if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
483         event->accept();
484     else
485         event->ignore();
486 }
487
488 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
489 {
490     QPoint origin = event->pos();
491
492     int i_pos = getParentPosInLayout( origin );
493     bool b_end = false;
494
495     /* Both sides of the frame */
496     if( i_pos == -1 )
497     {
498         if( rubberband ) rubberband->hide();
499         return;
500     }
501
502     /* Last item is special because of underlying items */
503     if( i_pos >= controlLayout->count() )
504     {
505         i_pos--;
506         b_end = true;
507     }
508
509     /* Query the underlying item for size && middles */
510     QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
511     QWidget *temp = tempItem->widget(); assert( temp );
512
513     /* Position assignment */
514     origin.ry() = 0;
515     origin.rx() = temp->x() - 2;
516
517     if( b_end ) origin.rx() += temp->width();
518
519     if( !rubberband )
520         rubberband = new QRubberBand( QRubberBand::Line, this );
521     rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
522     rubberband->show();
523 }
524
525 inline int DroppingController::getParentPosInLayout( QPoint point )
526 {
527     point.ry() = height() / 2 ;
528     QPoint origin = mapToGlobal ( point );
529
530     QWidget *tempWidg = QApplication::widgetAt( origin );
531
532     int i = -1;
533     if( tempWidg != NULL)
534     {
535         i = controlLayout->indexOf( tempWidg );
536         if( i == -1 )
537         {
538             i = controlLayout->indexOf( tempWidg->parentWidget() );
539             tempWidg = tempWidg->parentWidget();
540         }
541     }
542
543     /* Return the nearest position */
544     if( ( point.x() - tempWidg->x()  > tempWidg->width() / 2 ) && i != -1 )
545         i++;
546
547     //    msg_Dbg( p_intf, "%i", i);
548     return i;
549 }
550
551 void DroppingController::dropEvent( QDropEvent *event )
552 {
553     int i = getParentPosInLayout( event->pos() );
554
555     QByteArray data = event->mimeData()->data( "vlc/button-bar" );
556     QDataStream dataStream(&data, QIODevice::ReadOnly);
557
558     int i_option = 0, i_type = 0;
559     dataStream >> i_type >> i_option;
560
561     createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
562
563     /* Hide by precaution, you don't exactly know what could have happened in
564        between */
565     if( rubberband ) rubberband->hide();
566 }
567
568 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
569 {
570     if( rubberband ) rubberband->hide();
571     event->accept();
572 }
573
574 /**
575  * Overloading doAction to block any action
576  **/
577 void DroppingController::doAction( int i )
578 {
579     VLC_UNUSED( i );
580 }
581
582 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
583 {
584     switch( event->type() )
585     {
586         case QEvent::MouseButtonPress:
587             b_draging = true;
588             return true;
589         case QEvent::MouseButtonRelease:
590             b_draging = false;
591             return true;
592         case QEvent::MouseMove:
593             {
594             if( !b_draging ) return true;
595             QWidget *widg = static_cast<QWidget*>(obj);
596
597             QByteArray itemData;
598             QDataStream dataStream( &itemData, QIODevice::WriteOnly );
599
600             int i = -1;
601             i = controlLayout->indexOf( widg );
602             if( i == -1 )
603             {
604                 i = controlLayout->indexOf( widg->parentWidget() );
605                 widg = widg->parentWidget();
606                 /* NOTE: be extra-careful Now with widg access */
607             }
608
609             if( i == -1 ) return true;
610             doubleInt *dI = widgetList.at( i );
611
612             int i_type = dI->i_type;
613             int i_option = dI->i_option;
614             dataStream << i_type << i_option;
615
616             /* With correct mimedata */
617             QMimeData *mimeData = new QMimeData;
618             mimeData->setData( "vlc/button-bar", itemData );
619
620             QDrag *drag = new QDrag( widg );
621             drag->setMimeData( mimeData );
622
623             /* Start the effective drag */
624             drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
625
626             widgetList.removeAt( i );
627             controlLayout->removeWidget( widg );
628             widg->hide();
629             b_draging = false;
630             }
631             return true;
632
633         case QEvent::MouseButtonDblClick:
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