ui: Improved QtCodeView to annotate code

Added some features to QTCodeSnippet:
* Code is now annotated using information from TokenLocationFile
* Start of line numbering can be set
* Clicking on an annotation in the code gives the corresponding Token Id

fortune cookie message = Your kindness will be repaid.
This commit is contained in:
Eberhard Graether
2014-07-03 10:35:30 +02:00
parent e63106b13b
commit 4c7bbbe28e
8 changed files with 316 additions and 15 deletions
+14 -6
View File
@@ -1,7 +1,10 @@
#include "qt/view/QtCodeView.h"
#include <iostream>
#include <QtWidgets>
#include "data/location/TokenLocationFile.h"
#include "qt/element/QtCodeSnippet.h"
#include "qt/QtWidgetWrapper.h"
#include "qt/utility/QtHighLighter.h"
#include "qt/utility/utilityQt.h"
@@ -9,7 +12,7 @@
QtCodeView::QtCodeView(ViewLayout* viewLayout)
: CodeView(viewLayout)
, m_clearCodeSnippetsFunctor(std::bind(&QtCodeView::doClearCodeSnippets, this))
, m_addCodeSnippetFunctor(std::bind(&QtCodeView::doAddCodeSnippet, this, std::placeholders::_1))
, m_addCodeSnippetFunctor(std::bind(&QtCodeView::doAddCodeSnippet, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3))
{
}
@@ -37,9 +40,9 @@ void QtCodeView::initGui()
m_font.setPointSize(10);
}
void QtCodeView::addCodeSnippet(std::string str)
void QtCodeView::addCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber)
{
m_addCodeSnippetFunctor(str);
m_addCodeSnippetFunctor(str, locationFile, startLineNumber);
}
void QtCodeView::clearCodeSnippets()
@@ -47,17 +50,22 @@ void QtCodeView::clearCodeSnippets()
m_clearCodeSnippetsFunctor();
}
void QtCodeView::doAddCodeSnippet(std::string str)
void QtCodeView::activateToken(Id tokenId) const
{
std::cout << "TODO: activate Token: " << tokenId << std::endl;
}
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<QTextEdit>();
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);
QWidget* widget = QtWidgetWrapper::getWidgetOfView(this);
widget->layout()->addWidget(snippet->textField.get());
+8 -4
View File
@@ -8,7 +8,9 @@
#include "component/view/CodeView.h"
#include "qt/utility/QtThreadedFunctor.h"
#include "utility/types.h"
class QtCodeSnippet;
class QtHighlighter;
class QTextEdit;
@@ -23,24 +25,26 @@ public:
virtual void initGui();
// CodeView implementation
virtual void addCodeSnippet(std::string str);
virtual void addCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber);
virtual void clearCodeSnippets();
void activateToken(Id tokenId) const;
private:
struct Snippet
{
std::shared_ptr<QTextEdit> textField;
std::shared_ptr<QtCodeSnippet> textField;
std::shared_ptr<QtHighlighter> highlighter;
};
std::vector<std::shared_ptr<Snippet>> m_snippets;
void doAddCodeSnippet(std::string str);
void doAddCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber);
void doClearCodeSnippets();
QFont m_font;
QtThreadedFunctor<void> m_clearCodeSnippetsFunctor;
QtThreadedFunctor<std::string> m_addCodeSnippetFunctor;
QtThreadedFunctor<const std::string&, const TokenLocationFile&, int> m_addCodeSnippetFunctor;
};
# endif // QT_CODE_VIEW_H