]> git.sesse.net Git - vlc/blob - modules/gui/qt4/dialogs/plugins.cpp
Fix sorting of plugin scores in the QT plugin dialog.
[vlc] / modules / gui / qt4 / dialogs / plugins.cpp
1 /*****************************************************************************
2  * plugins.hpp : Plug-ins and extensions listing
3  ****************************************************************************
4  * Copyright (C) 2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jean-Baptiste Kempf <jb (at) videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include "plugins.hpp"
29
30 #include "util/customwidgets.hpp"
31
32 //#include <vlc_modules.h>
33
34 #include <QTreeWidget>
35 #include <QStringList>
36 #include <QHeaderView>
37 #include <QDialogButtonBox>
38 #include <QLineEdit>
39 #include <QLabel>
40
41 PluginDialog *PluginDialog::instance = NULL;
42
43 PluginDialog::PluginDialog( intf_thread_t *_p_intf ) : QVLCFrame( _p_intf )
44 {
45     setWindowTitle( qtr( "Plugins and extensions" ) );
46     setWindowRole( "vlc-plugins" );
47     QGridLayout *layout = new QGridLayout( this );
48
49     /* Main Tree for modules */
50     treePlugins = new QTreeWidget;
51     layout->addWidget( treePlugins, 0, 0, 1, -1 );
52
53     /* Users cannot move the columns around but we need to sort */
54     treePlugins->header()->setMovable( false );
55     treePlugins->header()->setSortIndicatorShown( true );
56     //    treePlugins->header()->setResizeMode( QHeaderView::ResizeToContents );
57     treePlugins->setAlternatingRowColors( true );
58     treePlugins->setColumnWidth( 0, 200 );
59
60     QStringList headerNames;
61     headerNames << qtr("Name") << qtr("Capability" ) << qtr( "Score" );
62     treePlugins->setHeaderLabels( headerNames );
63
64     FillTree();
65
66     /* Set capability column to the correct Size*/
67     treePlugins->resizeColumnToContents( 1 );
68     treePlugins->header()->restoreState(
69             getSettings()->value( "Plugins/Header-State" ).toByteArray() );
70
71     treePlugins->setSortingEnabled( true );
72     treePlugins->sortByColumn( 1, Qt::AscendingOrder );
73
74     QLabel *label = new QLabel( qtr("&Search:"), this );
75     edit = new SearchLineEdit( this );
76     label->setBuddy( edit );
77
78     layout->addWidget( label, 1, 0 );
79     layout->addWidget( edit, 1, 1, 1, -1 );
80     CONNECT( edit, textChanged( const QString& ),
81             this, search( const QString& ) );
82
83     QDialogButtonBox *box = new QDialogButtonBox;
84     QPushButton *okButton = new QPushButton( qtr( "&Close" ), this );
85     box->addButton( okButton, QDialogButtonBox::AcceptRole );
86     layout->addWidget( box, 2, 2 );
87
88     BUTTONACT( okButton, close() );
89
90     setMinimumSize( 500, 300 );
91     readSettings( "Plugins", QSize( 540, 400 ) );
92 }
93
94 inline void PluginDialog::FillTree()
95 {
96     module_t **p_list = module_list_get( NULL );
97     module_t *p_module;
98
99     for( unsigned int i = 0; (p_module = p_list[i] ) != NULL; i++ )
100     {
101         QStringList qs_item;
102         qs_item << qfu( module_get_name( p_module, true ) )
103                 << qfu( module_get_capability( p_module ) )
104                 << QString::number( module_get_score( p_module ) );
105 #ifndef DEBUG
106         if( qs_item.at(1).isEmpty() ) continue;
107 #endif
108
109         QTreeWidgetItem *item = new PluginTreeItem( qs_item );
110         treePlugins->addTopLevelItem( item );
111     }
112 }
113
114 void PluginDialog::search( const QString& qs )
115 {
116     QList<QTreeWidgetItem *> items = treePlugins->findItems( qs, Qt::MatchContains );
117     items += treePlugins->findItems( qs, Qt::MatchContains, 1 );
118
119     QTreeWidgetItem *item = NULL;
120     for( int i = 0; i < treePlugins->topLevelItemCount(); i++ )
121     {
122         item = treePlugins->topLevelItem( i );
123         item->setHidden( !items.contains( item ) );
124     }
125 }
126
127 PluginDialog::~PluginDialog()
128 {
129     writeSettings( "Plugins" );
130     getSettings()->setValue( "Plugins/Header-State",
131                              treePlugins->header()->saveState() );
132 }
133
134 bool PluginTreeItem::operator< ( const QTreeWidgetItem & other ) const
135 {
136     int col = treeWidget()->sortColumn();
137     if( col == 2 )
138         return text( col ).toInt() < other.text( col ).toInt();
139     return text( col ) < other.text( col );
140 }