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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#include "qt/element/QtMainWindow.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QFileDialog>
|
||||
#include <QDockWidget>
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ public:
|
||||
|
||||
public slots:
|
||||
void about();
|
||||
void newProject();
|
||||
void openProject(const QString &path = QString());
|
||||
|
||||
private:
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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<MessageLoadProject>
|
||||
class Application
|
||||
: public MessageListener<MessageLoadProject>
|
||||
, public MessageListener<MessageLoadSource>
|
||||
{
|
||||
public:
|
||||
static std::shared_ptr<Application> 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<Project> m_project;
|
||||
std::shared_ptr<Storage> m_storage;
|
||||
|
||||
@@ -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() != "")
|
||||
|
||||
@@ -16,6 +16,10 @@ public:
|
||||
~Project();
|
||||
|
||||
bool loadProjectSettings(const std::string& projectSettingsFile);
|
||||
void clearProjectSettings();
|
||||
|
||||
bool setSourceDirectoryPath(const std::string& sourceDirectoryPath);
|
||||
|
||||
void parseCode();
|
||||
|
||||
private:
|
||||
|
||||
@@ -25,7 +25,7 @@ std::string ProjectSettings::getSourcePath() const
|
||||
return getValue<std::string>("SourcePath", "");
|
||||
}
|
||||
|
||||
void ProjectSettings::setSourcePath(const std::string& sourcePath)
|
||||
bool ProjectSettings::setSourcePath(const std::string& sourcePath)
|
||||
{
|
||||
setValue<std::string>("SourcePath", sourcePath);
|
||||
return setValue<std::string>("SourcePath", sourcePath);
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ public:
|
||||
~ProjectSettings();
|
||||
|
||||
std::string getSourcePath() const;
|
||||
void setSourcePath(const std::string& sourcePath);
|
||||
bool setSourcePath(const std::string& sourcePath);
|
||||
|
||||
private:
|
||||
ProjectSettings();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
+5
-2
@@ -14,12 +14,13 @@ public:
|
||||
|
||||
bool load(const std::string& filePath);
|
||||
void save(const std::string& filePath);
|
||||
void clear();
|
||||
|
||||
template<typename T>
|
||||
T getValue(const std::string& key, T defaultValue) const;
|
||||
|
||||
template<typename T>
|
||||
void setValue(const std::string& key, T value);
|
||||
bool setValue(const std::string& key, T value);
|
||||
|
||||
private:
|
||||
std::shared_ptr<ConfigManager> m_config;
|
||||
@@ -40,12 +41,14 @@ T Settings::getValue(const std::string& key, T defaultValue) const
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
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
|
||||
|
||||
@@ -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> ConfigManager::createEmpty()
|
||||
{
|
||||
return std::shared_ptr<ConfigManager>(new ConfigManager());
|
||||
}
|
||||
|
||||
std::shared_ptr<ConfigManager> ConfigManager::createAndLoad(const std::shared_ptr<TextAccess> textAccess)
|
||||
{
|
||||
std::shared_ptr<ConfigManager> configManager = std::shared_ptr<ConfigManager>(new ConfigManager(textAccess));
|
||||
configManager->load();
|
||||
std::shared_ptr<ConfigManager> configManager = std::shared_ptr<ConfigManager>(new ConfigManager());
|
||||
configManager->load(textAccess);
|
||||
return configManager;
|
||||
}
|
||||
|
||||
@@ -14,14 +21,14 @@ bool ConfigManager::getValue(const std::string& key, std::string& value) const
|
||||
{
|
||||
std::map<std::string, std::string>::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<std::string, std::string>::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> 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> 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()) + "/");
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "tinyxml/tinyxml.h"
|
||||
|
||||
class TextAccess;
|
||||
class TiXmlNode;
|
||||
|
||||
class ConfigManager
|
||||
{
|
||||
public:
|
||||
static std::shared_ptr<ConfigManager> createEmpty();
|
||||
static std::shared_ptr<ConfigManager> createAndLoad(const std::shared_ptr<TextAccess> 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> textAccess);
|
||||
void save();
|
||||
|
||||
private:
|
||||
ConfigManager(const std::shared_ptr<TextAccess> textAccess);
|
||||
ConfigManager();
|
||||
ConfigManager(const ConfigManager&);
|
||||
void operator=(const ConfigManager&);
|
||||
|
||||
void parseSubtree(TiXmlNode* parentElement, const std::string& currentPath);
|
||||
|
||||
const std::shared_ptr<TextAccess> m_textAccess;
|
||||
std::map<std::string, std::string> m_values;
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef MESSAGE_LOAD_SOURCE_H
|
||||
#define MESSAGE_LOAD_SOURCE_H
|
||||
|
||||
#include "utility/messaging/Message.h"
|
||||
|
||||
class MessageLoadSource: public Message<MessageLoadSource>
|
||||
{
|
||||
public:
|
||||
MessageLoadSource(const std::string& sourceDir)
|
||||
: sourceDirectoryPath(sourceDir)
|
||||
{
|
||||
}
|
||||
|
||||
static const std::string getStaticType()
|
||||
{
|
||||
return "MessageLoadSource";
|
||||
}
|
||||
|
||||
const std::string sourceDirectoryPath;
|
||||
};
|
||||
|
||||
#endif // MESSAGE_LOAD_SOURCE_H
|
||||
@@ -71,6 +71,32 @@ public:
|
||||
TS_ASSERT(!value);
|
||||
}
|
||||
|
||||
void test_config_manager_adds_new_key_when_empty()
|
||||
{
|
||||
std::shared_ptr<ConfigManager> 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<ConfigManager> 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<TextAccess> getConfigTextAccess()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user