Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / plasma / tools / engineexplorer / engineexplorer.cpp
blobeffac1696d5b562df67443b48ee5801e64524a27
1 /*
2 * Copyright 2007 Aaron Seigo <aseigo@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "engineexplorer.h"
22 #include <QApplication>
23 #include <QStandardItemModel>
24 #include <QVBoxLayout>
25 #include <QHBoxLayout>
26 #include <QSpinBox>
28 #include <KAction>
29 #include <KIconLoader>
30 #include <KIconTheme>
31 #include <KStandardAction>
33 #include "plasma/dataenginemanager.h"
35 EngineExplorer::EngineExplorer(QWidget* parent)
36 : KDialog(parent),
37 m_engine(0),
38 m_sourceCount(0)
40 setButtons(0);
41 setWindowTitle(i18n("Plasma Engine Explorer"));
42 QWidget* mainWidget = new QWidget(this);
43 setMainWidget(mainWidget);
44 setupUi(mainWidget);
46 m_engineManager = new Plasma::DataEngineManager();
47 m_dataModel = new QStandardItemModel(this);
48 KIcon pix("plasma");
49 int size = IconSize(KIconLoader::Dialog);
50 m_title->setPixmap(pix.pixmap(size, size));
51 connect(m_engines, SIGNAL(activated(QString)), this, SLOT(showEngine(QString)));
52 connect(m_sourceRequesterButton, SIGNAL(clicked(bool)), this, SLOT(requestSource()));
53 m_data->setModel(m_dataModel);
55 listEngines();
56 m_engines->setFocus();
58 addAction(KStandardAction::quit(qApp, SLOT(quit()), this));
61 EngineExplorer::~EngineExplorer()
65 void EngineExplorer::dataUpdated(const QString& source, const Plasma::DataEngine::Data& data)
67 QList<QStandardItem*> items = m_dataModel->findItems(source, 0);
69 if (items.count() < 1) {
70 return;
73 QStandardItem* parent = items.first();
75 while (parent->hasChildren()) {
76 parent->removeRow(0);
79 showData(parent, data);
82 void EngineExplorer::listEngines()
84 m_engines->clear();
85 m_engines->addItem("");
86 QStringList engines = m_engineManager->knownEngines();
87 qSort(engines);
88 m_engines->addItems(engines);
91 void EngineExplorer::showEngine(const QString& name)
93 m_sourceRequester->setEnabled(false);
94 m_sourceRequesterButton->setEnabled(false);
95 m_dataModel->clear();
96 m_dataModel->setColumnCount(3);
97 QStringList headers;
98 headers << i18n("DataSource") << i18n("Key") << i18n("Value");
99 m_dataModel->setHorizontalHeaderLabels(headers);
100 m_engine = 0;
101 m_sourceCount = 0;
103 if (!m_engineName.isEmpty()) {
104 m_engineManager->unload(m_engineName);
107 m_engineName = name;
108 if (m_engineName.isEmpty()) {
109 updateTitle();
110 return;
113 m_engine = m_engineManager->load(m_engineName);
114 if (!m_engine) {
115 m_engineName.clear();
116 updateTitle();
117 return;
120 QStringList sources = m_engine->sources();
122 //kDebug() << "showing engine " << m_engine->objectName();
123 //kDebug() << "we have " << sources.count() << " data sources";
124 foreach (const QString& source, sources) {
125 //kDebug() << "adding " << source;
126 addSource(source);
129 m_sourceRequesterButton->setEnabled(true);
130 m_updateInterval->setEnabled(true);
131 m_sourceRequester->setEnabled(true);
132 m_sourceRequester->setFocus();
133 connect(m_engine, SIGNAL(newSource(QString)), this, SLOT(addSource(QString)));
134 connect(m_engine, SIGNAL(sourceRemoved(QString)), this, SLOT(removeSource(QString)));
135 updateTitle();
138 void EngineExplorer::addSource(const QString& source)
140 QStandardItem* parent = new QStandardItem(source);
141 m_dataModel->appendRow(parent);
143 //kDebug() << "getting data for source " << source;
144 Plasma::DataEngine::Data data = m_engine->query(source);
145 showData(parent, data);
146 m_engine->connectSource(source, this);
148 ++m_sourceCount;
149 updateTitle();
152 void EngineExplorer::removeSource(const QString& source)
154 QList<QStandardItem*> items = m_dataModel->findItems(source, 0);
156 if (items.count() < 1) {
157 return;
160 foreach (QStandardItem* item, items) {
161 m_dataModel->removeRow(item->row());
164 --m_sourceCount;
165 updateTitle();
168 void EngineExplorer::requestSource()
170 if (!m_engine) {
171 return;
174 QString source = m_sourceRequester->text();
176 if (source.isEmpty()) {
177 return;
180 m_engine->connectSource(source, this, (uint)m_updateInterval->value());
183 QString EngineExplorer::convertToString(const QVariant &value) const
185 if (value.canConvert(QVariant::String)) {
186 return value.toString();
189 switch (value.type())
191 case QVariant::Point: {
192 QPoint point = value.toPoint();
193 return QString("(%1, %2)").arg(point.x()).arg(point.y());
196 default: {
197 return "<unknown>";
202 void EngineExplorer::showData(QStandardItem* parent, Plasma::DataEngine::Data data)
204 int rowCount = 0;
205 Plasma::DataEngine::DataIterator it(data);
206 // parent->insertRows(0, data.count());
207 // parent->setColumnCount(3);
208 while (it.hasNext()) {
209 it.next();
210 parent->setChild(rowCount, 1, new QStandardItem(it.key()));
211 if (it.value().canConvert(QVariant::List)) {
212 foreach(QVariant var, it.value().toList()) {
213 parent->setChild(rowCount, 2, new QStandardItem(convertToString(var)));
214 ++rowCount;
217 else {
218 parent->setChild(rowCount, 2, new QStandardItem(convertToString(it.value())));
219 ++rowCount;
224 void EngineExplorer::updateTitle()
226 if (!m_engine) {
227 m_title->setPixmap(KIcon("plasma").pixmap(IconSize(KIconLoader::Dialog)));
228 m_title->setText(i18n("Plasma DataEngine Explorer"));
229 return;
232 m_title->setText(i18nc("The name of the engine followed by the number of data sources",
233 "%1 - %2 data sources", m_engine->objectName(), m_sourceCount));
234 if (m_engine->icon().isEmpty()) {
235 m_title->setPixmap(KIcon("plasma").pixmap(IconSize(KIconLoader::Dialog)));
236 } else {
237 //m_title->setPixmap(KIcon("alarmclock").pixmap(IconSize(KIconLoader::Dialog)));
238 m_title->setPixmap(KIcon(m_engine->icon()).pixmap(IconSize(KIconLoader::Dialog)));
242 #include "engineexplorer.moc"