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.
This commit is contained in:
Eberhard Graether
2014-07-03 16:10:55 +02:00
parent 4ae6f4a04a
commit 042a508422
15 changed files with 254 additions and 125 deletions
+9
View File
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<config>
<code>
<TabWidth>4</TabWidth>
<FontName>Courier</FontName>
<FontSize>12</FontSize>
<LinkColor>255 255 0 100</LinkColor>
</code>
</config>
+36 -4
View File
@@ -1,10 +1,13 @@
#include "qt/element/QtCodeSnippet.h"
#include <QFont>
#include <QtWidgets>
#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;
+12 -3
View File
@@ -4,7 +4,6 @@
#include <vector>
#include <QPlainTextEdit>
#include <QObject>
#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<Annotation> m_annotations;
};
+2 -15
View File
@@ -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> snippet = std::make_shared<Snippet>();
snippet->textField = std::make_shared<QtCodeSnippet>(this, startLineNumber);
snippet->textField->setReadOnly(true);
snippet->textField->setFont(m_font);
snippet->highlighter = std::make_shared<QtHighlighter>(snippet->textField->document());
snippet->textField->setPlainText(QString::fromUtf8(str.c_str()));
snippet->textField->annotateText(locationFile);
std::shared_ptr<QtCodeSnippet> snippet = std::make_shared<QtCodeSnippet>(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);
}
+1 -12
View File
@@ -4,15 +4,11 @@
#include <memory>
#include <vector>
#include <QFont>
#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<QtCodeSnippet> textField;
std::shared_ptr<QtHighlighter> highlighter;
};
std::vector<std::shared_ptr<Snippet>> m_snippets;
void doAddCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber);
void doClearCodeSnippets();
QFont m_font;
std::vector<std::shared_ptr<QtCodeSnippet> > m_snippets;
QtThreadedFunctor<void> m_clearCodeSnippetsFunctor;
QtThreadedFunctor<const std::string&, const TokenLocationFile&, int> m_addCodeSnippetFunctor;
+3
View File
@@ -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> Application::create(GuiFactory* guiFactory)
std::shared_ptr<ConsoleLogger> consoleLogger = std::make_shared<ConsoleLogger>(); // TODO: move to main
LogManager::getInstance()->addLogger(consoleLogger);
ApplicationSettings::getInstance()->load("data/ApplicationSettings.xml");
std::shared_ptr<Application> ptr(new Application());
ptr->m_storage = std::make_shared<Storage>();
+45 -3
View File
@@ -12,10 +12,52 @@ std::shared_ptr<ApplicationSettings> ApplicationSettings::getInstance()
return s_instance;
}
ApplicationSettings::ApplicationSettings()
{
}
ApplicationSettings::~ApplicationSettings()
{
}
int ApplicationSettings::getCodeTabWidth() const
{
return getValue<int>("code/TabWidth", 4);
}
void ApplicationSettings::setCodeTabWidth(int codeTabWidth)
{
setValue<int>("code/TabWidth", codeTabWidth);
}
std::string ApplicationSettings::getCodeFontName() const
{
return getValue<std::string>("code/FontName", "Courier");
}
void ApplicationSettings::setCodeFontName(const std::string& codeFontName)
{
setValue<std::string>("code/FontName", codeFontName);
}
int ApplicationSettings::getCodeFontSize() const
{
return getValue<int>("code/FontSize", 12);
}
void ApplicationSettings::setCodeFontSize(int codeFontSize)
{
setValue<int>("code/FontSize", codeFontSize);
}
Colori ApplicationSettings::getCodeLinkColor() const
{
return Colori::fromString(getValue<std::string>("code/LinkColor", Colori(255, 255, 0, 100).toString()));
}
void ApplicationSettings::setCodeLinkColor(Colori codeLinkColor)
{
setValue<std::string>("code/LinkColor", codeLinkColor.toString());
}
ApplicationSettings::ApplicationSettings()
{
}
+17 -2
View File
@@ -3,13 +3,28 @@
#include <memory>
class ApplicationSettings
#include "utility/math/Color.h"
#include "Settings.h"
class ApplicationSettings: public Settings
{
public:
static std::shared_ptr<ApplicationSettings> 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&);
+6 -4
View File
@@ -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
)
+2 -40
View File
@@ -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> ProjectSettings::s_instance;
std::shared_ptr<ProjectSettings> 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<std::string>("SourcePath", "");
}
void ProjectSettings::setSourcePath(const std::string& sourcePath)
{
if (m_config)
{
m_config->setValue("SourcePath", sourcePath);
}
setValue<std::string>("SourcePath", sourcePath);
}
+4 -9
View File
@@ -1,30 +1,25 @@
#ifndef PROJECT_SETTINGS_H
#define PROJECT_SETTINGS_H
#include <string>
#include <memory.h>
#include <memory>
#include "utility/ConfigManager.h"
#include "Settings.h"
class ProjectSettings
class ProjectSettings: public Settings
{
public:
static std::shared_ptr<ProjectSettings> 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<ProjectSettings> s_instance;
ProjectSettings();
ProjectSettings(const ProjectSettings&);
void operator=(const ProjectSettings&);
std::shared_ptr<ConfigManager> m_config;
static std::shared_ptr<ProjectSettings> s_instance;
};
#endif // PROJECT_SETTINGS_H
+41
View File
@@ -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.");
}
}
+51
View File
@@ -0,0 +1,51 @@
#ifndef SETTINGS_H
#define SETTINGS_H
#include <string>
#include <memory>
#include "utility/ConfigManager.h"
class Settings
{
public:
Settings();
virtual ~Settings();
bool load(const std::string& filePath);
void save(const std::string& filePath);
template<typename T>
T getValue(const std::string& key, T defaultValue) const;
template<typename T>
void setValue(const std::string& key, T value);
private:
std::shared_ptr<ConfigManager> m_config;
};
template<typename T>
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<typename T>
void Settings::setValue(const std::string& key, T value)
{
if (m_config)
{
m_config->setValue(key, value);
}
}
#endif // SETTINGS_H
-33
View File
@@ -33,39 +33,6 @@ void ComponentManager::setup()
std::shared_ptr<Component> codeComponent = m_componentFactory->createCodeComponent();
m_components.push_back(codeComponent);
CodeView* codeView = codeComponent->getView<CodeView>();
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()
+25
View File
@@ -1,16 +1,33 @@
#ifndef COLOR_H
#define COLOR_H
#include <string>
#include <sstream>
template<class T>
class Color
{
public:
static Color<T> fromString(std::string str);
Color();
Color(T r, T g, T b, T a);
std::string toString() const;
T r, g, b, a;
};
template<class T>
Color<T> Color<T>::fromString(std::string str)
{
Color<T> color;
std::stringstream ss;
ss << str;
ss >> color.r >> color.g >> color.b >> color.a;
return color;
}
template<class T>
Color<T>::Color()
: r(0)
@@ -27,6 +44,14 @@ Color<T>::Color(T r, T g, T b, T a)
, a(a)
{}
template<class T>
std::string Color<T>::toString() const
{
std::stringstream ss;
ss << r << ' ' << g << ' ' << b << ' ' << a;
return ss.str();
}
typedef Color<float> Colorf;
typedef Color<int> Colori;