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 positionCombo = new QComboBox;
78 positionCombo->addItem( qtr( "Under the Video" ), QVariant( 0 ) );
79 positionCombo->addItem( qtr( "Above the Video" ), QVariant( 1 ) );
80 mainTboxLayout->addWidget( positionCombo, 0, 2, 1, 1 );
82 QLabel *line1Label = new QLabel( "Line 1:" );
83 QString line1 = getSettings()->value( "MainWindow/MainToolbar1",
84 MAIN_TB1_DEFAULT ).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 MAIN_TB2_DEFAULT ).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 ADV_TB_DEFAULT ).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 INPT_TB_DEFAULT ).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 FSC_TB_DEFAULT ).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/ToolbarPos",
156 positionCombo->itemData( positionCombo->currentIndex() ).toInt() );
157 getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
158 getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
159 getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
160 getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
161 getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
164 void ToolbarEditDialog::cancel()
169 /************************************************
171 * Creation of the list of drawed lovely buttons
172 ************************************************/
173 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
174 : QListWidget( _parent )
176 /* We need the parent to know the options checked */
177 parent = qobject_cast<ToolbarEditDialog *>(_parent);
181 setViewMode( QListView::IconMode );
183 setDragEnabled( true );
184 setMinimumHeight( 250 );
186 /* All the buttons do not need a special rendering */
187 for( int i = 0; i < BUTTON_MAX; i++ )
189 QListWidgetItem *widgetItem = new QListWidgetItem( this );
190 widgetItem->setText( nameL[i] );
191 widgetItem->setIcon( QIcon( iconL[i] ) );
192 widgetItem->setData( Qt::UserRole, QVariant( i ) );
193 addItem( widgetItem );
196 /* Spacers are yet again a different thing */
197 QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
198 qtr( "Spacer" ), this );
199 widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
200 addItem( widgetItem );
202 widgetItem = new QListWidgetItem( QIcon( ":/space" ),
203 qtr( "Expanding Spacer" ), this );
204 widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
205 addItem( widgetItem );
208 * For all other widgets, we create then, do a pseudo rendering in
209 * a pixmaps for the view, and delete the object
211 * A lot of code is retaken from the Abstract, but not exactly...
213 * They are better ways to deal with this, but I doubt that this is
214 * necessary. If you feel like you have the time, be my guest.
218 for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
220 QWidget *widget = NULL;
221 QListWidgetItem *widgetItem = new QListWidgetItem( this );
226 QFrame *line = new QFrame( this );
227 line->setFrameShape( QFrame::VLine );
228 line->setFrameShadow( QFrame::Raised );
229 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
232 widgetItem->setText( qtr("Splitter") );
236 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
239 widgetItem->setText( qtr("Time Slider") );
243 SoundWidget *snd = new SoundWidget( this, p_intf,
244 parent->getOptions() & WIDGET_SHINY );
247 widgetItem->setText( qtr("Volume") );
251 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
254 widgetItem->setText( qtr("Time") );
258 QWidget *discFrame = new QWidget( this );
259 //discFrame->setLineWidth( 1 );
260 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
261 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
263 QToolButton *prevSectionButton = new QToolButton( discFrame );
264 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
265 discLayout->addWidget( prevSectionButton );
267 QToolButton *menuButton = new QToolButton( discFrame );
268 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
269 discLayout->addWidget( menuButton );
271 QToolButton *nextButton = new QToolButton( discFrame );
272 nextButton->setIcon( QIcon( ":/dvd_next" ) );
273 discLayout->addWidget( nextButton );
277 widgetItem->setText( qtr("DVD menus") );
279 case TELETEXT_BUTTONS:
281 QWidget *telexFrame = new QWidget( this );
282 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
283 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
285 QToolButton *telexOn = new QToolButton( telexFrame );
286 telexOn->setIcon( QIcon( ":/tv" ) );
287 telexLayout->addWidget( telexOn );
289 QToolButton *telexTransparent = new QToolButton;
290 telexOn->setIcon( QIcon( ":/tvtelx-trans" ) );
291 telexLayout->addWidget( telexTransparent );
293 QSpinBox *telexPage = new QSpinBox;
294 telexLayout->addWidget( telexPage );
298 widgetItem->setText( qtr("Teletext") );
300 case ADVANCED_CONTROLLER:
302 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
303 widget = advControls;
305 widgetItem->setText( qtr("Advanced Buttons") );
308 msg_Warn( p_intf, "This should not happen %i", i );
312 if( widget == NULL ) continue;
315 widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
317 widgetItem->setData( Qt::UserRole, QVariant( i ) );
319 addItem( widgetItem );
324 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
326 QListWidgetItem *item = currentItem();
329 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
331 int i_type = item->data( Qt::UserRole ).toInt();
332 int i_option = parent->getOptions();
333 dataStream << i_type << i_option;
335 /* Create a new dragging event */
336 QDrag *drag = new QDrag( this );
338 /* With correct mimedata */
339 QMimeData *mimeData = new QMimeData;
340 mimeData->setData( "vlc/button-bar", itemData );
341 drag->setMimeData( mimeData );
343 /* And correct pixmap */
344 QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
345 drag->setPixmap( aPixmap );
346 drag->setHotSpot( QPoint( 20, 20 ) );
348 /* We want to keep a copy */
349 drag->exec( Qt::CopyAction | Qt::MoveAction );
353 * The special controller with drag'n drop abilities.
354 * We don't do this in the main controller, since we don't want the OverHead
355 * to propagate there too
357 DroppingController::DroppingController( intf_thread_t *_p_intf,
360 : AbstractController( _p_intf, _parent )
364 setAcceptDrops( true );
365 controlLayout = new QHBoxLayout( this );
366 controlLayout->setSpacing( 5 );
367 controlLayout->setMargin( 0 );
368 setFrameShape( QFrame::StyledPanel );
369 setFrameShadow( QFrame::Raised );
371 parseAndCreate( line, controlLayout );
374 /* Overloading the AbstractController one, because we don't manage the
375 Spacing items in the same ways */
376 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
381 /* Special case for SPACERS, who aren't QWidgets */
382 if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
384 QLabel *label = new QLabel;
385 label->setPixmap( QPixmap( ":/space" ) );
386 if( i_type == WIDGET_SPACER_EXTEND )
388 label->setSizePolicy( QSizePolicy::MinimumExpanding,
389 QSizePolicy::Preferred );
391 /* Create a box around it */
392 label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
393 label->setLineWidth ( 1 );
394 label->setAlignment( Qt::AlignCenter );
397 label->setSizePolicy( QSizePolicy::Fixed,
398 QSizePolicy::Preferred );
400 /* Install event Filter for drag'n drop */
401 label->installEventFilter( this );
402 controlLayout->insertWidget( i_index, label );
408 QWidget *widg = createWidget( i_type, i_option );
411 /* Install the Event Filter in order to catch the drag */
412 widg->installEventFilter( this );
414 /* We are in a complex widget, we need to stop events on children too */
415 if( i_type >= VOLUME && i_type < SPECIAL_MAX )
417 QList<QObject *>children = widg->children();
420 foreach( child, children )
423 if( childWidg = qobject_cast<QWidget *>( child ) )
425 child->installEventFilter( this );
426 childWidg->setEnabled( true );
430 /* Decorating the frames when possible */
432 if( i_type >= MENU_BUTTONS /* Don't bother to check for volume */
433 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
435 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
436 frame->setLineWidth ( 1 );
440 /* Some Widgets are deactivated at creation */
441 widg->setEnabled( true );
443 controlLayout->insertWidget( i_index, widg );
446 /* QList and QBoxLayout don't act the same with insert() */
447 if( i_index < 0 ) i_index = controlLayout->count() - 1;
449 /* Insert in the value listing */
450 doubleInt *value = new doubleInt;
451 value->i_type = i_type;
452 value->i_option = i_option;
453 widgetList.insert( i_index, value );
456 DroppingController::~DroppingController()
458 qDeleteAll( widgetList );
462 QString DroppingController::getValue()
466 for( int i = 0; i < controlLayout->count(); i++ )
468 doubleInt *dI = widgetList.at( i );
471 qs.append( QString::number( dI->i_type ) );
472 if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
478 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
480 if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
486 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
488 QPoint origin = event->pos();
490 int i_pos = getParentPosInLayout( origin );
493 /* Both sides of the frame */
496 if( rubberband ) rubberband->hide();
500 /* Last item is special because of underlying items */
501 if( i_pos >= controlLayout->count() )
507 /* Query the underlying item for size && middles */
508 QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
509 QWidget *temp = tempItem->widget(); assert( temp );
511 /* Position assignment */
513 origin.rx() = temp->x() - 2;
515 if( b_end ) origin.rx() += temp->width();
518 rubberband = new QRubberBand( QRubberBand::Line, this );
519 rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
523 inline int DroppingController::getParentPosInLayout( QPoint point )
525 point.ry() = height() / 2 ;
526 QPoint origin = mapToGlobal ( point );
528 QWidget *tempWidg = QApplication::widgetAt( origin );
531 if( tempWidg != NULL)
533 i = controlLayout->indexOf( tempWidg );
536 i = controlLayout->indexOf( tempWidg->parentWidget() );
537 tempWidg = tempWidg->parentWidget();
541 /* Return the nearest position */
542 if( ( point.x() - tempWidg->x() > tempWidg->width() / 2 ) && i != -1 )
545 // msg_Dbg( p_intf, "%i", i);
549 void DroppingController::dropEvent( QDropEvent *event )
551 int i = getParentPosInLayout( event->pos() );
553 QByteArray data = event->mimeData()->data( "vlc/button-bar" );
554 QDataStream dataStream(&data, QIODevice::ReadOnly);
556 int i_option = 0, i_type = 0;
557 dataStream >> i_type >> i_option;
559 createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
561 /* Hide by precaution, you don't exactly know what could have happened in
563 if( rubberband ) rubberband->hide();
566 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
568 if( rubberband ) rubberband->hide();
573 * Overloading doAction to block any action
575 void DroppingController::doAction( int i )
580 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
582 switch( event->type() )
584 case QEvent::MouseButtonPress:
587 case QEvent::MouseButtonRelease:
590 case QEvent::MouseMove:
592 if( !b_draging ) return true;
593 QWidget *widg = static_cast<QWidget*>(obj);
596 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
599 i = controlLayout->indexOf( widg );
602 i = controlLayout->indexOf( widg->parentWidget() );
603 widg = widg->parentWidget();
604 /* NOTE: be extra-careful Now with widg access */
607 if( i == -1 ) return true;
608 doubleInt *dI = widgetList.at( i );
610 int i_type = dI->i_type;
611 int i_option = dI->i_option;
612 dataStream << i_type << i_option;
614 /* With correct mimedata */
615 QMimeData *mimeData = new QMimeData;
616 mimeData->setData( "vlc/button-bar", itemData );
618 QDrag *drag = new QDrag( widg );
619 drag->setMimeData( mimeData );
621 /* Start the effective drag */
622 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
624 widgetList.removeAt( i );
625 controlLayout->removeWidget( widg );
631 case QEvent::EnabledChange:
633 case QEvent::HideToParent:
635 case QEvent::ZOrderChange: