]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/plugins.cpp
Qt: kill unused_parameters warnings
[vlc] / modules / gui / qt4 / dialogs / plugins.cpp
1 /*****************************************************************************
2  * plugins.hpp : Plug-ins and extensions listing
3  ****************************************************************************
4  * Copyright (C) 2008-2010 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
8  *          Jean-Philippe AndrĂ© <jpeg (at) videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "plugins.hpp"
30
31 #include "util/searchlineedit.hpp"
32 #include "extensions_manager.hpp"
33
34 #include <assert.h>
35
36 #include <vlc_modules.h>
37
38 #include <QTreeWidget>
39 #include <QStringList>
40 #include <QTabWidget>
41 #include <QHeaderView>
42 #include <QDialogButtonBox>
43 #include <QLineEdit>
44 #include <QLabel>
45 #include <QVBoxLayout>
46 #include <QComboBox>
47 #include <QHBoxLayout>
48 #include <QVBoxLayout>
49 #include <QSpacerItem>
50 #include <QListView>
51 #include <QPainter>
52 #include <QStyleOptionViewItem>
53 #include <QKeyEvent>
54 #include <QPushButton>
55 #include <QPixmap>
56
57 static QPixmap *loadPixmapFromData( char *, int size );
58
59
60 PluginDialog::PluginDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
61 {
62     setWindowTitle( qtr( "Plugins and extensions" ) );
63     setWindowRole( "vlc-plugins" );
64
65     QVBoxLayout *layout = new QVBoxLayout( this );
66     tabs = new QTabWidget( this );
67     tabs->addTab( extensionTab = new ExtensionTab( p_intf ),
68                   qtr( "Extensions" ) );
69     tabs->addTab( pluginTab = new PluginTab( p_intf ),
70                   qtr( "Plugins" ) );
71     layout->addWidget( tabs );
72
73     QDialogButtonBox *box = new QDialogButtonBox;
74     QPushButton *okButton = new QPushButton( qtr( "&Close" ), this );
75     box->addButton( okButton, QDialogButtonBox::AcceptRole );
76     layout->addWidget( box );
77     BUTTONACT( okButton, close() );
78     readSettings( "PluginsDialog", QSize( 435, 280 ) );
79 }
80
81 PluginDialog::~PluginDialog()
82 {
83     writeSettings( "PluginsDialog" );
84 }
85
86 /* Plugins tab */
87
88 PluginTab::PluginTab( intf_thread_t *p_intf )
89         : QVLCFrame( p_intf )
90 {
91     QGridLayout *layout = new QGridLayout( this );
92
93     /* Main Tree for modules */
94     treePlugins = new QTreeWidget;
95     layout->addWidget( treePlugins, 0, 0, 1, -1 );
96
97     /* Users cannot move the columns around but we need to sort */
98     treePlugins->header()->setMovable( false );
99     treePlugins->header()->setSortIndicatorShown( true );
100     //    treePlugins->header()->setResizeMode( QHeaderView::ResizeToContents );
101     treePlugins->setAlternatingRowColors( true );
102     treePlugins->setColumnWidth( 0, 200 );
103
104     QStringList headerNames;
105     headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
106     treePlugins->setHeaderLabels( headerNames );
107
108     FillTree();
109
110     /* Set capability column to the correct Size*/
111     treePlugins->resizeColumnToContents( 1 );
112     treePlugins->header()->restoreState(
113             getSettings()->value( "Plugins/Header-State" ).toByteArray() );
114
115     treePlugins->setSortingEnabled( true );
116     treePlugins->sortByColumn( 1, Qt::AscendingOrder );
117
118     QLabel *label = new QLabel( qtr("&Search:"), this );
119     edit = new SearchLineEdit( this );
120     label->setBuddy( edit );
121
122     layout->addWidget( label, 1, 0 );
123     layout->addWidget( edit, 1, 1, 1, 1 );
124     CONNECT( edit, textChanged( const QString& ),
125             this, search( const QString& ) );
126
127     setMinimumSize( 500, 300 );
128     readSettings( "Plugins", QSize( 540, 400 ) );
129 }
130
131 inline void PluginTab::FillTree()
132 {
133     module_t **p_list = module_list_get( NULL );
134     module_t *p_module;
135
136     for( unsigned int i = 0; (p_module = p_list[i] ) != NULL; i++ )
137     {
138         QStringList qs_item;
139         qs_item << qfu( module_get_name( p_module, true ) )
140                 << qfu( module_get_capability( p_module ) )
141                 << QString::number( module_get_score( p_module ) );
142 #ifndef DEBUG
143         if( qs_item.at(1).isEmpty() ) continue;
144 #endif
145
146         QTreeWidgetItem *item = new PluginTreeItem( qs_item );
147         treePlugins->addTopLevelItem( item );
148     }
149     module_list_free( p_list );
150 }
151
152 void PluginTab::search( const QString& qs )
153 {
154     QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
155     items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
156
157     QTreeWidgetItem *item = NULL;
158     for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
159     {
160         item = treePlugins->topLevelItem( i );
161         item->setHidden( !items.contains( item ) );
162     }
163 }
164
165 PluginTab::~PluginTab()
166 {
167     writeSettings( "Plugins" );
168     getSettings()->setValue( "Plugins/Header-State",
169                              treePlugins->header()->saveState() );
170 }
171
172 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
173 {
174     int col = treeWidget()->sortColumn();
175     if( col == 2 )
176         return text( col ).toInt() < other.text( col ).toInt();
177     return text( col ) < other.text( col );
178 }
179
180 /* Extensions tab */
181 ExtensionTab::ExtensionTab( intf_thread_t *p_intf )
182         : QVLCFrame( p_intf )
183 {
184     // Layout
185     QVBoxLayout *layout = new QVBoxLayout( this );
186
187     // ListView
188     extList = new QListView( this );
189     CONNECT( extList, activated( const QModelIndex& ),
190              this, moreInformation() );
191     layout->addWidget( extList );
192
193     // List item delegate
194     ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
195                                                                      extList );
196     extList->setItemDelegate( itemDelegate );
197
198     // Extension list look & feeling
199     extList->setAlternatingRowColors( true );
200     extList->setSelectionMode( QAbstractItemView::SingleSelection );
201
202     // Model
203     ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
204     extList->setModel( model );
205
206     // Buttons' layout
207     QHBoxLayout *hbox = new QHBoxLayout;
208     hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
209                                     QSizePolicy::Fixed ) );
210
211     // More information button
212     butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
213                                    qtr( "More information..." ),
214                                    this );
215     CONNECT( butMoreInfo, clicked(),
216              this, moreInformation() );
217     hbox->addWidget( butMoreInfo );
218
219     // Reload button
220     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
221     QPushButton *reload = new QPushButton( QIcon( ":/update" ),
222                                            qtr( "Reload extensions" ),
223                                            this );
224     CONNECT( reload, clicked(),
225              EM, reloadExtensions() );
226     hbox->addWidget( reload );
227
228     // Add buttons hbox
229     layout->addItem( hbox );
230 }
231
232 ExtensionTab::~ExtensionTab()
233 {
234 }
235
236 // Do not close on ESC or ENTER
237 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
238 {
239     keyEvent->ignore();
240 }
241
242 // Show more information
243 void ExtensionTab::moreInformation()
244 {
245     if( !extList->selectionModel() ||
246         extList->selectionModel()->selectedIndexes().isEmpty() )
247
248     {
249         return;
250     }
251
252     QModelIndex index = extList->selectionModel()->selectedIndexes().first();
253     ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
254     if( !ext )
255         return;
256
257     ExtensionInfoDialog dlg( *ext, p_intf, this );
258     dlg.exec();
259 }
260
261 /* Safe copy of the extension_t struct */
262 class ExtensionCopy
263 {
264 public:
265     ExtensionCopy( extension_t *p_ext )
266     {
267         name = qfu( p_ext->psz_name );
268         description = qfu( p_ext->psz_description );
269         shortdesc = qfu( p_ext->psz_shortdescription );
270         if( description.isEmpty() )
271             description = shortdesc;
272         if( shortdesc.isEmpty() && !description.isEmpty() )
273             shortdesc = description;
274         title = qfu( p_ext->psz_title );
275         author = qfu( p_ext->psz_author );
276         version = qfu( p_ext->psz_version );
277         url = qfu( p_ext->psz_url );
278         icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
279     }
280     ~ExtensionCopy() {}
281
282     QString name, title, description, shortdesc, author, version, url;
283     QPixmap *icon;
284 };
285
286 /* Extensions list model for the QListView */
287
288 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
289         : QAbstractListModel( view ), p_intf( intf )
290 {
291     // Connect to ExtensionsManager::extensionsUpdated()
292     ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
293     CONNECT( EM, extensionsUpdated(), this, updateList() );
294
295     // Load extensions now if not already loaded
296     EM->loadExtensions();
297 }
298
299 ExtensionListModel::~ExtensionListModel()
300 {
301     // Clear extensions list
302     while( !extensions.isEmpty() )
303         delete extensions.takeLast();
304 }
305
306 void ExtensionListModel::updateList()
307 {
308     ExtensionCopy *ext;
309
310     // Clear extensions list
311     while( !extensions.isEmpty() )
312     {
313         ext = extensions.takeLast();
314         delete ext;
315     }
316
317     // Find new extensions
318     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
319     extensions_manager_t *p_mgr = EM->getManager();
320     if( !p_mgr )
321         return;
322
323     vlc_mutex_lock( &p_mgr->lock );
324     extension_t *p_ext;
325     FOREACH_ARRAY( p_ext, p_mgr->extensions )
326     {
327         ext = new ExtensionCopy( p_ext );
328         extensions.push_back( ext );
329     }
330     FOREACH_END()
331     vlc_mutex_unlock( &p_mgr->lock );
332     vlc_object_release( p_mgr );
333
334     emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
335 }
336
337 int ExtensionListModel::rowCount( const QModelIndex& ) const
338 {
339     int count = 0;
340     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
341     extensions_manager_t *p_mgr = EM->getManager();
342     if( !p_mgr )
343         return 0;
344
345     vlc_mutex_lock( &p_mgr->lock );
346     count = p_mgr->extensions.i_size;
347     vlc_mutex_unlock( &p_mgr->lock );
348     vlc_object_release( p_mgr );
349
350     return count;
351 }
352
353 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
354 {
355     if( !index.isValid() )
356         return QVariant();
357
358     switch( role )
359     {
360     default:
361         return QVariant();
362     }
363 }
364
365 QModelIndex ExtensionListModel::index( int row, int column,
366                                        const QModelIndex& ) const
367 {
368     if( column != 0 )
369         return QModelIndex();
370     if( row < 0 || row >= extensions.size() )
371         return QModelIndex();
372
373     return createIndex( row, 0, extensions.at( row ) );
374 }
375
376
377 /* Extension List Widget Item */
378 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
379                                               QListView *view )
380         : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
381 {
382 }
383
384 ExtensionItemDelegate::~ExtensionItemDelegate()
385 {
386 }
387
388 void ExtensionItemDelegate::paint( QPainter *painter,
389                                    const QStyleOptionViewItem &option,
390                                    const QModelIndex &index ) const
391 {
392     ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
393     assert( ext != NULL );
394
395     int width = option.rect.width();
396
397     // Pixmap: buffer where to draw
398     QPixmap pix(option.rect.size());
399
400     // Draw background
401     pix.fill( Qt::transparent ); // FIXME
402
403     // ItemView primitive style
404     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
405                                           &option,
406                                           painter );
407
408     // Painter on the pixmap
409     QPainter *pixpaint = new QPainter(&pix);
410
411     // Text font & pen
412     QFont font = painter->font();
413     QPen pen = painter->pen();
414     if( view->selectionModel()->selectedIndexes().contains( index ) )
415     {
416         pen.setBrush( option.palette.highlightedText() );
417     }
418     else
419     {
420         pen.setBrush( option.palette.text() );
421     }
422     pixpaint->setPen( pen );
423     QFontMetrics metrics = option.fontMetrics;
424
425     // Icon
426     if( ext->icon != NULL )
427     {
428         pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(),
429                               *ext->icon );
430     }
431
432     // Title: bold
433     pixpaint->setRenderHint( QPainter::TextAntialiasing );
434     font.setBold( true );
435     pixpaint->setFont( font );
436     pixpaint->drawText( QRect( 17 + 2 * metrics.height(), 7,
437                                width - 40 - 2 * metrics.height(),
438                                metrics.height() ),
439                         Qt::AlignLeft, ext->title );
440
441     // Short description: normal
442     font.setBold( false );
443     pixpaint->setFont( font );
444     pixpaint->drawText( QRect( 17 + 2 * metrics.height(),
445                                7 + metrics.height(), width - 40,
446                                metrics.height() ),
447                         Qt::AlignLeft, ext->shortdesc );
448
449     // Flush paint operations
450     delete pixpaint;
451
452     // Draw it on the screen!
453     painter->drawPixmap( option.rect, pix );
454 }
455
456 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
457                                        const QModelIndex &index ) const
458 {
459     if (index.isValid() && index.column() == 0)
460     {
461         QFontMetrics metrics = option.fontMetrics;
462         return QSize( 200, 14 + 2 * metrics.height() );
463     }
464     else
465         return QSize();
466 }
467
468 /* "More information" dialog */
469
470 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
471                                           intf_thread_t *p_intf,
472                                           QWidget *parent )
473        : QVLCDialog( parent, p_intf ),
474          extension( new ExtensionCopy( extension ) )
475 {
476     // Let's be a modal dialog
477     setWindowModality( Qt::WindowModal );
478
479     // Window title
480     setWindowTitle( qtr( "About" ) + " " + extension.title );
481
482     // Layout
483     QGridLayout *layout = new QGridLayout( this );
484
485     // Icon
486     QLabel *icon = new QLabel( this );
487     if( !extension.icon )
488     {
489         QPixmap pix( ":/logo/vlc48.png" );
490         icon->setPixmap( pix );
491     }
492     else
493     {
494         icon->setPixmap( *extension.icon );
495     }
496     icon->setAlignment( Qt::AlignCenter );
497     icon->setFixedSize( 48, 48 );
498     layout->addWidget( icon, 1, 0, 2, 1 );
499
500     // Title
501     QLabel *label = new QLabel( extension.title, this );
502     QFont font = label->font();
503     font.setBold( true );
504     font.setPointSizeF( font.pointSizeF() * 1.3f );
505     label->setFont( font );
506     layout->addWidget( label, 0, 0, 1, -1 );
507
508     // Version
509     label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
510     layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
511     label = new QLabel( extension.version, this );
512     layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
513
514     // Author
515     label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
516     layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
517     label = new QLabel( extension.author, this );
518     layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
519
520
521     // Description
522     label = new QLabel( this );
523     label->setText( extension.description );
524     label->setWordWrap( true );
525     label->setOpenExternalLinks( true );
526     layout->addWidget( label, 4, 0, 1, -1 );
527
528     // URL
529     label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
530     layout->addWidget( label, 5, 0, 1, 2 );
531     QString txt = "<a href=\"";
532     txt += extension.url;
533     txt += "\">";
534     txt += extension.url;
535     txt += "</a>";
536     label = new QLabel( txt, this );
537     label->setText( txt );
538     label->setOpenExternalLinks( true );
539     layout->addWidget( label, 5, 2, 1, -1 );
540
541     // Script file
542     label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
543     layout->addWidget( label, 6, 0, 1, 2 );
544     QLineEdit *line = new QLineEdit( extension.name, this );
545     layout->addWidget( line, 6, 2, 1, -1 );
546
547     // Close button
548     QDialogButtonBox *group = new QDialogButtonBox( this );
549     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
550     group->addButton( closeButton, QDialogButtonBox::AcceptRole );
551     BUTTONACT( closeButton, close() );
552
553     layout->addWidget( group, 7, 0, 1, -1 );
554
555     // Fix layout
556     layout->setColumnStretch( 2, 1 );
557     layout->setRowStretch( 4, 1 );
558     setMinimumSize( 450, 350 );
559 }
560
561 ExtensionInfoDialog::~ExtensionInfoDialog()
562 {
563     delete extension;
564 }
565
566 static QPixmap *loadPixmapFromData( char *data, int size )
567 {
568     if( !data || size <= 0 )
569         return NULL;
570     QPixmap *pixmap = new QPixmap();
571     if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )
572     {
573         delete pixmap;
574         return NULL;
575     }
576     return pixmap;
577 }