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" ) );
62 boxLayout->addWidget( new WidgetListing( p_intf, this ), 0, 0, 1, -1);
63 boxLayout->addWidget( flatBox, 1, 0 );
64 boxLayout->addWidget( bigBox, 1, 1 );
65 boxLayout->addWidget( shinyBox, 1, 2 );
66 mainLayout->addWidget( widgetBox, 0, 0, 1, -1 );
70 QGroupBox *mainToolbarBox = new QGroupBox( qtr( "Main Toolbar" ), this );
71 QGridLayout *mainTboxLayout = new QGridLayout( mainToolbarBox );
73 QLabel *label = new QLabel( qtr( "Toolbar position:" ) );
74 mainTboxLayout->addWidget(label, 0, 0, 1, 2);
76 positionCombo = new QComboBox;
77 positionCombo->addItem( qtr( "Under the Video" ), QVariant( 0 ) );
78 positionCombo->addItem( qtr( "Above the Video" ), QVariant( 1 ) );
79 positionCombo->setCurrentIndex( positionCombo->findData(
80 getSettings()->value( "MainWindow/ToolbarPos", 0 ).toInt() ) );
81 mainTboxLayout->addWidget( positionCombo, 0, 2, 1, 1 );
83 QLabel *line1Label = new QLabel( "Line 1:" );
84 QString line1 = getSettings()->value( "MainWindow/MainToolbar1",
85 MAIN_TB1_DEFAULT ).toString();
86 controller1 = new DroppingController( p_intf, line1,
88 mainTboxLayout->addWidget( line1Label, 1, 0, 1, 1 );
89 mainTboxLayout->addWidget( controller1, 1, 1, 1, 2 );
91 QLabel *line2Label = new QLabel( "Line 2:" );
92 QString line2 = getSettings()->value( "MainWindow/MainToolbar2",
93 MAIN_TB2_DEFAULT ).toString();
94 controller2 = new DroppingController( p_intf, line2,
96 mainTboxLayout->addWidget( line2Label, 2, 0, 1, 1 );
97 mainTboxLayout->addWidget( controller2, 2, 1, 1, 2);
99 /* Advanced ToolBar */
100 QLabel *advLabel = new QLabel( qtr( "Advanced Widget toolbar:" ) );
101 QString lineA = getSettings()->value( "MainWindow/AdvToolbar",
102 ADV_TB_DEFAULT ).toString();
103 controllerA = new DroppingController( p_intf, lineA,
105 mainTboxLayout->addWidget( advLabel, 3, 0, 1, 2 );
106 mainTboxLayout->addWidget( controllerA, 3, 2, 1, 1 );
108 mainLayout->addWidget( mainToolbarBox, 1, 0, 1, -1 );
111 QGroupBox *timeToolbarBox = new QGroupBox( qtr( "Time Toolbar" ) , this );
112 QGridLayout *timeTboxLayout = new QGridLayout( timeToolbarBox );
114 QString line = getSettings()->value( "MainWindow/InputToolbar",
115 INPT_TB_DEFAULT ).toString();
116 controller = new DroppingController( p_intf, line,
118 timeTboxLayout->addWidget( controller, 0, 0, 1, -1 );
120 mainLayout->addWidget( timeToolbarBox, 2, 0, 1, -1 );
123 QGroupBox *FSCToolbarBox = new QGroupBox( qtr( "Fullscreen Controller" ),
125 QGridLayout *FSCTboxLayout = new QGridLayout( FSCToolbarBox );
127 QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar",
128 FSC_TB_DEFAULT ).toString();
129 controllerFSC = new DroppingController( p_intf,
131 FSCTboxLayout->addWidget( controllerFSC, 0, 0, 1, -1 );
133 mainLayout->addWidget( FSCToolbarBox, 3, 0, 1, -1 );
136 QDialogButtonBox *okCancel = new QDialogButtonBox;
137 QPushButton *okButton = new QPushButton( qtr( "Cl&ose" ), this );
138 okButton->setDefault( true );
139 QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ), this );
140 okCancel->addButton( okButton, QDialogButtonBox::AcceptRole );
141 okCancel->addButton( cancelButton, QDialogButtonBox::RejectRole );
143 BUTTONACT( okButton, close() );
144 BUTTONACT( cancelButton, cancel() );
145 mainLayout->addWidget( okCancel, 4, 2 );
149 ToolbarEditDialog::~ToolbarEditDialog()
153 void ToolbarEditDialog::close()
155 msg_Dbg( p_intf, "Close and save" );
157 getSettings()->setValue( "MainWindow/ToolbarPos",
158 positionCombo->itemData( positionCombo->currentIndex() ).toInt() );
159 getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
160 getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
161 getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
162 getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
163 getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
166 void ToolbarEditDialog::cancel()
171 /************************************************
173 * Creation of the list of drawed lovely buttons
174 ************************************************/
175 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
176 : QListWidget( _parent )
178 /* We need the parent to know the options checked */
179 parent = qobject_cast<ToolbarEditDialog *>(_parent);
183 setViewMode( QListView::IconMode );
185 setDragEnabled( true );
186 setMinimumHeight( 250 );
188 /* All the buttons do not need a special rendering */
189 for( int i = 0; i < BUTTON_MAX; i++ )
191 QListWidgetItem *widgetItem = new QListWidgetItem( this );
192 widgetItem->setText( nameL[i] );
193 widgetItem->setIcon( QIcon( iconL[i] ) );
194 widgetItem->setData( Qt::UserRole, QVariant( i ) );
195 addItem( widgetItem );
198 /* Spacers are yet again a different thing */
199 QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
200 qtr( "Spacer" ), this );
201 widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
202 addItem( widgetItem );
204 widgetItem = new QListWidgetItem( QIcon( ":/space" ),
205 qtr( "Expanding Spacer" ), this );
206 widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
207 addItem( widgetItem );
210 * For all other widgets, we create then, do a pseudo rendering in
211 * a pixmaps for the view, and delete the object
213 * A lot of code is retaken from the Abstract, but not exactly...
215 * They are better ways to deal with this, but I doubt that this is
216 * necessary. If you feel like you have the time, be my guest.
220 for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
222 QWidget *widget = NULL;
223 QListWidgetItem *widgetItem = new QListWidgetItem( this );
228 QFrame *line = new QFrame( this );
229 line->setFrameShape( QFrame::VLine );
230 line->setFrameShadow( QFrame::Raised );
231 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
234 widgetItem->setText( qtr("Splitter") );
238 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
241 widgetItem->setText( qtr("Time Slider") );
245 SoundWidget *snd = new SoundWidget( this, p_intf,
246 parent->getOptions() & WIDGET_SHINY );
249 widgetItem->setText( qtr("Volume") );
253 QListWidgetItem *widgetItem = new QListWidgetItem( this );
254 widgetItem->setText( "Small Volume" );
255 widgetItem->setIcon( QIcon( ":/volume-medium" ) );
256 widgetItem->setData( Qt::UserRole, QVariant( i ) );
257 addItem( widgetItem );
262 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
265 widgetItem->setText( qtr("Time") );
269 QWidget *discFrame = new QWidget( this );
270 //discFrame->setLineWidth( 1 );
271 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
272 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
274 QToolButton *prevSectionButton = new QToolButton( discFrame );
275 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
276 discLayout->addWidget( prevSectionButton );
278 QToolButton *menuButton = new QToolButton( discFrame );
279 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
280 discLayout->addWidget( menuButton );
282 QToolButton *nextButton = new QToolButton( discFrame );
283 nextButton->setIcon( QIcon( ":/dvd_next" ) );
284 discLayout->addWidget( nextButton );
288 widgetItem->setText( qtr("DVD menus") );
290 case TELETEXT_BUTTONS:
292 QWidget *telexFrame = new QWidget( this );
293 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
294 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
296 QToolButton *telexOn = new QToolButton( telexFrame );
297 telexOn->setIcon( QIcon( ":/tv" ) );
298 telexLayout->addWidget( telexOn );
300 QToolButton *telexTransparent = new QToolButton;
301 telexOn->setIcon( QIcon( ":/tvtelx" ) );
302 telexLayout->addWidget( telexTransparent );
304 QSpinBox *telexPage = new QSpinBox;
305 telexLayout->addWidget( telexPage );
309 widgetItem->setText( qtr("Teletext") );
311 case ADVANCED_CONTROLLER:
313 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
314 widget = advControls;
316 widgetItem->setText( qtr("Advanced Buttons") );
319 msg_Warn( p_intf, "This should not happen %i", i );
323 if( widget == NULL ) continue;
326 widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
328 widgetItem->setData( Qt::UserRole, QVariant( i ) );
330 addItem( widgetItem );
335 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
337 QListWidgetItem *item = currentItem();
340 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
342 int i_type = item->data( Qt::UserRole ).toInt();
343 int i_option = parent->getOptions();
344 dataStream << i_type << i_option;
346 /* Create a new dragging event */
347 QDrag *drag = new QDrag( this );
349 /* With correct mimedata */
350 QMimeData *mimeData = new QMimeData;
351 mimeData->setData( "vlc/button-bar", itemData );
352 drag->setMimeData( mimeData );
354 /* And correct pixmap */
355 QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
356 drag->setPixmap( aPixmap );
357 drag->setHotSpot( QPoint( 20, 20 ) );
359 /* We want to keep a copy */
360 drag->exec( Qt::CopyAction | Qt::MoveAction );
364 * The special controller with drag'n drop abilities.
365 * We don't do this in the main controller, since we don't want the OverHead
366 * to propagate there too
368 DroppingController::DroppingController( intf_thread_t *_p_intf,
371 : AbstractController( _p_intf, _parent )
375 setAcceptDrops( true );
376 controlLayout = new QHBoxLayout( this );
377 controlLayout->setSpacing( 5 );
378 controlLayout->setMargin( 0 );
379 setFrameShape( QFrame::StyledPanel );
380 setFrameShadow( QFrame::Raised );
382 parseAndCreate( line, controlLayout );
385 /* Overloading the AbstractController one, because we don't manage the
386 Spacing items in the same ways */
387 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
392 /* Special case for SPACERS, who aren't QWidgets */
393 if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
395 QLabel *label = new QLabel;
396 label->setPixmap( QPixmap( ":/space" ) );
397 if( i_type == WIDGET_SPACER_EXTEND )
399 label->setSizePolicy( QSizePolicy::MinimumExpanding,
400 QSizePolicy::Preferred );
402 /* Create a box around it */
403 label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
404 label->setLineWidth ( 1 );
405 label->setAlignment( Qt::AlignCenter );
408 label->setSizePolicy( QSizePolicy::Fixed,
409 QSizePolicy::Preferred );
411 /* Install event Filter for drag'n drop */
412 label->installEventFilter( this );
413 controlLayout->insertWidget( i_index, label );
419 QWidget *widg = createWidget( i_type, i_option );
422 /* Install the Event Filter in order to catch the drag */
423 widg->installEventFilter( this );
425 /* We are in a complex widget, we need to stop events on children too */
426 if( i_type >= VOLUME && i_type < SPECIAL_MAX )
428 QList<QObject *>children = widg->children();
431 foreach( child, children )
434 if( childWidg = qobject_cast<QWidget *>( child ) )
436 child->installEventFilter( this );
437 childWidg->setEnabled( true );
441 /* Decorating the frames when possible */
443 if( i_type >= MENU_BUTTONS /* Don't bother to check for volume */
444 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
446 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
447 frame->setLineWidth ( 1 );
451 /* Some Widgets are deactivated at creation */
452 widg->setEnabled( true );
454 controlLayout->insertWidget( i_index, widg );
457 /* QList and QBoxLayout don't act the same with insert() */
458 if( i_index < 0 ) i_index = controlLayout->count() - 1;
460 /* Insert in the value listing */
461 doubleInt *value = new doubleInt;
462 value->i_type = i_type;
463 value->i_option = i_option;
464 widgetList.insert( i_index, value );
467 DroppingController::~DroppingController()
469 qDeleteAll( widgetList );
473 QString DroppingController::getValue()
477 for( int i = 0; i < controlLayout->count(); i++ )
479 doubleInt *dI = widgetList.at( i );
482 qs.append( QString::number( dI->i_type ) );
483 if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
489 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
491 if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
497 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
499 QPoint origin = event->pos();
501 int i_pos = getParentPosInLayout( origin );
504 /* Both sides of the frame */
507 if( rubberband ) rubberband->hide();
511 /* Last item is special because of underlying items */
512 if( i_pos >= controlLayout->count() )
518 /* Query the underlying item for size && middles */
519 QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
520 QWidget *temp = tempItem->widget(); assert( temp );
522 /* Position assignment */
524 origin.rx() = temp->x() - 2;
526 if( b_end ) origin.rx() += temp->width();
529 rubberband = new QRubberBand( QRubberBand::Line, this );
530 rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
534 inline int DroppingController::getParentPosInLayout( QPoint point )
536 point.ry() = height() / 2 ;
537 QPoint origin = mapToGlobal ( point );
539 QWidget *tempWidg = QApplication::widgetAt( origin );
542 if( tempWidg != NULL)
544 i = controlLayout->indexOf( tempWidg );
547 i = controlLayout->indexOf( tempWidg->parentWidget() );
548 tempWidg = tempWidg->parentWidget();
552 /* Return the nearest position */
553 if( ( point.x() - tempWidg->x() > tempWidg->width() / 2 ) && i != -1 )
556 // msg_Dbg( p_intf, "%i", i);
560 void DroppingController::dropEvent( QDropEvent *event )
562 int i = getParentPosInLayout( event->pos() );
564 QByteArray data = event->mimeData()->data( "vlc/button-bar" );
565 QDataStream dataStream(&data, QIODevice::ReadOnly);
567 int i_option = 0, i_type = 0;
568 dataStream >> i_type >> i_option;
570 createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
572 /* Hide by precaution, you don't exactly know what could have happened in
574 if( rubberband ) rubberband->hide();
577 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
579 if( rubberband ) rubberband->hide();
584 * Overloading doAction to block any action
586 void DroppingController::doAction( int i )
591 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
593 switch( event->type() )
595 case QEvent::MouseButtonPress:
598 case QEvent::MouseButtonRelease:
601 case QEvent::MouseMove:
603 if( !b_draging ) return true;
604 QWidget *widg = static_cast<QWidget*>(obj);
607 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
610 i = controlLayout->indexOf( widg );
613 i = controlLayout->indexOf( widg->parentWidget() );
614 widg = widg->parentWidget();
615 /* NOTE: be extra-careful Now with widg access */
618 if( i == -1 ) return true;
619 doubleInt *dI = widgetList.at( i );
621 int i_type = dI->i_type;
622 int i_option = dI->i_option;
623 dataStream << i_type << i_option;
625 /* With correct mimedata */
626 QMimeData *mimeData = new QMimeData;
627 mimeData->setData( "vlc/button-bar", itemData );
629 QDrag *drag = new QDrag( widg );
630 drag->setMimeData( mimeData );
632 /* Start the effective drag */
633 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
635 widgetList.removeAt( i );
636 controlLayout->removeWidget( widg );
642 case QEvent::MouseButtonDblClick:
643 case QEvent::EnabledChange:
645 case QEvent::HideToParent:
647 case QEvent::ZOrderChange: