Commit 0503c892 authored by Volker Krause's avatar Volker Krause
Browse files

Also make PropertyRatioSource work with multiple Provider instances

parent ded920fd
......@@ -26,6 +26,7 @@
#include <QDebug>
#include <QtTest/qtest.h>
#include <QObject>
#include <QSettings>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QStandardPaths>
#endif
......@@ -129,6 +130,37 @@ private slots:
QCOMPARE(v.type(), QVariant::Double);
}
void testMultiPropertyRatioSource()
{
QSettings s;
s.remove(QStringLiteral("MultiPropSource"));
s.beginGroup(QStringLiteral("MultiPropSource"));
{
setProp(5198);
PropertyRatioSource src1(this, "prop", QStringLiteral("ratioSample"));
src1.addValueMapping(5198, QStringLiteral("value1"));
src1.load(&s);
PropertyRatioSource src2(this, "prop", QStringLiteral("ratioSample"));
src2.addValueMapping(5198, QStringLiteral("value2"));
src2.load(&s);
QTest::qWait(1200);
src1.store(&s);
src2.store(&s);
}
PropertyRatioSource src3(this, "prop", QStringLiteral("ratioSample"));
src3.load(&s);
const auto map = src3.data().toMap();
QCOMPARE(map.size(), 2);
QVERIFY(map.contains(QStringLiteral("value1")));
QVERIFY(map.contains(QStringLiteral("value2")));
}
void testApplicationVersionSource()
{
ApplicationVersionSource src;
......
......@@ -45,7 +45,8 @@ public:
QMetaProperty property;
QString previousValue;
QTime lastChangeTime;
QHash<QString, int> ratioSet;
QHash<QString, int> ratioSet; // data we are currently tracking
QHash<QString, int> baseRatioSet; // data loaded from storage
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
QMap<QVariant, QString> valueMap;
#else
......@@ -166,11 +167,12 @@ QVariant PropertyRatioSource::data()
QVariantMap m;
int total = 0;
for (auto it = d->ratioSet.constBegin(); it != d->ratioSet.constEnd(); ++it)
total += it.value();
total += it.value() + d->baseRatioSet.value(it.key());
for (auto it = d->ratioSet.constBegin(); it != d->ratioSet.constEnd(); ++it) {
double currentValue = it.value() + d->baseRatioSet.value(it.key());
QVariantMap v;
v.insert(QStringLiteral("property"), (double)it.value() / (double)(total));
v.insert(QStringLiteral("property"), currentValue / (double)(total));
m.insert(it.key(), v);
}
......@@ -181,8 +183,10 @@ void PropertyRatioSource::load(QSettings *settings)
{
Q_D(PropertyRatioSource);
foreach (const auto &value, settings->childKeys()) {
const auto amount = settings->value(value, 0).toInt();
d->ratioSet.insert(value, amount);
const auto amount = std::max(settings->value(value, 0).toInt(), 0);
d->baseRatioSet.insert(value, amount);
if (!d->ratioSet.contains(value))
d->ratioSet.insert(value, 0);
}
}
......@@ -192,9 +196,15 @@ void PropertyRatioSource::store(QSettings *settings)
Q_D(PropertyRatioSource);
d->propertyChanged();
settings->remove(QString());
for (auto it = d->ratioSet.constBegin(); it != d->ratioSet.constEnd(); ++it) {
settings->setValue(it.key(), it.value());
// note that a second process can have updated the data meanwhile!
for (auto it = d->ratioSet.begin(); it != d->ratioSet.end(); ++it) {
if (it.value() == 0)
continue;
const auto oldValue = std::max(settings->value(it.key(), 0).toInt(), 0);
const auto newValue = oldValue + it.value();
settings->setValue(it.key(), newValue);
*it = 0;
d->baseRatioSet.insert(it.key(), newValue);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment