Skip to content

Commit

Permalink
commandlineparser: Make it possible to load dummydata like qmlscene
Browse files Browse the repository at this point in the history
The code is copied over from qmlscene.
  • Loading branch information
sh-zam committed Jan 4, 2023
1 parent 868da21 commit 161f36e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/build/
/compile_commands.json
28 changes: 28 additions & 0 deletions src/commandline/commandlineparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QtCore/QJsonDocument>
#include <QtCore/QJsonArray>
#include <QtCore/QJsonObject>
#include <QtQml/QQmlContext>

#include <utility>

Expand Down Expand Up @@ -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<QQmlError> 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)) {
Expand Down
2 changes: 2 additions & 0 deletions src/commandline/commandlineparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class CommandLineParser : public QCommandLineParser {
*/
void setApplication(QCoreApplication* application);

void loadDummyData(QQmlEngine *engine);

private:
struct OptionStruct {
QCommandLineOption option;
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ int main(int argc, char *argv[])
ProvidesSomething::self()->setFilePath(QUrl::fromUserInput(dir.absolutePath()));
}
}
commandLineParser.loadDummyData(&appEngine);

return app.exec();
}

0 comments on commit 161f36e

Please sign in to comment.