refs #116 using CVariant in the implementation of CIndexVariantMap

This commit is contained in:
Mathew Sutcliffe
2014-05-19 23:15:05 +01:00
parent 8f2c733f47
commit c6e9301078
3 changed files with 18 additions and 40 deletions

View File

@@ -42,16 +42,16 @@ namespace BlackMisc
QString s;
foreach(int index, this->m_values.keys())
{
QVariant qv = this->m_values.value(index);
CVariant v = this->m_values.value(index);
s.isEmpty() ?
s.append("{wildcard: ").append(this->m_wildcard ? "true" : "false").append(" ") :
s.append(", ");
s.append('{').append(QString::number(index)).append(": ");
s.append("(").append(QString::number(qv.userType())).append(") ");
QString qvs = BlackMisc::qVariantToString(qv, i18n);
s.append(qvs);
s.append("(").append(QString::number(v.userType())).append(") ");
QString vs = v.toString(i18n);
s.append(vs);
s.append('}');
}
s = s.append("}");
@@ -90,18 +90,8 @@ namespace BlackMisc
*/
void CIndexVariantMap::marshallToDbus(QDBusArgument &argument) const
{
// remark, tried both sending as QDbusVariant and QVariant
// does not make a difference
QList<int> unifiedBlackTypeIds;
QList<QDBusVariant> dbusVariants;
foreach(QVariant qv, m_values.values())
{
unifiedBlackTypeIds << qv.userType() - BlackMisc::firstBlackMetaType();
dbusVariants << QDBusVariant(qv);
}
argument << this->m_values.keys(); // indexes
argument << dbusVariants;
argument << unifiedBlackTypeIds;
argument << this->m_values.keys();
argument << this->m_values.values();
}
/*
@@ -110,27 +100,14 @@ namespace BlackMisc
void CIndexVariantMap::unmarshallFromDbus(const QDBusArgument &argument)
{
QList<int> indexes;
QList<QDBusVariant> values;
QList<int> unifiedBlackTypeIds;
QList<CVariant> values;
argument >> indexes;
argument >> values;
argument >> unifiedBlackTypeIds;
QMap<int, QVariant> newMap;
Q_ASSERT(indexes.size() == values.size());
QMap<int, CVariant> newMap;
for (int i = 0; i < indexes.size(); i++)
{
QVariant qv = values.at(i).variant();
int index = indexes.at(i);
if (qv.canConvert<QDBusArgument>())
{
int userType = unifiedBlackTypeIds.at(i) + BlackMisc::firstBlackMetaType();
QVariant concrete = BlackMisc::fixQVariantFromDbusArgument(qv, userType);
newMap.insert(index, concrete);
}
else
{
// value already OK
newMap.insert(index, qv);
}
newMap.insert(indexes[i], values[i]);
}
// replace values in one step
this->m_values.swap(newMap);

View File

@@ -8,6 +8,7 @@
*/
#include "valueobject.h"
#include "variant.h"
#include <QVariantMap>
#include <QDBusArgument>
@@ -45,13 +46,13 @@ namespace BlackMisc
//! Add a value as non QVariant
template<class T> void addValue(int index, const T &value) { this->m_values.insert(index, QVariant::fromValue(value)); }
template<class T> void addValue(int index, const T &value) { this->m_values.insert(index, CVariant::fromValue(value)); }
//! Is empty?
bool isEmpty() const { return this->m_values.isEmpty(); }
//! Value
QVariant value(int index) const { return this->m_values.value(index); }
QVariant value(int index) const { return this->m_values.value(index).toQVariant(); }
//! Set value
void value(int index, const QVariant &value) { this->m_values.value(index, value); }
@@ -63,7 +64,7 @@ namespace BlackMisc
bool contains(int index) const { return this->m_values.contains(index); }
//! values
QList<QVariant> values() const { return this->m_values.values(); }
QList<CVariant> values() const { return this->m_values.values(); }
//! \brief Wildcard, only relevant when used in search
bool isWildcard() const { return this->m_wildcard; }
@@ -81,7 +82,7 @@ namespace BlackMisc
bool operator !=(const CIndexVariantMap &other) const;
//! Map
const QMap<int, QVariant> &map() const { return this->m_values; }
const QMap<int, CVariant> &map() const { return this->m_values; }
//! \copydoc CValueObject::getValueHash
virtual uint getValueHash() const override;
@@ -90,7 +91,7 @@ namespace BlackMisc
static void registerMetadata();
protected:
QMap<int, QVariant> m_values; //!< values
QMap<int, CVariant> m_values; //!< values
bool m_wildcard; //!< wildcard
//! \copydoc CValueObject::convertToQString

View File

@@ -115,7 +115,7 @@ namespace BlackMisc
const auto &map = indexMap.map();
for (auto it = map.begin(); it != map.end(); ++it)
{
this->setPropertyByIndex(it.value(), it.key());
this->setPropertyByIndex(it.value().toQVariant(), it.key());
}
return c;
}
@@ -131,7 +131,7 @@ namespace BlackMisc
{
// QVariant cannot be compared directly
QVariant p = valueObject.propertyByIndex(it.key()); // from value object
QVariant v = it.value(); // from map
QVariant v = it.value().toQVariant(); // from map
if (!BlackMisc::equalQVariants(p, v)) return false;
}
return true;