]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/plugins.cpp
Qt: show embedded icons of Lua extensions
[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& parent ) 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& parent ) 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     int height = option.rect.height();
397
398     // Pixmap: buffer where to draw
399     QPixmap pix(option.rect.size());
400
401     // Draw background
402     pix.fill( Qt::transparent ); // FIXME
403
404     // ItemView primitive style
405     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
406                                           &option,
407                                           painter );
408
409     // Painter on the pixmap
410     QPainter *pixpaint = new QPainter(&pix);
411
412     // Text font & pen
413     QFont font = painter->font();
414     QPen pen = painter->pen();
415     if( view->selectionModel()->selectedIndexes().contains( index ) )
416     {
417         pen.setBrush( option.palette.highlightedText() );
418     }
419     else
420     {
421         pen.setBrush( option.palette.text() );
422     }
423     pixpaint->setPen( pen );
424     QFontMetrics metrics = option.fontMetrics;
425
426     // Icon
427     if( ext->icon != NULL )
428     {
429         pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(),
430                               *ext->icon );
431     }
432
433     // Title: bold
434     pixpaint->setRenderHint( QPainter::TextAntialiasing );
435     font.setBold( true );
436     pixpaint->setFont( font );
437     pixpaint->drawText( QRect( 17 + 2 * metrics.height(), 7,
438                                width - 40 - 2 * metrics.height(),
439                                metrics.height() ),
440                         Qt::AlignLeft, ext->title );
441
442     // Short description: normal
443     font.setBold( false );
444     pixpaint->setFont( font );
445     pixpaint->drawText( QRect( 17 + 2 * metrics.height(),
446                                7 + metrics.height(), width - 40,
447                                metrics.height() ),
448                         Qt::AlignLeft, ext->shortdesc );
449
450     // Flush paint operations
451     delete pixpaint;
452
453     // Draw it on the screen!
454     painter->drawPixmap( option.rect, pix );
455 }
456
457 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
458                                        const QModelIndex &index ) const
459 {
460     if (index.isValid() && index.column() == 0)
461     {
462         QFontMetrics metrics = option.fontMetrics;
463         return QSize( 200, 14 + 2 * metrics.height() );
464     }
465     else
466         return QSize();
467 }
468
469 /* "More information" dialog */
470
471 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
472                                           intf_thread_t *p_intf,
473                                           QWidget *parent )
474        : QVLCDialog( parent, p_intf ),
475          extension( new ExtensionCopy( extension ) )
476 {
477     // Let's be a modal dialog
478     setWindowModality( Qt::WindowModal );
479
480     // Window title
481     setWindowTitle( qtr( "About" ) + " " + extension.title );
482
483     // Layout
484     QGridLayout *layout = new QGridLayout( this );
485
486     // Icon
487     QLabel *icon = new QLabel( this );
488     if( !extension.icon )
489     {
490         QPixmap pix( ":/logo/vlc48.png" );
491         icon->setPixmap( pix );
492     }
493     else
494     {
495         icon->setPixmap( *extension.icon );
496     }
497     icon->setAlignment( Qt::AlignCenter );
498     icon->setFixedSize( 48, 48 );
499     layout->addWidget( icon, 1, 0, 2, 1 );
500
501     // Title
502     QLabel *label = new QLabel( extension.title, this );
503     QFont font = label->font();
504     font.setBold( true );
505     font.setPointSizeF( font.pointSizeF() * 1.3f );
506     label->setFont( font );
507     layout->addWidget( label, 0, 0, 1, -1 );
508
509     // Version
510     label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
511     layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
512     label = new QLabel( extension.version, this );
513     layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
514
515     // Author
516     label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
517     layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
518     label = new QLabel( extension.author, this );
519     layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
520
521
522     // Description
523     label = new QLabel( this );
524     label->setText( extension.description );
525     label->setWordWrap( true );
526     label->setOpenExternalLinks( true );
527     layout->addWidget( label, 4, 0, 1, -1 );
528
529     // URL
530     label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
531     layout->addWidget( label, 5, 0, 1, 2 );
532     QString txt = "<a href=\"";
533     txt += extension.url;
534     txt += "\">";
535     txt += extension.url;
536     txt += "</a>";
537     label = new QLabel( txt, this );
538     label->setText( txt );
539     label->setOpenExternalLinks( true );
540     layout->addWidget( label, 5, 2, 1, -1 );
541
542     // Script file
543     label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
544     layout->addWidget( label, 6, 0, 1, 2 );
545     QLineEdit *line = new QLineEdit( extension.name, this );
546     layout->addWidget( line, 6, 2, 1, -1 );
547
548     // Close button
549     QDialogButtonBox *group = new QDialogButtonBox( this );
550     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
551     group->addButton( closeButton, QDialogButtonBox::AcceptRole );
552     BUTTONACT( closeButton, close() );
553
554     layout->addWidget( group, 7, 0, 1, -1 );
555
556     // Fix layout
557     layout->setColumnStretch( 2, 1 );
558     layout->setRowStretch( 4, 1 );
559     setMinimumSize( 450, 350 );
560 }
561
562 ExtensionInfoDialog::~ExtensionInfoDialog()
563 {
564     delete extension;
565 }
566
567 static QPixmap *loadPixmapFromData( char *data, int size )
568 {
569     if( !data || size <= 0 )
570         return NULL;
571     QPixmap *pixmap = new QPixmap();
572     if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )
573     {
574         delete pixmap;
575         return NULL;
576     }
577     return pixmap;
578 }