From kde-commits Tue Jul 31 23:29:06 2007 From: Sebastian Sauer Date: Tue, 31 Jul 2007 23:29:06 +0000 To: kde-commits Subject: KDE/kdeutils/superkaramba/skapplet Message-Id: <1185924546.908476.21475.nullmailer () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=118592456418545 SVN commit 694880 by sebsauer: We are now able to connect a Plasma::DataEngine to a SuperKaramba meter-widget and it works fine :) See also the second sample at http://techbase.kde.org/Development/Tutorials/SuperKaramba#Plasma M +41 -28 skengineadaptor.h --- trunk/KDE/kdeutils/superkaramba/skapplet/skengineadaptor.h #694879:694880 @@ -30,6 +30,27 @@ namespace Skip { +/// \internal helper function that translates plasma data into a QVariantMap. +static QVariantMap dataToMap(Plasma::DataEngine::Data data) { + QVariantMap map; + Plasma::DataEngine::DataIterator it(data); + while( it.hasNext() ) { + it.next(); + map.insert(it.key(), it.value()); + } + return map; +} + +/* +/// \internal helper function that translates a QVariantMap into plasma data. +static Plasma::DataEngine::Data mapToData(QVariantMap map) { + Plasma::DataEngine::Data data; + for(QVariantMap::Iterator it = map.begin(); it != map.end(); ++it) + data.insert(it.key(), it.value()); + return data; +} +*/ + /** * This is a helper class that connects a Plasma::DataEngine together * with a SuperKaramba Meter. @@ -38,7 +59,7 @@ { Q_OBJECT public: - explicit EngineConnector(Meter *meter) : QObject(meter), m_meter(meter) { Q_ASSERT(m_meter); } + explicit EngineConnector(Meter *meter, const QString& source) : QObject(meter), m_meter(meter), m_source(source) { Q_ASSERT(m_meter); } virtual ~EngineConnector() {} Meter* meter() const { return m_meter; } @@ -66,12 +87,18 @@ private Q_SLOTS: + /// Plasma calls this if data changed. void updated(const QString& source, Plasma::DataEngine::Data data) { - Q_UNUSED(source); - Q_UNUSED(data); - kDebug()<<"##################### EngineConnector::updated()"<setValue(m_format, data.contains(m_data) ? data[m_data] : QVariant()); + if( source != m_source ) + return; + QString v = m_format; + Plasma::DataEngine::DataIterator it(data); + while( it.hasNext() ) { + it.next(); + QString s = QString("%%1").arg( it.key() ); + v.replace(s,it.value().toString()); + } + m_meter->setValue(v); } private: @@ -121,11 +148,14 @@ * Connect with a source. Each time the source is updated * the sourceUpdated() signal will be emitted. */ - void connectSource(const QString& source, QObject* visualization = 0) { - if( Meter* m = dynamic_cast(visualization) ) - m_engine->connectSource(source, new EngineConnector(m)); - else - m_engine->connectSource(source, visualization ? visualization : this); + QObject* connectSource(const QString& source, QObject* visualization = 0) { + if( Meter* m = dynamic_cast(visualization) ) { + EngineConnector* c = new EngineConnector(m, source); + m_engine->connectSource(source, c); + return c; + } + m_engine->connectSource(source, visualization ? visualization : this); + return 0; } /** @@ -171,23 +201,6 @@ private: Plasma::DataEngine* m_engine; - - QVariantMap dataToMap(Plasma::DataEngine::Data data) { - QVariantMap map; - Plasma::DataEngine::DataIterator it(data); - while( it.hasNext() ) { - it.next(); - map.insert(it.key(), it.value()); - } - return map; - } - - Plasma::DataEngine::Data mapToData(QVariantMap map) { - Plasma::DataEngine::Data data; - for(QVariantMap::Iterator it = map.begin(); it != map.end(); ++it) - data.insert(it.key(), it.value()); - return data; - } }; } // end of namespace SuperKarambaPlasmaApplet