]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/plugins.cpp
Qt: ExtensionItemDelegate: rewrite (fix #10407)
[vlc] / modules / gui / qt4 / dialogs / plugins.cpp
1 /*****************************************************************************
2  * plugins.cpp : 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::RejectRole );
76     layout->addWidget( box );
77     BUTTONACT( okButton, close() );
78     restoreWidgetPosition( "PluginsDialog", QSize( 435, 280 ) );
79 }
80
81 PluginDialog::~PluginDialog()
82 {
83     saveWidgetPosition( "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 #if QT_VERSION >= 0x050000
99     treePlugins->header()->setSectionsMovable( false );
100 #else
101     treePlugins->header()->setMovable( false );
102 #endif
103     treePlugins->header()->setSortIndicatorShown( true );
104     //    treePlugins->header()->setResizeMode( QHeaderView::ResizeToContents );
105     treePlugins->setAlternatingRowColors( true );
106     treePlugins->setColumnWidth( 0, 200 );
107
108     QStringList headerNames;
109     headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
110     treePlugins->setHeaderLabels( headerNames );
111
112     FillTree();
113
114     /* Set capability column to the correct Size*/
115     treePlugins->resizeColumnToContents( 1 );
116     treePlugins->header()->restoreState(
117             getSettings()->value( "Plugins/Header-State" ).toByteArray() );
118
119     treePlugins->setSortingEnabled( true );
120     treePlugins->sortByColumn( 1, Qt::AscendingOrder );
121
122     QLabel *label = new QLabel( qtr("&Search:"), this );
123     edit = new SearchLineEdit( this );
124     label->setBuddy( edit );
125
126     layout->addWidget( label, 1, 0 );
127     layout->addWidget( edit, 1, 1, 1, 1 );
128     CONNECT( edit, textChanged( const QString& ),
129             this, search( const QString& ) );
130
131     setMinimumSize( 500, 300 );
132     restoreWidgetPosition( "Plugins", QSize( 540, 400 ) );
133 }
134
135 inline void PluginTab::FillTree()
136 {
137     size_t count;
138     module_t **p_list = module_list_get( &count );
139
140     for( unsigned int i = 0; i < count; i++ )
141     {
142         module_t *p_module = p_list[i];
143
144         QStringList qs_item;
145         qs_item << qfu( module_get_name( p_module, true ) )
146                 << qfu( module_get_capability( p_module ) )
147                 << QString::number( module_get_score( p_module ) );
148 #ifndef DEBUG
149         if( qs_item.at(1).isEmpty() ) continue;
150 #endif
151
152         QTreeWidgetItem *item = new PluginTreeItem( qs_item );
153         treePlugins->addTopLevelItem( item );
154     }
155     module_list_free( p_list );
156 }
157
158 void PluginTab::search( const QString& qs )
159 {
160     QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
161     items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
162
163     QTreeWidgetItem *item = NULL;
164     for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
165     {
166         item = treePlugins->topLevelItem( i );
167         item->setHidden( !items.contains( item ) );
168     }
169 }
170
171 PluginTab::~PluginTab()
172 {
173     saveWidgetPosition( "Plugins" );
174     getSettings()->setValue( "Plugins/Header-State",
175                              treePlugins->header()->saveState() );
176 }
177
178 void PluginTab::keyPressEvent( QKeyEvent *keyEvent )
179 {
180     if( keyEvent->key() == Qt::Key_Return ||
181         keyEvent->key() == Qt::Key_Enter )
182         keyEvent->accept();
183     else
184         keyEvent->ignore();
185 }
186
187 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
188 {
189     int col = treeWidget()->sortColumn();
190     if( col == PluginTab::SCORE )
191         return text( col ).toInt() < other.text( col ).toInt();
192     else if ( col == PluginTab::CAPABILITY )
193     {
194         if ( text( PluginTab::CAPABILITY ) == other.text( PluginTab::CAPABILITY ) )
195             return text( PluginTab::NAME ) < other.text( PluginTab::NAME );
196         else
197             return text( PluginTab::CAPABILITY ) < other.text( PluginTab::CAPABILITY );
198     }
199     return text( col ) < other.text( col );
200 }
201
202 /* Extensions tab */
203 ExtensionTab::ExtensionTab( intf_thread_t *p_intf_ )
204         : QVLCFrame( p_intf_ )
205 {
206     // Layout
207     QVBoxLayout *layout = new QVBoxLayout( this );
208
209     QLabel *notice = new QLabel( qtr("Get more extensions from")
210             + QString( " <a href=\"http://addons.videolan.org/\">"
211                        "addons.videolan.org</a>." ) );
212     notice->setOpenExternalLinks( true );
213     layout->addWidget( notice );
214
215     // ListView
216     extList = new QListView( this );
217     CONNECT( extList, activated( const QModelIndex& ),
218              this, moreInformation() );
219     layout->addWidget( extList );
220
221     // List item delegate
222     ExtensionItemDelegate *itemDelegate = new ExtensionItemDelegate( p_intf,
223                                                                      extList );
224     extList->setItemDelegate( itemDelegate );
225
226     // Extension list look & feeling
227     extList->setAlternatingRowColors( true );
228     extList->setSelectionMode( QAbstractItemView::SingleSelection );
229
230     // Model
231     ExtensionListModel *model = new ExtensionListModel( extList, p_intf );
232     extList->setModel( model );
233
234     // Buttons' layout
235     QDialogButtonBox *buttonsBox = new QDialogButtonBox;
236
237     // More information button
238     butMoreInfo = new QPushButton( QIcon( ":/menu/info" ),
239                                    qtr( "More information..." ),
240                                    this );
241     CONNECT( butMoreInfo, clicked(), this, moreInformation() );
242     buttonsBox->addButton( butMoreInfo, QDialogButtonBox::ActionRole );
243
244     // Reload button
245     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
246     QPushButton *reload = new QPushButton( QIcon( ":/update" ),
247                                            qtr( "Reload extensions" ),
248                                            this );
249     CONNECT( reload, clicked(), EM, reloadExtensions() );
250     CONNECT( reload, clicked(), this, updateButtons() );
251     CONNECT( extList->selectionModel(),
252              selectionChanged( const QItemSelection &, const QItemSelection & ),
253              this,
254              updateButtons() );
255     buttonsBox->addButton( reload, QDialogButtonBox::ResetRole );
256
257     layout->addWidget( buttonsBox );
258     updateButtons();
259 }
260
261 ExtensionTab::~ExtensionTab()
262 {
263 }
264
265 void ExtensionTab::updateButtons()
266 {
267     butMoreInfo->setEnabled( extList->selectionModel()->hasSelection() );
268 }
269
270 // Do not close on ESC or ENTER
271 void ExtensionTab::keyPressEvent( QKeyEvent *keyEvent )
272 {
273     if( keyEvent->key() == Qt::Key_Return ||
274         keyEvent->key() == Qt::Key_Enter )
275         keyEvent->accept();
276     else
277         keyEvent->ignore();
278 }
279
280 // Show more information
281 void ExtensionTab::moreInformation()
282 {
283     QModelIndex index = extList->selectionModel()->selectedIndexes().first();
284
285     if( !index.isValid() )
286         return;
287
288     ExtensionInfoDialog dlg( index, p_intf, this );
289     dlg.exec();
290 }
291
292 /* Safe copy of the extension_t struct */
293 ExtensionListModel::ExtensionCopy::ExtensionCopy( extension_t *p_ext )
294 {
295     name = qfu( p_ext->psz_name );
296     description = qfu( p_ext->psz_description );
297     shortdesc = qfu( p_ext->psz_shortdescription );
298     if( description.isEmpty() )
299         description = shortdesc;
300     if( shortdesc.isEmpty() && !description.isEmpty() )
301         shortdesc = description;
302     title = qfu( p_ext->psz_title );
303     author = qfu( p_ext->psz_author );
304     version = qfu( p_ext->psz_version );
305     url = qfu( p_ext->psz_url );
306     icon = loadPixmapFromData( p_ext->p_icondata, p_ext->i_icondata_size );
307 }
308
309 ExtensionListModel::ExtensionCopy::~ExtensionCopy()
310 {
311     delete icon;
312 }
313
314 QVariant ExtensionListModel::ExtensionCopy::data( int role ) const
315 {
316     switch( role )
317     {
318     case Qt::DisplayRole:
319         return title;
320     case Qt::DecorationRole:
321         if ( !icon ) return QPixmap( ":/logo/vlc48.png" );
322         return *icon;
323     case DescriptionRole:
324         return shortdesc;
325     case VersionRole:
326         return version;
327     case AuthorRole:
328         return author;
329     case LinkRole:
330         return url;
331     case NameRole:
332         return name;
333     default:
334         return QVariant();
335     }
336 }
337
338 /* Extensions list model for the QListView */
339
340 ExtensionListModel::ExtensionListModel( QListView *view, intf_thread_t *intf )
341         : QAbstractListModel( view ), p_intf( intf )
342 {
343     // Connect to ExtensionsManager::extensionsUpdated()
344     ExtensionsManager* EM = ExtensionsManager::getInstance( p_intf );
345     CONNECT( EM, extensionsUpdated(), this, updateList() );
346
347     // Load extensions now if not already loaded
348     EM->loadExtensions();
349 }
350
351 ExtensionListModel::~ExtensionListModel()
352 {
353     // Clear extensions list
354     while( !extensions.isEmpty() )
355         delete extensions.takeLast();
356 }
357
358 void ExtensionListModel::updateList()
359 {
360     ExtensionCopy *ext;
361
362     // Clear extensions list
363     while( !extensions.isEmpty() )
364     {
365         ext = extensions.takeLast();
366         delete ext;
367     }
368
369     // Find new extensions
370     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
371     extensions_manager_t *p_mgr = EM->getManager();
372     if( !p_mgr )
373         return;
374
375     vlc_mutex_lock( &p_mgr->lock );
376     extension_t *p_ext;
377     FOREACH_ARRAY( p_ext, p_mgr->extensions )
378     {
379         ext = new ExtensionCopy( p_ext );
380         extensions.append( ext );
381     }
382     FOREACH_END()
383     vlc_mutex_unlock( &p_mgr->lock );
384     vlc_object_release( p_mgr );
385
386     emit dataChanged( index( 0 ), index( rowCount() - 1 ) );
387 }
388
389 int ExtensionListModel::rowCount( const QModelIndex& ) const
390 {
391     int count = 0;
392     ExtensionsManager *EM = ExtensionsManager::getInstance( p_intf );
393     extensions_manager_t *p_mgr = EM->getManager();
394     if( !p_mgr )
395         return 0;
396
397     vlc_mutex_lock( &p_mgr->lock );
398     count = p_mgr->extensions.i_size;
399     vlc_mutex_unlock( &p_mgr->lock );
400     vlc_object_release( p_mgr );
401
402     return count;
403 }
404
405 QVariant ExtensionListModel::data( const QModelIndex& index, int role ) const
406 {
407     if( !index.isValid() )
408         return QVariant();
409
410     ExtensionCopy * extension =
411             static_cast<ExtensionCopy *>(index.internalPointer());
412
413     return extension->data( role );
414 }
415
416 QModelIndex ExtensionListModel::index( int row, int column,
417                                        const QModelIndex& ) const
418 {
419     if( column != 0 )
420         return QModelIndex();
421     if( row < 0 || row >= extensions.count() )
422         return QModelIndex();
423
424     return createIndex( row, 0, extensions.at( row ) );
425 }
426
427
428 /* Extension List Widget Item */
429 ExtensionItemDelegate::ExtensionItemDelegate( intf_thread_t *p_intf,
430                                               QListView *view )
431         : QStyledItemDelegate( view ), view( view ), p_intf( p_intf )
432 {
433     margins = QMargins( 4, 4, 4, 4 );
434 }
435
436 ExtensionItemDelegate::~ExtensionItemDelegate()
437 {
438 }
439
440 void ExtensionItemDelegate::paint( QPainter *painter,
441                                    const QStyleOptionViewItem &option,
442                                    const QModelIndex &index ) const
443 {
444     QStyleOptionViewItemV4 opt = option;
445     initStyleOption( &opt, index );
446
447     // Draw background
448     if ( opt.state & QStyle::State_Selected )
449         painter->fillRect( opt.rect, opt.palette.highlight() );
450
451     // Icon
452     QPixmap icon = index.data( Qt::DecorationRole ).value<QPixmap>();
453     if( !icon.isNull() )
454     {
455         painter->drawPixmap( opt.rect.left() + margins.left(),
456                              opt.rect.top() + margins.top(),
457                              icon.scaled( opt.decorationSize,
458                                           Qt::KeepAspectRatio,
459                                           Qt::SmoothTransformation )
460         );
461     }
462
463     painter->save();
464     painter->setRenderHint( QPainter::TextAntialiasing );
465
466     if ( opt.state & QStyle::State_Selected )
467         painter->setPen( opt.palette.highlightedText().color() );
468
469     QFont font( option.font );
470     font.setBold( true );
471     painter->setFont( font );
472     QRect textrect( opt.rect );
473     textrect.adjust( 2 * margins.left() + margins.right() + opt.decorationSize.width(),
474                      margins.top(),
475                      - margins.right(),
476                      - margins.bottom() - opt.fontMetrics.height() );
477
478     painter->drawText( textrect, Qt::AlignLeft,
479                        index.data( Qt::DisplayRole ).toString() );
480
481     font.setBold( false );
482     painter->setFont( font );
483     painter->drawText( textrect.translated( 0, option.fontMetrics.height() ),
484                        Qt::AlignLeft,
485                        index.data( ExtensionListModel::DescriptionRole ).toString() );
486
487     painter->restore();
488 }
489
490 QSize ExtensionItemDelegate::sizeHint( const QStyleOptionViewItem &option,
491                                        const QModelIndex &index ) const
492 {
493     if ( index.isValid() )
494     {
495         return QSize( 200, 2 * option.fontMetrics.height()
496                       + margins.top() + margins.bottom() );
497     }
498     else
499         return QSize();
500 }
501
502 void ExtensionItemDelegate::initStyleOption( QStyleOptionViewItem *option,
503                                              const QModelIndex &index ) const
504 {
505     QStyledItemDelegate::initStyleOption( option, index );
506     option->decorationSize = QSize( option->rect.height(), option->rect.height() );
507     option->decorationSize -= QSize( margins.left() + margins.right(),
508                                      margins.top() + margins.bottom() );
509 }
510
511 /* "More information" dialog */
512
513 ExtensionInfoDialog::ExtensionInfoDialog( const QModelIndex &index,
514                                           intf_thread_t *p_intf,
515                                           QWidget *parent )
516        : QVLCDialog( parent, p_intf )
517 {
518     // Let's be a modal dialog
519     setWindowModality( Qt::WindowModal );
520
521     // Window title
522     setWindowTitle( qtr( "About" ) + " " + index.data(Qt::DisplayRole).toString() );
523
524     // Layout
525     QGridLayout *layout = new QGridLayout( this );
526
527     // Icon
528     QLabel *icon = new QLabel( this );
529     QPixmap pix = index.data(Qt::DecorationRole).value<QPixmap>();
530     Q_ASSERT( !pix.isNull() );
531     icon->setPixmap( pix );
532     icon->setAlignment( Qt::AlignCenter );
533     icon->setFixedSize( 48, 48 );
534     layout->addWidget( icon, 1, 0, 2, 1 );
535
536     // Title
537     QLabel *label = new QLabel( index.data(Qt::DisplayRole).toString(), this );
538     QFont font = label->font();
539     font.setBold( true );
540     font.setPointSizeF( font.pointSizeF() * 1.3f );
541     label->setFont( font );
542     layout->addWidget( label, 0, 0, 1, -1 );
543
544     // Version
545     label = new QLabel( "<b>" + qtr( "Version" ) + ":</b>", this );
546     layout->addWidget( label, 1, 1, 1, 1, Qt::AlignBottom );
547     label = new QLabel( index.data(ExtensionListModel::VersionRole).toString(), this );
548     layout->addWidget( label, 1, 2, 1, 2, Qt::AlignBottom );
549
550     // Author
551     label = new QLabel( "<b>" + qtr( "Author" ) + ":</b>", this );
552     layout->addWidget( label, 2, 1, 1, 1, Qt::AlignTop );
553     label = new QLabel( index.data(ExtensionListModel::AuthorRole).toString(), this );
554     layout->addWidget( label, 2, 2, 1, 2, Qt::AlignTop );
555
556
557     // Description
558     label = new QLabel( this );
559     label->setText( index.data(ExtensionListModel::DescriptionRole).toString() );
560     label->setWordWrap( true );
561     label->setOpenExternalLinks( true );
562     layout->addWidget( label, 4, 0, 1, -1 );
563
564     // URL
565     label = new QLabel( "<b>" + qtr( "Website" ) + ":</b>", this );
566     layout->addWidget( label, 5, 0, 1, 2 );
567     label = new QLabel( QString("<a href=\"%1\">%2</a>")
568                         .arg( index.data(ExtensionListModel::LinkRole).toString() )
569                         .arg( index.data(ExtensionListModel::LinkRole).toString() )
570                         , this );
571     label->setOpenExternalLinks( true );
572     layout->addWidget( label, 5, 2, 1, -1 );
573
574     // Script file
575     label = new QLabel( "<b>" + qtr( "File" ) + ":</b>", this );
576     layout->addWidget( label, 6, 0, 1, 2 );
577     QLineEdit *line =
578             new QLineEdit( index.data(ExtensionListModel::NameRole).toString(), this );
579     line->setReadOnly( true );
580     layout->addWidget( line, 6, 2, 1, -1 );
581
582     // Close button
583     QDialogButtonBox *group = new QDialogButtonBox( this );
584     QPushButton *closeButton = new QPushButton( qtr( "&Close" ) );
585     group->addButton( closeButton, QDialogButtonBox::RejectRole );
586     BUTTONACT( closeButton, close() );
587
588     layout->addWidget( group, 7, 0, 1, -1 );
589
590     // Fix layout
591     layout->setColumnStretch( 2, 1 );
592     layout->setRowStretch( 4, 1 );
593     setMinimumSize( 450, 350 );
594 }
595
596 static QPixmap *loadPixmapFromData( char *data, int size )
597 {
598     if( !data || size <= 0 )
599         return NULL;
600     QPixmap *pixmap = new QPixmap();
601     if( !pixmap->loadFromData( (const uchar*) data, (uint) size ) )
602     {
603         delete pixmap;
604         return NULL;
605     }
606     return pixmap;
607 }