Fix crash if key bindings specified in profile cannot be found. Improve
[personal-kdebase.git] / apps / kinfocenter / moduletreeview.cpp
blob5e23cd5c699837ee76dc309f716860aa52873757
1 /*
2 Copyright (c) 2000 Matthias Elter <elter@kde.org>
3 Copyright (c) 1999 Matthias Hoelzer-Kluepfel <hoelzer@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 "moduletreeview.h"
21 #include "global.h"
22 #include "modules.h"
24 #include <klocale.h>
25 #include <kiconloader.h>
26 #include <kservicegroup.h>
27 #include <kdebug.h>
28 #include <kicon.h>
31 #include "moduletreeview.moc"
33 ModuleTreeView::ModuleTreeView(ConfigModuleList *modules, QWidget * parent) :
34 QListWidget(parent), _modules(modules) {
36 connect(this, SIGNAL(itemSelectionChanged()), this, SLOT(selectItem()));
38 setSortingEnabled(false);
39 _generalItem = NULL;
42 void ModuleTreeView::fill() {
43 _generalItem = new QListWidgetItem(KIcon(KINFOCENTER_ICON_NAME), i18n("General Information"), this);
45 foreach(ConfigModule* configModule, *_modules) {
46 new ModuleTreeItem(this, configModule);
50 void ModuleTreeView::selectItem() {
51 QListWidgetItem* item = this->currentItem();
52 if (item==NULL)
53 return;
55 if (isGeneralItem(item)) {
56 kDebug() << "General Selected" << endl;
57 emit generalSelected();
58 return;
61 kDebug() << "Select item" << endl;
62 ModuleTreeItem* moduleItem = static_cast<ModuleTreeItem*>(item);
64 emit moduleSelected(moduleItem->module());
65 kDebug() << "Select item end" << endl;
69 bool ModuleTreeView::isGeneralItem(const QListWidgetItem* item) const {
70 if (item == _generalItem)
71 return true;
73 return false;
76 QListWidgetItem* ModuleTreeView::generalItem() const {
77 return _generalItem;
80 ModuleTreeItem* ModuleTreeView::findMatchingItem(ConfigModule* configModule) const {
81 for (int i = 0; i < count() ; ++i) {
82 QListWidgetItem* tempItem = item(i);
83 if (isGeneralItem(tempItem)) {
84 continue;
87 ModuleTreeItem* moduleItem = static_cast<ModuleTreeItem*>(tempItem);
88 if (moduleItem->module()==configModule)
89 return moduleItem;
93 kDebug() << "Unable to find the matching item" << endl;
94 return NULL;
98 ModuleTreeItem::ModuleTreeItem(QListWidget *parent, ConfigModule *module) :
99 QListWidgetItem(parent), _module(module) {
101 setText(module->moduleName());
103 setIcon(module->realIcon(KIconLoader::SizeSmall));
107 ConfigModule* ModuleTreeItem::module() const {
108 return _module;
112 ModuleWidgetSearchLine::ModuleWidgetSearchLine(QWidget* parent, ModuleTreeView* listWidget) :
113 KListWidgetSearchLine(parent, listWidget) {
117 bool ModuleWidgetSearchLine::itemMatches(const QListWidgetItem* item, const QString& search) const {
119 const ModuleTreeView* moduleTree = static_cast<const ModuleTreeView*>(listWidget());
120 if (moduleTree->isGeneralItem(item)) {
121 return true;
124 const ModuleTreeItem* moduleItem = static_cast<const ModuleTreeItem*>(item);
126 QStringList itemMatches;
127 itemMatches << moduleItem->module()->moduleName();
128 //kDebug() << "Module name " << moduleItem->module()->moduleName() << endl;
130 QStringList keywords = moduleItem->module()->keywords();
131 foreach(const QString &keyword, keywords) {
132 //kDebug() << "Key word " << keyword << endl;
133 itemMatches.append(keyword);
137 foreach(const QString &itemMatch, itemMatches) {
138 if (itemMatch.contains(search, Qt::CaseInsensitive)) {
139 return true;
143 return false;