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 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
256 widgetItem->setText( qtr("Time") );
260 QWidget *discFrame = new QWidget( this );
261 //discFrame->setLineWidth( 1 );
262 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
263 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
265 QToolButton *prevSectionButton = new QToolButton( discFrame );
266 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
267 discLayout->addWidget( prevSectionButton );
269 QToolButton *menuButton = new QToolButton( discFrame );
270 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
271 discLayout->addWidget( menuButton );
273 QToolButton *nextButton = new QToolButton( discFrame );
274 nextButton->setIcon( QIcon( ":/dvd_next" ) );
275 discLayout->addWidget( nextButton );
279 widgetItem->setText( qtr("DVD menus") );
281 case TELETEXT_BUTTONS:
283 QWidget *telexFrame = new QWidget( this );
284 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
285 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
287 QToolButton *telexOn = new QToolButton( telexFrame );
288 telexOn->setIcon( QIcon( ":/tv" ) );
289 telexLayout->addWidget( telexOn );
291 QToolButton *telexTransparent = new QToolButton;
292 telexOn->setIcon( QIcon( ":/tvtelx-trans" ) );
293 telexLayout->addWidget( telexTransparent );
295 QSpinBox *telexPage = new QSpinBox;
296 telexLayout->addWidget( telexPage );
300 widgetItem->setText( qtr("Teletext") );
302 case ADVANCED_CONTROLLER:
304 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
305 widget = advControls;
307 widgetItem->setText( qtr("Advanced Buttons") );
310 msg_Warn( p_intf, "This should not happen %i", i );
314 if( widget == NULL ) continue;
317 widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
319 widgetItem->setData( Qt::UserRole, QVariant( i ) );
321 addItem( widgetItem );
326 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
328 QListWidgetItem *item = currentItem();
331 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
333 int i_type = item->data( Qt::UserRole ).toInt();
334 int i_option = parent->getOptions();
335 dataStream << i_type << i_option;
337 /* Create a new dragging event */
338 QDrag *drag = new QDrag( this );
340 /* With correct mimedata */
341 QMimeData *mimeData = new QMimeData;
342 mimeData->setData( "vlc/button-bar", itemData );
343 drag->setMimeData( mimeData );
345 /* And correct pixmap */
346 QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
347 drag->setPixmap( aPixmap );
348 drag->setHotSpot( QPoint( 20, 20 ) );
350 /* We want to keep a copy */
351 drag->exec( Qt::CopyAction | Qt::MoveAction );
355 * The special controller with drag'n drop abilities.
356 * We don't do this in the main controller, since we don't want the OverHead
357 * to propagate there too
359 DroppingController::DroppingController( intf_thread_t *_p_intf,
362 : AbstractController( _p_intf, _parent )
366 setAcceptDrops( true );
367 controlLayout = new QHBoxLayout( this );
368 controlLayout->setSpacing( 5 );
369 controlLayout->setMargin( 0 );
370 setFrameShape( QFrame::StyledPanel );
371 setFrameShadow( QFrame::Raised );
373 parseAndCreate( line, controlLayout );
376 /* Overloading the AbstractController one, because we don't manage the
377 Spacing items in the same ways */
378 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
383 /* Special case for SPACERS, who aren't QWidgets */
384 if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
386 QLabel *label = new QLabel;
387 label->setPixmap( QPixmap( ":/space" ) );
388 if( i_type == WIDGET_SPACER_EXTEND )
390 label->setSizePolicy( QSizePolicy::MinimumExpanding,
391 QSizePolicy::Preferred );
393 /* Create a box around it */
394 label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
395 label->setLineWidth ( 1 );
396 label->setAlignment( Qt::AlignCenter );
399 label->setSizePolicy( QSizePolicy::Fixed,
400 QSizePolicy::Preferred );
402 /* Install event Filter for drag'n drop */
403 label->installEventFilter( this );
404 controlLayout->insertWidget( i_index, label );
410 QWidget *widg = createWidget( i_type, i_option );
413 /* Install the Event Filter in order to catch the drag */
414 widg->installEventFilter( this );
416 /* We are in a complex widget, we need to stop events on children too */
417 if( i_type >= VOLUME && i_type < SPECIAL_MAX )
419 QList<QObject *>children = widg->children();
422 foreach( child, children )
425 if( childWidg = qobject_cast<QWidget *>( child ) )
427 child->installEventFilter( this );
428 childWidg->setEnabled( true );
432 /* Decorating the frames when possible */
434 if( i_type >= MENU_BUTTONS /* Don't bother to check for volume */
435 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
437 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
438 frame->setLineWidth ( 1 );
442 /* Some Widgets are deactivated at creation */
443 widg->setEnabled( true );
445 controlLayout->insertWidget( i_index, widg );
448 /* QList and QBoxLayout don't act the same with insert() */
449 if( i_index < 0 ) i_index = controlLayout->count() - 1;
451 /* Insert in the value listing */
452 doubleInt *value = new doubleInt;
453 value->i_type = i_type;
454 value->i_option = i_option;
455 widgetList.insert( i_index, value );
458 DroppingController::~DroppingController()
460 qDeleteAll( widgetList );
464 QString DroppingController::getValue()
468 for( int i = 0; i < controlLayout->count(); i++ )
470 doubleInt *dI = widgetList.at( i );
473 qs.append( QString::number( dI->i_type ) );
474 if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
480 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
482 if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
488 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
490 QPoint origin = event->pos();
492 int i_pos = getParentPosInLayout( origin );
495 /* Both sides of the frame */
498 if( rubberband ) rubberband->hide();
502 /* Last item is special because of underlying items */
503 if( i_pos >= controlLayout->count() )
509 /* Query the underlying item for size && middles */
510 QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
511 QWidget *temp = tempItem->widget(); assert( temp );
513 /* Position assignment */
515 origin.rx() = temp->x() - 2;
517 if( b_end ) origin.rx() += temp->width();
520 rubberband = new QRubberBand( QRubberBand::Line, this );
521 rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
525 inline int DroppingController::getParentPosInLayout( QPoint point )
527 point.ry() = height() / 2 ;
528 QPoint origin = mapToGlobal ( point );
530 QWidget *tempWidg = QApplication::widgetAt( origin );
533 if( tempWidg != NULL)
535 i = controlLayout->indexOf( tempWidg );
538 i = controlLayout->indexOf( tempWidg->parentWidget() );
539 tempWidg = tempWidg->parentWidget();
543 /* Return the nearest position */
544 if( ( point.x() - tempWidg->x() > tempWidg->width() / 2 ) && i != -1 )
547 // msg_Dbg( p_intf, "%i", i);
551 void DroppingController::dropEvent( QDropEvent *event )
553 int i = getParentPosInLayout( event->pos() );
555 QByteArray data = event->mimeData()->data( "vlc/button-bar" );
556 QDataStream dataStream(&data, QIODevice::ReadOnly);
558 int i_option = 0, i_type = 0;
559 dataStream >> i_type >> i_option;
561 createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
563 /* Hide by precaution, you don't exactly know what could have happened in
565 if( rubberband ) rubberband->hide();
568 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
570 if( rubberband ) rubberband->hide();
575 * Overloading doAction to block any action
577 void DroppingController::doAction( int i )
582 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
584 switch( event->type() )
586 case QEvent::MouseButtonPress:
589 case QEvent::MouseButtonRelease:
592 case QEvent::MouseMove:
594 if( !b_draging ) return true;
595 QWidget *widg = static_cast<QWidget*>(obj);
598 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
601 i = controlLayout->indexOf( widg );
604 i = controlLayout->indexOf( widg->parentWidget() );
605 widg = widg->parentWidget();
606 /* NOTE: be extra-careful Now with widg access */
609 if( i == -1 ) return true;
610 doubleInt *dI = widgetList.at( i );
612 int i_type = dI->i_type;
613 int i_option = dI->i_option;
614 dataStream << i_type << i_option;
616 /* With correct mimedata */
617 QMimeData *mimeData = new QMimeData;
618 mimeData->setData( "vlc/button-bar", itemData );
620 QDrag *drag = new QDrag( widg );
621 drag->setMimeData( mimeData );
623 /* Start the effective drag */
624 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
626 widgetList.removeAt( i );
627 controlLayout->removeWidget( widg );
633 case QEvent::MouseButtonDblClick:
634 case QEvent::EnabledChange:
636 case QEvent::HideToParent:
638 case QEvent::ZOrderChange: