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