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