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