Add (and install) svg for the new krunner interface.
[kdebase/uwolfer.git] / apps / kinfocenter / searchwidget.cpp
blob3828229a0aed0c10497b7986f57f7c00a0164321
1 /*
2 Copyright (c) 2000 Matthias Elter <elter@kde.org>
3 Copyright (c) 2004 Daniel Molkentin <molkentin@kde.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "searchwidget.h"
22 #include <Q3PtrList>
23 #include <QLabel>
24 #include <QListWidgetItem>
25 #include <QRegExp>
26 #include <QVBoxLayout>
28 #include <klineedit.h>
29 #include <kiconloader.h>
30 #include <klocale.h>
31 #include <klistwidget.h>
33 #include "searchwidget.moc"
35 /**
36 * Helper class for sorting icon modules by name without losing the fileName ID
38 class ModuleItem : public QListWidgetItem
40 public:
41 ModuleItem(ConfigModule *module, QListWidget * listbox = 0) :
42 QListWidgetItem(listbox)
43 , m_module(module)
45 setText( module->moduleName() );
46 setIcon( KIconLoader::global()->loadIcon(module->icon(), KIconLoader::Desktop, KIconLoader::SizeSmall) );
49 ConfigModule *module() const { return m_module; }
51 protected:
52 ConfigModule *m_module;
56 KeywordListEntry::KeywordListEntry(const QString& name, ConfigModule* module)
57 : _name(name)
59 if(module)
60 _modules.append(module);
63 void KeywordListEntry::addModule(ConfigModule* module)
65 if(module)
66 _modules.append(module);
69 SearchWidget::SearchWidget(QWidget *parent)
70 : QWidget(parent)
72 _keywords.setAutoDelete(true);
74 QVBoxLayout * l = new QVBoxLayout(this);
75 l->setMargin(0);
76 l->setSpacing(2);
78 // input
79 _input = new KLineEdit(this);
80 _input->setFocus();
81 QLabel *inputl = new QLabel(i18n("Se&arch:"), this);
82 inputl->setBuddy(_input);
84 l->addWidget(inputl);
85 l->addWidget(_input);
87 // keyword list
88 _keyList = new KListWidget(this);
89 QLabel *keyl = new QLabel(i18n("&Keywords:"), this);
90 keyl->setBuddy(_keyList);
92 l->addWidget(keyl);
93 l->addWidget(_keyList);
95 // result list
96 _resultList = new KListWidget(this);
97 QLabel *resultl = new QLabel(i18n("&Results:"), this);
98 resultl->setBuddy(_resultList);
100 l->addWidget(resultl);
101 l->addWidget(_resultList);
103 // set stretch factors
104 l->setStretchFactor(_resultList, 1);
105 l->setStretchFactor(_keyList, 2);
108 connect(_input, SIGNAL(textChanged(const QString&)),
109 this, SLOT(slotSearchTextChanged(const QString&)));
111 connect(_keyList, SIGNAL(currentTextChanged(const QString&)),
112 this, SLOT(slotKeywordSelected(const QString&)));
114 connect(_resultList, SIGNAL(itemChanged (QListWidgetItem*)),
115 this, SLOT(slotModuleSelected(QListWidgetItem *)));
116 connect(_resultList, SIGNAL(executed(QListWidgetItem *)),
117 this, SLOT(slotModuleClicked(QListWidgetItem *)));
120 void SearchWidget::populateKeywordList(ConfigModuleList *list)
122 ConfigModule *module;
124 // loop through all control modules
125 for (module=list->first(); module != 0; module=list->next())
127 if (module->library().isEmpty())
128 continue;
130 // get the modules keyword list
131 QStringList kw = module->keywords();
133 // loop through the keyword list to populate _keywords
134 for(QStringList::ConstIterator it = kw.begin(); it != kw.end(); ++it)
136 QString name = (*it).toLower();
137 bool found = false;
139 // look if _keywords already has an entry for this keyword
140 for(KeywordListEntry *k = _keywords.first(); k != 0; k = _keywords.next())
142 // if there is an entry for this keyword, add the module to the entries module list
143 if (k->moduleName() == name)
145 k->addModule(module);
146 found = true;
147 break;
151 // if there is entry for this keyword, create a new one
152 if (!found)
154 KeywordListEntry *k = new KeywordListEntry(name, module);
155 _keywords.append(k);
159 populateKeyListBox("*");
162 void SearchWidget::populateKeyListBox(const QString& s)
164 _keyList->clear();
166 QStringList matches;
168 for(KeywordListEntry *k = _keywords.first(); k != 0; k = _keywords.next())
170 if ( QRegExp(s, Qt::CaseInsensitive, QRegExp::Wildcard).indexIn(k->moduleName()) >= 0)
171 matches.append(k->moduleName().trimmed());
174 for(QStringList::ConstIterator it = matches.begin(); it != matches.end(); ++it)
175 _keyList->addItem(*it);
177 _keyList->model()->sort(0);
180 void SearchWidget::populateResultListBox(const QString& s)
182 _resultList->clear();
184 Q3PtrList<ModuleItem> results;
186 for(KeywordListEntry *k = _keywords.first(); k != 0; k = _keywords.next())
188 if (k->moduleName() == s)
190 Q3PtrList<ConfigModule> modules = k->modules();
192 for(ConfigModule *m = modules.first(); m != 0; m = modules.next())
193 new ModuleItem(m, _resultList);
197 _resultList->model()->sort(0);
200 void SearchWidget::slotSearchTextChanged(const QString & s)
202 QString regexp = s;
203 regexp += '*';
204 populateKeyListBox(regexp);
207 void SearchWidget::slotKeywordSelected(const QString & s)
209 populateResultListBox(s);
212 void SearchWidget::slotModuleSelected(QListWidgetItem *item)
214 if (item)
215 emit moduleSelected( static_cast<ModuleItem*>(item)->module() );
218 void SearchWidget::slotModuleClicked(QListWidgetItem *item)
220 if (item)
221 emit moduleSelected( static_cast<ModuleItem*>(item)->module() );