]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/plugins.cpp
Qt: use count() iso size() on Qt Containers
[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 void PluginTab::keyPressEvent( QKeyEvent *keyEvent )
173 {
174     if( keyEvent->key() == Qt::Key_Return ||
175         keyEvent->key() == Qt::Key_Enter )
176         keyEvent->accept();
177     else
178         keyEvent->ignore();
179 }
180
181 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
182 {
183     int col = treeWidget()->sortColumn();
184     if( col == 2 )
185         return text( col ).toInt() < other.text( col ).toInt();
186     return text( col ) < other.text( col );
187 }
188
189 /* Extensions tab */
190 ExtensionTab::ExtensionTab( intf_thread_t *p_intf )
191         : QVLCFrame( p_intf )
192 {
193     // Layout
194     QVBoxLayout *layout = new QVBoxLayout( this );
195
196     // ListView
197     extList = new QListView( this );
198     CONNECT( extList, activated( const QModelIndex& ),
199              this, moreInformation() );
200     layout->addWidget( extList );
201
202     // List item delegate
203     ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
204                                                                      extList );
205     extList->setItemDelegate( itemDelegate );
206
207     // Extension list look & feeling
208     extList->setAlternatingRowColors( true );
209     extList->setSelectionMode( QAbstractItemView::SingleSelection );
210
211     // Model
212     ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
213     extList->setModel( model );
214
215     // Buttons' layout
216     QHBoxLayout *hbox = new QHBoxLayout;
217     hbox->addItem( new QSpacerItem( 1, 1, QSizePolicy::Expanding,
218                                     QSizePolicy::Fixed ) );
219
220     // More information button
221     butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
222                                    qtr( "More information..." ),
223                                    this );
224     CONNECT( butMoreInfo, clicked(),
225              this, moreInformation() );
226     hbox->addWidget( butMoreInfo );
227
228     // Reload button
229     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
230     QPushButton *reload = new QPushButton( QIcon( ":/update" ),
231                                            qtr( "Reload extensions" ),
232                                            this );
233     CONNECT( reload, clicked(),
234              EM, reloadExtensions() );
235     hbox->addWidget( reload );
236
237     // Add buttons hbox
238     layout->addItem( hbox );
239 }
240
241 ExtensionTab::~ExtensionTab()
242 {
243 }
244
245 // Do not close on ESC or ENTER
246 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
247 {
248     if( keyEvent->key() == Qt::Key_Return ||
249         keyEvent->key() == Qt::Key_Enter )
250         keyEvent->accept();
251     else
252         keyEvent->ignore();
253 }
254
255 // Show more information
256 void ExtensionTab::moreInformation()
257 {
258     if( !extList->selectionModel() ||
259         extList->selectionModel()->selectedIndexes().isEmpty() )
260
261     {
262         return;
263     }
264
265     QModelIndex index = extList->selectionModel()->selectedIndexes().first();
266     ExtensionCopy *ext = (ExtensionCopy*) index.internalPointer();
267     if( !ext )
268         return;
269
270     ExtensionInfoDialog dlg( *ext, p_intf, this );
271     dlg.exec();
272 }
273
274 /* Safe copy of the extension_t struct */
275 class ExtensionCopy
276 {
277 public:
278     ExtensionCopy( extension_t *p_ext )
279     {
280         name = qfu( p_ext->psz_name );
281         description = qfu( p_ext->psz_description );
282         shortdesc = qfu( p_ext->psz_shortdescription );
283         if( description.isEmpty() )
284             description = shortdesc;
285         if( shortdesc.isEmpty() && !description.isEmpty() )
286             shortdesc = description;
287         title = qfu( p_ext->psz_title );
288         author = qfu( p_ext->psz_author );
289         version = qfu( p_ext->psz_version );
290         url = qfu( p_ext->psz_url );
291         icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
292     }
293     ~ExtensionCopy() {}
294
295     QString name, title, description, shortdesc, author, version, url;
296     QPixmap *icon;
297 };
298
299 /* Extensions list model for the QListView */
300
301 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
302         : QAbstractListModel( view ), p_intf( intf )
303 {
304     // Connect to ExtensionsManager::extensionsUpdated()
305     ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
306     CONNECT( EM, extensionsUpdated(), this, updateList() );
307
308     // Load extensions now if not already loaded
309     EM->loadExtensions();
310 }
311
312 ExtensionListModel::~ExtensionListModel()
313 {
314     // Clear extensions list
315     while( !extensions.isEmpty() )
316         delete extensions.takeLast();
317 }
318
319 void ExtensionListModel::updateList()
320 {
321     ExtensionCopy *ext;
322
323     // Clear extensions list
324     while( !extensions.isEmpty() )
325     {
326         ext = extensions.takeLast();
327         delete ext;
328     }
329
330     // Find new extensions
331     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
332     extensions_manager_t *p_mgr = EM->getManager();
333     if( !p_mgr )
334         return;
335
336     vlc_mutex_lock( &p_mgr->lock );
337     extension_t *p_ext;
338     FOREACH_ARRAY( p_ext, p_mgr->extensions )
339     {
340         ext = new ExtensionCopy( p_ext );
341         extensions.push_back( ext );
342     }
343     FOREACH_END()
344     vlc_mutex_unlock( &p_mgr->lock );
345     vlc_object_release( p_mgr );
346
347     emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
348 }
349
350 int ExtensionListModel::rowCount( const QModelIndex& ) const
351 {
352     int count = 0;
353     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
354     extensions_manager_t *p_mgr = EM->getManager();
355     if( !p_mgr )
356         return 0;
357
358     vlc_mutex_lock( &p_mgr->lock );
359     count = p_mgr->extensions.i_size;
360     vlc_mutex_unlock( &p_mgr->lock );
361     vlc_object_release( p_mgr );
362
363     return count;
364 }
365
366 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
367 {
368     if( !index.isValid() )
369         return QVariant();
370
371     switch( role )
372     {
373     default:
374         return QVariant();
375     }
376 }
377
378 QModelIndex ExtensionListModel::index( int row, int column,
379                                        const QModelIndex& ) const
380 {
381     if( column != 0 )
382         return QModelIndex();
383     if( row < 0 || row >= extensions.count() )
384         return QModelIndex();
385
386     return createIndex( row, 0, extensions.at( row ) );
387 }
388
389
390 /* Extension List Widget Item */
391 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
392                                               QListView *view )
393         : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
394 {
395 }
396
397 ExtensionItemDelegate::~ExtensionItemDelegate()
398 {
399 }
400
401 void ExtensionItemDelegate::paint( QPainter *painter,
402                                    const QStyleOptionViewItem &option,
403                                    const QModelIndex &index ) const
404 {
405     ExtensionCopy *ext = ( ExtensionCopy* ) index.internalPointer();
406     assert( ext != NULL );
407
408     int width = option.rect.width();
409
410     // Pixmap: buffer where to draw
411     QPixmap pix(option.rect.size());
412
413     // Draw background
414     pix.fill( Qt::transparent ); // FIXME
415
416     // ItemView primitive style
417     QApplication::style()->drawPrimitive( QStyle::PE_PanelItemViewItem,
418                                           &option,
419                                           painter );
420
421     // Painter on the pixmap
422     QPainter *pixpaint = new QPainter(&pix);
423
424     // Text font & pen
425     QFont font = painter->font();
426     QPen pen = painter->pen();
427     if( view->selectionModel()->selectedIndexes().contains( index ) )
428     {
429         pen.setBrush( option.palette.highlightedText() );
430     }
431     else
432     {
433         pen.setBrush( option.palette.text() );
434     }
435     pixpaint->setPen( pen );
436     QFontMetrics metrics = option.fontMetrics;
437
438     // Icon
439     if( ext->icon != NULL )
440     {
441         pixpaint->drawPixmap( 7, 7, 2*metrics.height(), 2*metrics.height(),
442                               *ext->icon );
443     }
444
445     // Title: bold
446     pixpaint->setRenderHint( QPainter::TextAntialiasing );
447     font.setBold( true );
448     pixpaint->setFont( font );
449     pixpaint->drawText( QRect( 17 + 2 * metrics.height(), 7,
450                                width - 40 - 2 * metrics.height(),
451                                metrics.height() ),
452                         Qt::AlignLeft, ext->title );
453
454     // Short description: normal
455     font.setBold( false );
456     pixpaint->setFont( font );
457     pixpaint->drawText( QRect( 17 + 2 * metrics.height(),
458                                7 + metrics.height(), width - 40,
459                                metrics.height() ),
460                         Qt::AlignLeft, ext->shortdesc );
461
462     // Flush paint operations
463     delete pixpaint;
464
465     // Draw it on the screen!
466     painter->drawPixmap( option.rect, pix );
467 }
468
469 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
470                                        const QModelIndex &index ) const
471 {
472     if (index.isValid() && index.column() == 0)
473     {
474         QFontMetrics metrics = option.fontMetrics;
475         return QSize( 200, 14 + 2 * metrics.height() );
476     }
477     else
478         return QSize();
479 }
480
481 /* "More information" dialog */
482
483 ExtensionInfoDialog::ExtensionInfoDialog( const ExtensionCopy& extension,
484                                           intf_thread_t *p_intf,
485                                           QWidget *parent )
486        : QVLCDialog( parent, p_intf ),
487          extension( new ExtensionCopy( extension ) )
488 {
489     // Let's be a modal dialog
490     setWindowModality( Qt::WindowModal );
491
492     // Window title
493     setWindowTitle( qtr( "About" ) + " " + extension.title );
494
495     // Layout
496     QGridLayout *layout = new QGridLayout( this );
497
498     // Icon
499     QLabel *icon = new QLabel( this );
500     if( !extension.icon )
501     {
502         QPixmap pix( ":/logo/vlc48.png" );
503         icon->setPixmap( pix );
504     }
505     else
506     {
507         icon->setPixmap( *extension.icon );
508     }
509     icon->setAlignment( Qt::AlignCenter );
510     icon->setFixedSize( 48, 48 );
511     layout->addWidget( icon, 1, 0, 2, 1 );
512
513     // Title
514     QLabel *label = new QLabel( extension.title, this );
515     QFont font = label->font();
516     font.setBold( true );
517     font.setPointSizeF( font.pointSizeF() * 1.3f );
518     label->setFont( font );
519     layout->addWidget( label, 0, 0, 1, -1 );
520
521     // Version
522     label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
523     layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
524     label = new QLabel( extension.version, this );
525     layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
526
527     // Author
528     label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
529     layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
530     label = new QLabel( extension.author, this );
531     layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
532
533
534     // Description
535     label = new QLabel( this );
536     label->setText( extension.description );
537     label->setWordWrap( true );
538     label->setOpenExternalLinks( true );
539     layout->addWidget( label, 4, 0, 1, -1 );
540
541     // URL
542     label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
543     layout->addWidget( label, 5, 0, 1, 2 );
544     QString txt = "<a href=\"";
545     txt += extension.url;
546     txt += "\">";
547     txt += extension.url;
548     txt += "</a>";
549     label = new QLabel( txt, this );
550     label->setText( txt );
551     label->setOpenExternalLinks( true );
552     layout->addWidget( label, 5, 2, 1, -1 );
553
554     // Script file
555     label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
556     layout->addWidget( label, 6, 0, 1, 2 );
557     QLineEdit *line = new QLineEdit( extension.name, this );
558     layout->addWidget( line, 6, 2, 1, -1 );
559
560     // Close button
561     QDialogButtonBox *group = new QDialogButtonBox( this );
562     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
563     group->addButton( closeButton, QDialogButtonBox::AcceptRole );
564     BUTTONACT( closeButton, close() );
565
566     layout->addWidget( group, 7, 0, 1, -1 );
567
568     // Fix layout
569     layout->setColumnStretch( 2, 1 );
570     layout->setRowStretch( 4, 1 );
571     setMinimumSize( 450, 350 );
572 }
573
574 ExtensionInfoDialog::~ExtensionInfoDialog()
575 {
576     delete extension;
577 }
578
579 static QPixmap *loadPixmapFromData( char *data, int size )
580 {
581     if( !data || size <= 0 )
582         return NULL;
583     QPixmap *pixmap = new QPixmap();
584     if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )
585     {
586         delete pixmap;
587         return NULL;
588     }
589     return pixmap;
590 }