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