Paste P147

Masterwork From Distant Lands
ActivePublic

Authored by dkazakov on Dec 27 2017, 5:53 PM.
diff --git a/libs/image/kis_properties_configuration.cc b/libs/image/kis_properties_configuration.cc
index eac5aab..571cb77 100644
--- a/libs/image/kis_properties_configuration.cc
+++ b/libs/image/kis_properties_configuration.cc
@@ -338,6 +338,67 @@ void KisPropertiesConfiguration::setPrefixedProperties(const QString &prefix, co
setPrefixedProperties(prefix, config.data());
}
+QString KisPropertiesConfiguration::escapeString(const QString &string)
+{
+ QString result = string;
+ result.replace(";", "\\;");
+ result.replace("]", "\\]");
+ result.replace(">", "\\>");
+ return result;
+}
+
+QString KisPropertiesConfiguration::unescapeString(const QString &string)
+{
+ QString result = string;
+ result.replace("\\;", ";");
+ result.replace("\\]", "]");
+ result.replace("\\>", ">");
+ return result;
+}
+
+void KisPropertiesConfiguration::setProperty(const QString &name, const QStringList &value)
+{
+ QStringList escapedList;
+ escapedList.reserve(value.size());
+
+ Q_FOREACH (const QString &str, value) {
+ escapedList << escapeString(str);
+ }
+
+ setProperty(name, escapedList.join(';'));
+}
+
+QStringList KisPropertiesConfiguration::getStringList(const QString &name, const QStringList &defaultValue)
+{
+ if (!hasProperty(name)) return defaultValue;
+
+ const QString joined = getString(name);
+
+ QStringList result;
+
+ int afterLastMatch = -1;
+ for (int i = 0; i < joined.size(); i++) {
+ const bool lastChunk = i == joined.size() - 1;
+ const bool matchedSplitter = joined[i] == ';' && (i == 0 || joined[i - 1] != '\\');
+
+ if (lastChunk || matchedSplitter) {
+ result << unescapeString(joined.mid(afterLastMatch, i - afterLastMatch + int(lastChunk && !matchedSplitter)));
+ afterLastMatch = i + 1;
+ }
+
+ if (lastChunk && matchedSplitter) {
+ result << QString();
+ }
+ }
+
+ return result;
+}
+
+QStringList KisPropertiesConfiguration::getPropertyLazy(const QString &name, const QStringList &defaultValue)
+{
+ return getStringList(name, defaultValue);
+}
+
// --- factory ---
struct Q_DECL_HIDDEN KisPropertiesConfigurationFactory::Private {
diff --git a/libs/image/kis_properties_configuration.h b/libs/image/kis_properties_configuration.h
index 0c7be15..9650108 100644
--- a/libs/image/kis_properties_configuration.h
+++ b/libs/image/kis_properties_configuration.h
@@ -171,6 +171,13 @@ public:
*/
void setPrefixedProperties(const QString &prefix, const KisPropertiesConfigurationSP config);
+ static QString escapeString(const QString &string);
+ static QString unescapeString(const QString &string);
+
+ void setProperty(const QString &name, const QStringList &value);
+ QStringList getStringList(const QString &name, const QStringList &defaultValue = QStringList());
+ QStringList getPropertyLazy(const QString &name, const QStringList &defaultValue);
+
public:
void dump() const;
diff --git a/libs/image/tests/kis_properties_configuration_test.cpp b/libs/image/tests/kis_properties_configuration_test.cpp
index 7a42fda..f59eb37 100644
--- a/libs/image/tests/kis_properties_configuration_test.cpp
+++ b/libs/image/tests/kis_properties_configuration_test.cpp
@@ -121,5 +121,56 @@ void KisPropertiesConfigurationTest::testCopy()
QCOMPARE(p2.getBool("testBool2", true), false);
}
+void roundTripStringList(const QStringList &refList)
+{
+ KisPropertiesConfiguration config1;
+ config1.setProperty("testProp", refList);
+
+ const QString xmlData = config1.toXML();
+
+ KisPropertiesConfiguration config2;
+ config2.fromXML(xmlData);
+
+ const QStringList results = config2.getStringList("testProp");
+
+ QCOMPARE(results, refList);
+}
+
+void KisPropertiesConfigurationTest::testLists()
+{
+ const QString str1("str1 str2\\ str3/ str4; str5% str6& str7;; str8\\; str9]]> str10");
+ const QString str2("str1 str2\\ str3/ str4; str5% str6& str7;; str8\\; str9]]> str10;");
+
+ {
+ const QStringList refList({str1, str1});
+ roundTripStringList(refList);
+ }
+
+ {
+ const QStringList refList({str2, str2});
+ roundTripStringList(refList);
+ }
+
+ {
+ const QStringList refList({"", str2, str2});
+ roundTripStringList(refList);
+ }
+
+ {
+ const QStringList refList({str2, str2, ""});
+ roundTripStringList(refList);
+ }
+
+ {
+ const QStringList refList({str2, str2, ";"});
+ roundTripStringList(refList);
+ }
+
+ {
+ const QStringList refList({";", str2, str2});
+ roundTripStringList(refList);
+ }
+}
+
QTEST_MAIN(KisPropertiesConfigurationTest)
diff --git a/libs/image/tests/kis_properties_configuration_test.h b/libs/image/tests/kis_properties_configuration_test.h
index 7528c8c..1802e55 100644
--- a/libs/image/tests/kis_properties_configuration_test.h
+++ b/libs/image/tests/kis_properties_configuration_test.h
@@ -35,6 +35,9 @@ private Q_SLOTS:
void testDefaultValues();
void testNotSavedValues();
void testCopy();
+
+ void testLists();
+
private:
KisPropertiesConfigurationSP createConfig();
void testConfig(KisPropertiesConfigurationSP config);
dkazakov edited the content of this paste. (Show Details)Dec 27 2017, 5:53 PM
dkazakov changed the title of this paste from untitled to Masterwork From Distant Lands.
dkazakov updated the paste's language from autodetect to autodetect.