From 042a5084226ce05d9199563c93ba9a518a06a715 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Thu, 3 Jul 2014 16:10:55 +0200 Subject: [PATCH] ui: Refactored QtCodeSnippet and added view settings to ApplicationSettings This change refactors parts of the QtCodeSnippet and places some settings into ApplicationSettings. Additionally Application- and ProjectSettings were abstracted to the common base class Settings, which takes care of the getting and setting values of the Configmanager member. --- bin/app/data/ApplicationSettings.xml | 9 +++++ src/app/qt/element/QtCodeSnippet.cpp | 40 ++++++++++++++++++-- src/app/qt/element/QtCodeSnippet.h | 15 ++++++-- src/app/qt/view/QtCodeView.cpp | 17 +-------- src/app/qt/view/QtCodeView.h | 13 +------ src/lib/Application.cpp | 3 ++ src/lib/ApplicationSettings.cpp | 48 ++++++++++++++++++++++-- src/lib/ApplicationSettings.h | 19 +++++++++- src/lib/CMakeLists.txt | 10 +++-- src/lib/ProjectSettings.cpp | 42 +-------------------- src/lib/ProjectSettings.h | 13 ++----- src/lib/Settings.cpp | 41 +++++++++++++++++++++ src/lib/Settings.h | 51 ++++++++++++++++++++++++++ src/lib/component/ComponentManager.cpp | 33 ----------------- src/lib/utility/math/Color.h | 25 +++++++++++++ 15 files changed, 254 insertions(+), 125 deletions(-) create mode 100644 bin/app/data/ApplicationSettings.xml create mode 100644 src/lib/Settings.cpp create mode 100644 src/lib/Settings.h diff --git a/bin/app/data/ApplicationSettings.xml b/bin/app/data/ApplicationSettings.xml new file mode 100644 index 00000000..130a768d --- /dev/null +++ b/bin/app/data/ApplicationSettings.xml @@ -0,0 +1,9 @@ + + + + 4 + Courier + 12 + 255 255 0 100 + + diff --git a/src/app/qt/element/QtCodeSnippet.cpp b/src/app/qt/element/QtCodeSnippet.cpp index 94e61453..5038b533 100644 --- a/src/app/qt/element/QtCodeSnippet.cpp +++ b/src/app/qt/element/QtCodeSnippet.cpp @@ -1,10 +1,13 @@ #include "qt/element/QtCodeSnippet.h" +#include #include +#include "ApplicationSettings.h" #include "data/location/TokenLocation.h" #include "data/location/TokenLocationFile.h" #include "data/location/TokenLocationLine.h" +#include "qt/utility/QtHighLighter.h" #include "qt/view/QtCodeView.h" QtCodeSnippet::LineNumberArea::LineNumberArea(QtCodeSnippet *codeSnippet) @@ -28,16 +31,39 @@ void QtCodeSnippet::LineNumberArea::paintEvent(QPaintEvent *event) } -QtCodeSnippet::QtCodeSnippet(QtCodeView* parentView, int startLineNumber, QWidget *parent) +QtCodeSnippet::QtCodeSnippet( + QtCodeView* parentView, + const std::string& code, + const TokenLocationFile& locationFile, + int startLineNumber, + QWidget *parent +) : QPlainTextEdit(parent) , m_parentView(parentView) , m_startLineNumber(startLineNumber) { m_lineNumberArea = new LineNumberArea(this); + setReadOnly(true); + + QFont font; + font.setFamily(ApplicationSettings::getInstance()->getCodeFontName().c_str()); + font.setFixedPitch(true); + font.setPointSize(ApplicationSettings::getInstance()->getCodeFontSize()); + setFont(font); + + int tabWidth = ApplicationSettings::getInstance()->getCodeTabWidth(); + QFontMetrics metrics(font); + setTabStopWidth(tabWidth * metrics.width(' ')); + + m_highlighter = new QtHighlighter(document()); + setPlainText(QString::fromUtf8(code.c_str())); + annotateText(locationFile); + connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(clickTokenLocation())); + connect(this, SIGNAL(selectionChanged()), this, SLOT(clearSelection())); updateLineNumberAreaWidth(0); } @@ -111,9 +137,8 @@ void QtCodeSnippet::annotateText(const TokenLocationFile& locationFile) QTextEdit::ExtraSelection selection; - QColor color = QColor(Qt::red); - color.setAlphaF(0.3f); - selection.format.setBackground(color); + Colori color = ApplicationSettings::getInstance()->getCodeLinkColor(); + selection.format.setBackground(QColor(color.r, color.g, color.b, color.a)); selection.cursor = textCursor(); selection.cursor.clearSelection(); @@ -170,6 +195,13 @@ void QtCodeSnippet::clickTokenLocation() } } +void QtCodeSnippet::clearSelection() +{ + QTextCursor cursor = textCursor(); + cursor.clearSelection(); + setTextCursor(cursor); +} + int QtCodeSnippet::toTextEditPosition(int lineNumber, int columnNumber) const { lineNumber -= m_startLineNumber - 1; diff --git a/src/app/qt/element/QtCodeSnippet.h b/src/app/qt/element/QtCodeSnippet.h index f1e66f70..9b755e12 100644 --- a/src/app/qt/element/QtCodeSnippet.h +++ b/src/app/qt/element/QtCodeSnippet.h @@ -4,7 +4,6 @@ #include #include -#include #include "utility/types.h" @@ -12,6 +11,7 @@ class QPaintEvent; class QResizeEvent; class QSize; class QtCodeView; +class QtHighlighter; class QWidget; class TokenLocationFile; @@ -35,7 +35,13 @@ public: QtCodeSnippet *m_codeSnippet; }; - QtCodeSnippet(QtCodeView* parentView, int startLineNumber, QWidget *parent = 0); + QtCodeSnippet( + QtCodeView* parentView, + const std::string& code, + const TokenLocationFile& locationFile, + int startLineNumber, + QWidget *parent = 0 + ); virtual ~QtCodeSnippet(); void lineNumberAreaPaintEvent(QPaintEvent *event); @@ -50,6 +56,7 @@ private slots: void updateLineNumberAreaWidth(int newBlockCount); void updateLineNumberArea(const QRect &, int); void clickTokenLocation(); + void clearSelection(); private: struct Annotation @@ -61,10 +68,12 @@ private: int toTextEditPosition(int lineNumber, int columnNumber) const; - QWidget *m_lineNumberArea; QtCodeView* m_parentView; + QtHighlighter* m_highlighter; + QWidget *m_lineNumberArea; const int m_startLineNumber; + std::vector m_annotations; }; diff --git a/src/app/qt/view/QtCodeView.cpp b/src/app/qt/view/QtCodeView.cpp index 1c6e2f70..772272e4 100644 --- a/src/app/qt/view/QtCodeView.cpp +++ b/src/app/qt/view/QtCodeView.cpp @@ -6,7 +6,6 @@ #include "data/location/TokenLocationFile.h" #include "qt/element/QtCodeSnippet.h" #include "qt/QtWidgetWrapper.h" -#include "qt/utility/QtHighLighter.h" #include "qt/utility/utilityQt.h" #include "utility/messaging/type/MessageActivateToken.h" @@ -35,10 +34,6 @@ void QtCodeView::initGui() layout->setSpacing(3); layout->setContentsMargins(3, 3, 3, 3); widget->setLayout(layout); - - m_font.setFamily("Courier"); - m_font.setFixedPitch(true); - m_font.setPointSize(10); } void QtCodeView::addCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber) @@ -59,18 +54,10 @@ void QtCodeView::activateToken(Id tokenId) const void QtCodeView::doAddCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber) { - std::shared_ptr snippet = std::make_shared(); - - snippet->textField = std::make_shared(this, startLineNumber); - snippet->textField->setReadOnly(true); - snippet->textField->setFont(m_font); - - snippet->highlighter = std::make_shared(snippet->textField->document()); - snippet->textField->setPlainText(QString::fromUtf8(str.c_str())); - snippet->textField->annotateText(locationFile); + std::shared_ptr snippet = std::make_shared(this, str, locationFile, startLineNumber); QWidget* widget = QtWidgetWrapper::getWidgetOfView(this); - widget->layout()->addWidget(snippet->textField.get()); + widget->layout()->addWidget(snippet.get()); m_snippets.push_back(snippet); } diff --git a/src/app/qt/view/QtCodeView.h b/src/app/qt/view/QtCodeView.h index 6a034ede..ed7723bd 100644 --- a/src/app/qt/view/QtCodeView.h +++ b/src/app/qt/view/QtCodeView.h @@ -4,15 +4,11 @@ #include #include -#include - #include "component/view/CodeView.h" #include "qt/utility/QtThreadedFunctor.h" #include "utility/types.h" class QtCodeSnippet; -class QtHighlighter; -class QTextEdit; class QtCodeView: public CodeView { @@ -31,17 +27,10 @@ public: void activateToken(Id tokenId) const; private: - struct Snippet - { - std::shared_ptr textField; - std::shared_ptr highlighter; - }; - std::vector> m_snippets; - void doAddCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber); void doClearCodeSnippets(); - QFont m_font; + std::vector > m_snippets; QtThreadedFunctor m_clearCodeSnippetsFunctor; QtThreadedFunctor m_addCodeSnippetFunctor; diff --git a/src/lib/Application.cpp b/src/lib/Application.cpp index 767c9a85..2e8a9118 100644 --- a/src/lib/Application.cpp +++ b/src/lib/Application.cpp @@ -1,5 +1,6 @@ #include "Application.h" +#include "ApplicationSettings.h" #include "component/view/MainView.h" #include "data/Storage.h" #include "gui/GuiFactory.h" @@ -12,6 +13,8 @@ std::shared_ptr Application::create(GuiFactory* guiFactory) std::shared_ptr consoleLogger = std::make_shared(); // TODO: move to main LogManager::getInstance()->addLogger(consoleLogger); + ApplicationSettings::getInstance()->load("data/ApplicationSettings.xml"); + std::shared_ptr ptr(new Application()); ptr->m_storage = std::make_shared(); diff --git a/src/lib/ApplicationSettings.cpp b/src/lib/ApplicationSettings.cpp index d9fb30b9..426889dc 100644 --- a/src/lib/ApplicationSettings.cpp +++ b/src/lib/ApplicationSettings.cpp @@ -12,10 +12,52 @@ std::shared_ptr ApplicationSettings::getInstance() return s_instance; } -ApplicationSettings::ApplicationSettings() -{ -} + ApplicationSettings::~ApplicationSettings() { } + +int ApplicationSettings::getCodeTabWidth() const +{ + return getValue("code/TabWidth", 4); +} + +void ApplicationSettings::setCodeTabWidth(int codeTabWidth) +{ + setValue("code/TabWidth", codeTabWidth); +} + +std::string ApplicationSettings::getCodeFontName() const +{ + return getValue("code/FontName", "Courier"); +} + +void ApplicationSettings::setCodeFontName(const std::string& codeFontName) +{ + setValue("code/FontName", codeFontName); +} + +int ApplicationSettings::getCodeFontSize() const +{ + return getValue("code/FontSize", 12); +} + +void ApplicationSettings::setCodeFontSize(int codeFontSize) +{ + setValue("code/FontSize", codeFontSize); +} + +Colori ApplicationSettings::getCodeLinkColor() const +{ + return Colori::fromString(getValue("code/LinkColor", Colori(255, 255, 0, 100).toString())); +} + +void ApplicationSettings::setCodeLinkColor(Colori codeLinkColor) +{ + setValue("code/LinkColor", codeLinkColor.toString()); +} + +ApplicationSettings::ApplicationSettings() +{ +} diff --git a/src/lib/ApplicationSettings.h b/src/lib/ApplicationSettings.h index e620436b..8f7e14fd 100644 --- a/src/lib/ApplicationSettings.h +++ b/src/lib/ApplicationSettings.h @@ -3,13 +3,28 @@ #include -class ApplicationSettings +#include "utility/math/Color.h" + +#include "Settings.h" + +class ApplicationSettings: public Settings { public: static std::shared_ptr getInstance(); - ~ApplicationSettings(); + int getCodeTabWidth() const; + void setCodeTabWidth(int codeTabWidth); + + std::string getCodeFontName() const; + void setCodeFontName(const std::string& codeFontName); + + int getCodeFontSize() const; + void setCodeFontSize(int codeFontSize); + + Colori getCodeLinkColor() const; + void setCodeLinkColor(Colori codeLinkColor); + private: ApplicationSettings(); ApplicationSettings(const ApplicationSettings&); diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index e5cf7798..f0c34300 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -25,7 +25,7 @@ add_files( component/view/graphElements/GraphNode.cpp component/view/graphElements/GraphNode.h - + component/view/CodeView.cpp component/view/CodeView.h component/view/GraphView.cpp @@ -35,8 +35,8 @@ add_files( component/view/View.cpp component/view/View.h component/view/ViewLayout.cpp - component/view/ViewLayout.h - + component/view/ViewLayout.h + component/Component.cpp component/Component.h component/ComponentFactory.cpp @@ -109,7 +109,7 @@ add_files( utility/math/VectorBase.h utility/messaging/type/MessageActivateToken.h - + utility/messaging/Message.h utility/messaging/MessageBase.h utility/messaging/MessageListener.h @@ -138,4 +138,6 @@ add_files( Project.h ProjectSettings.cpp ProjectSettings.h + Settings.cpp + Settings.h ) diff --git a/src/lib/ProjectSettings.cpp b/src/lib/ProjectSettings.cpp index 2a8fd0e2..8206c5a0 100644 --- a/src/lib/ProjectSettings.cpp +++ b/src/lib/ProjectSettings.cpp @@ -1,9 +1,5 @@ #include "ProjectSettings.h" -#include "utility/FileSystem.h" -#include "utility/logging/logging.h" -#include "utility/text/TextAccess.h" - std::shared_ptr ProjectSettings::s_instance; std::shared_ptr ProjectSettings::getInstance() @@ -24,46 +20,12 @@ ProjectSettings::~ProjectSettings() { } -bool ProjectSettings::load(const std::string& projectSettingsFilePath) -{ - m_config.reset(); - if (FileSystem::exists(projectSettingsFilePath)) - { - m_config = ConfigManager::createAndLoad(TextAccess::createFromFile(projectSettingsFilePath)); - return true; - } - else - { - LOG_WARNING("File for Projectsettings not found"); - return false; - } -} - -void ProjectSettings::save(const std::string& projectSettingsFilePath) -{ - if (m_config) - { - m_config->save(); - } -} - std::string ProjectSettings::getSourcePath() const { - if (m_config) - { - std::string sourcePath; - if (m_config->getValue("SourcePath", sourcePath)) - { - return sourcePath; - } - } - return ""; + return getValue("SourcePath", ""); } void ProjectSettings::setSourcePath(const std::string& sourcePath) { - if (m_config) - { - m_config->setValue("SourcePath", sourcePath); - } + setValue("SourcePath", sourcePath); } diff --git a/src/lib/ProjectSettings.h b/src/lib/ProjectSettings.h index 2106d3fb..13076563 100644 --- a/src/lib/ProjectSettings.h +++ b/src/lib/ProjectSettings.h @@ -1,30 +1,25 @@ #ifndef PROJECT_SETTINGS_H #define PROJECT_SETTINGS_H -#include -#include +#include -#include "utility/ConfigManager.h" +#include "Settings.h" -class ProjectSettings +class ProjectSettings: public Settings { public: static std::shared_ptr getInstance(); ~ProjectSettings(); - bool load(const std::string& projectSettingsFilePath); - void save(const std::string& projectSettingsFilePath); - std::string getSourcePath() const; void setSourcePath(const std::string& sourcePath); private: - static std::shared_ptr s_instance; ProjectSettings(); ProjectSettings(const ProjectSettings&); void operator=(const ProjectSettings&); - std::shared_ptr m_config; + static std::shared_ptr s_instance; }; #endif // PROJECT_SETTINGS_H diff --git a/src/lib/Settings.cpp b/src/lib/Settings.cpp new file mode 100644 index 00000000..4abdc716 --- /dev/null +++ b/src/lib/Settings.cpp @@ -0,0 +1,41 @@ +#include "Settings.h" + +#include "utility/FileSystem.h" +#include "utility/logging/logging.h" +#include "utility/text/TextAccess.h" + +Settings::Settings() +{ +} + +Settings::~Settings() +{ +} + +bool Settings::load(const std::string& filePath) +{ + m_config.reset(); + + if (FileSystem::exists(filePath)) + { + m_config = ConfigManager::createAndLoad(TextAccess::createFromFile(filePath)); + return true; + } + else + { + LOG_WARNING("File for Settings not found."); + return false; + } +} + +void Settings::save(const std::string& filePath) +{ + if (m_config) + { + m_config->save(); + } + else + { + LOG_WARNING("Settings were not saved."); + } +} diff --git a/src/lib/Settings.h b/src/lib/Settings.h new file mode 100644 index 00000000..7f032cd7 --- /dev/null +++ b/src/lib/Settings.h @@ -0,0 +1,51 @@ +#ifndef SETTINGS_H +#define SETTINGS_H + +#include +#include + +#include "utility/ConfigManager.h" + +class Settings +{ +public: + Settings(); + virtual ~Settings(); + + bool load(const std::string& filePath); + void save(const std::string& filePath); + + template + T getValue(const std::string& key, T defaultValue) const; + + template + void setValue(const std::string& key, T value); + +private: + std::shared_ptr m_config; +}; + +template +T Settings::getValue(const std::string& key, T defaultValue) const +{ + if (m_config) + { + T value; + if (m_config->getValue(key, value)) + { + return value; + } + } + return defaultValue; +} + +template +void Settings::setValue(const std::string& key, T value) +{ + if (m_config) + { + m_config->setValue(key, value); + } +} + +#endif // SETTINGS_H diff --git a/src/lib/component/ComponentManager.cpp b/src/lib/component/ComponentManager.cpp index e3a01d29..f5cfc12d 100644 --- a/src/lib/component/ComponentManager.cpp +++ b/src/lib/component/ComponentManager.cpp @@ -33,39 +33,6 @@ void ComponentManager::setup() std::shared_ptr codeComponent = m_componentFactory->createCodeComponent(); m_components.push_back(codeComponent); - - CodeView* codeView = codeComponent->getView(); - - std::string code = - "class HelloWorld;\n" - "static int n = 42; // the answer.\n" - "\n" - "int sum(int a, int b)\n" - "{\n" - " return a + b;\n" - "}\n"; - - TokenLocationFile locationFile("test.cpp"); - locationFile.addTokenLocation(1, 1, 7, 1, 16); - locationFile.addTokenLocation(2, 2, 8, 2, 10); - locationFile.addTokenLocation(3, 2, 12, 2, 12); - locationFile.addTokenLocation(4, 4, 1, 7, 1); - - codeView->addCodeSnippet(code, locationFile, 1); - - std::string code2 = - "const char* name = new char[10];\n" - "\n" - "name = \"MetaVizz\\0\";"; - - TokenLocationFile locationFile2("test.cpp"); - locationFile2.addTokenLocation(5, 123, 1, 123, 11); - locationFile2.addTokenLocation(6, 123, 13, 123, 16); - locationFile2.addTokenLocation(7, 125, 1, 125, 4); - - codeView->addCodeSnippet(code2, locationFile2, 123); - - // codeView->clearCodeSnippets(); } ComponentManager::ComponentManager() diff --git a/src/lib/utility/math/Color.h b/src/lib/utility/math/Color.h index 794cf6c1..dac24182 100644 --- a/src/lib/utility/math/Color.h +++ b/src/lib/utility/math/Color.h @@ -1,16 +1,33 @@ #ifndef COLOR_H #define COLOR_H +#include +#include + template class Color { public: + static Color fromString(std::string str); + Color(); Color(T r, T g, T b, T a); + std::string toString() const; + T r, g, b, a; }; +template +Color Color::fromString(std::string str) +{ + Color color; + std::stringstream ss; + ss << str; + ss >> color.r >> color.g >> color.b >> color.a; + return color; +} + template Color::Color() : r(0) @@ -27,6 +44,14 @@ Color::Color(T r, T g, T b, T a) , a(a) {} +template +std::string Color::toString() const +{ + std::stringstream ss; + ss << r << ' ' << g << ' ' << b << ' ' << a; + return ss.str(); +} + typedef Color Colorf; typedef Color Colori;