From 46a629358c8a84dca5bcc26179b75895400258de Mon Sep 17 00:00:00 2001 From: Andreas Stallinger Date: Tue, 12 Jan 2016 13:24:04 +0100 Subject: [PATCH] utility: loading project via commanlineargument loading a project via commanline with --project or -p argument --- src/app/main.cpp | 9 +++ src/lib/Application.cpp | 1 + src/lib/utility/ConfigManager.cpp | 9 ++- src/lib/utility/ConfigManager.h | 2 +- src/lib_gui/CMakeLists.txt | 3 + .../qt/commandline/QtCommandLineParser.cpp | 69 +++++++++++++++++++ .../qt/commandline/QtCommandLineParser.h | 19 +++++ 7 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 src/lib_gui/qt/commandline/QtCommandLineParser.cpp create mode 100644 src/lib_gui/qt/commandline/QtCommandLineParser.h diff --git a/src/app/main.cpp b/src/app/main.cpp index 960e4309..606bd0c9 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -9,6 +9,7 @@ #include "Application.h" #include "includes.h" // defines 'void setup(int argc, char *argv[])' +#include "qt/commandline/QtCommandLineParser.h" #include "qt/window/QtMainWindow.h" #include "qt/window/QtSplashScreen.h" #include "qt/network/QtNetworkFactory.h" @@ -38,6 +39,12 @@ int main(int argc, char *argv[]) setup(argc, argv); QApplication qtApp(argc, argv); + QApplication::setApplicationName("Coati"); + QApplication::setApplicationVersion(version.toDisplayString().c_str()); + + QtCommandLineParser commandLineParser; + commandLineParser.setup(); + commandLineParser.process(qtApp); qtApp.setAttribute(Qt::AA_UseHighDpiPixmaps); @@ -63,5 +70,7 @@ int main(int argc, char *argv[]) delete splash; } + commandLineParser.parseCommandline(); + return qtApp.exec(); } diff --git a/src/lib/Application.cpp b/src/lib/Application.cpp index 061a34c1..f662e1d2 100644 --- a/src/lib/Application.cpp +++ b/src/lib/Application.cpp @@ -91,6 +91,7 @@ void Application::loadProject(const FilePath& projectSettingsFilePath) m_project->loadProject(projectSettingsFilePath); m_mainView->updateRecentProjectMenu(); + m_mainView->hideStartScreen(); } void Application::refreshProject() diff --git a/src/lib/utility/ConfigManager.cpp b/src/lib/utility/ConfigManager.cpp index 764d2a2a..2d7fecc3 100644 --- a/src/lib/utility/ConfigManager.cpp +++ b/src/lib/utility/ConfigManager.cpp @@ -215,7 +215,7 @@ void ConfigManager::setValues(const std::string& key, const std::vector& v setValues(key, stringValues); } -void ConfigManager::load(const std::shared_ptr textAccess) +bool ConfigManager::load(const std::shared_ptr textAccess) { std::string text = textAccess->getText(); @@ -225,6 +225,11 @@ void ConfigManager::load(const std::shared_ptr textAccess) { TiXmlHandle docHandle(&doc); TiXmlNode *rootNode = docHandle.FirstChild("config").ToNode(); + if(rootNode == nullptr) + { + LOG_ERROR("No rootelement 'config' in the configfile"); + return false; + } for (TiXmlNode *childNode = rootNode->FirstChild(); childNode; childNode = childNode->NextSibling()) { parseSubtree(childNode, ""); @@ -233,7 +238,9 @@ void ConfigManager::load(const std::shared_ptr textAccess) else { LOG_ERROR("Unable to load file."); + return false; } + return true; } void ConfigManager::save(const std::string filepath) diff --git a/src/lib/utility/ConfigManager.h b/src/lib/utility/ConfigManager.h index c6922130..53d085de 100644 --- a/src/lib/utility/ConfigManager.h +++ b/src/lib/utility/ConfigManager.h @@ -37,7 +37,7 @@ public: void setValues(const std::string& key, const std::vector& values); void setValues(const std::string& key, const std::vector& values); - void load(const std::shared_ptr textAccess); + bool load(const std::shared_ptr textAccess); void save(const std::string filepath); std::string toString(); diff --git a/src/lib_gui/CMakeLists.txt b/src/lib_gui/CMakeLists.txt index b79895f5..e717ed66 100644 --- a/src/lib_gui/CMakeLists.txt +++ b/src/lib_gui/CMakeLists.txt @@ -5,6 +5,9 @@ add_files( platform_includes/includesMac.h platform_includes/includesWindows.h + qt/commandline/QtCommandLineParser.cpp + qt/commandline/QtCommandLineParser.h + qt/element/QtAutocompletionList.cpp qt/element/QtAutocompletionList.h qt/element/QtCodeArea.cpp diff --git a/src/lib_gui/qt/commandline/QtCommandLineParser.cpp b/src/lib_gui/qt/commandline/QtCommandLineParser.cpp new file mode 100644 index 00000000..0ebf5b98 --- /dev/null +++ b/src/lib_gui/qt/commandline/QtCommandLineParser.cpp @@ -0,0 +1,69 @@ +#include "qt/commandline/QtCommandLineParser.h" + +#include "utility/ConfigManager.h" +#include "utility/messaging/type/MessageLoadProject.h" +#include "utility/messaging/type/MessageStatus.h" +#include "utility/text/TextAccess.h" + + +QtCommandLineParser::QtCommandLineParser() +{ +} + +QtCommandLineParser::~QtCommandLineParser() +{ +} + +void QtCommandLineParser::setup() +{ + + setApplicationDescription("Coati helper"); + addVersionOption(); + addHelpOption(); + + QCommandLineOption projectOption(QStringList() << "p" << "project", + QCoreApplication::translate("main", "Load Coati with the a Coatiprojectfile ."), + QCoreApplication::translate("main", "CoatiProject")); + addOption(projectOption); +} + +void QtCommandLineParser::parseCommandline() +{ + + if(isSet("project")) + { + QString projectfilename = value("project") ; + FilePath projectfile(projectfilename.toStdString()); + bool isValidProjectfile = true; + std::stringstream errorstring("Provided Projectfile is not valid: "); + errorstring << "Provided Projectfile('" << projectfile.fileName() << ") "; + if(!projectfile.exists()) + { + errorstring << "does not exist"; + isValidProjectfile = false; + } + + if(projectfile.extension() != ".coatiproject") + { + errorstring << "has a wrong fileending"; + isValidProjectfile = false; + } + + std::shared_ptr configManager = ConfigManager::createEmpty(); + if(!configManager->load(TextAccess::createFromFile(projectfile.str()))) + { + errorstring << "could not be loaded"; + isValidProjectfile = false; + } + + if(isValidProjectfile) + { + MessageLoadProject(projectfile.str()).dispatch(); + } + else + { + MessageStatus(errorstring.str(), true).dispatch(); + LOG_ERROR(errorstring.str()); + } + } +} \ No newline at end of file diff --git a/src/lib_gui/qt/commandline/QtCommandLineParser.h b/src/lib_gui/qt/commandline/QtCommandLineParser.h new file mode 100644 index 00000000..08cd2f5f --- /dev/null +++ b/src/lib_gui/qt/commandline/QtCommandLineParser.h @@ -0,0 +1,19 @@ +#ifndef QTCOMMANDLINEPARSER_H +#define QTCOMMANDLINEPARSER_H + +#include +#include "Application.h" + +class QtCommandLineParser : public QCommandLineParser +{ +public: + QtCommandLineParser(); + ~QtCommandLineParser(); + void setup(); + void parseCommandline(); + +private: + +}; + +#endif //QTCOMMANDLINEPARSER_H