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