~ubuntu-sdk-team/ubuntu-ui-toolkit/sourceOverflow

« back to all changes in this revision

Viewing changes to modules/Ubuntu/Components/plugin/statesaverbackend_p.cpp

  • Committer: Tarmac
  • Author(s): Florian Boucault
  • Date: 2014-07-29 12:51:03 UTC
  • mfrom: (1162.2.5 state_saving_store_type)
  • Revision ID: tarmac-20140729125103-i14q5g10wz32i89k
StateSaver: also save the type of each property along with its value so that we can convert them back to the right type during state restoration.
This makes some problematic cases such as enumeration to work.

Approved by Christian Dywan, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
145
145
        }
146
146
        QQmlProperty qmlProperty(item, propertyName.toLocal8Bit().constData(), qmlContext(item));
147
147
        if (qmlProperty.isValid() && qmlProperty.isWritable()) {
148
 
            qmlProperty.write(value);
149
 
            result++;
 
148
            QVariant type = m_archive.data()->value(propertyName + "_TYPE");
 
149
            value.convert(type.toInt());
 
150
            bool writeSuccess = qmlProperty.write(value);
 
151
            if (writeSuccess) {
 
152
                result++;
 
153
            } else {
 
154
                qmlInfo(item) << UbuntuI18n::instance().tr("property \"%1\" of "
 
155
                    "object %2 has type %3 and cannot be set to value \"%4\" of"
 
156
                    " type %5").arg(propertyName)
 
157
                               .arg(qmlContext(item)->nameForObject(item))
 
158
                               .arg(qmlProperty.propertyTypeName())
 
159
                               .arg(value.toString())
 
160
                               .arg(value.typeName());
 
161
            }
150
162
        } else {
151
163
            qmlInfo(item) << UbuntuI18n::instance().tr("property \"%1\" does not exist or is not writable for object %2")
152
164
                             .arg(propertyName).arg(qmlContext(item)->nameForObject(item));
173
185
            QVariant value = qmlProperty.read();
174
186
            if (static_cast<QMetaType::Type>(value.type()) != QMetaType::QObjectStar) {
175
187
                m_archive.data()->setValue(propertyName, value);
 
188
                /* Save the type of the property along with its value.
 
189
                 * This is important because QSettings deserializes values as QString.
 
190
                 * Setting these strings to QML properties usually works because the
 
191
                 * implicit type conversion from string to the type of the QML property
 
192
                 * usually works. In some cases cases however (e.g. enum) it fails.
 
193
                 *
 
194
                 * See Qt Bug: https://bugreports.qt-project.org/browse/QTBUG-40474
 
195
                 */
 
196
                m_archive.data()->setValue(propertyName + "_TYPE", QVariant::fromValue((int)value.type()));
176
197
                result++;
177
198
            }
178
199
        }