@@ -18,6 +18,27 @@ void ScriptModel::updateValuesUnique(const QVariantList& newValues) {
1818 auto iter = this ->mValues .begin ();
1919 auto newIter = newValues.begin ();
2020
21+ // TODO: cache this
22+ auto getCmpKey = [&](const QVariant& v) {
23+ if (v.canConvert <QVariantMap>()) {
24+ auto vMap = v.value <QVariantMap>();
25+ if (vMap.contains (this ->cmpKey )) {
26+ return vMap.value (this ->cmpKey );
27+ }
28+ }
29+
30+ return v;
31+ };
32+
33+ auto variantCmp = [&](const QVariant& a, const QVariant& b) {
34+ if (!this ->cmpKey .isEmpty ()) return getCmpKey (a) == getCmpKey (b);
35+ else return a == b;
36+ };
37+
38+ auto eqPredicate = [&](const QVariant& b) {
39+ return [&](const QVariant& a) { return variantCmp (a, b); };
40+ };
41+
2142 while (true ) {
2243 if (newIter == newValues.end ()) {
2344 if (iter == this ->mValues .end ()) break ;
@@ -40,18 +61,19 @@ void ScriptModel::updateValuesUnique(const QVariantList& newValues) {
4061 this ->endInsertRows ();
4162
4263 break ;
43- } else if (*newIter != *iter) {
44- auto oldIter = std::find (iter, this ->mValues .end (), *newIter);
64+ } else if (! variantCmp ( *newIter, *iter) ) {
65+ auto oldIter = std::find_if (iter, this ->mValues .end (), eqPredicate ( *newIter) );
4566
4667 if (oldIter != this ->mValues .end ()) {
47- if (std::find (newIter, newValues.end (), *iter) == newValues.end ()) {
68+ if (std::find_if (newIter, newValues.end (), eqPredicate ( *iter) ) == newValues.end ()) {
4869 // Remove any entries we would otherwise move around that aren't in the new list.
4970 auto startIter = iter;
5071
5172 do {
5273 ++iter;
5374 } while (iter != this ->mValues .end ()
54- && std::find (newIter, newValues.end (), *iter) == newValues.end ());
75+ && std::find_if (newIter, newValues.end (), eqPredicate (*iter)) == newValues.end ()
76+ );
5577
5678 auto index = static_cast <qint32>(std::distance (this ->mValues .begin (), iter));
5779 auto startIndex = static_cast <qint32>(std::distance (this ->mValues .begin (), startIter));
@@ -66,7 +88,7 @@ void ScriptModel::updateValuesUnique(const QVariantList& newValues) {
6688 ++oldIter;
6789 ++newIter;
6890 } while (oldIter != this ->mValues .end () && newIter != newValues.end ()
69- && *oldIter == *newIter);
91+ && variantCmp ( *oldIter, *newIter) );
7092
7193 auto index = static_cast <qint32>(std::distance (this ->mValues .begin (), iter));
7294 auto oldStartIndex =
@@ -90,7 +112,8 @@ void ScriptModel::updateValuesUnique(const QVariantList& newValues) {
90112 do {
91113 newIter++;
92114 } while (newIter != newValues.end ()
93- && std::find (iter, this ->mValues .end (), *newIter) == this ->mValues .end ());
115+ && std::find_if (iter, this ->mValues .end (), eqPredicate (*newIter))
116+ == this ->mValues .end ());
94117
95118 auto index = static_cast <qint32>(std::distance (this ->mValues .begin (), iter));
96119 auto newIndex = static_cast <qint32>(std::distance (newValues.begin (), newIter));
@@ -123,6 +146,13 @@ void ScriptModel::setValues(const QVariantList& newValues) {
123146 emit this ->valuesChanged ();
124147}
125148
149+ void ScriptModel::setObjectProp (const QString& objectProp) {
150+ if (objectProp == this ->cmpKey ) return ;
151+ this ->cmpKey = objectProp;
152+ this ->updateValuesUnique (this ->mValues );
153+ emit this ->objectPropChanged ();
154+ }
155+
126156qint32 ScriptModel::rowCount (const QModelIndex& parent) const {
127157 if (parent != QModelIndex ()) return 0 ;
128158 return static_cast <qint32>(this ->mValues .length ());
0 commit comments