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