diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..db9f3cd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/build/ +/compile_commands.json diff --git a/src/commandline/commandlineparser.cpp b/src/commandline/commandlineparser.cpp index 4215bd2..a1475c7 100644 --- a/src/commandline/commandlineparser.cpp +++ b/src/commandline/commandlineparser.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -72,6 +73,33 @@ void CommandLineParser::setEngine(QQmlEngine* engine) } } +void CommandLineParser::loadDummyData(QQmlEngine *engine) +{ + const QFileInfo file = ProvidesSomething::self()->filePath().toLocalFile(); + const QString pathToQml = file.canonicalPath(); + + const QDir dir(pathToQml + "/dummydata/", "*.qml"); + QStringList list = dir.entryList(); + for (int i = 0; i < list.size(); ++i) { + QString qml = list.at(i); + QQmlComponent comp(engine, dir.filePath(qml)); + QObject *dummyData = comp.create(); + + if(comp.isError()) { + const QList errors = comp.errors(); + for (const QQmlError &error : errors) + fprintf(stderr, "%s\n", qPrintable(error.toString())); + } + + if (dummyData) { + fprintf(stderr, "Loaded dummy data: %s\n", qPrintable(dir.filePath(qml))); + qml.truncate(qml.length()-4); + engine->rootContext()->setContextProperty(qml, dummyData); + dummyData->setParent(engine); + } + } +} + void CommandLineParser::printHelp() { for (auto& optionStruct : std::as_const(_optionsStruct)) { diff --git a/src/commandline/commandlineparser.h b/src/commandline/commandlineparser.h index 603b9ee..38c0680 100644 --- a/src/commandline/commandlineparser.h +++ b/src/commandline/commandlineparser.h @@ -46,6 +46,8 @@ class CommandLineParser : public QCommandLineParser { */ void setApplication(QCoreApplication* application); + void loadDummyData(QQmlEngine *engine); + private: struct OptionStruct { QCommandLineOption option; diff --git a/src/main.cpp b/src/main.cpp index c398cbd..155f24a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,7 @@ int main(int argc, char *argv[]) ProvidesSomething::self()->setFilePath(QUrl::fromUserInput(dir.absolutePath())); } } + commandLineParser.loadDummyData(&appEngine); return app.exec(); }