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>
29 #include <KIconLoader>
31 #include <KStandardAction>
33 #include "plasma/dataenginemanager.h"
35 EngineExplorer::EngineExplorer(QWidget
* parent
)
41 setWindowTitle(i18n("Plasma Engine Explorer"));
42 QWidget
* mainWidget
= new QWidget(this);
43 setMainWidget(mainWidget
);
46 m_engineManager
= new Plasma::DataEngineManager();
47 m_dataModel
= new QStandardItemModel(this);
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
);
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) {
73 QStandardItem
* parent
= items
.first();
75 while (parent
->hasChildren()) {
79 showData(parent
, data
);
82 void EngineExplorer::listEngines()
85 m_engines
->addItem("");
86 QStringList engines
= m_engineManager
->knownEngines();
88 m_engines
->addItems(engines
);
91 void EngineExplorer::showEngine(const QString
& name
)
93 m_sourceRequester
->setEnabled(false);
94 m_sourceRequesterButton
->setEnabled(false);
96 m_dataModel
->setColumnCount(3);
98 headers
<< i18n("DataSource") << i18n("Key") << i18n("Value");
99 m_dataModel
->setHorizontalHeaderLabels(headers
);
103 if (!m_engineName
.isEmpty()) {
104 m_engineManager
->unload(m_engineName
);
108 if (m_engineName
.isEmpty()) {
113 m_engine
= m_engineManager
->load(m_engineName
);
115 m_engineName
.clear();
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;
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
)));
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);
152 void EngineExplorer::removeSource(const QString
& source
)
154 QList
<QStandardItem
*> items
= m_dataModel
->findItems(source
, 0);
156 if (items
.count() < 1) {
160 foreach (QStandardItem
* item
, items
) {
161 m_dataModel
->removeRow(item
->row());
168 void EngineExplorer::requestSource()
174 QString source
= m_sourceRequester
->text();
176 if (source
.isEmpty()) {
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());
202 void EngineExplorer::showData(QStandardItem
* parent
, Plasma::DataEngine::Data data
)
205 Plasma::DataEngine::DataIterator
it(data
);
206 // parent->insertRows(0, data.count());
207 // parent->setColumnCount(3);
208 while (it
.hasNext()) {
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
)));
218 parent
->setChild(rowCount
, 2, new QStandardItem(convertToString(it
.value())));
224 void EngineExplorer::updateTitle()
227 m_title
->setPixmap(KIcon("plasma").pixmap(IconSize(KIconLoader::Dialog
)));
228 m_title
->setText(i18n("Plasma DataEngine Explorer"));
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
)));
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"