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