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>
42 #include <QInputDialog>
44 ToolbarEditDialog *ToolbarEditDialog::instance = NULL;
46 ToolbarEditDialog::ToolbarEditDialog( intf_thread_t *_p_intf)
47 : QVLCFrame( _p_intf )
49 setWindowTitle( qtr( "Toolbars Editor" ) );
50 QGridLayout *mainLayout = new QGridLayout( this );
51 setMinimumWidth( 600 );
54 QGroupBox *widgetBox = new QGroupBox( qtr( "Toolbar Elements") , this );
55 widgetBox->setSizePolicy( QSizePolicy::Preferred,
56 QSizePolicy::MinimumExpanding );
57 QGridLayout *boxLayout = new QGridLayout( widgetBox );
59 QLabel *styleLabel = new QLabel( qtr( "Next widget style:" ) );
60 flatBox = new QCheckBox( qtr( "Flat Button" ) );
61 bigBox = new QCheckBox( qtr( "Big Button" ) );
62 shinyBox = new QCheckBox( qtr( "Native Slider" ) );
64 boxLayout->addWidget( new WidgetListing( p_intf, this ), 0, 0, 1, -1);
65 boxLayout->addWidget( styleLabel, 1, 0 );
66 boxLayout->addWidget( flatBox, 1, 1 );
67 boxLayout->addWidget( bigBox, 1, 2 );
68 boxLayout->addWidget( shinyBox, 1, 3 );
69 mainLayout->addWidget( widgetBox, 0, 0, 1, -1 );
73 QGroupBox *mainToolbarBox = new QGroupBox( qtr( "Main Toolbar" ), this );
74 QGridLayout *mainTboxLayout = new QGridLayout( mainToolbarBox );
76 QLabel *label = new QLabel( qtr( "Toolbar position:" ) );
77 mainTboxLayout->addWidget(label, 0, 0, 1, 2);
79 positionCombo = new QComboBox;
80 positionCombo->addItem( qtr( "Under the Video" ), QVariant( 0 ) );
81 positionCombo->addItem( qtr( "Above the Video" ), QVariant( 1 ) );
82 positionCombo->setCurrentIndex( positionCombo->findData(
83 getSettings()->value( "MainWindow/ToolbarPos", 0 ).toInt() ) );
84 mainTboxLayout->addWidget( positionCombo, 0, 2, 1, 1 );
86 QLabel *line1Label = new QLabel( "Line 1:" );
87 QString line1 = getSettings()->value( "MainWindow/MainToolbar1",
88 MAIN_TB1_DEFAULT ).toString();
89 controller1 = new DroppingController( p_intf, line1,
91 mainTboxLayout->addWidget( line1Label, 1, 0, 1, 1 );
92 mainTboxLayout->addWidget( controller1, 1, 1, 1, 2 );
94 QLabel *line2Label = new QLabel( "Line 2:" );
95 QString line2 = getSettings()->value( "MainWindow/MainToolbar2",
96 MAIN_TB2_DEFAULT ).toString();
97 controller2 = new DroppingController( p_intf, line2,
99 mainTboxLayout->addWidget( line2Label, 2, 0, 1, 1 );
100 mainTboxLayout->addWidget( controller2, 2, 1, 1, 2);
102 /* Advanced ToolBar */
103 QLabel *advLabel = new QLabel( qtr( "Advanced Widget toolbar:" ) );
104 QString lineA = getSettings()->value( "MainWindow/AdvToolbar",
105 ADV_TB_DEFAULT ).toString();
106 controllerA = new DroppingController( p_intf, lineA,
108 mainTboxLayout->addWidget( advLabel, 3, 0, 1, 2 );
109 mainTboxLayout->addWidget( controllerA, 3, 2, 1, 1 );
111 mainLayout->addWidget( mainToolbarBox, 1, 0, 1, -1 );
114 QGroupBox *timeToolbarBox = new QGroupBox( qtr( "Time Toolbar" ) , this );
115 QGridLayout *timeTboxLayout = new QGridLayout( timeToolbarBox );
117 QString line = getSettings()->value( "MainWindow/InputToolbar",
118 INPT_TB_DEFAULT ).toString();
119 controller = new DroppingController( p_intf, line,
121 timeTboxLayout->addWidget( controller, 0, 0, 1, -1 );
123 mainLayout->addWidget( timeToolbarBox, 2, 0, 1, -1 );
126 QGroupBox *FSCToolbarBox = new QGroupBox( qtr( "Fullscreen Controller" ),
128 QGridLayout *FSCTboxLayout = new QGridLayout( FSCToolbarBox );
130 QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar",
131 FSC_TB_DEFAULT ).toString();
132 controllerFSC = new DroppingController( p_intf,
134 FSCTboxLayout->addWidget( controllerFSC, 0, 0, 1, -1 );
136 mainLayout->addWidget( FSCToolbarBox, 3, 0, 1, -1 );
139 QGroupBox *profileBox = new QGroupBox( qtr( "Profile" ), this );
140 QGridLayout *profileBoxLayout = new QGridLayout( profileBox );
142 profileCombo = new QComboBox;
143 QLabel *profileLabel = new QLabel( qtr( "Select profile:" ), this );
145 QToolButton *newButton = new QToolButton;
146 newButton->setIcon( QIcon( ":/new" ) );
147 QToolButton *deleteButton = new QToolButton;
148 deleteButton->setIcon( QIcon( ":/clear" ) );
149 deleteButton->setToolTip( qtr( "Delete the current profile" ) );
151 profileBoxLayout->addWidget( profileLabel, 0, 0 );
152 profileBoxLayout->addWidget( profileCombo, 0, 1 );
153 profileBoxLayout->addWidget( newButton, 0, 2 );
154 profileBoxLayout->addWidget( deleteButton, 0, 3 );
156 mainLayout->addWidget( profileBox, 4, 0, 1, -1 );
159 int i_size = getSettings()->beginReadArray( "ToolbarProfiles" );
160 for( int i = 0; i < i_size; i++ )
162 getSettings()->setArrayIndex(i);
163 profileCombo->addItem( getSettings()->value( "ProfileName" ).toString(),
164 getSettings()->value( "Value" ).toString() );
166 getSettings()->endArray();
168 /* Load defaults ones if we have no combos */
169 /* We could decide that we load defaults on first launch of the dialog
170 or when the combo is back to 0. I choose the second solution, because some clueless
171 user might hit on delete a bit too much, but discussion is opened. -- jb */
174 profileCombo->addItem( PROFILE_NAME_1, QString( VALUE_1 ) );
175 profileCombo->addItem( PROFILE_NAME_2, QString( VALUE_2 ) );
176 profileCombo->addItem( PROFILE_NAME_3, QString( VALUE_3 ) );
177 profileCombo->addItem( PROFILE_NAME_4, QString( VALUE_4 ) );
178 profileCombo->addItem( PROFILE_NAME_5, QString( VALUE_5 ) );
180 profileCombo->setCurrentIndex( -1 );
183 QDialogButtonBox *okCancel = new QDialogButtonBox;
184 QPushButton *okButton = new QPushButton( qtr( "Cl&ose" ), this );
185 okButton->setDefault( true );
186 QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ), this );
187 okCancel->addButton( okButton, QDialogButtonBox::AcceptRole );
188 okCancel->addButton( cancelButton, QDialogButtonBox::RejectRole );
190 BUTTONACT( deleteButton, deleteProfile() );
191 BUTTONACT( newButton, newProfile() );
192 CONNECT( profileCombo, currentIndexChanged( int ), this, changeProfile( int ) );
193 BUTTONACT( okButton, close() );
194 BUTTONACT( cancelButton, cancel() );
195 mainLayout->addWidget( okCancel, 5, 2 );
199 ToolbarEditDialog::~ToolbarEditDialog()
201 getSettings()->beginWriteArray( "ToolbarProfiles" );
202 for( int i = 0; i < profileCombo->count(); i++ )
204 getSettings()->setArrayIndex(i);
205 getSettings()->setValue( "ProfileName", profileCombo->itemText( i ) );
206 getSettings()->setValue( "Value", profileCombo->itemData( i ) );
208 getSettings()->endArray();
211 void ToolbarEditDialog::newProfile()
214 QString name = QInputDialog::getText( this, qtr( "Profile Name" ),
215 qtr( "Please enter the new profile name." ), QLineEdit::Normal, 0, &ok );
218 QString temp = QString::number( positionCombo->currentIndex() );
219 temp += "|" + controller1->getValue();
220 temp += "|" + controller2->getValue();
221 temp += "|" + controllerA->getValue();
222 temp += "|" + controller->getValue();
223 temp += "|" + controllerFSC->getValue();
225 profileCombo->addItem( name, temp );
226 profileCombo->setCurrentIndex( profileCombo->count() - 1 );
229 void ToolbarEditDialog::deleteProfile()
231 profileCombo->removeItem( profileCombo->currentIndex() );
234 void ToolbarEditDialog::changeProfile( int i )
236 QStringList qs_list = profileCombo->itemData( i ).toString().split( "|" );
237 if( qs_list.count() < 6 )
240 positionCombo->setCurrentIndex( positionCombo->findData( qs_list[0].toInt() ) );
241 controller1->resetLine( qs_list[1] );
242 controller2->resetLine( qs_list[2] );
243 controllerA->resetLine( qs_list[3] );
244 controller->resetLine( qs_list[4] );
245 controllerFSC->resetLine( qs_list[5] );
248 void ToolbarEditDialog::close()
250 msg_Dbg( p_intf, "Close and save" );
252 getSettings()->setValue( "MainWindow/ToolbarPos",
253 positionCombo->itemData( positionCombo->currentIndex() ).toInt() );
254 getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
255 getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
256 getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
257 getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
258 getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
261 void ToolbarEditDialog::cancel()
266 /************************************************
268 * Creation of the list of drawed lovely buttons
269 ************************************************/
270 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
271 : QListWidget( _parent )
273 /* We need the parent to know the options checked */
274 parent = qobject_cast<ToolbarEditDialog *>(_parent);
278 setViewMode( QListView::IconMode );
280 setDragEnabled( true );
281 setMinimumHeight( 250 );
283 /* All the buttons do not need a special rendering */
284 for( int i = 0; i < BUTTON_MAX; i++ )
286 QListWidgetItem *widgetItem = new QListWidgetItem( this );
287 widgetItem->setText( nameL[i] );
288 widgetItem->setIcon( QIcon( iconL[i] ) );
289 widgetItem->setData( Qt::UserRole, QVariant( i ) );
290 addItem( widgetItem );
293 /* Spacers are yet again a different thing */
294 QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
295 qtr( "Spacer" ), this );
296 widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
297 addItem( widgetItem );
299 widgetItem = new QListWidgetItem( QIcon( ":/space" ),
300 qtr( "Expanding Spacer" ), this );
301 widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
302 addItem( widgetItem );
305 * For all other widgets, we create then, do a pseudo rendering in
306 * a pixmaps for the view, and delete the object
308 * A lot of code is retaken from the Abstract, but not exactly...
310 * They are better ways to deal with this, but I doubt that this is
311 * necessary. If you feel like you have the time, be my guest.
315 for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
317 QWidget *widget = NULL;
318 QListWidgetItem *widgetItem = new QListWidgetItem( this );
323 QFrame *line = new QFrame( this );
324 line->setFrameShape( QFrame::VLine );
325 line->setFrameShadow( QFrame::Raised );
326 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
329 widgetItem->setText( qtr("Splitter") );
333 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
336 widgetItem->setText( qtr("Time Slider") );
340 SoundWidget *snd = new SoundWidget( this, p_intf,
341 parent->getOptions() & WIDGET_SHINY );
344 widgetItem->setText( qtr("Volume") );
348 QListWidgetItem *widgetItem = new QListWidgetItem( this );
349 widgetItem->setText( "Small Volume" );
350 widgetItem->setIcon( QIcon( ":/volume-medium" ) );
351 widgetItem->setData( Qt::UserRole, QVariant( i ) );
352 addItem( widgetItem );
357 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
360 widgetItem->setText( qtr("Time") );
364 QWidget *discFrame = new QWidget( this );
365 //discFrame->setLineWidth( 1 );
366 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
367 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
369 QToolButton *prevSectionButton = new QToolButton( discFrame );
370 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
371 discLayout->addWidget( prevSectionButton );
373 QToolButton *menuButton = new QToolButton( discFrame );
374 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
375 discLayout->addWidget( menuButton );
377 QToolButton *nextButton = new QToolButton( discFrame );
378 nextButton->setIcon( QIcon( ":/dvd_next" ) );
379 discLayout->addWidget( nextButton );
383 widgetItem->setText( qtr("DVD menus") );
385 case TELETEXT_BUTTONS:
387 QWidget *telexFrame = new QWidget( this );
388 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
389 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
391 QToolButton *telexOn = new QToolButton( telexFrame );
392 telexOn->setIcon( QIcon( ":/tv" ) );
393 telexLayout->addWidget( telexOn );
395 QToolButton *telexTransparent = new QToolButton;
396 telexOn->setIcon( QIcon( ":/tvtelx" ) );
397 telexLayout->addWidget( telexTransparent );
399 QSpinBox *telexPage = new QSpinBox;
400 telexLayout->addWidget( telexPage );
404 widgetItem->setText( qtr("Teletext") );
406 case ADVANCED_CONTROLLER:
408 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
409 widget = advControls;
411 widgetItem->setText( qtr("Advanced Buttons") );
414 msg_Warn( p_intf, "This should not happen %i", i );
418 if( widget == NULL ) continue;
421 widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
423 widgetItem->setData( Qt::UserRole, QVariant( i ) );
425 addItem( widgetItem );
430 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
432 QListWidgetItem *item = currentItem();
435 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
437 int i_type = item->data( Qt::UserRole ).toInt();
438 int i_option = parent->getOptions();
439 dataStream << i_type << i_option;
441 /* Create a new dragging event */
442 QDrag *drag = new QDrag( this );
444 /* With correct mimedata */
445 QMimeData *mimeData = new QMimeData;
446 mimeData->setData( "vlc/button-bar", itemData );
447 drag->setMimeData( mimeData );
449 /* And correct pixmap */
450 QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
451 drag->setPixmap( aPixmap );
452 drag->setHotSpot( QPoint( 20, 20 ) );
454 /* We want to keep a copy */
455 drag->exec( Qt::CopyAction | Qt::MoveAction );
459 * The special controller with drag'n drop abilities.
460 * We don't do this in the main controller, since we don't want the OverHead
461 * to propagate there too
463 DroppingController::DroppingController( intf_thread_t *_p_intf,
466 : AbstractController( _p_intf, _parent )
470 setAcceptDrops( true );
471 controlLayout = new QHBoxLayout( this );
472 controlLayout->setSpacing( 5 );
473 controlLayout->setMargin( 0 );
474 setFrameShape( QFrame::StyledPanel );
475 setFrameShadow( QFrame::Raised );
477 parseAndCreate( line, controlLayout );
480 void DroppingController::resetLine( QString line )
485 while( (child = controlLayout->takeAt( 0 ) ) != 0 )
487 child->widget()->hide();
491 parseAndCreate( line, controlLayout );
495 /* Overloading the AbstractController one, because we don't manage the
496 Spacing items in the same ways */
497 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
502 /* Special case for SPACERS, who aren't QWidgets */
503 if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
505 QLabel *label = new QLabel;
506 label->setPixmap( QPixmap( ":/space" ) );
507 if( i_type == WIDGET_SPACER_EXTEND )
509 label->setSizePolicy( QSizePolicy::MinimumExpanding,
510 QSizePolicy::Preferred );
512 /* Create a box around it */
513 label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
514 label->setLineWidth ( 1 );
515 label->setAlignment( Qt::AlignCenter );
518 label->setSizePolicy( QSizePolicy::Fixed,
519 QSizePolicy::Preferred );
521 /* Install event Filter for drag'n drop */
522 label->installEventFilter( this );
523 controlLayout->insertWidget( i_index, label );
529 QWidget *widg = createWidget( i_type, i_option );
532 /* Install the Event Filter in order to catch the drag */
533 widg->installEventFilter( this );
535 /* We are in a complex widget, we need to stop events on children too */
536 if( i_type >= VOLUME && i_type < SPECIAL_MAX )
538 QList<QObject *>children = widg->children();
541 foreach( child, children )
544 if( childWidg = qobject_cast<QWidget *>( child ) )
546 child->installEventFilter( this );
547 childWidg->setEnabled( true );
551 /* Decorating the frames when possible */
553 if( i_type >= MENU_BUTTONS /* Don't bother to check for volume */
554 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
556 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
557 frame->setLineWidth ( 1 );
561 /* Some Widgets are deactivated at creation */
562 widg->setEnabled( true );
564 controlLayout->insertWidget( i_index, widg );
567 /* QList and QBoxLayout don't act the same with insert() */
568 if( i_index < 0 ) i_index = controlLayout->count() - 1;
570 /* Insert in the value listing */
571 doubleInt *value = new doubleInt;
572 value->i_type = i_type;
573 value->i_option = i_option;
574 widgetList.insert( i_index, value );
577 DroppingController::~DroppingController()
579 qDeleteAll( widgetList );
583 QString DroppingController::getValue()
587 for( int i = 0; i < controlLayout->count(); i++ )
589 doubleInt *dI = widgetList.at( i );
592 qs.append( QString::number( dI->i_type ) );
593 if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
599 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
601 if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
607 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
609 QPoint origin = event->pos();
611 int i_pos = getParentPosInLayout( origin );
614 /* Both sides of the frame */
617 if( rubberband ) rubberband->hide();
621 /* Last item is special because of underlying items */
622 if( i_pos >= controlLayout->count() )
628 /* Query the underlying item for size && middles */
629 QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
630 QWidget *temp = tempItem->widget(); assert( temp );
632 /* Position assignment */
634 origin.rx() = temp->x() - 2;
636 if( b_end ) origin.rx() += temp->width();
639 rubberband = new QRubberBand( QRubberBand::Line, this );
640 rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
644 inline int DroppingController::getParentPosInLayout( QPoint point )
646 point.ry() = height() / 2 ;
647 QPoint origin = mapToGlobal ( point );
649 QWidget *tempWidg = QApplication::widgetAt( origin );
652 if( tempWidg != NULL)
654 i = controlLayout->indexOf( tempWidg );
657 i = controlLayout->indexOf( tempWidg->parentWidget() );
658 tempWidg = tempWidg->parentWidget();
662 /* Return the nearest position */
663 if( ( point.x() - tempWidg->x() > tempWidg->width() / 2 ) && i != -1 )
666 // msg_Dbg( p_intf, "%i", i);
670 void DroppingController::dropEvent( QDropEvent *event )
672 int i = getParentPosInLayout( event->pos() );
674 QByteArray data = event->mimeData()->data( "vlc/button-bar" );
675 QDataStream dataStream(&data, QIODevice::ReadOnly);
677 int i_option = 0, i_type = 0;
678 dataStream >> i_type >> i_option;
680 createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
682 /* Hide by precaution, you don't exactly know what could have happened in
684 if( rubberband ) rubberband->hide();
687 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
689 if( rubberband ) rubberband->hide();
694 * Overloading doAction to block any action
696 void DroppingController::doAction( int i )
701 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
703 switch( event->type() )
705 case QEvent::MouseButtonPress:
708 case QEvent::MouseButtonRelease:
711 case QEvent::MouseMove:
713 if( !b_draging ) return true;
714 QWidget *widg = static_cast<QWidget*>(obj);
717 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
720 i = controlLayout->indexOf( widg );
723 i = controlLayout->indexOf( widg->parentWidget() );
724 widg = widg->parentWidget();
725 /* NOTE: be extra-careful Now with widg access */
728 if( i == -1 ) return true;
729 doubleInt *dI = widgetList.at( i );
731 int i_type = dI->i_type;
732 int i_option = dI->i_option;
733 dataStream << i_type << i_option;
735 /* With correct mimedata */
736 QMimeData *mimeData = new QMimeData;
737 mimeData->setData( "vlc/button-bar", itemData );
739 QDrag *drag = new QDrag( widg );
740 drag->setMimeData( mimeData );
742 /* Start the effective drag */
743 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
745 widgetList.removeAt( i );
746 controlLayout->removeWidget( widg );
752 case QEvent::MouseButtonDblClick:
753 case QEvent::EnabledChange:
755 case QEvent::HideToParent:
757 case QEvent::ZOrderChange: