]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/toolbar.cpp
Qt: kill many warnings.
[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",
84                         "64;36;37;38;65").toString();
85     controller1 = new DroppingController( p_intf, line1,
86             this );
87     mainTboxLayout->addWidget( line1Label, 1, 0, 1, 1 );
88     mainTboxLayout->addWidget( controller1, 1, 1, 1, 2 );
89
90     QLabel *line2Label = new QLabel( "Line 2:" );
91     QString line2 = getSettings()->value( "MainWindow/MainToolbar2",
92                         "0-2;64;3;1;4;64;7;10;9;65;34-4;" ).toString();
93     controller2 = new DroppingController( p_intf, line2,
94             this );
95     mainTboxLayout->addWidget( line2Label, 2, 0, 1, 1 );
96     mainTboxLayout->addWidget( controller2, 2, 1, 1, 2);
97
98     /* Advanced ToolBar */
99     QLabel *advLabel = new QLabel( qtr( "Advanced Widget toolbar:" ) );
100     QString lineA = getSettings()->value( "MainWindow/AdvToolbar",
101                         "12;11;13;14").toString();
102     controllerA = new DroppingController( p_intf, lineA,
103             this );
104     mainTboxLayout->addWidget( advLabel, 3, 0, 1, 2 );
105     mainTboxLayout->addWidget( controllerA, 3, 2, 1, 1 );
106
107     mainLayout->addWidget( mainToolbarBox, 1, 0, 1, -1 );
108
109     /* TimeToolBar */
110     QGroupBox *timeToolbarBox = new QGroupBox( qtr( "Time Toolbar" ) , this );
111     QGridLayout *timeTboxLayout = new QGridLayout( timeToolbarBox );
112
113     QString line = getSettings()->value( "MainWindow/InputToolbar",
114                         "5-1;33;6-1").toString();
115     controller = new DroppingController( p_intf, line,
116             this );
117     timeTboxLayout->addWidget( controller, 0, 0, 1, -1 );
118
119     mainLayout->addWidget( timeToolbarBox, 2, 0, 1, -1 );
120
121     /* FSCToolBar */
122     QGroupBox *FSCToolbarBox = new QGroupBox( qtr( "Fullscreen Controller" ),
123                                               this );
124     QGridLayout *FSCTboxLayout = new QGridLayout( FSCToolbarBox );
125
126     QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar",
127                        "0-2;64;3;1;4;64;36;64;37;64;8;65;35-4;34" ).toString();
128     controllerFSC = new DroppingController( p_intf,
129             lineFSC, this );
130     FSCTboxLayout->addWidget( controllerFSC, 0, 0, 1, -1 );
131
132     mainLayout->addWidget( FSCToolbarBox, 3, 0, 1, -1 );
133
134     /* Buttons */
135     QDialogButtonBox *okCancel = new QDialogButtonBox;
136     QPushButton *okButton = new QPushButton( qtr( "Cl&ose" ), this );
137     QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ), this );
138     okCancel->addButton( okButton, QDialogButtonBox::AcceptRole );
139     okCancel->addButton( cancelButton, QDialogButtonBox::RejectRole );
140
141     BUTTONACT( okButton, close() );
142     BUTTONACT( cancelButton, cancel() );
143     mainLayout->addWidget( okCancel, 4, 2 );
144 }
145
146
147 ToolbarEditDialog::~ToolbarEditDialog()
148 {
149 }
150
151 void ToolbarEditDialog::close()
152 {
153     msg_Dbg( p_intf, "Close and save" );
154     hide();
155     getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
156     getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
157     getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
158     getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
159     getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
160 }
161
162 void ToolbarEditDialog::cancel()
163 {
164     hide();
165 }
166
167 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
168               : QListWidget( _parent )
169 {
170     /* We need the parent to know the options checked */
171     parent = qobject_cast<ToolbarEditDialog *>(_parent);
172     assert( parent );
173
174     /* Normal options */
175     setViewMode( QListView::IconMode );
176     setSpacing( 20 );
177     setDragEnabled( true );
178     setMinimumHeight( 250 );
179
180     /* All the buttons do not need a special rendering */
181     for( int i = 0; i < BUTTON_MAX; i++ )
182     {
183         QListWidgetItem *widgetItem = new QListWidgetItem( this );
184         widgetItem->setText( nameL[i] );
185         widgetItem->setIcon( QIcon( iconL[i] ) );
186         widgetItem->setData( Qt::UserRole, QVariant( i ) );
187         addItem( widgetItem );
188     }
189
190     /* Spacers are yet again a different thing */
191     QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
192             qtr( "Spacer" ), this );
193     widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
194     addItem( widgetItem );
195
196     widgetItem = new QListWidgetItem( QIcon( ":/space" ),
197             qtr( "Expanding Spacer" ), this );
198     widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
199     addItem( widgetItem );
200
201     /**
202      * For all other widgets, we create then, do a pseudo rendering in
203      * a pixmaps for the view, and delete the object
204      *
205      * A lot of code is retaken from the Abstract, but not exactly...
206      * So, rewrite.
207      * They are better ways to deal with this, but I doubt that this is
208      * necessary. If you feel like you have the time, be my guest.
209      * --
210      * jb
211      **/
212     for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
213     {
214         QWidget *widget = NULL;
215         QListWidgetItem *widgetItem = new QListWidgetItem( this );
216         switch( i )
217         {
218         case SPLITTER:
219             {
220                 QFrame *line = new QFrame( this );
221                 line->setFrameShape( QFrame::VLine );
222                 line->setFrameShadow( QFrame::Raised );
223                 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
224                 widget = line;
225             }
226             widgetItem->setText( qtr("Splitter") );
227             break;
228         case INPUT_SLIDER:
229             {
230                 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
231                 widget = slider;
232             }
233             widgetItem->setText( qtr("Time Slider") );
234             break;
235         case VOLUME:
236             {
237                 SoundWidget *snd = new SoundWidget( this, p_intf,
238                         parent->getOptions() & WIDGET_SHINY );
239                 widget = snd;
240             }
241             widgetItem->setText( qtr("Volume") );
242             break;
243         case TIME_LABEL:
244             {
245                 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
246                 widget = timeLabel;
247             }
248             widgetItem->setText( qtr("Time") );
249             break;
250         case MENU_BUTTONS:
251             {
252                 QWidget *discFrame = new QWidget( this );
253                 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
254                 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
255
256                 QToolButton *prevSectionButton = new QToolButton( discFrame );
257                 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
258                 discLayout->addWidget( prevSectionButton );
259
260                 QToolButton *menuButton = new QToolButton( discFrame );
261                 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
262                 discLayout->addWidget( menuButton );
263
264                 QToolButton *nextButton = new QToolButton( discFrame );
265                 nextButton->setIcon( QIcon( ":/dvd_next" ) );
266                 discLayout->addWidget( nextButton );
267
268                 widget = discFrame;
269             }
270             widgetItem->setText( qtr("DVD menus") );
271             break;
272         case TELETEXT_BUTTONS:
273             {
274                 QWidget *telexFrame = new QWidget( this );
275                 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
276                 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
277
278                 QToolButton *telexOn = new QToolButton( telexFrame );
279                 telexOn->setIcon( QIcon( ":/tv" ) );
280                 telexLayout->addWidget( telexOn );
281
282                 QToolButton *telexTransparent = new QToolButton;
283                 telexOn->setIcon( QIcon( ":/tvtelx-trans" ) );
284                 telexLayout->addWidget( telexTransparent );
285
286                 QSpinBox *telexPage = new QSpinBox;
287                 telexLayout->addWidget( telexPage );
288
289                 widget = telexFrame;
290             }
291             widgetItem->setText( qtr("Teletext") );
292             break;
293         case ADVANCED_CONTROLLER:
294             {
295                 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
296                 widget = advControls;
297             }
298             widgetItem->setText( qtr("Advanced Buttons") );
299             break;
300         default:
301             msg_Warn( p_intf, "This should not happen %i", i );
302             break;
303         }
304
305         if( widget == NULL ) continue;
306
307
308         widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
309         widget->hide();
310         widgetItem->setData( Qt::UserRole, QVariant( i ) );
311
312         addItem( widgetItem );
313         delete widget;
314     }
315 }
316
317 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
318 {
319     QListWidgetItem *item =currentItem();
320
321     QByteArray itemData;
322     QDataStream dataStream( &itemData, QIODevice::WriteOnly );
323
324     int i_type = item->data( Qt::UserRole ).toInt();
325     int i_option = parent->getOptions();
326     dataStream << i_type << i_option;
327
328     QMimeData *mimeData = new QMimeData;
329     mimeData->setData( "vlc/button-bar", itemData );
330
331     QDrag *drag = new QDrag( this );
332     drag->setMimeData( mimeData );
333     QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
334     drag->setPixmap( aPixmap );
335     drag->setHotSpot( QPoint( 20, 20 ) );
336     drag->exec(Qt::CopyAction | Qt::MoveAction );
337 }
338
339
340 DroppingController::DroppingController( intf_thread_t *_p_intf, QString line, QWidget *_parent )
341                    : AbstractController( _p_intf, _parent )
342 {
343     rubberband = NULL;
344     setAcceptDrops( true );
345     controlLayout = new QHBoxLayout( this );
346     controlLayout->setSpacing( 0 );
347     controlLayout->setMargin( 0 );
348     setFrameShape( QFrame::StyledPanel );
349     setFrameShadow( QFrame::Raised );
350
351
352     parseAndCreate( line, controlLayout );
353
354 }
355
356 /* Overloading the AbstractController one, because we don't manage the
357    Spacing in the same ways */
358 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
359                                              int i_index,
360                                              buttonType_e i_type,
361                                              int i_option )
362 {
363     doubleInt *value = new doubleInt;
364     value->i_type = i_type;
365     value->i_option = i_option;
366
367     /* Special case for SPACERS, who aren't QWidgets */
368     if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
369     {
370         QLabel *label = new QLabel;
371         label->setPixmap( QPixmap( ":/space" ) );
372         if( i_type == WIDGET_SPACER_EXTEND )
373         {
374             label->setScaledContents( true );
375             label->setSizePolicy( QSizePolicy::MinimumExpanding,
376                     QSizePolicy::Preferred );
377         }
378         else
379             label->setSizePolicy( QSizePolicy::Fixed,
380                     QSizePolicy::Preferred );
381
382         controlLayout->insertWidget( i_index, label );
383     }
384     else
385     {
386         QWidget *widg = createWidget( i_type, i_option );
387         if( !widg ) return;
388
389         /* Some Widgets are deactivated at creation */
390         widg->setEnabled( true );
391         widg->show();
392         controlLayout->insertWidget( i_index, widg );
393     }
394     /* QList and QBoxLayout don't act the same with insert() */
395     if( i_index < 0 ) i_index = controlLayout->count() -1 ;
396
397     widgetList.insert( i_index, value );
398 }
399
400 QString DroppingController::getValue()
401 {
402     QString qs = "";
403
404     for( int i = 0; i < controlLayout->count(); i++ )
405     {
406         doubleInt *dI = widgetList.at( i );
407         assert( dI );
408
409         qs.append( QString::number( dI->i_type ) );
410         if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
411         qs.append( ';' );
412     }
413     return qs;
414 }
415
416 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
417 {
418     if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
419         event->accept();
420     else
421         event->ignore();
422 }
423
424 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
425 {
426     QPoint origin = event->pos();
427
428     int i_pos = getParentPosInLayout( origin );
429     bool b_end = false;
430
431     /* Both sides of the frame */
432     if( i_pos == -1 )
433     {
434         if( rubberband ) rubberband->hide();
435         return;
436     }
437
438     /* Last item is special because of underlying items */
439     if( i_pos >= controlLayout->count() )
440     {
441         i_pos--;
442         b_end = true;
443     }
444
445     /* Query the underlying item for size && middles */
446     QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
447     QWidget *temp = tempItem->widget(); assert( temp );
448
449     /* Position assignment */
450     origin.ry() = 0;
451     origin.rx() = temp->x() - 2;
452
453     if( b_end ) origin.rx() += temp->width();
454
455     if( !rubberband )
456         rubberband = new QRubberBand( QRubberBand::Line, this );
457     rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
458     rubberband->show();
459 }
460
461 inline int DroppingController::getParentPosInLayout( QPoint point)
462 {
463     point.ry() = height() / 2 ;
464     QPoint origin = mapToGlobal ( point );
465
466     QWidget *tempWidg = QApplication::widgetAt( origin );
467
468     int i = -1;
469     if( tempWidg != NULL)
470     {
471         i = controlLayout->indexOf( tempWidg );
472         if( i == -1 )
473         {
474             i = controlLayout->indexOf( tempWidg->parentWidget() );
475             tempWidg = tempWidg->parentWidget();
476         }
477     }
478
479     /* Return the nearest position */
480     if( ( point.x() - tempWidg->x()  > tempWidg->width() / 2 ) && i != -1 )
481         i++;
482
483     //    msg_Dbg( p_intf, "%i", i);
484     return i;
485 }
486
487 void DroppingController::dropEvent( QDropEvent *event )
488 {
489     int i = getParentPosInLayout( event->pos() );
490
491     QByteArray data = event->mimeData()->data( "vlc/button-bar" );
492     QDataStream dataStream(&data, QIODevice::ReadOnly);
493
494     int i_option = 0, i_type = 0;
495     dataStream >> i_type >> i_option;
496
497     createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
498
499     /* Hide by precaution, you don't exactly know what could have happened in
500        between */
501     if( rubberband ) rubberband->hide();
502 }
503
504 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
505 {
506     if( rubberband ) rubberband->hide();
507     event->accept();
508 }
509
510 /**
511  * Overloading doAction to block any action
512  **/
513 void DroppingController::doAction( int i )
514 {
515     VLC_UNUSED( i );
516 }
517