• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

superkaramba

  • sources
  • kde-4.12
  • kdeutils
  • superkaramba
  • src
  • sensors
plasmaengine.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Sebastian Sauer <mail@dipe.org>
3  *
4  * This file is part of SuperKaramba.
5  *
6  * SuperKaramba is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * SuperKaramba is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with SuperKaramba; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "plasmaengine.h"
22 #include <kdebug.h>
23 
24 #include <plasma/dataenginemanager.h>
25 
26 #if 0
27 #include <QFile>
28 #include <QTextStream>
29 #endif
30 
32 QVariantMap dataToMap(Plasma::DataEngine::Data data)
33 {
34  QVariantMap map;
35  Plasma::DataEngine::DataIterator it(data);
36  while( it.hasNext() ) {
37  it.next();
38  map.insert(it.key(), it.value());
39  }
40  return map;
41 }
42 
43 /*
45 Plasma::DataEngine::Data mapToData(QVariantMap map)
46 {
47  Plasma::DataEngine::Data data;
48  for(QVariantMap::Iterator it = map.begin(); it != map.end(); ++it)
49  data.insert(it.key(), it.value());
50  return data;
51 }
52 */
53 
54 /*****************************************************************************************
55 * PlasmaSensorConnector
56 */
57 
59 class PlasmaSensorConnector::Private
60 {
61  public:
62  Meter* meter;
63  QString source;
64  QString format;
65 };
66 
67 PlasmaSensorConnector::PlasmaSensorConnector(Meter *meter, const QString& source) : QObject(meter), d(new Private)
68 {
69  //kDebug()<<"PlasmaSensorConnector Ctor"<<endl;
70  setObjectName(source);
71  d->meter = meter;
72  d->source = source;
73 }
74 
75 PlasmaSensorConnector::~PlasmaSensorConnector()
76 {
77  //kDebug()<<"PlasmaSensorConnector Dtor"<<endl;
78  delete d;
79 }
80 
81 Meter* PlasmaSensorConnector::meter() const
82 {
83  return d->meter;
84 }
85 
86 QString PlasmaSensorConnector::source() const
87 {
88  return d->source;
89 }
90 
91 void PlasmaSensorConnector::setSource(const QString& source)
92 {
93  d->source = source;
94 }
95 
96 QString PlasmaSensorConnector::format() const
97 {
98  return d->format;
99 }
100 
101 void PlasmaSensorConnector::setFormat(const QString& format)
102 {
103  d->format = format;
104 }
105 
106 void PlasmaSensorConnector::dataUpdated(const QString& source, const Plasma::DataEngine::Data &data)
107 {
108  //kDebug()<<"PlasmaSensorConnector::dataUpdated d->source="<<d->source<<" source="<<source<<endl;
109  if( d->source.isEmpty() ) {
110  emit sourceUpdated(source, dataToMap(data));
111  return;
112  }
113  if( source != d->source ) {
114  return;
115  }
116  QString v = d->format;
117  Plasma::DataEngine::DataIterator it(data);
118  while( it.hasNext() ) {
119  it.next();
120  QString s = QString("%%1").arg( it.key() );
121  v.replace(s,it.value().toString());
122  }
123  d->meter->setValue(v);
124 }
125 
126 /*****************************************************************************************
127 * PlasmaSensor
128 */
129 
131 class PlasmaSensor::Private
132 {
133  public:
134  Plasma::DataEngine* engine;
135  QString engineName;
136  explicit Private() : engine(0) {}
137 };
138 
139 PlasmaSensor::PlasmaSensor(int msec) : Sensor(msec), d(new Private)
140 {
141  kDebug()<<"PlasmaSensor Ctor"<<endl;
142 }
143 
144 PlasmaSensor::~PlasmaSensor()
145 {
146  kDebug()<<"PlasmaSensor Dtor"<<endl;
147  delete d;
148 }
149 
150 Plasma::DataEngine* PlasmaSensor::engineImpl() const
151 {
152  return d->engine;
153 }
154 
155 void PlasmaSensor::setEngineImpl(Plasma::DataEngine* engine, const QString& engineName)
156 {
157  d->engine = engine;
158  d->engineName = engineName;
159 }
160 
161 QString PlasmaSensor::engine()
162 {
163  return d->engine ? d->engineName : QString();
164 }
165 
166 void PlasmaSensor::setEngine(const QString& name)
167 {
168  //kDebug()<<"PlasmaSensor::setEngine name="<<name<<endl;
169  if( d->engine ) {
170  disconnect(d->engine, SIGNAL(newSource(QString)), this, SIGNAL(sourceAdded(QString)));
171  disconnect(d->engine, SIGNAL(sourceRemoved(QString)), this, SIGNAL(sourceRemoved(QString)));
172  Plasma::DataEngineManager::self()->unloadEngine(d->engineName);
173  }
174  d->engineName.clear();
175  d->engine = Plasma::DataEngineManager::self()->engine(name);
176  if( ! d->engine || ! d->engine->isValid() ) {
177  d->engine = Plasma::DataEngineManager::self()->loadEngine(name);
178  if( ! d->engine || ! d->engine->isValid() ) {
179  kWarning()<<"PlasmaSensor::setEngine: No such engine: "<<name<<endl;
180  return;
181  }
182  }
183  d->engineName = name;
184  connect(d->engine, SIGNAL(newSource(QString)), this, SIGNAL(sourceAdded(QString)));
185  connect(d->engine, SIGNAL(sourceRemoved(QString)), this, SIGNAL(sourceRemoved(QString)));
186  //d->engine->setProperty("reportSeconds", true);
187 }
188 
189 bool PlasmaSensor::isValid() const
190 {
191  return d->engine && d->engine->isValid();
192 }
193 
194 QStringList PlasmaSensor::sources() const
195 {
196  return d->engine ? d->engine->sources() : QStringList();
197 }
198 
199 QVariant PlasmaSensor::property(const QByteArray& name) const
200 {
201  return d->engine ? d->engine->property(name) : QVariant();
202 }
203 
204 void PlasmaSensor::setProperty(const QByteArray& name, const QVariant& value)
205 {
206  if( d->engine )
207  d->engine->setProperty(name, value);
208 }
209 
210 QVariantMap PlasmaSensor::query(const QString& source)
211 {
212  //kDebug()<<"PlasmaSensor::query"<<endl;
213  return d->engine ? dataToMap(d->engine->query(source)) : QVariantMap();
214 }
215 
216 QObject* PlasmaSensor::connectSource(const QString& source, QObject* visualization)
217 {
218  //kDebug()<<"PlasmaSensor::connectSource source="<<source<<endl;
219  if( ! d->engine ) {
220  kWarning()<<"PlasmaSensor::connectSource: No engine"<<endl;
221  return 0;
222  }
223  if( Meter* m = dynamic_cast<Meter*>(visualization) ) {
224  PlasmaSensorConnector* c = new PlasmaSensorConnector(m, source);
225  d->engine->connectSource(source, c);
226  kDebug()<<"PlasmaSensor::connectSource meter, engine isValid="<<d->engine->isValid();
227  return c;
228  }
229  d->engine->connectSource(source, visualization ? visualization : this);
230  return 0;
231 }
232 
233 void PlasmaSensor::disconnectSource(const QString& source, QObject* visualization)
234 {
235  //kDebug()<<"PlasmaSensor::disconnectSource"<<endl;
236  if( Meter* m = dynamic_cast<Meter*>(visualization) ) {
237  foreach(PlasmaSensorConnector* c, m->findChildren<PlasmaSensorConnector*>(source))
238  if( c->meter() == m )
239  delete c;
240  }
241  else if( d->engine ) {
242  d->engine->disconnectSource(source, visualization ? visualization : this);
243  }
244  else
245  kWarning()<<"PlasmaSensor::disconnectSource: No engine"<<endl;
246 }
247 
248 void PlasmaSensor::update()
249 {
250  kDebug()<<"PlasmaSensor::update"<<endl;
251  /*TODO
252  foreach(QObject *it, *objList) {
253  SensorParams *sp = qobject_cast<SensorParams*>(it);
254  Meter *meter = sp->getMeter();
255  const QString format = sp->getParam("FORMAT");
256  //if (format.length() == 0) format = "%um";
257  //format.replace(QRegExp("%fmb", Qt::CaseInsensitive),QString::number((int)((totalMem - usedMemNoBuffers) / 1024.0 + 0.5)));
258  //meter->setValue(format);
259  }
260  */
261 }
262 
263 void PlasmaSensor::dataUpdated(const QString& source, Plasma::DataEngine::Data data)
264 {
265  //kDebug()<<"PlasmaSensor::dataUpdated source="<<source<<endl;
266  emit sourceUpdated(source, dataToMap(data));
267 }
268 
269 #include "plasmaengine.moc"
PlasmaSensor::engineImpl
Plasma::DataEngine * engineImpl() const
Definition: plasmaengine.cpp:150
PlasmaSensor::setEngineImpl
void setEngineImpl(Plasma::DataEngine *engine, const QString &engineName)
Definition: plasmaengine.cpp:155
PlasmaSensor::sourceRemoved
void sourceRemoved(const QString &source)
Emitted when a data source is removed.
PlasmaSensor::~PlasmaSensor
virtual ~PlasmaSensor()
Definition: plasmaengine.cpp:144
PlasmaSensor::dataUpdated
virtual void dataUpdated(const QString &source, Plasma::DataEngine::Data data)
Plasma calls this if data changed.
Definition: plasmaengine.cpp:263
PlasmaSensor::disconnectSource
virtual void disconnectSource(const QString &source, QObject *visualization=0)
Disconnect from a source.
Definition: plasmaengine.cpp:233
PlasmaSensor::update
virtual void update()
Request to update the sensor.
Definition: plasmaengine.cpp:248
QObject
PlasmaSensor::sourceAdded
void sourceAdded(const QString &source)
Emitted when a new data source is created.
Sensor
Definition: sensor.h:17
PlasmaSensor::property
QVariant property(const QByteArray &name) const
Get a property.
Definition: plasmaengine.cpp:199
PlasmaSensorConnector::meter
Meter * meter() const
Definition: plasmaengine.cpp:81
PlasmaSensor::PlasmaSensor
PlasmaSensor(int interval=-1)
Definition: plasmaengine.cpp:139
plasmaengine.h
PlasmaSensorConnector::source
QString source() const
Returns the name of the data source.
Definition: plasmaengine.cpp:86
PlasmaSensorConnector::setFormat
void setFormat(const QString &format)
Set the format the data should use for displaying.
Definition: plasmaengine.cpp:101
PlasmaSensorConnector
This is a helper class that connects a Plasma::DataEngine together with a SuperKaramba Meter widget...
Definition: plasmaengine.h:36
dataToMap
QVariantMap dataToMap(Plasma::DataEngine::Data data)
Definition: plasmaengine.cpp:32
PlasmaSensorConnector::format
QString format() const
Return the format the data should used for displaying.
Definition: plasmaengine.cpp:96
PlasmaSensor::setEngine
virtual void setEngine(const QString &name)
Set the engine that should be used.
Definition: plasmaengine.cpp:166
PlasmaSensor::query
QVariantMap query(const QString &source)
Gets the data associated with a data source.
Definition: plasmaengine.cpp:210
PlasmaSensorConnector::PlasmaSensorConnector
PlasmaSensorConnector(Meter *meter, const QString &source)
Definition: plasmaengine.cpp:67
PlasmaSensorConnector::~PlasmaSensorConnector
virtual ~PlasmaSensorConnector()
Definition: plasmaengine.cpp:75
PlasmaSensor::connectSource
virtual QObject * connectSource(const QString &source, QObject *visualization=0)
Connect with a source.
Definition: plasmaengine.cpp:216
PlasmaSensorConnector::sourceUpdated
void sourceUpdated(const QString &source, QVariantMap data)
Emitted when a data source got updated.
Meter
Definition: meters/meter.h:23
PlasmaSensor::sourceUpdated
void sourceUpdated(const QString &source, QVariantMap data)
Emitted when a data source got updated.
PlasmaSensor::engine
QString engine()
Return the name of the engine.
Definition: plasmaengine.cpp:161
PlasmaSensor::setProperty
void setProperty(const QByteArray &name, const QVariant &value)
Set a property.
Definition: plasmaengine.cpp:204
PlasmaSensorConnector::setSource
void setSource(const QString &source)
Set the name of the data source.
Definition: plasmaengine.cpp:91
PlasmaSensor::sources
QStringList sources() const
Return a list of all the data sources available via this DataEngine.
Definition: plasmaengine.cpp:194
PlasmaSensor::isValid
bool isValid() const
Returns true if an engine was defined and if the engine is valid else false got returned.
Definition: plasmaengine.cpp:189
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

superkaramba

Skip menu "superkaramba"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal