From f07f2051f23529d6e9a33853feee8ca9413b2309 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Wed, 9 Jul 2014 16:41:11 +0200 Subject: [PATCH] ui: load source from directory via MenuBar Added MenuBar action Project->New, which opens a directory dialog for choosing the new source directory to parse the files from. The ConfigManager can be created empty now and setValue calls can be used to set values. fortune cookie message = Your troubles will be faded by the luck you will soon have. --- bin/test/data/log/test_log.txt | 1 + src/app/qt/element/QtMainWindow.cpp | 34 ++++++++++------- src/app/qt/element/QtMainWindow.h | 1 + src/lib/Application.cpp | 18 +++++++++ src/lib/Application.h | 7 +++- src/lib/Project.cpp | 10 +++++ src/lib/Project.h | 4 ++ src/lib/ProjectSettings.cpp | 4 +- src/lib/ProjectSettings.h | 2 +- src/lib/Settings.cpp | 8 +++- src/lib/Settings.h | 7 +++- src/lib/utility/ConfigManager.cpp | 38 ++++++++++--------- src/lib/utility/ConfigManager.h | 9 ++--- .../messaging/type/MessageLoadSource.h | 22 +++++++++++ src/test/ConfigManagerTestSuite.h | 26 +++++++++++++ 15 files changed, 148 insertions(+), 43 deletions(-) create mode 100644 src/lib/utility/messaging/type/MessageLoadSource.h diff --git a/bin/test/data/log/test_log.txt b/bin/test/data/log/test_log.txt index 6d3c3676..fd67f034 100644 --- a/bin/test/data/log/test_log.txt +++ b/bin/test/data/log/test_log.txt @@ -1,3 +1,4 @@ +ConfigManager.cpp ERROR: value path/to/nowhere is not present in config. Graph.cpp ERROR: Can't remove member edge, without removing the child node. TextAccess.cpp WARNING: Index 'firstLine' has to be lower or equal index 'lastLine', is 2 > 1 TextAccess.cpp WARNING: Tried to access index 9. Maximum index is 7 diff --git a/src/app/qt/element/QtMainWindow.cpp b/src/app/qt/element/QtMainWindow.cpp index 006b4d99..88f93a74 100644 --- a/src/app/qt/element/QtMainWindow.cpp +++ b/src/app/qt/element/QtMainWindow.cpp @@ -1,7 +1,5 @@ #include "qt/element/QtMainWindow.h" -#include - #include #include #include @@ -11,6 +9,7 @@ #include "component/view/View.h" #include "qt/QtWidgetWrapper.h" #include "utility/messaging/type/MessageLoadProject.h" +#include "utility/messaging/type/MessageLoadSource.h" QtMainWindow::QtMainWindow() { @@ -41,6 +40,16 @@ void QtMainWindow::about() ); } +void QtMainWindow::newProject() +{ + QString sourceDir = QFileDialog::getExistingDirectory(this, tr("Open Directory")); + + if (!sourceDir.isEmpty()) + { + MessageLoadSource(sourceDir.toStdString()).dispatch(); + } +} + void QtMainWindow::openProject(const QString &path) { QString fileName = path; @@ -52,9 +61,7 @@ void QtMainWindow::openProject(const QString &path) if (!fileName.isEmpty()) { - std::cout << fileName.toStdString() << std::endl; - MessageLoadProject message(fileName.toStdString()); - message.dispatch(); + MessageLoadProject(fileName.toStdString()).dispatch(); } } @@ -81,18 +88,19 @@ void QtMainWindow::removeView(View* view) void QtMainWindow::setupProjectMenu() { - QMenu *fileMenu = new QMenu(tr("&Project"), this); - menuBar()->addMenu(fileMenu); + QMenu *menu = new QMenu(tr("&Project"), this); + menuBar()->addMenu(menu); - fileMenu->addAction(tr("&Open..."), this, SLOT(openProject()), QKeySequence::Open); - fileMenu->addAction(tr("E&xit"), QCoreApplication::instance(), SLOT(quit()), QKeySequence::Quit); + menu->addAction(tr("&New"), this, SLOT(newProject()), QKeySequence::New); + menu->addAction(tr("&Open..."), this, SLOT(openProject()), QKeySequence::Open); + menu->addAction(tr("E&xit"), QCoreApplication::instance(), SLOT(quit()), QKeySequence::Quit); } void QtMainWindow::setupHelpMenu() { - QMenu *helpMenu = new QMenu(tr("&Help"), this); - menuBar()->addMenu(helpMenu); + QMenu *menu = new QMenu(tr("&Help"), this); + menuBar()->addMenu(menu); - helpMenu->addAction(tr("&About"), this, SLOT(about())); - helpMenu->addAction(tr("About &Qt"), QCoreApplication::instance(), SLOT(aboutQt())); + menu->addAction(tr("&About"), this, SLOT(about())); + menu->addAction(tr("About &Qt"), QCoreApplication::instance(), SLOT(aboutQt())); } diff --git a/src/app/qt/element/QtMainWindow.h b/src/app/qt/element/QtMainWindow.h index 3a360443..bd500056 100644 --- a/src/app/qt/element/QtMainWindow.h +++ b/src/app/qt/element/QtMainWindow.h @@ -22,6 +22,7 @@ public: public slots: void about(); + void newProject(); void openProject(const QString &path = QString()); private: diff --git a/src/lib/Application.cpp b/src/lib/Application.cpp index 403ecdd2..15840318 100644 --- a/src/lib/Application.cpp +++ b/src/lib/Application.cpp @@ -50,7 +50,25 @@ void Application::loadProject(const std::string& projectSettingsFilePath) message.dispatch(); } +void Application::loadSource(const std::string& sourceDirectoryPath) +{ + m_storage->clear(); + m_project = Project::create(m_storage); + + m_project->clearProjectSettings(); + m_project->setSourceDirectoryPath(sourceDirectoryPath); + m_project->parseCode(); + + MessageActivateToken message(1); + message.dispatch(); +} + void Application::handleMessage(MessageLoadProject* message) { loadProject(message->projectSettingsFilePath); } + +void Application::handleMessage(MessageLoadSource* message) +{ + loadSource(message->sourceDirectoryPath); +} diff --git a/src/lib/Application.h b/src/lib/Application.h index a4f68f01..aaffaa9d 100644 --- a/src/lib/Application.h +++ b/src/lib/Application.h @@ -7,12 +7,15 @@ #include "Project.h" #include "utility/messaging/MessageListener.h" #include "utility/messaging/type/MessageLoadProject.h" +#include "utility/messaging/type/MessageLoadSource.h" class GuiFactory; class MainView; class Storage; -class Application: public MessageListener +class Application + : public MessageListener + , public MessageListener { public: static std::shared_ptr create(GuiFactory* guiFactory); @@ -20,11 +23,13 @@ public: ~Application(); void loadProject(const std::string& projectSettingsFilePath); + void loadSource(const std::string& sourceDirectoryPath); private: Application(); virtual void handleMessage(MessageLoadProject* message); + virtual void handleMessage(MessageLoadSource* message); std::shared_ptr m_project; std::shared_ptr m_storage; diff --git a/src/lib/Project.cpp b/src/lib/Project.cpp index 4bc04542..cce5619f 100644 --- a/src/lib/Project.cpp +++ b/src/lib/Project.cpp @@ -30,6 +30,16 @@ bool Project::loadProjectSettings(const std::string& projectSettingsFile) return ProjectSettings::getInstance()->load(projectSettingsFile); } +void Project::clearProjectSettings() +{ + ProjectSettings::getInstance()->clear(); +} + +bool Project::setSourceDirectoryPath(const std::string& sourceDirectoryPath) +{ + return ProjectSettings::getInstance()->setSourcePath(sourceDirectoryPath); +} + void Project::parseCode() { if (ProjectSettings::getInstance()->getSourcePath() != "") diff --git a/src/lib/Project.h b/src/lib/Project.h index f17933a0..37f6643f 100644 --- a/src/lib/Project.h +++ b/src/lib/Project.h @@ -16,6 +16,10 @@ public: ~Project(); bool loadProjectSettings(const std::string& projectSettingsFile); + void clearProjectSettings(); + + bool setSourceDirectoryPath(const std::string& sourceDirectoryPath); + void parseCode(); private: diff --git a/src/lib/ProjectSettings.cpp b/src/lib/ProjectSettings.cpp index 8206c5a0..a6e471c6 100644 --- a/src/lib/ProjectSettings.cpp +++ b/src/lib/ProjectSettings.cpp @@ -25,7 +25,7 @@ std::string ProjectSettings::getSourcePath() const return getValue("SourcePath", ""); } -void ProjectSettings::setSourcePath(const std::string& sourcePath) +bool ProjectSettings::setSourcePath(const std::string& sourcePath) { - setValue("SourcePath", sourcePath); + return setValue("SourcePath", sourcePath); } diff --git a/src/lib/ProjectSettings.h b/src/lib/ProjectSettings.h index 13076563..63677501 100644 --- a/src/lib/ProjectSettings.h +++ b/src/lib/ProjectSettings.h @@ -12,7 +12,7 @@ public: ~ProjectSettings(); std::string getSourcePath() const; - void setSourcePath(const std::string& sourcePath); + bool setSourcePath(const std::string& sourcePath); private: ProjectSettings(); diff --git a/src/lib/Settings.cpp b/src/lib/Settings.cpp index 4abdc716..dda0db21 100644 --- a/src/lib/Settings.cpp +++ b/src/lib/Settings.cpp @@ -14,8 +14,6 @@ Settings::~Settings() bool Settings::load(const std::string& filePath) { - m_config.reset(); - if (FileSystem::exists(filePath)) { m_config = ConfigManager::createAndLoad(TextAccess::createFromFile(filePath)); @@ -23,6 +21,7 @@ bool Settings::load(const std::string& filePath) } else { + m_config = ConfigManager::createEmpty(); LOG_WARNING("File for Settings not found."); return false; } @@ -39,3 +38,8 @@ void Settings::save(const std::string& filePath) LOG_WARNING("Settings were not saved."); } } + +void Settings::clear() +{ + m_config = ConfigManager::createEmpty(); +} diff --git a/src/lib/Settings.h b/src/lib/Settings.h index 7f032cd7..690ff13e 100644 --- a/src/lib/Settings.h +++ b/src/lib/Settings.h @@ -14,12 +14,13 @@ public: bool load(const std::string& filePath); void save(const std::string& filePath); + void clear(); template T getValue(const std::string& key, T defaultValue) const; template - void setValue(const std::string& key, T value); + bool setValue(const std::string& key, T value); private: std::shared_ptr m_config; @@ -40,12 +41,14 @@ T Settings::getValue(const std::string& key, T defaultValue) const } template -void Settings::setValue(const std::string& key, T value) +bool Settings::setValue(const std::string& key, T value) { if (m_config) { m_config->setValue(key, value); + return true; } + return false; } #endif // SETTINGS_H diff --git a/src/lib/utility/ConfigManager.cpp b/src/lib/utility/ConfigManager.cpp index be72fc1a..942a6d59 100644 --- a/src/lib/utility/ConfigManager.cpp +++ b/src/lib/utility/ConfigManager.cpp @@ -1,12 +1,19 @@ #include "utility/ConfigManager.h" -#include "logging/logging.h" +#include "tinyxml/tinyxml.h" + +#include "utility/logging/logging.h" #include "utility/text/TextAccess.h" +std::shared_ptr ConfigManager::createEmpty() +{ + return std::shared_ptr(new ConfigManager()); +} + std::shared_ptr ConfigManager::createAndLoad(const std::shared_ptr textAccess) { - std::shared_ptr configManager = std::shared_ptr(new ConfigManager(textAccess)); - configManager->load(); + std::shared_ptr configManager = std::shared_ptr(new ConfigManager()); + configManager->load(textAccess); return configManager; } @@ -14,14 +21,14 @@ bool ConfigManager::getValue(const std::string& key, std::string& value) const { std::map::const_iterator it = m_values.find(key); - if(it != m_values.end()) + if (it != m_values.end()) { value = it->second; return true; } else { - // LOG ERROR + LOG_ERROR("value " + key + " is not present in config."); return false; } } @@ -63,13 +70,13 @@ void ConfigManager::setValue(const std::string& key, const std::string& value) { std::map::iterator it = m_values.find(key); - if(it != m_values.end()) + if (it != m_values.end()) { it->second = value; } else { - // LOG ERROR + m_values.emplace(key, value); } } @@ -85,13 +92,12 @@ void ConfigManager::setValue(const std::string& key, const float value) void ConfigManager::setValue(const std::string& key, const bool value) { - setValue(key, (value ? "1" : "0")); + setValue(key, std::string(value ? "1" : "0")); } - -void ConfigManager::load() +void ConfigManager::load(const std::shared_ptr textAccess) { - std::string text = m_textAccess->getText(); + std::string text = textAccess->getText(); TiXmlDocument doc; const char* pTest = doc.Parse(text.c_str(), 0, TIXML_ENCODING_UTF8); @@ -99,25 +105,23 @@ void ConfigManager::load() { TiXmlHandle docHandle(&doc); TiXmlNode *rootNode = docHandle.FirstChild("config").ToNode(); - for(TiXmlNode *childNode = rootNode->FirstChild(); childNode; childNode = childNode->NextSibling()) + for (TiXmlNode *childNode = rootNode->FirstChild(); childNode; childNode = childNode->NextSibling()) { parseSubtree(childNode, ""); } } else { - // LOG ERROR Unable to load file. + LOG_ERROR("Unable to load file."); } } void ConfigManager::save() { LOG_ERROR("function: configmanager::save not implemented"); - // LOG ("Saving config to file: " + m_filePath) } -ConfigManager::ConfigManager(const std::shared_ptr textAccess) - : m_textAccess(textAccess) +ConfigManager::ConfigManager() { } @@ -130,7 +134,7 @@ void ConfigManager::parseSubtree(TiXmlNode* currentNode, const std::string& curr } else { - for(TiXmlNode *childNode = currentNode->FirstChild(); childNode; childNode = childNode->NextSibling()) + for (TiXmlNode *childNode = currentNode->FirstChild(); childNode; childNode = childNode->NextSibling()) { parseSubtree(childNode, currentPath + std::string(currentNode->Value()) + "/"); } diff --git a/src/lib/utility/ConfigManager.h b/src/lib/utility/ConfigManager.h index f6040f5e..33ae8ebe 100644 --- a/src/lib/utility/ConfigManager.h +++ b/src/lib/utility/ConfigManager.h @@ -5,13 +5,13 @@ #include #include -#include "tinyxml/tinyxml.h" - class TextAccess; +class TiXmlNode; class ConfigManager { public: + static std::shared_ptr createEmpty(); static std::shared_ptr createAndLoad(const std::shared_ptr textAccess); bool getValue(const std::string& key, std::string& value) const; @@ -24,17 +24,16 @@ public: void setValue(const std::string& key, const float value); void setValue(const std::string& key, const bool value); - void load(); + void load(const std::shared_ptr textAccess); void save(); private: - ConfigManager(const std::shared_ptr textAccess); + ConfigManager(); ConfigManager(const ConfigManager&); void operator=(const ConfigManager&); void parseSubtree(TiXmlNode* parentElement, const std::string& currentPath); - const std::shared_ptr m_textAccess; std::map m_values; }; diff --git a/src/lib/utility/messaging/type/MessageLoadSource.h b/src/lib/utility/messaging/type/MessageLoadSource.h new file mode 100644 index 00000000..1b7260e6 --- /dev/null +++ b/src/lib/utility/messaging/type/MessageLoadSource.h @@ -0,0 +1,22 @@ +#ifndef MESSAGE_LOAD_SOURCE_H +#define MESSAGE_LOAD_SOURCE_H + +#include "utility/messaging/Message.h" + +class MessageLoadSource: public Message +{ +public: + MessageLoadSource(const std::string& sourceDir) + : sourceDirectoryPath(sourceDir) + { + } + + static const std::string getStaticType() + { + return "MessageLoadSource"; + } + + const std::string sourceDirectoryPath; +}; + +#endif // MESSAGE_LOAD_SOURCE_H diff --git a/src/test/ConfigManagerTestSuite.h b/src/test/ConfigManagerTestSuite.h index 3b024e16..3c8121eb 100644 --- a/src/test/ConfigManagerTestSuite.h +++ b/src/test/ConfigManagerTestSuite.h @@ -71,6 +71,32 @@ public: TS_ASSERT(!value); } + void test_config_manager_adds_new_key_when_empty() + { + std::shared_ptr config = ConfigManager::createEmpty(); + + config->setValue("path/to/true_bool", true); + + bool value = false; + bool success(config->getValue("path/to/true_bool", value)); + + TS_ASSERT(success); + TS_ASSERT(value); + } + + void test_config_manager_adds_new_key_when_not_empty() + { + std::shared_ptr config = ConfigManager::createAndLoad(getConfigTextAccess()); + + config->setValue("path/to/true_bool", true); + + bool value = false; + bool success(config->getValue("path/to/true_bool", value)); + + TS_ASSERT(success); + TS_ASSERT(value); + } + private: std::shared_ptr getConfigTextAccess() {