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 positionCombo->setCurrentIndex( positionCombo->findData(
81 getSettings()->value( "MainWindow/ToolbarPos", 0 ).toInt() ) );
82 mainTboxLayout->addWidget( positionCombo, 0, 2, 1, 1 );
84 QLabel *line1Label = new QLabel( "Line 1:" );
85 QString line1 = getSettings()->value( "MainWindow/MainToolbar1",
86 MAIN_TB1_DEFAULT ).toString();
87 controller1 = new DroppingController( p_intf, line1,
89 mainTboxLayout->addWidget( line1Label, 1, 0, 1, 1 );
90 mainTboxLayout->addWidget( controller1, 1, 1, 1, 2 );
92 QLabel *line2Label = new QLabel( "Line 2:" );
93 QString line2 = getSettings()->value( "MainWindow/MainToolbar2",
94 MAIN_TB2_DEFAULT ).toString();
95 controller2 = new DroppingController( p_intf, line2,
97 mainTboxLayout->addWidget( line2Label, 2, 0, 1, 1 );
98 mainTboxLayout->addWidget( controller2, 2, 1, 1, 2);
100 /* Advanced ToolBar */
101 QLabel *advLabel = new QLabel( qtr( "Advanced Widget toolbar:" ) );
102 QString lineA = getSettings()->value( "MainWindow/AdvToolbar",
103 ADV_TB_DEFAULT ).toString();
104 controllerA = new DroppingController( p_intf, lineA,
106 mainTboxLayout->addWidget( advLabel, 3, 0, 1, 2 );
107 mainTboxLayout->addWidget( controllerA, 3, 2, 1, 1 );
109 mainLayout->addWidget( mainToolbarBox, 1, 0, 1, -1 );
112 QGroupBox *timeToolbarBox = new QGroupBox( qtr( "Time Toolbar" ) , this );
113 QGridLayout *timeTboxLayout = new QGridLayout( timeToolbarBox );
115 QString line = getSettings()->value( "MainWindow/InputToolbar",
116 INPT_TB_DEFAULT ).toString();
117 controller = new DroppingController( p_intf, line,
119 timeTboxLayout->addWidget( controller, 0, 0, 1, -1 );
121 mainLayout->addWidget( timeToolbarBox, 2, 0, 1, -1 );
124 QGroupBox *FSCToolbarBox = new QGroupBox( qtr( "Fullscreen Controller" ),
126 QGridLayout *FSCTboxLayout = new QGridLayout( FSCToolbarBox );
128 QString lineFSC = getSettings()->value( "MainWindow/FSCtoolbar",
129 FSC_TB_DEFAULT ).toString();
130 controllerFSC = new DroppingController( p_intf,
132 FSCTboxLayout->addWidget( controllerFSC, 0, 0, 1, -1 );
134 mainLayout->addWidget( FSCToolbarBox, 3, 0, 1, -1 );
137 QDialogButtonBox *okCancel = new QDialogButtonBox;
138 QPushButton *okButton = new QPushButton( qtr( "Cl&ose" ), this );
139 okButton->setDefault( true );
140 QPushButton *cancelButton = new QPushButton( qtr( "&Cancel" ), this );
141 okCancel->addButton( okButton, QDialogButtonBox::AcceptRole );
142 okCancel->addButton( cancelButton, QDialogButtonBox::RejectRole );
144 BUTTONACT( okButton, close() );
145 BUTTONACT( cancelButton, cancel() );
146 mainLayout->addWidget( okCancel, 4, 2 );
150 ToolbarEditDialog::~ToolbarEditDialog()
154 void ToolbarEditDialog::close()
156 msg_Dbg( p_intf, "Close and save" );
158 getSettings()->setValue( "MainWindow/ToolbarPos",
159 positionCombo->itemData( positionCombo->currentIndex() ).toInt() );
160 getSettings()->setValue( "MainWindow/MainToolbar1", controller1->getValue() );
161 getSettings()->setValue( "MainWindow/MainToolbar2", controller2->getValue() );
162 getSettings()->setValue( "MainWindow/AdvToolbar", controllerA->getValue() );
163 getSettings()->setValue( "MainWindow/InputToolbar", controller->getValue() );
164 getSettings()->setValue( "MainWindow/FSCtoolbar", controllerFSC->getValue() );
167 void ToolbarEditDialog::cancel()
172 /************************************************
174 * Creation of the list of drawed lovely buttons
175 ************************************************/
176 WidgetListing::WidgetListing( intf_thread_t *p_intf, QWidget *_parent )
177 : QListWidget( _parent )
179 /* We need the parent to know the options checked */
180 parent = qobject_cast<ToolbarEditDialog *>(_parent);
184 setViewMode( QListView::IconMode );
186 setDragEnabled( true );
187 setMinimumHeight( 250 );
189 /* All the buttons do not need a special rendering */
190 for( int i = 0; i < BUTTON_MAX; i++ )
192 QListWidgetItem *widgetItem = new QListWidgetItem( this );
193 widgetItem->setText( nameL[i] );
194 widgetItem->setIcon( QIcon( iconL[i] ) );
195 widgetItem->setData( Qt::UserRole, QVariant( i ) );
196 addItem( widgetItem );
199 /* Spacers are yet again a different thing */
200 QListWidgetItem *widgetItem = new QListWidgetItem( QIcon( ":/space" ),
201 qtr( "Spacer" ), this );
202 widgetItem->setData( Qt::UserRole, WIDGET_SPACER );
203 addItem( widgetItem );
205 widgetItem = new QListWidgetItem( QIcon( ":/space" ),
206 qtr( "Expanding Spacer" ), this );
207 widgetItem->setData( Qt::UserRole, WIDGET_SPACER_EXTEND );
208 addItem( widgetItem );
211 * For all other widgets, we create then, do a pseudo rendering in
212 * a pixmaps for the view, and delete the object
214 * A lot of code is retaken from the Abstract, but not exactly...
216 * They are better ways to deal with this, but I doubt that this is
217 * necessary. If you feel like you have the time, be my guest.
221 for( int i = SPLITTER; i < SPECIAL_MAX; i++ )
223 QWidget *widget = NULL;
224 QListWidgetItem *widgetItem = new QListWidgetItem( this );
229 QFrame *line = new QFrame( this );
230 line->setFrameShape( QFrame::VLine );
231 line->setFrameShadow( QFrame::Raised );
232 line->setLineWidth( 0 ); line->setMidLineWidth( 1 );
235 widgetItem->setText( qtr("Splitter") );
239 InputSlider *slider = new InputSlider( Qt::Horizontal, this );
242 widgetItem->setText( qtr("Time Slider") );
246 SoundWidget *snd = new SoundWidget( this, p_intf,
247 parent->getOptions() & WIDGET_SHINY );
250 widgetItem->setText( qtr("Volume") );
254 QLabel *timeLabel = new QLabel( "12:42/2:12:42", this );
257 widgetItem->setText( qtr("Time") );
261 QWidget *discFrame = new QWidget( this );
262 //discFrame->setLineWidth( 1 );
263 QHBoxLayout *discLayout = new QHBoxLayout( discFrame );
264 discLayout->setSpacing( 0 ); discLayout->setMargin( 0 );
266 QToolButton *prevSectionButton = new QToolButton( discFrame );
267 prevSectionButton->setIcon( QIcon( ":/dvd_prev" ) );
268 discLayout->addWidget( prevSectionButton );
270 QToolButton *menuButton = new QToolButton( discFrame );
271 menuButton->setIcon( QIcon( ":/dvd_menu" ) );
272 discLayout->addWidget( menuButton );
274 QToolButton *nextButton = new QToolButton( discFrame );
275 nextButton->setIcon( QIcon( ":/dvd_next" ) );
276 discLayout->addWidget( nextButton );
280 widgetItem->setText( qtr("DVD menus") );
282 case TELETEXT_BUTTONS:
284 QWidget *telexFrame = new QWidget( this );
285 QHBoxLayout *telexLayout = new QHBoxLayout( telexFrame );
286 telexLayout->setSpacing( 0 ); telexLayout->setMargin( 0 );
288 QToolButton *telexOn = new QToolButton( telexFrame );
289 telexOn->setIcon( QIcon( ":/tv" ) );
290 telexLayout->addWidget( telexOn );
292 QToolButton *telexTransparent = new QToolButton;
293 telexOn->setIcon( QIcon( ":/tvtelx-trans" ) );
294 telexLayout->addWidget( telexTransparent );
296 QSpinBox *telexPage = new QSpinBox;
297 telexLayout->addWidget( telexPage );
301 widgetItem->setText( qtr("Teletext") );
303 case ADVANCED_CONTROLLER:
305 AdvControlsWidget *advControls = new AdvControlsWidget( p_intf, this );
306 widget = advControls;
308 widgetItem->setText( qtr("Advanced Buttons") );
311 msg_Warn( p_intf, "This should not happen %i", i );
315 if( widget == NULL ) continue;
318 widgetItem->setIcon( QIcon( QPixmap::grabWidget( widget ) ) );
320 widgetItem->setData( Qt::UserRole, QVariant( i ) );
322 addItem( widgetItem );
327 void WidgetListing::startDrag( Qt::DropActions /*supportedActions*/ )
329 QListWidgetItem *item = currentItem();
332 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
334 int i_type = item->data( Qt::UserRole ).toInt();
335 int i_option = parent->getOptions();
336 dataStream << i_type << i_option;
338 /* Create a new dragging event */
339 QDrag *drag = new QDrag( this );
341 /* With correct mimedata */
342 QMimeData *mimeData = new QMimeData;
343 mimeData->setData( "vlc/button-bar", itemData );
344 drag->setMimeData( mimeData );
346 /* And correct pixmap */
347 QPixmap aPixmap = item->icon().pixmap( QSize( 22, 22 ) );
348 drag->setPixmap( aPixmap );
349 drag->setHotSpot( QPoint( 20, 20 ) );
351 /* We want to keep a copy */
352 drag->exec( Qt::CopyAction | Qt::MoveAction );
356 * The special controller with drag'n drop abilities.
357 * We don't do this in the main controller, since we don't want the OverHead
358 * to propagate there too
360 DroppingController::DroppingController( intf_thread_t *_p_intf,
363 : AbstractController( _p_intf, _parent )
367 setAcceptDrops( true );
368 controlLayout = new QHBoxLayout( this );
369 controlLayout->setSpacing( 5 );
370 controlLayout->setMargin( 0 );
371 setFrameShape( QFrame::StyledPanel );
372 setFrameShadow( QFrame::Raised );
374 parseAndCreate( line, controlLayout );
377 /* Overloading the AbstractController one, because we don't manage the
378 Spacing items in the same ways */
379 void DroppingController::createAndAddWidget( QBoxLayout *controlLayout,
384 /* Special case for SPACERS, who aren't QWidgets */
385 if( i_type == WIDGET_SPACER || i_type == WIDGET_SPACER_EXTEND )
387 QLabel *label = new QLabel;
388 label->setPixmap( QPixmap( ":/space" ) );
389 if( i_type == WIDGET_SPACER_EXTEND )
391 label->setSizePolicy( QSizePolicy::MinimumExpanding,
392 QSizePolicy::Preferred );
394 /* Create a box around it */
395 label->setFrameStyle( QFrame::Panel | QFrame::Sunken );
396 label->setLineWidth ( 1 );
397 label->setAlignment( Qt::AlignCenter );
400 label->setSizePolicy( QSizePolicy::Fixed,
401 QSizePolicy::Preferred );
403 /* Install event Filter for drag'n drop */
404 label->installEventFilter( this );
405 controlLayout->insertWidget( i_index, label );
411 QWidget *widg = createWidget( i_type, i_option );
414 /* Install the Event Filter in order to catch the drag */
415 widg->installEventFilter( this );
417 /* We are in a complex widget, we need to stop events on children too */
418 if( i_type >= VOLUME && i_type < SPECIAL_MAX )
420 QList<QObject *>children = widg->children();
423 foreach( child, children )
426 if( childWidg = qobject_cast<QWidget *>( child ) )
428 child->installEventFilter( this );
429 childWidg->setEnabled( true );
433 /* Decorating the frames when possible */
435 if( i_type >= MENU_BUTTONS /* Don't bother to check for volume */
436 && ( frame = qobject_cast<QFrame *>( widg ) ) != NULL )
438 frame->setFrameStyle( QFrame::Panel | QFrame::Raised );
439 frame->setLineWidth ( 1 );
443 /* Some Widgets are deactivated at creation */
444 widg->setEnabled( true );
446 controlLayout->insertWidget( i_index, widg );
449 /* QList and QBoxLayout don't act the same with insert() */
450 if( i_index < 0 ) i_index = controlLayout->count() - 1;
452 /* Insert in the value listing */
453 doubleInt *value = new doubleInt;
454 value->i_type = i_type;
455 value->i_option = i_option;
456 widgetList.insert( i_index, value );
459 DroppingController::~DroppingController()
461 qDeleteAll( widgetList );
465 QString DroppingController::getValue()
469 for( int i = 0; i < controlLayout->count(); i++ )
471 doubleInt *dI = widgetList.at( i );
474 qs.append( QString::number( dI->i_type ) );
475 if( dI->i_option ) qs.append( "-" + QString::number( dI->i_option ) );
481 void DroppingController::dragEnterEvent( QDragEnterEvent * event )
483 if( event->mimeData()->hasFormat( "vlc/button-bar" ) )
489 void DroppingController::dragMoveEvent( QDragMoveEvent *event )
491 QPoint origin = event->pos();
493 int i_pos = getParentPosInLayout( origin );
496 /* Both sides of the frame */
499 if( rubberband ) rubberband->hide();
503 /* Last item is special because of underlying items */
504 if( i_pos >= controlLayout->count() )
510 /* Query the underlying item for size && middles */
511 QLayoutItem *tempItem = controlLayout->itemAt( i_pos ); assert( tempItem );
512 QWidget *temp = tempItem->widget(); assert( temp );
514 /* Position assignment */
516 origin.rx() = temp->x() - 2;
518 if( b_end ) origin.rx() += temp->width();
521 rubberband = new QRubberBand( QRubberBand::Line, this );
522 rubberband->setGeometry( origin.x(), origin.y(), 4, height() );
526 inline int DroppingController::getParentPosInLayout( QPoint point )
528 point.ry() = height() / 2 ;
529 QPoint origin = mapToGlobal ( point );
531 QWidget *tempWidg = QApplication::widgetAt( origin );
534 if( tempWidg != NULL)
536 i = controlLayout->indexOf( tempWidg );
539 i = controlLayout->indexOf( tempWidg->parentWidget() );
540 tempWidg = tempWidg->parentWidget();
544 /* Return the nearest position */
545 if( ( point.x() - tempWidg->x() > tempWidg->width() / 2 ) && i != -1 )
548 // msg_Dbg( p_intf, "%i", i);
552 void DroppingController::dropEvent( QDropEvent *event )
554 int i = getParentPosInLayout( event->pos() );
556 QByteArray data = event->mimeData()->data( "vlc/button-bar" );
557 QDataStream dataStream(&data, QIODevice::ReadOnly);
559 int i_option = 0, i_type = 0;
560 dataStream >> i_type >> i_option;
562 createAndAddWidget( controlLayout, i, (buttonType_e)i_type, i_option );
564 /* Hide by precaution, you don't exactly know what could have happened in
566 if( rubberband ) rubberband->hide();
569 void DroppingController::dragLeaveEvent ( QDragLeaveEvent * event )
571 if( rubberband ) rubberband->hide();
576 * Overloading doAction to block any action
578 void DroppingController::doAction( int i )
583 bool DroppingController::eventFilter( QObject *obj, QEvent *event )
585 switch( event->type() )
587 case QEvent::MouseButtonPress:
590 case QEvent::MouseButtonRelease:
593 case QEvent::MouseMove:
595 if( !b_draging ) return true;
596 QWidget *widg = static_cast<QWidget*>(obj);
599 QDataStream dataStream( &itemData, QIODevice::WriteOnly );
602 i = controlLayout->indexOf( widg );
605 i = controlLayout->indexOf( widg->parentWidget() );
606 widg = widg->parentWidget();
607 /* NOTE: be extra-careful Now with widg access */
610 if( i == -1 ) return true;
611 doubleInt *dI = widgetList.at( i );
613 int i_type = dI->i_type;
614 int i_option = dI->i_option;
615 dataStream << i_type << i_option;
617 /* With correct mimedata */
618 QMimeData *mimeData = new QMimeData;
619 mimeData->setData( "vlc/button-bar", itemData );
621 QDrag *drag = new QDrag( widg );
622 drag->setMimeData( mimeData );
624 /* Start the effective drag */
625 drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::MoveAction);
627 widgetList.removeAt( i );
628 controlLayout->removeWidget( widg );
634 case QEvent::EnabledChange:
636 case QEvent::HideToParent:
638 case QEvent::ZOrderChange: