]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/toolbar.cpp
[Qt] Save the values saved during Toolbar edition.
[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/Controls1",
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/Controls2",
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/AdvControl",
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/InputControl",
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/FSCline",
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/Controls1", controller1->getValue() );
155     getSettings()->setValue( "MainWindow/Controls2", controller2->getValue() );
156     getSettings()->setValue( "MainWindow/AdcControl", controllerA->getValue() );
157     getSettings()->setValue( "MainWindow/InputControl", controller->getValue() );
158     getSettings()->setValue( "MainWindow/FSCline", 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 //    drag->setHotSpot(QPoint(pixmap.width()/2, pixmap.height()/2));
333
334     drag->exec(Qt::CopyAction | Qt::MoveAction );
335 }
336
337
338 DroppingController::DroppingController( intf_thread_t *_p_intf, QString line, QWidget *_parent )
339                    : AbstractController( _p_intf, _parent )
340 {
341     rubberband = NULL;
342     setAcceptDrops( true );
343     controlLayout = new QHBoxLayout( this );
344     controlLayout->setSpacing( 0 );
345     controlLayout->setMargin( 0 );
346     setFrameShape( QFrame::StyledPanel );
347     setFrameShadow( QFrame::Raised );
348
349
350     parseAndCreate( line, controlLayout );
351
352 }
353
354 /* Overloading the AbstractController one, because we don't manage the
355    Spacing in the same ways */
356 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
357                                              int i_index,
358                                              buttonType_e i_type,
359                                              int i_option )
360 {
361     doubleInt *value = new doubleInt;
362     value->i_type = i_type;
363     value->i_option = i_option;
364
365     /* Special case for SPACERS, who aren't QWidgets */
366     if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
367     {
368         QLabel *label = new QLabel;
369         label->setPixmap( QPixmap( ":/space" ) );
370         if( i_type == WIDGET_SPACER_EXTEND )
371         {
372             label->setScaledContents( true );
373             label->setSizePolicy( QSizePolicy::MinimumExpanding,
374                     QSizePolicy::Preferred );
375         }
376         else
377             label->setSizePolicy( QSizePolicy::Fixed,
378                     QSizePolicy::Preferred );
379
380         controlLayout->insertWidget( i_index, label );
381     }
382     else
383     {
384         QWidget *widg = createWidget( i_type, i_option );
385         if( !widg ) return;
386
387         /* Some Widgets are deactivated at creation */
388         widg->setEnabled( true );
389         widg->show();
390         controlLayout->insertWidget( i_index, widg );
391     }
392     /* QList and QBoxLayout don't act the same with insert() */
393     if( i_index < 0 ) i_index = controlLayout->count() -1 ;
394
395     widgetList.insert( i_index, value );
396 }
397
398 QString DroppingController::getValue()
399 {
400     QString qs = "";
401
402     for( int i = 0; i < controlLayout->count(); i++ )
403     {
404         doubleInt *dI = widgetList.at( i );
405         assert( dI );
406
407         qs.append( QString::number( dI->i_type ) );
408         if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
409         qs.append( ';' );
410     }
411     return qs;
412 }
413
414 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
415 {
416     if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
417         event->accept();
418     else
419         event->ignore();
420 }
421
422 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
423 {
424     QPoint origin = event->pos();
425
426     int i_pos = getParentPosInLayout( origin );
427     bool b_end = false;
428
429     /* Both sides of the frame */
430     if( i_pos == -1 )
431     {
432         if( rubberband ) rubberband->hide();
433         return;
434     }
435
436     /* Last item is special because of underlying items */
437     if( i_pos >= controlLayout->count() )
438     {
439         i_pos--;
440         b_end = true;
441     }
442
443     /* Query the underlying item for size && middles */
444     QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
445     QWidget *temp = tempItem->widget(); assert( temp );
446
447     /* Position assignment */
448     origin.ry() = 0;
449     origin.rx() = temp->x() - 2;
450
451     if( b_end ) origin.rx() += temp->width();
452
453     if( !rubberband )
454         rubberband = new QRubberBand( QRubberBand::Line, this );
455     rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
456     rubberband->show();
457 }
458
459 inline int DroppingController::getParentPosInLayout( QPoint point)
460 {
461     point.ry() = height() / 2 ;
462     QPoint origin = mapToGlobal ( point );
463
464     QWidget *tempWidg = QApplication::widgetAt( origin );
465
466     int i = -1;
467     if( tempWidg != NULL)
468     {
469         i = controlLayout->indexOf( tempWidg );
470         if( i == -1 )
471         {
472             i = controlLayout->indexOf( tempWidg->parentWidget() );
473             tempWidg = tempWidg->parentWidget();
474         }
475     }
476
477     /* Return the nearest position */
478     if( ( point.x() - tempWidg->x()  > tempWidg->width() / 2 ) && i != -1 )
479         i++;
480
481     //    msg_Dbg( p_intf, "%i", i);
482     return i;
483 }
484
485 void DroppingController::dropEvent( QDropEvent *event )
486 {
487     int i = getParentPosInLayout( event->pos() );
488
489     QByteArray data = event->mimeData()->data( "vlc/button-bar" );
490     QDataStream dataStream(&data, QIODevice::ReadOnly);
491
492     int i_option = 0, i_type = 0;
493     dataStream >> i_type >> i_option;
494
495     createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
496
497     /* Hide by precaution, you don't exactly know what could have happened in
498        between */
499     if( rubberband ) rubberband->hide();
500 }
501
502 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
503 {
504     if( rubberband ) rubberband->hide();
505 }
506
507 /**
508  * Overloading doAction to block any action
509  **/
510 void DroppingController::doAction( int i ){}
511