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" ).toString();
84 controller1 = new DroppingController( p_intf, line1,
86 mainTboxLayout->addWidget( line1Label, 1, 0, 1, 1 );
87 mainTboxLayout->addWidget( controller1, 1, 1, 1, 2 );
89 QLabel *line2Label = new QLabel( "Line 2:" );
90 QString line2 = getSettings()->value( "MainWindow/MainToolbar2" ).toString();
91 controller2 = new DroppingController( p_intf, line2,
93 mainTboxLayout->addWidget( line2Label, 2, 0, 1, 1 );
94 mainTboxLayout->addWidget( controller2, 2, 1, 1, 2);
96 /* Advanced ToolBar */
97 QLabel *advLabel = new QLabel( qtr( "Advanced Widget toolbar:" ) );
98 QString lineA = getSettings()->value( "MainWindow/AdvToolbar" )
100 controllerA = new DroppingController( p_intf, lineA,
102 mainTboxLayout->addWidget( advLabel, 3, 0, 1, 2 );
103 mainTboxLayout->addWidget( controllerA, 3, 2, 1, 1 );
105 mainLayout->addWidget( mainToolbarBox, 1, 0, 1, -1 );
108 QGroupBox *timeToolbarBox = new QGroupBox( qtr( "Time Toolbar" ) , this );
109 QGridLayout *timeTboxLayout = new QGridLayout( timeToolbarBox );
111 QString line = getSettings()->value( "MainWindow/InputToolbar" ).toString();
112 controller = new DroppingController( p_intf, line,
114 timeTboxLayout->addWidget( controller, 0, 0, 1, -1 );
116 mainLayout->addWidget( timeToolbarBox, 2, 0, 1, -1 );
119 QGroupBox *FSCToolbarBox = new QGroupBox( qtr( "Fullscreen Controller" ),
121 QGridLayout *FSCTboxLayout = new QGridLayout( FSCToolbarBox );
123 QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar" )
125 controllerFSC = new DroppingController( p_intf,
127 FSCTboxLayout->addWidget( controllerFSC, 0, 0, 1, -1 );
129 mainLayout->addWidget( FSCToolbarBox, 3, 0, 1, -1 );
132 QDialogButtonBox *okCancel = new QDialogButtonBox;
133 QPushButton *okButton = new QPushButton( qtr( "Cl&ose" ), this );
134 QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ), this );
135 okCancel->addButton( okButton, QDialogButtonBox::AcceptRole );
136 okCancel->addButton( cancelButton, QDialogButtonBox::RejectRole );
138 BUTTONACT( okButton, close() );
139 BUTTONACT( cancelButton, cancel() );
140 mainLayout->addWidget( okCancel, 4, 2 );
144 ToolbarEditDialog::~ToolbarEditDialog()
148 void ToolbarEditDialog::close()
150 msg_Dbg( p_intf, "Close and save" );
152 getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
153 getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
154 getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
155 getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
156 getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
159 void ToolbarEditDialog::cancel()
164 /************************************************
166 * Creation of the list of drawed lovely buttons
167 ************************************************/
168 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
169 : QListWidget( _parent )
171 /* We need the parent to know the options checked */
172 parent = qobject_cast<ToolbarEditDialog *>(_parent);
176 setViewMode( QListView::IconMode );
178 setDragEnabled( true );
179 setMinimumHeight( 250 );
181 /* All the buttons do not need a special rendering */
182 for( int i = 0; i < BUTTON_MAX; i++ )
184 QListWidgetItem *widgetItem = new QListWidgetItem( this );
185 widgetItem->setText( nameL[i] );
186 widgetItem->setIcon( QIcon( iconL[i] ) );
187 widgetItem->setData( Qt::UserRole, QVariant( i ) );
188 addItem( widgetItem );
191 /* Spacers are yet again a different thing */
192 QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
193 qtr( "Spacer" ), this );
194 widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
195 addItem( widgetItem );
197 widgetItem = new QListWidgetItem( QIcon( ":/space" ),
198 qtr( "Expanding Spacer" ), this );
199 widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
200 addItem( widgetItem );
203 * For all other widgets, we create then, do a pseudo rendering in
204 * a pixmaps for the view, and delete the object
206 * A lot of code is retaken from the Abstract, but not exactly...
208 * They are better ways to deal with this, but I doubt that this is
209 * necessary. If you feel like you have the time, be my guest.
213 for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
215 QWidget *widget = NULL;
216 QListWidgetItem *widgetItem = new QListWidgetItem( this );
221 QFrame *line = new QFrame( this );
222 line->setFrameShape( QFrame::VLine );
223 line->setFrameShadow( QFrame::Raised );
224 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
227 widgetItem->setText( qtr("Splitter") );
231 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
234 widgetItem->setText( qtr("Time Slider") );
238 SoundWidget *snd = new SoundWidget( this, p_intf,
239 parent->getOptions() & WIDGET_SHINY );
242 widgetItem->setText( qtr("Volume") );
246 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
249 widgetItem->setText( qtr("Time") );
253 QWidget *discFrame = new QWidget( this );
254 //discFrame->setLineWidth( 1 );
255 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
256 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
258 QToolButton *prevSectionButton = new QToolButton( discFrame );
259 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
260 discLayout->addWidget( prevSectionButton );
262 QToolButton *menuButton = new QToolButton( discFrame );
263 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
264 discLayout->addWidget( menuButton );
266 QToolButton *nextButton = new QToolButton( discFrame );
267 nextButton->setIcon( QIcon( ":/dvd_next" ) );
268 discLayout->addWidget( nextButton );
272 widgetItem->setText( qtr("DVD menus") );
274 case TELETEXT_BUTTONS:
276 QWidget *telexFrame = new QWidget( this );
277 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
278 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
280 QToolButton *telexOn = new QToolButton( telexFrame );
281 telexOn->setIcon( QIcon( ":/tv" ) );
282 telexLayout->addWidget( telexOn );
284 QToolButton *telexTransparent = new QToolButton;
285 telexOn->setIcon( QIcon( ":/tvtelx-trans" ) );
286 telexLayout->addWidget( telexTransparent );
288 QSpinBox *telexPage = new QSpinBox;
289 telexLayout->addWidget( telexPage );
293 widgetItem->setText( qtr("Teletext") );
295 case ADVANCED_CONTROLLER:
297 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
298 widget = advControls;
300 widgetItem->setText( qtr("Advanced Buttons") );
303 msg_Warn( p_intf, "This should not happen %i", i );
307 if( widget == NULL ) continue;
310 widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
312 widgetItem->setData( Qt::UserRole, QVariant( i ) );
314 addItem( widgetItem );
319 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
321 QListWidgetItem *item = currentItem();
324 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
326 int i_type = item->data( Qt::UserRole ).toInt();
327 int i_option = parent->getOptions();
328 dataStream << i_type << i_option;
330 /* Create a new dragging event */
331 QDrag *drag = new QDrag( this );
333 /* With correct mimedata */
334 QMimeData *mimeData = new QMimeData;
335 mimeData->setData( "vlc/button-bar", itemData );
336 drag->setMimeData( mimeData );
338 /* And correct pixmap */
339 QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
340 drag->setPixmap( aPixmap );
341 drag->setHotSpot( QPoint( 20, 20 ) );
343 /* We want to keep a copy */
344 drag->exec( Qt::CopyAction | Qt::MoveAction );
348 * The special controller with drag'n drop abilities.
349 * We don't do this in the main controller, since we don't want the OverHead
350 * to propagate there too
352 DroppingController::DroppingController( intf_thread_t *_p_intf,
355 : AbstractController( _p_intf, _parent )
359 setAcceptDrops( true );
360 controlLayout = new QHBoxLayout( this );
361 controlLayout->setSpacing( 5 );
362 controlLayout->setMargin( 0 );
363 setFrameShape( QFrame::StyledPanel );
364 setFrameShadow( QFrame::Raised );
366 parseAndCreate( line, controlLayout );
369 /* Overloading the AbstractController one, because we don't manage the
370 Spacing items in the same ways */
371 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
376 /* Special case for SPACERS, who aren't QWidgets */
377 if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
379 QLabel *label = new QLabel;
380 label->setPixmap( QPixmap( ":/space" ) );
381 if( i_type == WIDGET_SPACER_EXTEND )
383 label->setSizePolicy( QSizePolicy::MinimumExpanding,
384 QSizePolicy::Preferred );
386 /* Create a box around it */
387 label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
388 label->setLineWidth ( 1 );
389 label->setAlignment( Qt::AlignCenter );
392 label->setSizePolicy( QSizePolicy::Fixed,
393 QSizePolicy::Preferred );
395 /* Install event Filter for drag'n drop */
396 label->installEventFilter( this );
397 controlLayout->insertWidget( i_index, label );
403 QWidget *widg = createWidget( i_type, i_option );
406 /* Install the Event Filter in order to catch the drag */
407 widg->installEventFilter( this );
409 /* We are in a complex widget, we need to stop events on children too */
410 if( i_type >= VOLUME && i_type < SPECIAL_MAX )
412 QList<QObject *>children = widg->children();
415 foreach( child, children )
418 if( childWidg = qobject_cast<QWidget *>( child ) )
420 child->installEventFilter( this );
421 childWidg->setEnabled( true );
425 /* Decorating the frames when possible */
427 if( i_type >= MENU_BUTTONS /* Don't bother to check for volume */
428 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
430 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
431 frame->setLineWidth ( 1 );
435 /* Some Widgets are deactivated at creation */
436 widg->setEnabled( true );
438 controlLayout->insertWidget( i_index, widg );
441 /* QList and QBoxLayout don't act the same with insert() */
442 if( i_index < 0 ) i_index = controlLayout->count() - 1;
444 /* Insert in the value listing */
445 doubleInt *value = new doubleInt;
446 value->i_type = i_type;
447 value->i_option = i_option;
448 widgetList.insert( i_index, value );
451 DroppingController::~DroppingController()
453 qDeleteAll( widgetList );
457 QString DroppingController::getValue()
461 for( int i = 0; i < controlLayout->count(); i++ )
463 doubleInt *dI = widgetList.at( i );
466 qs.append( QString::number( dI->i_type ) );
467 if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
473 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
475 if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
481 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
483 QPoint origin = event->pos();
485 int i_pos = getParentPosInLayout( origin );
488 /* Both sides of the frame */
491 if( rubberband ) rubberband->hide();
495 /* Last item is special because of underlying items */
496 if( i_pos >= controlLayout->count() )
502 /* Query the underlying item for size && middles */
503 QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
504 QWidget *temp = tempItem->widget(); assert( temp );
506 /* Position assignment */
508 origin.rx() = temp->x() - 2;
510 if( b_end ) origin.rx() += temp->width();
513 rubberband = new QRubberBand( QRubberBand::Line, this );
514 rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
518 inline int DroppingController::getParentPosInLayout( QPoint point )
520 point.ry() = height() / 2 ;
521 QPoint origin = mapToGlobal ( point );
523 QWidget *tempWidg = QApplication::widgetAt( origin );
526 if( tempWidg != NULL)
528 i = controlLayout->indexOf( tempWidg );
531 i = controlLayout->indexOf( tempWidg->parentWidget() );
532 tempWidg = tempWidg->parentWidget();
536 /* Return the nearest position */
537 if( ( point.x() - tempWidg->x() > tempWidg->width() / 2 ) && i != -1 )
540 // msg_Dbg( p_intf, "%i", i);
544 void DroppingController::dropEvent( QDropEvent *event )
546 int i = getParentPosInLayout( event->pos() );
548 QByteArray data = event->mimeData()->data( "vlc/button-bar" );
549 QDataStream dataStream(&data, QIODevice::ReadOnly);
551 int i_option = 0, i_type = 0;
552 dataStream >> i_type >> i_option;
554 createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
556 /* Hide by precaution, you don't exactly know what could have happened in
558 if( rubberband ) rubberband->hide();
561 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
563 if( rubberband ) rubberband->hide();
568 * Overloading doAction to block any action
570 void DroppingController::doAction( int i )
575 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
577 switch( event->type() )
579 case QEvent::MouseButtonPress:
582 case QEvent::MouseButtonRelease:
585 case QEvent::MouseMove:
587 if( !b_draging ) return true;
588 QWidget *widg = static_cast<QWidget*>(obj);
591 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
594 i = controlLayout->indexOf( widg );
597 i = controlLayout->indexOf( widg->parentWidget() );
598 widg = widg->parentWidget();
599 /* NOTE: be extra-careful Now with widg access */
602 if( i == -1 ) return true;
603 doubleInt *dI = widgetList.at( i );
605 int i_type = dI->i_type;
606 int i_option = dI->i_option;
607 dataStream << i_type << i_option;
609 /* With correct mimedata */
610 QMimeData *mimeData = new QMimeData;
611 mimeData->setData( "vlc/button-bar", itemData );
613 QDrag *drag = new QDrag( widg );
614 drag->setMimeData( mimeData );
616 /* Start the effective drag */
617 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
619 widgetList.removeAt( i );
620 controlLayout->removeWidget( widg );
626 case QEvent::EnabledChange:
628 case QEvent::HideToParent:
630 case QEvent::ZOrderChange: