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