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 flatBox = new QCheckBox( qtr( "Flat Button" ) );
59 bigBox = new QCheckBox( qtr( "Big Button" ) );
60 shinyBox = new QCheckBox( qtr( "Native Slider" ) );
61 shinyBox->setChecked( true );
63 boxLayout->addWidget( new WidgetListing( p_intf, this ), 0, 0, 1, -1);
64 boxLayout->addWidget( flatBox, 1, 0 );
65 boxLayout->addWidget( bigBox, 1, 1 );
66 boxLayout->addWidget( shinyBox, 1, 2 );
67 mainLayout->addWidget( widgetBox, 0, 0, 1, -1 );
71 QGroupBox *mainToolbarBox = new QGroupBox( qtr( "Main Toolbar" ), this );
72 QGridLayout *mainTboxLayout = new QGridLayout( mainToolbarBox );
74 QLabel *label = new QLabel( qtr( "Toolbar position:" ) );
75 mainTboxLayout->addWidget(label, 0, 0, 1, 2);
77 QComboBox *positionCombo = new QComboBox;
78 positionCombo->addItems( QStringList() << qtr( "Above the Video" )
79 << qtr( "Under the Video" ) );
80 mainTboxLayout->addWidget( positionCombo, 0, 2, 1, 1 );
82 QLabel *line1Label = new QLabel( "Line 1:" );
83 QString line1 = getSettings()->value( "MainWindow/MainToolbar1",
84 "64;36;37;38;65").toString();
85 controller1 = new DroppingController( p_intf, line1,
87 mainTboxLayout->addWidget( line1Label, 1, 0, 1, 1 );
88 mainTboxLayout->addWidget( controller1, 1, 1, 1, 2 );
90 QLabel *line2Label = new QLabel( "Line 2:" );
91 QString line2 = getSettings()->value( "MainWindow/MainToolbar2",
92 "0-2;64;3;1;4;64;7;10;9;65;34-4;" ).toString();
93 controller2 = new DroppingController( p_intf, line2,
95 mainTboxLayout->addWidget( line2Label, 2, 0, 1, 1 );
96 mainTboxLayout->addWidget( controller2, 2, 1, 1, 2);
98 /* Advanced ToolBar */
99 QLabel *advLabel = new QLabel( qtr( "Advanced Widget toolbar:" ) );
100 QString lineA = getSettings()->value( "MainWindow/AdvToolbar",
101 "12;11;13;14").toString();
102 controllerA = new DroppingController( p_intf, lineA,
104 mainTboxLayout->addWidget( advLabel, 3, 0, 1, 2 );
105 mainTboxLayout->addWidget( controllerA, 3, 2, 1, 1 );
107 mainLayout->addWidget( mainToolbarBox, 1, 0, 1, -1 );
110 QGroupBox *timeToolbarBox = new QGroupBox( qtr( "Time Toolbar" ) , this );
111 QGridLayout *timeTboxLayout = new QGridLayout( timeToolbarBox );
113 QString line = getSettings()->value( "MainWindow/InputToolbar",
114 "5-1;34;6-1").toString();
115 controller = new DroppingController( p_intf, line,
117 timeTboxLayout->addWidget( controller, 0, 0, 1, -1 );
119 mainLayout->addWidget( timeToolbarBox, 2, 0, 1, -1 );
122 QGroupBox *FSCToolbarBox = new QGroupBox( qtr( "Fullscreen Controller" ),
124 QGridLayout *FSCTboxLayout = new QGridLayout( FSCToolbarBox );
126 QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar",
127 "0-2;64;3;1;4;64;36;64;37;64;8;65;35-4;33" ).toString();
128 controllerFSC = new DroppingController( p_intf,
130 FSCTboxLayout->addWidget( controllerFSC, 0, 0, 1, -1 );
132 mainLayout->addWidget( FSCToolbarBox, 3, 0, 1, -1 );
135 QDialogButtonBox *okCancel = new QDialogButtonBox;
136 QPushButton *okButton = new QPushButton( qtr( "Cl&ose" ), this );
137 QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ), this );
138 okCancel->addButton( okButton, QDialogButtonBox::AcceptRole );
139 okCancel->addButton( cancelButton, QDialogButtonBox::RejectRole );
141 BUTTONACT( okButton, close() );
142 BUTTONACT( cancelButton, cancel() );
143 mainLayout->addWidget( okCancel, 4, 2 );
147 ToolbarEditDialog::~ToolbarEditDialog()
151 void ToolbarEditDialog::close()
153 msg_Dbg( p_intf, "Close and save" );
155 getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
156 getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
157 getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
158 getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
159 getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
162 void ToolbarEditDialog::cancel()
167 /************************************************
169 * Creation of the list of drawed lovely buttons
170 ************************************************/
171 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
172 : QListWidget( _parent )
174 /* We need the parent to know the options checked */
175 parent = qobject_cast<ToolbarEditDialog *>(_parent);
179 setViewMode( QListView::IconMode );
181 setDragEnabled( true );
182 setMinimumHeight( 250 );
184 /* All the buttons do not need a special rendering */
185 for( int i = 0; i < BUTTON_MAX; i++ )
187 QListWidgetItem *widgetItem = new QListWidgetItem( this );
188 widgetItem->setText( nameL[i] );
189 widgetItem->setIcon( QIcon( iconL[i] ) );
190 widgetItem->setData( Qt::UserRole, QVariant( i ) );
191 addItem( widgetItem );
194 /* Spacers are yet again a different thing */
195 QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
196 qtr( "Spacer" ), this );
197 widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
198 addItem( widgetItem );
200 widgetItem = new QListWidgetItem( QIcon( ":/space" ),
201 qtr( "Expanding Spacer" ), this );
202 widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
203 addItem( widgetItem );
206 * For all other widgets, we create then, do a pseudo rendering in
207 * a pixmaps for the view, and delete the object
209 * A lot of code is retaken from the Abstract, but not exactly...
211 * They are better ways to deal with this, but I doubt that this is
212 * necessary. If you feel like you have the time, be my guest.
216 for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
218 QWidget *widget = NULL;
219 QListWidgetItem *widgetItem = new QListWidgetItem( this );
224 QFrame *line = new QFrame( this );
225 line->setFrameShape( QFrame::VLine );
226 line->setFrameShadow( QFrame::Raised );
227 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
230 widgetItem->setText( qtr("Splitter") );
234 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
237 widgetItem->setText( qtr("Time Slider") );
241 SoundWidget *snd = new SoundWidget( this, p_intf,
242 parent->getOptions() & WIDGET_SHINY );
245 widgetItem->setText( qtr("Volume") );
249 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
252 widgetItem->setText( qtr("Time") );
256 QWidget *discFrame = new QWidget( this );
257 //discFrame->setLineWidth( 1 );
258 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
259 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
261 QToolButton *prevSectionButton = new QToolButton( discFrame );
262 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
263 discLayout->addWidget( prevSectionButton );
265 QToolButton *menuButton = new QToolButton( discFrame );
266 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
267 discLayout->addWidget( menuButton );
269 QToolButton *nextButton = new QToolButton( discFrame );
270 nextButton->setIcon( QIcon( ":/dvd_next" ) );
271 discLayout->addWidget( nextButton );
275 widgetItem->setText( qtr("DVD menus") );
277 case TELETEXT_BUTTONS:
279 QWidget *telexFrame = new QWidget( this );
280 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
281 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
283 QToolButton *telexOn = new QToolButton( telexFrame );
284 telexOn->setIcon( QIcon( ":/tv" ) );
285 telexLayout->addWidget( telexOn );
287 QToolButton *telexTransparent = new QToolButton;
288 telexOn->setIcon( QIcon( ":/tvtelx-trans" ) );
289 telexLayout->addWidget( telexTransparent );
291 QSpinBox *telexPage = new QSpinBox;
292 telexLayout->addWidget( telexPage );
296 widgetItem->setText( qtr("Teletext") );
298 case ADVANCED_CONTROLLER:
300 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
301 widget = advControls;
303 widgetItem->setText( qtr("Advanced Buttons") );
306 msg_Warn( p_intf, "This should not happen %i", i );
310 if( widget == NULL ) continue;
313 widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
315 widgetItem->setData( Qt::UserRole, QVariant( i ) );
317 addItem( widgetItem );
322 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
324 QListWidgetItem *item = currentItem();
327 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
329 int i_type = item->data( Qt::UserRole ).toInt();
330 int i_option = parent->getOptions();
331 dataStream << i_type << i_option;
333 /* Create a new dragging event */
334 QDrag *drag = new QDrag( this );
336 /* With correct mimedata */
337 QMimeData *mimeData = new QMimeData;
338 mimeData->setData( "vlc/button-bar", itemData );
339 drag->setMimeData( mimeData );
341 /* And correct pixmap */
342 QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
343 drag->setPixmap( aPixmap );
344 drag->setHotSpot( QPoint( 20, 20 ) );
346 /* We want to keep a copy */
347 drag->exec( Qt::CopyAction | Qt::MoveAction );
351 * The special controller with drag'n drop abilities.
352 * We don't do this in the main controller, since we don't want the OverHead
353 * to propagate there too
355 DroppingController::DroppingController( intf_thread_t *_p_intf,
358 : AbstractController( _p_intf, _parent )
362 setAcceptDrops( true );
363 controlLayout = new QHBoxLayout( this );
364 controlLayout->setSpacing( 5 );
365 controlLayout->setMargin( 0 );
366 setFrameShape( QFrame::StyledPanel );
367 setFrameShadow( QFrame::Raised );
369 parseAndCreate( line, controlLayout );
372 /* Overloading the AbstractController one, because we don't manage the
373 Spacing items in the same ways */
374 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
379 /* Special case for SPACERS, who aren't QWidgets */
380 if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
382 QLabel *label = new QLabel;
383 label->setPixmap( QPixmap( ":/space" ) );
384 if( i_type == WIDGET_SPACER_EXTEND )
386 label->setSizePolicy( QSizePolicy::MinimumExpanding,
387 QSizePolicy::Preferred );
389 /* Create a box around it */
390 label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
391 label->setLineWidth ( 1 );
392 label->setAlignment( Qt::AlignCenter );
395 label->setSizePolicy( QSizePolicy::Fixed,
396 QSizePolicy::Preferred );
398 /* Install event Filter for drag'n drop */
399 label->installEventFilter( this );
400 controlLayout->insertWidget( i_index, label );
406 QWidget *widg = createWidget( i_type, i_option );
409 /* Install the Event Filter in order to catch the drag */
410 widg->installEventFilter( this );
412 /* We are in a complex widget, we need to stop events on children too */
413 if( i_type >= VOLUME && i_type < SPECIAL_MAX )
415 QList<QObject *>children = widg->children();
418 foreach( child, children )
421 if( childWidg = qobject_cast<QWidget *>( child ) )
423 child->installEventFilter( this );
424 childWidg->setEnabled( true );
428 /* Decorating the frames when possible */
430 if( i_type >= MENU_BUTTONS /* Don't bother to check for volume */
431 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
433 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
434 frame->setLineWidth ( 1 );
438 /* Some Widgets are deactivated at creation */
439 widg->setEnabled( true );
441 controlLayout->insertWidget( i_index, widg );
444 /* QList and QBoxLayout don't act the same with insert() */
445 if( i_index < 0 ) i_index = controlLayout->count() - 1;
447 /* Insert in the value listing */
448 doubleInt *value = new doubleInt;
449 value->i_type = i_type;
450 value->i_option = i_option;
451 widgetList.insert( i_index, value );
454 DroppingController::~DroppingController()
456 qDeleteAll( widgetList );
460 QString DroppingController::getValue()
464 for( int i = 0; i < controlLayout->count(); i++ )
466 doubleInt *dI = widgetList.at( i );
469 qs.append( QString::number( dI->i_type ) );
470 if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
476 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
478 if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
484 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
486 QPoint origin = event->pos();
488 int i_pos = getParentPosInLayout( origin );
491 /* Both sides of the frame */
494 if( rubberband ) rubberband->hide();
498 /* Last item is special because of underlying items */
499 if( i_pos >= controlLayout->count() )
505 /* Query the underlying item for size && middles */
506 QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
507 QWidget *temp = tempItem->widget(); assert( temp );
509 /* Position assignment */
511 origin.rx() = temp->x() - 2;
513 if( b_end ) origin.rx() += temp->width();
516 rubberband = new QRubberBand( QRubberBand::Line, this );
517 rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
521 inline int DroppingController::getParentPosInLayout( QPoint point )
523 point.ry() = height() / 2 ;
524 QPoint origin = mapToGlobal ( point );
526 QWidget *tempWidg = QApplication::widgetAt( origin );
529 if( tempWidg != NULL)
531 i = controlLayout->indexOf( tempWidg );
534 i = controlLayout->indexOf( tempWidg->parentWidget() );
535 tempWidg = tempWidg->parentWidget();
539 /* Return the nearest position */
540 if( ( point.x() - tempWidg->x() > tempWidg->width() / 2 ) && i != -1 )
543 // msg_Dbg( p_intf, "%i", i);
547 void DroppingController::dropEvent( QDropEvent *event )
549 int i = getParentPosInLayout( event->pos() );
551 QByteArray data = event->mimeData()->data( "vlc/button-bar" );
552 QDataStream dataStream(&data, QIODevice::ReadOnly);
554 int i_option = 0, i_type = 0;
555 dataStream >> i_type >> i_option;
557 createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
559 /* Hide by precaution, you don't exactly know what could have happened in
561 if( rubberband ) rubberband->hide();
564 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
566 if( rubberband ) rubberband->hide();
571 * Overloading doAction to block any action
573 void DroppingController::doAction( int i )
578 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
580 switch( event->type() )
582 case QEvent::MouseButtonPress:
585 case QEvent::MouseButtonRelease:
588 case QEvent::MouseMove:
590 if( !b_draging ) return true;
591 QWidget *widg = static_cast<QWidget*>(obj);
594 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
597 i = controlLayout->indexOf( widg );
600 i = controlLayout->indexOf( widg->parentWidget() );
601 widg = widg->parentWidget();
602 /* NOTE: be extra-careful Now with widg access */
605 if( i == -1 ) return true;
606 doubleInt *dI = widgetList.at( i );
608 int i_type = dI->i_type;
609 int i_option = dI->i_option;
610 dataStream << i_type << i_option;
612 /* With correct mimedata */
613 QMimeData *mimeData = new QMimeData;
614 mimeData->setData( "vlc/button-bar", itemData );
616 QDrag *drag = new QDrag( widg );
617 drag->setMimeData( mimeData );
619 /* Start the effective drag */
620 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
622 widgetList.removeAt( i );
623 controlLayout->removeWidget( widg );
629 case QEvent::EnabledChange:
631 case QEvent::HideToParent:
633 case QEvent::ZOrderChange: