Improve startup code for WIP Qt Quick GUI

This commit is contained in:
Martchus 2018-04-22 20:38:29 +02:00
parent b356851617
commit 65c8797079
3 changed files with 30 additions and 11 deletions

View File

@ -43,6 +43,7 @@ int main(int argc, char *argv[])
// Qt configuration arguments // Qt configuration arguments
QT_CONFIG_ARGUMENTS qtConfigArgs; QT_CONFIG_ARGUMENTS qtConfigArgs;
qtConfigArgs.qtWidgetsGuiArg().addSubArgument(&fileArg); qtConfigArgs.qtWidgetsGuiArg().addSubArgument(&fileArg);
qtConfigArgs.qtQuickGuiArg().addSubArgument(&fileArg);
// cli argument // cli argument
Argument cliArg("interactive-cli", 'i', "starts the interactive command line interface"); Argument cliArg("interactive-cli", 'i', "starts the interactive command line interface");
cliArg.setDenotesOperation(true); cliArg.setDenotesOperation(true);
@ -60,17 +61,14 @@ int main(int argc, char *argv[])
if (cliArg.isPresent()) { if (cliArg.isPresent()) {
Cli::InteractiveCli cli; Cli::InteractiveCli cli;
if (fileArg.isPresent()) { if (fileArg.isPresent()) {
cli.run(fileArg.values().front()); cli.run(fileArg.firstValue());
} else { } else {
cli.run(); cli.run();
} }
} else if (qtConfigArgs.areQtGuiArgsPresent()) { } else if (qtConfigArgs.areQtGuiArgsPresent()) {
// run Qt gui if no arguments, --qt-gui or --qt-quick-gui specified, a file might be specified // run Qt gui if no arguments, --qt-gui or --qt-quick-gui specified, a file might be specified
#if defined(PASSWORD_MANAGER_GUI_QTWIDGETS) || defined(PASSWORD_MANAGER_GUI_QTQUICK) #if defined(PASSWORD_MANAGER_GUI_QTWIDGETS) || defined(PASSWORD_MANAGER_GUI_QTQUICK)
QString file; const auto file(fileArg.isPresent() ? QString::fromLocal8Bit(fileArg.firstValue()) : QString());
if (fileArg.isPresent()) {
file = QString::fromLocal8Bit(fileArg.values().front());
}
#endif #endif
if (qtConfigArgs.qtWidgetsGuiArg().isPresent()) { if (qtConfigArgs.qtWidgetsGuiArg().isPresent()) {
#ifdef PASSWORD_MANAGER_GUI_QTWIDGETS #ifdef PASSWORD_MANAGER_GUI_QTWIDGETS
@ -81,14 +79,14 @@ int main(int argc, char *argv[])
#endif #endif
} else if (qtConfigArgs.qtQuickGuiArg().isPresent()) { } else if (qtConfigArgs.qtQuickGuiArg().isPresent()) {
#ifdef PASSWORD_MANAGER_GUI_QTQUICK #ifdef PASSWORD_MANAGER_GUI_QTQUICK
res = QtGui::runQuickGui(argc, argv, qtConfigArgs); res = QtGui::runQuickGui(argc, argv, qtConfigArgs, file);
#else #else
CMD_UTILS_START_CONSOLE; CMD_UTILS_START_CONSOLE;
cerr << "The application has not been built with Qt quick support." << endl; cerr << "The application has not been built with Qt quick support." << endl;
#endif #endif
} else { } else {
#if defined(PASSWORD_MANAGER_GUI_QTQUICK) #if defined(PASSWORD_MANAGER_GUI_QTQUICK)
res = QtGui::runQuickGui(argc, argv, qtConfigArgs); res = QtGui::runQuickGui(argc, argv, qtConfigArgs, file);
#elif defined(PASSWORD_MANAGER_GUI_QTWIDGETS) #elif defined(PASSWORD_MANAGER_GUI_QTWIDGETS)
res = QtGui::runWidgetsGui(argc, argv, qtConfigArgs, file); res = QtGui::runWidgetsGui(argc, argv, qtConfigArgs, file);
#else #else

View File

@ -32,25 +32,46 @@ static QObject *applicationInfo(QQmlEngine *engine, QJSEngine *scriptEngine)
return new ApplicationInfo(); return new ApplicationInfo();
} }
int runQuickGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs) int runQuickGui(int argc, char *argv[], const QtConfigArguments &qtConfigArgs, const QString &file)
{ {
// init application // init application
#ifdef __ANDROID__
qputenv("QT_QUICK_CONTROLS_STYLE", "material");
#endif
SET_QT_APPLICATION_INFO; SET_QT_APPLICATION_INFO;
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#if defined(GUI_QTWIDGETS) #if defined(GUI_QTWIDGETS)
QApplication a(argc, argv); QApplication a(argc, argv);
#else #else
QGuiApplication a(argc, argv); QGuiApplication a(argc, argv);
#endif #endif
// apply settings specified via command line args // apply settings specified via command line args
qtConfigArgs.applySettings(); qtConfigArgs.applySettings();
LOAD_QT_TRANSLATIONS;
// load translations and enforce UTF-8 locale
QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8")); QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
// init quick GUI LOAD_QT_TRANSLATIONS;
// determine user paths
const QVariantMap userPaths{
{ QStringLiteral("desktop"), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation) },
{ QStringLiteral("documents"), QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation) },
{ QStringLiteral("music"), QStandardPaths::writableLocation(QStandardPaths::MusicLocation) },
{ QStringLiteral("movies"), QStandardPaths::writableLocation(QStandardPaths::MoviesLocation) },
{ QStringLiteral("pictures"), QStandardPaths::writableLocation(QStandardPaths::PicturesLocation) },
{ QStringLiteral("home"), QStandardPaths::writableLocation(QStandardPaths::HomeLocation) },
};
// init Quick GUI
qmlRegisterSingletonType<QtGui::ApplicationInfo>("martchus.passwordmanager", 2, 0, "ApplicationInfo", applicationInfo); qmlRegisterSingletonType<QtGui::ApplicationInfo>("martchus.passwordmanager", 2, 0, "ApplicationInfo", applicationInfo);
qmlRegisterType<QtGui::EntryFilterModel>("martchus.passwordmanager", 2, 0, "EntryFilterModel"); qmlRegisterType<QtGui::EntryFilterModel>("martchus.passwordmanager", 2, 0, "EntryFilterModel");
qmlRegisterType<QtGui::EntryModel>("martchus.passwordmanager", 2, 0, "EntryModel"); qmlRegisterType<QtGui::EntryModel>("martchus.passwordmanager", 2, 0, "EntryModel");
qmlRegisterType<QtGui::FieldModel>("martchus.passwordmanager", 2, 0, "FieldModel"); qmlRegisterType<QtGui::FieldModel>("martchus.passwordmanager", 2, 0, "FieldModel");
QQmlApplicationEngine engine(QUrl("qrc:/qml/main.qml")); QQmlApplicationEngine engine(QUrl("qrc:/qml/main.qml"));
engine.rootContext()->setContextProperty(QStringLiteral("userPaths"), userPaths);
engine.rootContext()->setContextProperty(QStringLiteral("file"), file);
// start event loop // start event loop
int res = a.exec(); int res = a.exec();
return res; return res;

View File

@ -11,7 +11,7 @@ class QtConfigArguments;
namespace QtGui { namespace QtGui {
int runQuickGui(int argc, char *argv[], const ApplicationUtilities::QtConfigArguments &qtConfigArgs); int runQuickGui(int argc, char *argv[], const ApplicationUtilities::QtConfigArguments &qtConfigArgs, const QString &file);
} }
#endif // QT_QUICK_GUI_INITIATE_H #endif // QT_QUICK_GUI_INITIATE_H