1 /*****************************************************************************
2 * ToolbarEdit.cpp : ToolbarEdit and About dialogs
3 ****************************************************************************
4 * Copyright (C) 2008 the VideoLAN team
7 * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
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.
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.
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 *****************************************************************************/
28 #include "dialogs/toolbar.hpp"
30 #include "util/input_slider.hpp"
31 #include "util/customwidgets.hpp"
32 #include "components/interface_widgets.hpp"
34 #include <QScrollArea>
38 #include <QListWidget>
40 #include <QDragEnterEvent>
41 #include <QDialogButtonBox>
43 ToolbarEditDialog *ToolbarEditDialog::instance = NULL;
45 ToolbarEditDialog::ToolbarEditDialog( intf_thread_t *_p_intf)
46 : QVLCFrame( _p_intf )
48 setWindowTitle( qtr( "Toolbars Editor" ) );
49 QGridLayout *mainLayout = new QGridLayout( this );
50 setMinimumWidth( 600 );
53 QGroupBox *widgetBox = new QGroupBox( qtr( "Toolbar Elements") , this );
54 widgetBox->setSizePolicy( QSizePolicy::Preferred,
55 QSizePolicy::MinimumExpanding );
56 QGridLayout *boxLayout = new QGridLayout( widgetBox );
58 QLabel *styleLabel = new QLabel( qtr( "Next widget style:" ) );
59 flatBox = new QCheckBox( qtr( "Flat Button" ) );
60 bigBox = new QCheckBox( qtr( "Big Button" ) );
61 shinyBox = new QCheckBox( qtr( "Native Slider" ) );
63 boxLayout->addWidget( new WidgetListing( p_intf, this ), 0, 0, 1, -1);
64 boxLayout->addWidget( styleLabel, 1, 0 );
65 boxLayout->addWidget( flatBox, 1, 1 );
66 boxLayout->addWidget( bigBox, 1, 2 );
67 boxLayout->addWidget( shinyBox, 1, 3 );
68 mainLayout->addWidget( widgetBox, 0, 0, 1, -1 );
72 QGroupBox *mainToolbarBox = new QGroupBox( qtr( "Main Toolbar" ), this );
73 QGridLayout *mainTboxLayout = new QGridLayout( mainToolbarBox );
75 QLabel *label = new QLabel( qtr( "Toolbar position:" ) );
76 mainTboxLayout->addWidget(label, 0, 0, 1, 2);
78 positionCombo = new QComboBox;
79 positionCombo->addItem( qtr( "Under the Video" ), QVariant( 0 ) );
80 positionCombo->addItem( qtr( "Above the Video" ), QVariant( 1 ) );
81 positionCombo->setCurrentIndex( positionCombo->findData(
82 getSettings()->value( "MainWindow/ToolbarPos", 0 ).toInt() ) );
83 mainTboxLayout->addWidget( positionCombo, 0, 2, 1, 1 );
85 QLabel *line1Label = new QLabel( "Line 1:" );
86 QString line1 = getSettings()->value( "MainWindow/MainToolbar1",
87 MAIN_TB1_DEFAULT ).toString();
88 controller1 = new DroppingController( p_intf, line1,
90 mainTboxLayout->addWidget( line1Label, 1, 0, 1, 1 );
91 mainTboxLayout->addWidget( controller1, 1, 1, 1, 2 );
93 QLabel *line2Label = new QLabel( "Line 2:" );
94 QString line2 = getSettings()->value( "MainWindow/MainToolbar2",
95 MAIN_TB2_DEFAULT ).toString();
96 controller2 = new DroppingController( p_intf, line2,
98 mainTboxLayout->addWidget( line2Label, 2, 0, 1, 1 );
99 mainTboxLayout->addWidget( controller2, 2, 1, 1, 2);
101 /* Advanced ToolBar */
102 QLabel *advLabel = new QLabel( qtr( "Advanced Widget toolbar:" ) );
103 QString lineA = getSettings()->value( "MainWindow/AdvToolbar",
104 ADV_TB_DEFAULT ).toString();
105 controllerA = new DroppingController( p_intf, lineA,
107 mainTboxLayout->addWidget( advLabel, 3, 0, 1, 2 );
108 mainTboxLayout->addWidget( controllerA, 3, 2, 1, 1 );
110 mainLayout->addWidget( mainToolbarBox, 1, 0, 1, -1 );
113 QGroupBox *timeToolbarBox = new QGroupBox( qtr( "Time Toolbar" ) , this );
114 QGridLayout *timeTboxLayout = new QGridLayout( timeToolbarBox );
116 QString line = getSettings()->value( "MainWindow/InputToolbar",
117 INPT_TB_DEFAULT ).toString();
118 controller = new DroppingController( p_intf, line,
120 timeTboxLayout->addWidget( controller, 0, 0, 1, -1 );
122 mainLayout->addWidget( timeToolbarBox, 2, 0, 1, -1 );
125 QGroupBox *FSCToolbarBox = new QGroupBox( qtr( "Fullscreen Controller" ),
127 QGridLayout *FSCTboxLayout = new QGridLayout( FSCToolbarBox );
129 QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar",
130 FSC_TB_DEFAULT ).toString();
131 controllerFSC = new DroppingController( p_intf,
133 FSCTboxLayout->addWidget( controllerFSC, 0, 0, 1, -1 );
135 mainLayout->addWidget( FSCToolbarBox, 3, 0, 1, -1 );
138 QDialogButtonBox *okCancel = new QDialogButtonBox;
139 QPushButton *okButton = new QPushButton( qtr( "Cl&ose" ), this );
140 okButton->setDefault( true );
141 QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ), this );
142 okCancel->addButton( okButton, QDialogButtonBox::AcceptRole );
143 okCancel->addButton( cancelButton, QDialogButtonBox::RejectRole );
145 BUTTONACT( okButton, close() );
146 BUTTONACT( cancelButton, cancel() );
147 mainLayout->addWidget( okCancel, 4, 2 );
151 ToolbarEditDialog::~ToolbarEditDialog()
155 void ToolbarEditDialog::close()
157 msg_Dbg( p_intf, "Close and save" );
159 getSettings()->setValue( "MainWindow/ToolbarPos",
160 positionCombo->itemData( positionCombo->currentIndex() ).toInt() );
161 getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
162 getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
163 getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
164 getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
165 getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
168 void ToolbarEditDialog::cancel()
173 /************************************************
175 * Creation of the list of drawed lovely buttons
176 ************************************************/
177 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
178 : QListWidget( _parent )
180 /* We need the parent to know the options checked */
181 parent = qobject_cast<ToolbarEditDialog *>(_parent);
185 setViewMode( QListView::IconMode );
187 setDragEnabled( true );
188 setMinimumHeight( 250 );
190 /* All the buttons do not need a special rendering */
191 for( int i = 0; i < BUTTON_MAX; i++ )
193 QListWidgetItem *widgetItem = new QListWidgetItem( this );
194 widgetItem->setText( nameL[i] );
195 widgetItem->setIcon( QIcon( iconL[i] ) );
196 widgetItem->setData( Qt::UserRole, QVariant( i ) );
197 addItem( widgetItem );
200 /* Spacers are yet again a different thing */
201 QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
202 qtr( "Spacer" ), this );
203 widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
204 addItem( widgetItem );
206 widgetItem = new QListWidgetItem( QIcon( ":/space" ),
207 qtr( "Expanding Spacer" ), this );
208 widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
209 addItem( widgetItem );
212 * For all other widgets, we create then, do a pseudo rendering in
213 * a pixmaps for the view, and delete the object
215 * A lot of code is retaken from the Abstract, but not exactly...
217 * They are better ways to deal with this, but I doubt that this is
218 * necessary. If you feel like you have the time, be my guest.
222 for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
224 QWidget *widget = NULL;
225 QListWidgetItem *widgetItem = new QListWidgetItem( this );
230 QFrame *line = new QFrame( this );
231 line->setFrameShape( QFrame::VLine );
232 line->setFrameShadow( QFrame::Raised );
233 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
236 widgetItem->setText( qtr("Splitter") );
240 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
243 widgetItem->setText( qtr("Time Slider") );
247 SoundWidget *snd = new SoundWidget( this, p_intf,
248 parent->getOptions() & WIDGET_SHINY );
251 widgetItem->setText( qtr("Volume") );
255 QListWidgetItem *widgetItem = new QListWidgetItem( this );
256 widgetItem->setText( "Small Volume" );
257 widgetItem->setIcon( QIcon( ":/volume-medium" ) );
258 widgetItem->setData( Qt::UserRole, QVariant( i ) );
259 addItem( widgetItem );
264 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
267 widgetItem->setText( qtr("Time") );
271 QWidget *discFrame = new QWidget( this );
272 //discFrame->setLineWidth( 1 );
273 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
274 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
276 QToolButton *prevSectionButton = new QToolButton( discFrame );
277 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
278 discLayout->addWidget( prevSectionButton );
280 QToolButton *menuButton = new QToolButton( discFrame );
281 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
282 discLayout->addWidget( menuButton );
284 QToolButton *nextButton = new QToolButton( discFrame );
285 nextButton->setIcon( QIcon( ":/dvd_next" ) );
286 discLayout->addWidget( nextButton );
290 widgetItem->setText( qtr("DVD menus") );
292 case TELETEXT_BUTTONS:
294 QWidget *telexFrame = new QWidget( this );
295 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
296 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
298 QToolButton *telexOn = new QToolButton( telexFrame );
299 telexOn->setIcon( QIcon( ":/tv" ) );
300 telexLayout->addWidget( telexOn );
302 QToolButton *telexTransparent = new QToolButton;
303 telexOn->setIcon( QIcon( ":/tvtelx" ) );
304 telexLayout->addWidget( telexTransparent );
306 QSpinBox *telexPage = new QSpinBox;
307 telexLayout->addWidget( telexPage );
311 widgetItem->setText( qtr("Teletext") );
313 case ADVANCED_CONTROLLER:
315 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
316 widget = advControls;
318 widgetItem->setText( qtr("Advanced Buttons") );
321 msg_Warn( p_intf, "This should not happen %i", i );
325 if( widget == NULL ) continue;
328 widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
330 widgetItem->setData( Qt::UserRole, QVariant( i ) );
332 addItem( widgetItem );
337 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
339 QListWidgetItem *item = currentItem();
342 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
344 int i_type = item->data( Qt::UserRole ).toInt();
345 int i_option = parent->getOptions();
346 dataStream << i_type << i_option;
348 /* Create a new dragging event */
349 QDrag *drag = new QDrag( this );
351 /* With correct mimedata */
352 QMimeData *mimeData = new QMimeData;
353 mimeData->setData( "vlc/button-bar", itemData );
354 drag->setMimeData( mimeData );
356 /* And correct pixmap */
357 QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
358 drag->setPixmap( aPixmap );
359 drag->setHotSpot( QPoint( 20, 20 ) );
361 /* We want to keep a copy */
362 drag->exec( Qt::CopyAction | Qt::MoveAction );
366 * The special controller with drag'n drop abilities.
367 * We don't do this in the main controller, since we don't want the OverHead
368 * to propagate there too
370 DroppingController::DroppingController( intf_thread_t *_p_intf,
373 : AbstractController( _p_intf, _parent )
377 setAcceptDrops( true );
378 controlLayout = new QHBoxLayout( this );
379 controlLayout->setSpacing( 5 );
380 controlLayout->setMargin( 0 );
381 setFrameShape( QFrame::StyledPanel );
382 setFrameShadow( QFrame::Raised );
384 parseAndCreate( line, controlLayout );
387 /* Overloading the AbstractController one, because we don't manage the
388 Spacing items in the same ways */
389 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
394 /* Special case for SPACERS, who aren't QWidgets */
395 if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
397 QLabel *label = new QLabel;
398 label->setPixmap( QPixmap( ":/space" ) );
399 if( i_type == WIDGET_SPACER_EXTEND )
401 label->setSizePolicy( QSizePolicy::MinimumExpanding,
402 QSizePolicy::Preferred );
404 /* Create a box around it */
405 label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
406 label->setLineWidth ( 1 );
407 label->setAlignment( Qt::AlignCenter );
410 label->setSizePolicy( QSizePolicy::Fixed,
411 QSizePolicy::Preferred );
413 /* Install event Filter for drag'n drop */
414 label->installEventFilter( this );
415 controlLayout->insertWidget( i_index, label );
421 QWidget *widg = createWidget( i_type, i_option );
424 /* Install the Event Filter in order to catch the drag */
425 widg->installEventFilter( this );
427 /* We are in a complex widget, we need to stop events on children too */
428 if( i_type >= VOLUME && i_type < SPECIAL_MAX )
430 QList<QObject *>children = widg->children();
433 foreach( child, children )
436 if( childWidg = qobject_cast<QWidget *>( child ) )
438 child->installEventFilter( this );
439 childWidg->setEnabled( true );
443 /* Decorating the frames when possible */
445 if( i_type >= MENU_BUTTONS /* Don't bother to check for volume */
446 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
448 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
449 frame->setLineWidth ( 1 );
453 /* Some Widgets are deactivated at creation */
454 widg->setEnabled( true );
456 controlLayout->insertWidget( i_index, widg );
459 /* QList and QBoxLayout don't act the same with insert() */
460 if( i_index < 0 ) i_index = controlLayout->count() - 1;
462 /* Insert in the value listing */
463 doubleInt *value = new doubleInt;
464 value->i_type = i_type;
465 value->i_option = i_option;
466 widgetList.insert( i_index, value );
469 DroppingController::~DroppingController()
471 qDeleteAll( widgetList );
475 QString DroppingController::getValue()
479 for( int i = 0; i < controlLayout->count(); i++ )
481 doubleInt *dI = widgetList.at( i );
484 qs.append( QString::number( dI->i_type ) );
485 if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
491 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
493 if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
499 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
501 QPoint origin = event->pos();
503 int i_pos = getParentPosInLayout( origin );
506 /* Both sides of the frame */
509 if( rubberband ) rubberband->hide();
513 /* Last item is special because of underlying items */
514 if( i_pos >= controlLayout->count() )
520 /* Query the underlying item for size && middles */
521 QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
522 QWidget *temp = tempItem->widget(); assert( temp );
524 /* Position assignment */
526 origin.rx() = temp->x() - 2;
528 if( b_end ) origin.rx() += temp->width();
531 rubberband = new QRubberBand( QRubberBand::Line, this );
532 rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
536 inline int DroppingController::getParentPosInLayout( QPoint point )
538 point.ry() = height() / 2 ;
539 QPoint origin = mapToGlobal ( point );
541 QWidget *tempWidg = QApplication::widgetAt( origin );
544 if( tempWidg != NULL)
546 i = controlLayout->indexOf( tempWidg );
549 i = controlLayout->indexOf( tempWidg->parentWidget() );
550 tempWidg = tempWidg->parentWidget();
554 /* Return the nearest position */
555 if( ( point.x() - tempWidg->x() > tempWidg->width() / 2 ) && i != -1 )
558 // msg_Dbg( p_intf, "%i", i);
562 void DroppingController::dropEvent( QDropEvent *event )
564 int i = getParentPosInLayout( event->pos() );
566 QByteArray data = event->mimeData()->data( "vlc/button-bar" );
567 QDataStream dataStream(&data, QIODevice::ReadOnly);
569 int i_option = 0, i_type = 0;
570 dataStream >> i_type >> i_option;
572 createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
574 /* Hide by precaution, you don't exactly know what could have happened in
576 if( rubberband ) rubberband->hide();
579 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
581 if( rubberband ) rubberband->hide();
586 * Overloading doAction to block any action
588 void DroppingController::doAction( int i )
593 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
595 switch( event->type() )
597 case QEvent::MouseButtonPress:
600 case QEvent::MouseButtonRelease:
603 case QEvent::MouseMove:
605 if( !b_draging ) return true;
606 QWidget *widg = static_cast<QWidget*>(obj);
609 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
612 i = controlLayout->indexOf( widg );
615 i = controlLayout->indexOf( widg->parentWidget() );
616 widg = widg->parentWidget();
617 /* NOTE: be extra-careful Now with widg access */
620 if( i == -1 ) return true;
621 doubleInt *dI = widgetList.at( i );
623 int i_type = dI->i_type;
624 int i_option = dI->i_option;
625 dataStream << i_type << i_option;
627 /* With correct mimedata */
628 QMimeData *mimeData = new QMimeData;
629 mimeData->setData( "vlc/button-bar", itemData );
631 QDrag *drag = new QDrag( widg );
632 drag->setMimeData( mimeData );
634 /* Start the effective drag */
635 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
637 widgetList.removeAt( i );
638 controlLayout->removeWidget( widg );
644 case QEvent::MouseButtonDblClick:
645 case QEvent::EnabledChange:
647 case QEvent::HideToParent:
649 case QEvent::ZOrderChange: