Add dtk-settings-tool
authorIceyer <me@iceyer.net>
Thu, 27 Jul 2017 06:30:25 +0000 (14:30 +0800)
committerIceyer <me@iceyer.net>
Thu, 27 Jul 2017 06:30:25 +0000 (14:30 +0800)
Change-Id: I5d2db81bf7ba3e29c099b6ad92cb7d3c813d4207

debian/libdtkcore-dev.install
dtkcore.pro
tool/main.cpp [new file with mode: 0644]
tool/tool.pro [new file with mode: 0644]

index 682da5cdc56a4638ef02b723f29620c18ed87ebe..c42bad43b4f377f431b791ad05bb55965b5f1c57 100644 (file)
@@ -1,3 +1,4 @@
 usr/lib/*/lib*.so
 usr/include
 usr/lib/*/pkgconfig/*.pc
+usr/bin/dtk-settings-tool
index 2df8d60cecbdc62e79c606c6fe649af1e155c3e5..39f7fbfd0bb1a3aca92ce700b5f622cb372c82f5 100644 (file)
@@ -3,4 +3,5 @@ TEMPLATE = subdirs
 CONFIG = ordered
 
 SUBDIRS +=   \
-    src
+    src \
+    tool
diff --git a/tool/main.cpp b/tool/main.cpp
new file mode 100644 (file)
index 0000000..ba27490
--- /dev/null
@@ -0,0 +1,117 @@
+#include <QCoreApplication>
+
+#include <iostream>
+
+#include <QDebug>
+#include <QFile>
+#include <QCommandLineParser>
+
+#include "settings/dsettings.h"
+#include "settings/dsettingsgroup.h"
+#include "settings/dsettingsoption.h"
+
+
+static QString CppTemplate =
+    "#include <DSettings>\n"
+    "\n"
+    "void GenerateSettingTranslate()\n"
+    "{\n"
+    "%1"
+    "}\n";
+
+int main(int argc, char *argv[])
+{
+    QCoreApplication app(argc, argv);
+    app.setOrganizationName("deepin");
+    app.setApplicationName("dtk-settings-tools");
+    app.setApplicationVersion("0.1.2");
+
+    QCommandLineParser parser;
+    parser.setApplicationDescription("Generate translation of dtksetting.");
+    parser.addHelpOption();
+    parser.addVersionOption();
+
+    QCommandLineOption outputFileArg(QStringList() << "o" << "output",
+                                     QCoreApplication::tr("Output cpp file"),
+                                     "cpp-file");
+    parser.addOption(outputFileArg);
+    parser.addPositionalArgument("json-file", QCoreApplication::tr("Json file description config"));
+    parser.process(app);
+
+    if (0 == (parser.optionNames().length() + parser.positionalArguments().length())) {
+        parser.showHelp(0);
+    }
+
+    auto jsonFile = parser.positionalArguments().value(0);
+    auto settings = Dtk::Core::DSettings::fromJsonFile(jsonFile);
+
+    QMap<QString, QString> transtaleMaps;
+
+//    qDebug() << settings->groupKeys();
+    for (QString groupKey : settings->groupKeys()) {
+        auto codeKey = QString(groupKey).replace(".", "_");
+        auto group = settings->group(groupKey);
+//        qDebug() << codeKey << group->name();
+        // add Name
+        if (!group->name().isEmpty()) {
+            transtaleMaps.insert("group_" + codeKey + "Name", group->name());
+        }
+
+        // TODO: only two level
+        for (auto childGroup : group->childGroups()) {
+            auto codeKey = childGroup->key().replace(".", "_");
+//            qDebug() << codeKey << childGroup->name();
+            // add Name
+            if (!childGroup->name().isEmpty()) {
+                transtaleMaps.insert("group_" + codeKey + "Name", childGroup->name());
+            }
+        }
+    }
+
+    for (QString key : settings->keys()) {
+        auto codeKey = QString(key).replace(".", "_");
+        auto opt = settings->option(key);
+
+        // add Name
+        if (!opt->name().isEmpty()) {
+            transtaleMaps.insert(codeKey + "Name", opt->name());
+        }
+
+        // add text
+        if (!opt->data("text").toString().isEmpty()) {
+            transtaleMaps.insert(codeKey + "Text", opt->data("text").toString());
+        }
+
+        // add items
+        if (!opt->data("items").toStringList().isEmpty()) {
+            auto items = opt->data("items").toStringList();
+            for (int i = 0; i < items.length(); ++i) {
+                transtaleMaps.insert(codeKey + QString("Text%1").arg(i), items.value(i));
+            }
+        }
+    }
+
+    transtaleMaps.insert("reset_button_name", "Restore Defaults");
+
+    QString cppCode;
+    for (auto key : transtaleMaps.keys()) {
+        auto stringCode = QString("    auto %1 = QObject::tr(\"%2\");\n").arg(key).arg(transtaleMaps.value(key));
+        cppCode.append(stringCode);
+    }
+
+    QString outputCpp = CppTemplate.arg(cppCode);
+
+    if (parser.isSet(outputFileArg)) {
+        QFile outputFile(parser.value(outputFileArg));
+        if (!outputFile.open(QIODevice::WriteOnly)) {
+            qCritical() << "can not open output file!";
+            exit(1);
+        }
+        outputFile.write(outputCpp.toUtf8());
+        outputFile.close();
+    } else {
+        std::cout << outputCpp.toStdString();
+    }
+    return 0;
+}
+
diff --git a/tool/tool.pro b/tool/tool.pro
new file mode 100644 (file)
index 0000000..5e2c1c9
--- /dev/null
@@ -0,0 +1,28 @@
+QT += core
+QT -= gui
+
+CONFIG += c++11
+
+TARGET = dtk-settings-tool
+CONFIG += console link_pkgconfig
+CONFIG -= app_bundle
+
+TEMPLATE = app
+
+SOURCES += main.cpp
+
+isEmpty(PREFIX){
+    PREFIX = /usr
+}
+
+binary.files += $${OUT_PWD}/dtk-settings-tool
+binary.path = $${PREFIX}/bin
+
+INSTALLS += binary
+
+win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../src/release/ -ldtkcore
+else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../src/debug/ -ldtkcore
+else:unix: LIBS += -L$$OUT_PWD/../src/ -ldtkcore
+
+INCLUDEPATH += $$PWD/../src
+DEPENDPATH += $$PWD/../src