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