ui: basic CodeView with syntax highlighting
Added CodeView interface and QtCodeView implementation for showing syntax highlighted code snippets. This change avoids using the GuiElement hierarchy by using directly using QtClasses and creating QtCodeView in the QtElementFactory.
This commit is contained in:
@@ -7,17 +7,23 @@ add_files(
|
||||
qt/element/QtCanvas.h
|
||||
qt/element/QtLayout.cpp
|
||||
qt/element/QtLayout.h
|
||||
|
||||
|
||||
qt/utility/QtHighLighter.cpp
|
||||
qt/utility/QtHighLighter.h
|
||||
|
||||
qt/view/QtCodeView.cpp
|
||||
qt/view/QtCodeView.h
|
||||
|
||||
qt/QtWidgetWrapper.cpp
|
||||
qt/QtWidgetWrapper.h
|
||||
qt/QtElementFactory.cpp
|
||||
qt/QtElementFactory.h
|
||||
qt/QtWindow.cpp
|
||||
qt/QtWindow.h
|
||||
|
||||
|
||||
utility/utilityQt.cpp
|
||||
utility/utilityQt.h
|
||||
|
||||
|
||||
includesWindows.h
|
||||
main.cpp
|
||||
)
|
||||
|
||||
+3
-8
@@ -1,20 +1,15 @@
|
||||
#include <memory>
|
||||
|
||||
#include <QApplication>
|
||||
#include <QTextEdit>
|
||||
#include <qlayout.h>
|
||||
#include <qgroupbox.h>
|
||||
|
||||
#include "Application.h"
|
||||
#include "includes.h"
|
||||
#include "qt/element/QtArea.h"
|
||||
#include "qt/element/QtCanvas.h"
|
||||
#include "qt/QtElementFactory.h"
|
||||
#include "qt/QtWindow.h"
|
||||
|
||||
int main(int argv, char **args)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication qtApp(argv, args);
|
||||
QApplication qtApp(argc, argv);
|
||||
std::shared_ptr<GuiElementFactory> elementFactory = std::make_shared<QtElementFactory>();
|
||||
|
||||
std::shared_ptr<QtWindow> window = std::make_shared<QtWindow>();
|
||||
@@ -23,4 +18,4 @@ int main(int argv, char **args)
|
||||
app->loadProject();
|
||||
|
||||
return qtApp.exec();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
#include "qt/QtElementFactory.h"
|
||||
|
||||
#include "component/view/CodeView.h"
|
||||
#include "qt/element/QtArea.h"
|
||||
#include "qt/element/QtCanvas.h"
|
||||
#include "qt/element/QtLayout.h"
|
||||
#include "qt/view/QtCodeView.h"
|
||||
#include "qt/QtWindow.h"
|
||||
|
||||
QtElementFactory::QtElementFactory()
|
||||
@@ -29,3 +31,8 @@ std::shared_ptr<GuiLayout> QtElementFactory::createLayout() const
|
||||
{
|
||||
return std::make_shared<QtLayout>();
|
||||
}
|
||||
|
||||
std::shared_ptr<CodeView> QtElementFactory::createCodeView(std::shared_ptr<ViewManager> viewManager) const
|
||||
{
|
||||
return std::make_shared<QtCodeView>(viewManager);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
|
||||
#include "gui/GuiElementFactory.h"
|
||||
|
||||
class CodeView;
|
||||
class ViewManager;
|
||||
|
||||
class QtElementFactory: public GuiElementFactory
|
||||
{
|
||||
public:
|
||||
@@ -12,6 +15,8 @@ public:
|
||||
virtual std::shared_ptr<GuiArea> createArea() const;
|
||||
virtual std::shared_ptr<GuiCanvas> createCanvas(std::shared_ptr<GuiWindow> window) const;
|
||||
virtual std::shared_ptr<GuiLayout> createLayout() const;
|
||||
|
||||
virtual std::shared_ptr<CodeView> createCodeView(std::shared_ptr<ViewManager> viewManager) const;
|
||||
};
|
||||
|
||||
# endif // QT_ELEMENT_FACTORY_H
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
#include "qt/utility/QtHighlighter.h"
|
||||
|
||||
QtHighlighter::QtHighlighter(QTextDocument *parent)
|
||||
: QSyntaxHighlighter(parent)
|
||||
{
|
||||
HighlightingRule rule;
|
||||
|
||||
keywordFormat.setForeground(Qt::darkBlue);
|
||||
keywordFormat.setFontWeight(QFont::Bold);
|
||||
|
||||
QStringList keywordPatterns;
|
||||
keywordPatterns
|
||||
<< "\\bchar\\b" << "\\bclass\\b" << "\\bconst\\b"
|
||||
<< "\\bdouble\\b" << "\\benum\\b" << "\\bexplicit\\b"
|
||||
<< "\\bfriend\\b" << "\\binline\\b" << "\\bint\\b"
|
||||
<< "\\blong\\b" << "\\bnamespace\\b" << "\\boperator\\b"
|
||||
<< "\\bprivate\\b" << "\\bprotected\\b" << "\\bpublic\\b"
|
||||
<< "\\bshort\\b" << "\\bsignals\\b" << "\\bsigned\\b"
|
||||
<< "\\bslots\\b" << "\\bstatic\\b" << "\\bstruct\\b"
|
||||
<< "\\btemplate\\b" << "\\btypedef\\b" << "\\btypename\\b"
|
||||
<< "\\bunion\\b" << "\\bunsigned\\b" << "\\bvirtual\\b"
|
||||
<< "\\bvoid\\b" << "\\bvolatile\\b";
|
||||
|
||||
foreach (const QString &pattern, keywordPatterns)
|
||||
{
|
||||
rule.pattern = QRegExp(pattern);
|
||||
rule.format = keywordFormat;
|
||||
highlightingRules.append(rule);
|
||||
}
|
||||
|
||||
classFormat.setFontWeight(QFont::Bold);
|
||||
classFormat.setForeground(Qt::darkMagenta);
|
||||
rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
|
||||
rule.format = classFormat;
|
||||
highlightingRules.append(rule);
|
||||
|
||||
singleLineCommentFormat.setForeground(Qt::red);
|
||||
rule.pattern = QRegExp("//[^\n]*");
|
||||
rule.format = singleLineCommentFormat;
|
||||
highlightingRules.append(rule);
|
||||
|
||||
multiLineCommentFormat.setForeground(Qt::red);
|
||||
|
||||
quotationFormat.setForeground(Qt::darkGreen);
|
||||
rule.pattern = QRegExp("\".*\"");
|
||||
rule.format = quotationFormat;
|
||||
highlightingRules.append(rule);
|
||||
|
||||
functionFormat.setFontItalic(true);
|
||||
functionFormat.setForeground(Qt::blue);
|
||||
rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
|
||||
rule.format = functionFormat;
|
||||
highlightingRules.append(rule);
|
||||
|
||||
commentStartExpression = QRegExp("/\\*");
|
||||
commentEndExpression = QRegExp("\\*/");
|
||||
}
|
||||
|
||||
void QtHighlighter::highlightBlock(const QString &text)
|
||||
{
|
||||
foreach (const HighlightingRule &rule, highlightingRules)
|
||||
{
|
||||
QRegExp expression(rule.pattern);
|
||||
int index = expression.indexIn(text);
|
||||
while (index >= 0)
|
||||
{
|
||||
int length = expression.matchedLength();
|
||||
setFormat(index, length, rule.format);
|
||||
index = expression.indexIn(text, index + length);
|
||||
}
|
||||
}
|
||||
setCurrentBlockState(0);
|
||||
|
||||
int startIndex = 0;
|
||||
if (previousBlockState() != 1)
|
||||
{
|
||||
startIndex = commentStartExpression.indexIn(text);
|
||||
}
|
||||
|
||||
while (startIndex >= 0)
|
||||
{
|
||||
int endIndex = commentEndExpression.indexIn(text, startIndex);
|
||||
int commentLength;
|
||||
|
||||
if (endIndex == -1)
|
||||
{
|
||||
setCurrentBlockState(1);
|
||||
commentLength = text.length() - startIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
commentLength = endIndex - startIndex + commentEndExpression.matchedLength();
|
||||
}
|
||||
|
||||
setFormat(startIndex, commentLength, multiLineCommentFormat);
|
||||
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef QT_HIGHLIGHTER_H
|
||||
#define QT_HIGHLIGHTER_H
|
||||
|
||||
#include <QSyntaxHighlighter>
|
||||
#include <QTextCharFormat>
|
||||
|
||||
class QTextDocument;
|
||||
|
||||
class QtHighlighter : public QSyntaxHighlighter
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
QtHighlighter(QTextDocument *parent = 0);
|
||||
|
||||
protected:
|
||||
void highlightBlock(const QString &text);
|
||||
|
||||
private:
|
||||
struct HighlightingRule
|
||||
{
|
||||
QRegExp pattern;
|
||||
QTextCharFormat format;
|
||||
};
|
||||
QVector<HighlightingRule> highlightingRules;
|
||||
|
||||
QRegExp commentStartExpression;
|
||||
QRegExp commentEndExpression;
|
||||
|
||||
QTextCharFormat keywordFormat;
|
||||
QTextCharFormat classFormat;
|
||||
QTextCharFormat singleLineCommentFormat;
|
||||
QTextCharFormat multiLineCommentFormat;
|
||||
QTextCharFormat quotationFormat;
|
||||
QTextCharFormat functionFormat;
|
||||
};
|
||||
|
||||
#endif // QT_HIGHLIGHTER_H
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "qt/view/QtCodeView.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
|
||||
#include "qt/element/QtArea.h"
|
||||
#include "qt/QtWidgetWrapper.h"
|
||||
#include "qt/utility/QtHighLighter.h"
|
||||
|
||||
QtCodeView::QtCodeView(std::shared_ptr<ViewManager> viewManager)
|
||||
: CodeView(viewManager, std::make_shared<QtArea>())
|
||||
{
|
||||
getRootElement()->setBackgroundColor(255, 125, 0, 255);
|
||||
|
||||
std::shared_ptr<QWidget> widget = QtWidgetWrapper::getWidgetOfElement(getRootElement());
|
||||
QBoxLayout* layout = new QBoxLayout(QBoxLayout::TopToBottom);
|
||||
layout->setSpacing(3);
|
||||
layout->setContentsMargins(3, 3, 3, 3);
|
||||
widget->setLayout(layout);
|
||||
|
||||
m_font.setFamily("Courier");
|
||||
m_font.setFixedPitch(true);
|
||||
m_font.setPointSize(10);
|
||||
}
|
||||
|
||||
QtCodeView::~QtCodeView()
|
||||
{
|
||||
}
|
||||
|
||||
void QtCodeView::addCodeSnippet(std::string str)
|
||||
{
|
||||
std::shared_ptr<Snippet> snippet = std::make_shared<Snippet>();
|
||||
|
||||
snippet->textField = std::make_shared<QTextEdit>();
|
||||
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()));
|
||||
|
||||
std::shared_ptr<QWidget> widget = QtWidgetWrapper::getWidgetOfElement(getRootElement());
|
||||
widget->layout()->addWidget(snippet->textField.get());
|
||||
|
||||
m_snippets.push_back(snippet);
|
||||
}
|
||||
|
||||
void QtCodeView::clearCodeSnippets()
|
||||
{
|
||||
m_snippets.clear();
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef QT_CODE_VIEW_H
|
||||
#define QT_CODE_VIEW_H
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#include <QFont>
|
||||
|
||||
#include "component/view/CodeView.h"
|
||||
|
||||
class QtHighlighter;
|
||||
class QTextEdit;
|
||||
class ViewManager;
|
||||
|
||||
class QtCodeView: public CodeView
|
||||
{
|
||||
public:
|
||||
QtCodeView(std::shared_ptr<ViewManager> viewManager);
|
||||
~QtCodeView();
|
||||
|
||||
// CodeView implementation
|
||||
virtual void addCodeSnippet(std::string str);
|
||||
virtual void clearCodeSnippets();
|
||||
|
||||
private:
|
||||
struct Snippet
|
||||
{
|
||||
std::shared_ptr<QTextEdit> textField;
|
||||
std::shared_ptr<QtHighlighter> highlighter;
|
||||
};
|
||||
std::vector<std::shared_ptr<Snippet>> m_snippets;
|
||||
|
||||
QFont m_font;
|
||||
};
|
||||
|
||||
# endif // QT_CODE_VIEW_H
|
||||
@@ -23,6 +23,8 @@ add_files(
|
||||
component/controller/DummyController.cpp
|
||||
component/controller/DummyController.h
|
||||
|
||||
component/view/CodeView.cpp
|
||||
component/view/CodeView.h
|
||||
component/view/DummyView.cpp
|
||||
component/view/DummyView.h
|
||||
component/view/View.cpp
|
||||
@@ -114,10 +116,10 @@ add_files(
|
||||
utility/messaging/MessageListenerBase.h
|
||||
utility/messaging/MessageQueue.cpp
|
||||
utility/messaging/MessageQueue.h
|
||||
|
||||
|
||||
utility/text/TextAccess.cpp
|
||||
utility/text/TextAccess.h
|
||||
|
||||
|
||||
utility/ConfigManager.cpp
|
||||
utility/ConfigManager.h
|
||||
utility/FileSystem.cpp
|
||||
|
||||
@@ -9,3 +9,13 @@ Component::Component(std::shared_ptr<View> view, std::shared_ptr<Controller> con
|
||||
Component::~Component()
|
||||
{
|
||||
}
|
||||
|
||||
Controller* Component::getController() const
|
||||
{
|
||||
return m_controller.get();
|
||||
}
|
||||
|
||||
View* Component::getView() const
|
||||
{
|
||||
return m_view.get();
|
||||
}
|
||||
|
||||
@@ -10,12 +10,14 @@ class Component
|
||||
{
|
||||
public:
|
||||
Component(std::shared_ptr<View> view, std::shared_ptr<Controller> controller);
|
||||
|
||||
~Component();
|
||||
|
||||
Controller* getController() const;
|
||||
View* getView() const;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Controller> m_controller;
|
||||
std::shared_ptr<View> m_view;
|
||||
const std::shared_ptr<Controller> m_controller;
|
||||
const std::shared_ptr<View> m_view;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "component/ComponentFactory.h"
|
||||
|
||||
#include "component/view/DummyView.h"
|
||||
#include "component/controller/CodeController.h"
|
||||
#include "component/controller/DummyController.h"
|
||||
#include "component/view/CodeView.h"
|
||||
#include "component/view/DummyView.h"
|
||||
|
||||
std::shared_ptr<ComponentFactory> ComponentFactory::create(
|
||||
std::shared_ptr<ViewManager> viewManager,
|
||||
@@ -30,6 +32,15 @@ std::shared_ptr<Component> ComponentFactory::createDummyComponent()
|
||||
return component;
|
||||
}
|
||||
|
||||
std::shared_ptr<Component> ComponentFactory::createCodeComponent()
|
||||
{
|
||||
std::shared_ptr<View> view = m_guiElementFactory->createCodeView(m_viewManger);
|
||||
std::shared_ptr<Controller> controller = std::make_shared<CodeController>();
|
||||
|
||||
std::shared_ptr<Component> component = std::make_shared<Component>(view, controller);
|
||||
return component;
|
||||
}
|
||||
|
||||
ComponentFactory::ComponentFactory()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ public:
|
||||
~ComponentFactory();
|
||||
|
||||
std::shared_ptr<Component> createDummyComponent();
|
||||
std::shared_ptr<Component> createCodeComponent();
|
||||
|
||||
private:
|
||||
ComponentFactory();
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "component/ComponentManager.h"
|
||||
|
||||
#include "component/view/CodeView.h"
|
||||
|
||||
std::shared_ptr<ComponentManager> ComponentManager::create(
|
||||
std::shared_ptr<ViewManager> viewManager,
|
||||
std::shared_ptr<GuiElementFactory> guiElementFactory,
|
||||
@@ -14,6 +16,14 @@ std::shared_ptr<ComponentManager> ComponentManager::create(
|
||||
void ComponentManager::setup()
|
||||
{
|
||||
m_components.push_back(m_componentFactory->createDummyComponent());
|
||||
|
||||
std::shared_ptr<Component> codeComponent = m_componentFactory->createCodeComponent();
|
||||
m_components.push_back(codeComponent);
|
||||
|
||||
CodeView* codeView = dynamic_cast<CodeView*>(codeComponent->getView());
|
||||
codeView->addCodeSnippet("class HelloWorld;");
|
||||
codeView->addCodeSnippet("static int n = 42; // the answer.");
|
||||
// codeView->clearCodeSnippets();
|
||||
}
|
||||
|
||||
ComponentManager::ComponentManager()
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#include "component/view/CodeView.h"
|
||||
|
||||
CodeView::CodeView(std::shared_ptr<ViewManager> viewManager, std::shared_ptr<GuiElement> rootElement)
|
||||
: View(viewManager, rootElement, Vec2i(100, 100))
|
||||
{
|
||||
}
|
||||
|
||||
CodeView::~CodeView()
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef CODE_VIEW_H
|
||||
#define CODE_VIEW_H
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "component/view/View.h"
|
||||
|
||||
class GuiElementFactory;
|
||||
|
||||
class CodeView: public View
|
||||
{
|
||||
public:
|
||||
CodeView(std::shared_ptr<ViewManager> viewManager, std::shared_ptr<GuiElement> rootElement);
|
||||
virtual ~CodeView();
|
||||
|
||||
virtual void addCodeSnippet(std::string str) = 0;
|
||||
virtual void clearCodeSnippets() = 0;
|
||||
};
|
||||
|
||||
#endif // CODE_VIEW_H
|
||||
@@ -3,11 +3,15 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
class CodeView;
|
||||
|
||||
class GuiArea;
|
||||
class GuiCanvas;
|
||||
class GuiLayout;
|
||||
class GuiWindow;
|
||||
|
||||
class ViewManager;
|
||||
|
||||
class GuiElementFactory
|
||||
{
|
||||
public:
|
||||
@@ -16,6 +20,8 @@ public:
|
||||
virtual std::shared_ptr<GuiArea> createArea() const = 0;
|
||||
virtual std::shared_ptr<GuiCanvas> createCanvas(std::shared_ptr<GuiWindow> window) const = 0;
|
||||
virtual std::shared_ptr<GuiLayout> createLayout() const = 0;
|
||||
|
||||
virtual std::shared_ptr<CodeView> createCodeView(std::shared_ptr<ViewManager> viewManager) const = 0;
|
||||
};
|
||||
|
||||
#endif // GUI_ELEMENT_FACTORY_H
|
||||
|
||||
Reference in New Issue
Block a user