ui: Improved scrolling and layouting in QtCodeView and colored locations of active TokenId differently
This commit is contained in:
@@ -13,6 +13,7 @@ QtCodeFile::QtCodeFile(QtCodeView* parentView, const std::string& fileName, QWid
|
||||
QVBoxLayout* layout = new QVBoxLayout(this);
|
||||
layout->setMargin(0);
|
||||
layout->setSpacing(2);
|
||||
layout->setAlignment(Qt::AlignTop);
|
||||
setLayout(layout);
|
||||
|
||||
QLabel* label = new QLabel(fileName.c_str(), this);
|
||||
@@ -20,6 +21,7 @@ QtCodeFile::QtCodeFile(QtCodeView* parentView, const std::string& fileName, QWid
|
||||
|
||||
label->setStyleSheet("background-color: #E1E1E1; padding: 3px;");
|
||||
label->setFixedWidth(metrics.boundingRect(fileName.c_str()).width() + 12);
|
||||
label->setSizePolicy(sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
|
||||
layout->addWidget(label);
|
||||
}
|
||||
|
||||
@@ -32,10 +34,11 @@ const std::string& QtCodeFile::getFileName() const
|
||||
return m_fileName;
|
||||
}
|
||||
|
||||
void QtCodeFile::addCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber)
|
||||
{
|
||||
void QtCodeFile::addCodeSnippet(
|
||||
const std::string& str, const TokenLocationFile& locationFile, int startLineNumber, Id activeTokenId
|
||||
){
|
||||
std::shared_ptr<QtCodeSnippet> snippet =
|
||||
std::make_shared<QtCodeSnippet>(m_parentView, str, locationFile, startLineNumber, this);
|
||||
std::make_shared<QtCodeSnippet>(m_parentView, str, locationFile, startLineNumber, activeTokenId, this);
|
||||
layout()->addWidget(snippet.get());
|
||||
m_snippets.push_back(snippet);
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
#include "utility/types.h"
|
||||
|
||||
class QtCodeSnippet;
|
||||
class QtCodeView;
|
||||
class TokenLocationFile;
|
||||
@@ -19,7 +21,9 @@ public:
|
||||
|
||||
const std::string& getFileName() const;
|
||||
|
||||
void addCodeSnippet(const std::string& str, const TokenLocationFile& locationFile, int startLineNumber);
|
||||
void addCodeSnippet(
|
||||
const std::string& str, const TokenLocationFile& locationFile, int startLineNumber, Id activeTokenId
|
||||
);
|
||||
|
||||
private:
|
||||
QtCodeView* m_parentView;
|
||||
|
||||
@@ -36,21 +36,26 @@ QtCodeSnippet::QtCodeSnippet(
|
||||
const std::string& code,
|
||||
const TokenLocationFile& locationFile,
|
||||
int startLineNumber,
|
||||
Id activeTokenId,
|
||||
QWidget *parent
|
||||
)
|
||||
: QPlainTextEdit(parent)
|
||||
, m_parentView(parentView)
|
||||
, m_startLineNumber(startLineNumber)
|
||||
, m_activeTokenId(activeTokenId)
|
||||
{
|
||||
m_lineNumberArea = new LineNumberArea(this);
|
||||
|
||||
setReadOnly(true);
|
||||
setFrameStyle(QFrame::NoFrame);
|
||||
setSizePolicy(sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
|
||||
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setLineWrapMode(QPlainTextEdit::NoWrap);
|
||||
|
||||
QFont font;
|
||||
font.setFamily(ApplicationSettings::getInstance()->getCodeFontName().c_str());
|
||||
font.setFixedPitch(true);
|
||||
font.setPointSize(ApplicationSettings::getInstance()->getCodeFontSize());
|
||||
font.setFixedPitch(true);
|
||||
setFont(font);
|
||||
|
||||
int tabWidth = ApplicationSettings::getInstance()->getCodeTabWidth();
|
||||
@@ -58,7 +63,14 @@ QtCodeSnippet::QtCodeSnippet(
|
||||
setTabStopWidth(tabWidth * metrics.width(' '));
|
||||
|
||||
m_highlighter = new QtHighlighter(document());
|
||||
setPlainText(QString::fromUtf8(code.c_str()));
|
||||
|
||||
std::string displayCode = code;
|
||||
if (*code.rbegin() == '\n')
|
||||
{
|
||||
displayCode.pop_back();
|
||||
}
|
||||
|
||||
setPlainText(QString::fromUtf8(displayCode.c_str()));
|
||||
annotateText(locationFile);
|
||||
|
||||
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int)));
|
||||
@@ -66,6 +78,8 @@ QtCodeSnippet::QtCodeSnippet(
|
||||
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(clickTokenLocation()));
|
||||
connect(this, SIGNAL(selectionChanged()), this, SLOT(clearSelection()));
|
||||
|
||||
setMaximumHeight(sizeHint().height());
|
||||
|
||||
updateLineNumberAreaWidth(0);
|
||||
}
|
||||
|
||||
@@ -73,6 +87,13 @@ QtCodeSnippet::~QtCodeSnippet()
|
||||
{
|
||||
}
|
||||
|
||||
QSize QtCodeSnippet::sizeHint() const
|
||||
{
|
||||
int width = lineNumberAreaWidth() + document()->size().width();
|
||||
int height = (document()->size().height() + 1) * QFontMetrics(font()).lineSpacing();
|
||||
return QSize(width, height);
|
||||
}
|
||||
|
||||
void QtCodeSnippet::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(m_lineNumberArea);
|
||||
@@ -99,7 +120,7 @@ void QtCodeSnippet::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
}
|
||||
}
|
||||
|
||||
int QtCodeSnippet::lineNumberAreaWidth()
|
||||
int QtCodeSnippet::lineNumberAreaWidth() const
|
||||
{
|
||||
int digits = 1;
|
||||
int max = qMax(1, m_startLineNumber + blockCount());
|
||||
@@ -138,7 +159,16 @@ void QtCodeSnippet::annotateText(const TokenLocationFile& locationFile)
|
||||
|
||||
QTextEdit::ExtraSelection selection;
|
||||
|
||||
Colori color = ApplicationSettings::getInstance()->getCodeLinkColor();
|
||||
Colori color;
|
||||
if (location->getTokenId() == m_activeTokenId)
|
||||
{
|
||||
color = ApplicationSettings::getInstance()->getCodeActiveLinkColor();
|
||||
}
|
||||
else
|
||||
{
|
||||
color = ApplicationSettings::getInstance()->getCodeLinkColor();
|
||||
}
|
||||
|
||||
selection.format.setBackground(QColor(color.r, color.g, color.b, color.a));
|
||||
|
||||
selection.cursor = textCursor();
|
||||
|
||||
@@ -40,12 +40,15 @@ public:
|
||||
const std::string& code,
|
||||
const TokenLocationFile& locationFile,
|
||||
int startLineNumber,
|
||||
Id activeTokenId,
|
||||
QWidget *parent = 0
|
||||
);
|
||||
virtual ~QtCodeSnippet();
|
||||
|
||||
QSize sizeHint() const;
|
||||
|
||||
void lineNumberAreaPaintEvent(QPaintEvent *event);
|
||||
int lineNumberAreaWidth();
|
||||
int lineNumberAreaWidth() const;
|
||||
|
||||
void annotateText(const TokenLocationFile& locationFile);
|
||||
|
||||
@@ -73,6 +76,7 @@ private:
|
||||
|
||||
QWidget *m_lineNumberArea;
|
||||
const int m_startLineNumber;
|
||||
const Id m_activeTokenId;
|
||||
|
||||
std::vector<Annotation> m_annotations;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user