ui: styled code view
* updated spacings and colors in QtCodeView and QtCodeFile * using Source Code Pro Regular (had to remove light cut from the Fonts folder because it got always used) * updated syntax highlighted colors to fit design * changed colors of highlights * indent all QtCodeSnippets by maximum line number in QtCodeFile * fixed syntax highlighting to work with sole multiline comment end fortune cookie message = Your present plans are going to succeed.
This commit is contained in:
@@ -10,17 +10,18 @@ QtCodeFile::QtCodeFile(QtCodeView* parentView, const std::string& fileName, QWid
|
||||
, m_parentView(parentView)
|
||||
, m_fileName(fileName)
|
||||
{
|
||||
setObjectName("code_file");
|
||||
|
||||
QVBoxLayout* layout = new QVBoxLayout(this);
|
||||
layout->setMargin(0);
|
||||
layout->setSpacing(2);
|
||||
layout->setSpacing(0);
|
||||
layout->setAlignment(Qt::AlignTop);
|
||||
setLayout(layout);
|
||||
|
||||
QLabel* label = new QLabel(fileName.c_str(), this);
|
||||
QFontMetrics metrics(label->font());
|
||||
|
||||
label->setStyleSheet("background-color: #E1E1E1; padding: 3px;");
|
||||
label->setFixedWidth(metrics.boundingRect(fileName.c_str()).width() + 12);
|
||||
label->setObjectName("title_label");
|
||||
label->minimumSizeHint(); // force font loading
|
||||
label->setFixedWidth(label->fontMetrics().width(fileName.c_str()) + 32);
|
||||
label->setSizePolicy(sizePolicy().horizontalPolicy(), QSizePolicy::Fixed);
|
||||
layout->addWidget(label);
|
||||
}
|
||||
@@ -42,4 +43,15 @@ void QtCodeFile::addCodeSnippet(
|
||||
);
|
||||
layout()->addWidget(snippet.get());
|
||||
m_snippets.push_back(snippet);
|
||||
|
||||
int maxDigits = 1;
|
||||
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
||||
{
|
||||
maxDigits = qMax(maxDigits, snippet->lineNumberDigits());
|
||||
}
|
||||
|
||||
for (std::shared_ptr<QtCodeSnippet> snippet : m_snippets)
|
||||
{
|
||||
snippet->updateLineNumberAreaWidthForDigits(maxDigits);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ QtCodeSnippet::LineNumberArea::LineNumberArea(QtCodeSnippet *codeSnippet)
|
||||
: QWidget(codeSnippet)
|
||||
, m_codeSnippet(codeSnippet)
|
||||
{
|
||||
setObjectName("line_number_area");
|
||||
}
|
||||
|
||||
QtCodeSnippet::LineNumberArea::~LineNumberArea()
|
||||
@@ -43,7 +44,10 @@ QtCodeSnippet::QtCodeSnippet(
|
||||
, m_parentView(parentView)
|
||||
, m_startLineNumber(startLineNumber)
|
||||
, m_activeTokenId(activeTokenId)
|
||||
, m_digits(0)
|
||||
{
|
||||
minimumSizeHint(); // force font loading
|
||||
setObjectName("code_snippet");
|
||||
m_lineNumberArea = new LineNumberArea(this);
|
||||
|
||||
setReadOnly(true);
|
||||
@@ -52,15 +56,8 @@ QtCodeSnippet::QtCodeSnippet(
|
||||
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
||||
setLineWrapMode(QPlainTextEdit::NoWrap);
|
||||
|
||||
QFont font;
|
||||
font.setFamily(ApplicationSettings::getInstance()->getCodeFontName().c_str());
|
||||
font.setPointSize(ApplicationSettings::getInstance()->getCodeFontSize());
|
||||
font.setFixedPitch(true);
|
||||
setFont(font);
|
||||
|
||||
int tabWidth = ApplicationSettings::getInstance()->getCodeTabWidth();
|
||||
QFontMetrics metrics(font);
|
||||
setTabStopWidth(tabWidth * metrics.width(' '));
|
||||
setTabStopWidth(tabWidth * fontMetrics().width('9'));
|
||||
|
||||
m_highlighter = new QtHighlighter(document());
|
||||
|
||||
@@ -80,6 +77,7 @@ QtCodeSnippet::QtCodeSnippet(
|
||||
|
||||
setMaximumHeight(sizeHint().height());
|
||||
|
||||
m_digits = lineNumberDigits();
|
||||
updateLineNumberAreaWidth(0);
|
||||
}
|
||||
|
||||
@@ -90,14 +88,13 @@ QtCodeSnippet::~QtCodeSnippet()
|
||||
QSize QtCodeSnippet::sizeHint() const
|
||||
{
|
||||
int width = lineNumberAreaWidth() + document()->size().width();
|
||||
int height = (document()->size().height() + 1) * QFontMetrics(font()).lineSpacing();
|
||||
int height = (document()->size().height() + 1) * fontMetrics().lineSpacing() * 1.1f;
|
||||
return QSize(width, height);
|
||||
}
|
||||
|
||||
void QtCodeSnippet::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
{
|
||||
QPainter painter(m_lineNumberArea);
|
||||
painter.fillRect(event->rect(), QColor(225,225,225));
|
||||
|
||||
QTextBlock block = firstVisibleBlock();
|
||||
int blockNumber = block.blockNumber();
|
||||
@@ -110,7 +107,7 @@ void QtCodeSnippet::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
{
|
||||
QString number = QString::number(blockNumber + m_startLineNumber);
|
||||
painter.setPen(Qt::black);
|
||||
painter.drawText(0, top, m_lineNumberArea->width() - 3, fontMetrics().height(), Qt::AlignRight, number);
|
||||
painter.drawText(0, top, m_lineNumberArea->width() - 13, fontMetrics().height(), Qt::AlignRight, number);
|
||||
}
|
||||
|
||||
block = block.next();
|
||||
@@ -120,7 +117,7 @@ void QtCodeSnippet::lineNumberAreaPaintEvent(QPaintEvent *event)
|
||||
}
|
||||
}
|
||||
|
||||
int QtCodeSnippet::lineNumberAreaWidth() const
|
||||
int QtCodeSnippet::lineNumberDigits() const
|
||||
{
|
||||
int digits = 1;
|
||||
int max = qMax(1, m_startLineNumber + blockCount());
|
||||
@@ -130,9 +127,18 @@ int QtCodeSnippet::lineNumberAreaWidth() const
|
||||
max /= 10;
|
||||
digits++;
|
||||
}
|
||||
return digits;
|
||||
}
|
||||
|
||||
int width = fontMetrics().width(QLatin1Char('9')) * digits + 6;
|
||||
return width;
|
||||
int QtCodeSnippet::lineNumberAreaWidth() const
|
||||
{
|
||||
return fontMetrics().width(QLatin1Char('9')) * m_digits + 30;
|
||||
}
|
||||
|
||||
void QtCodeSnippet::updateLineNumberAreaWidthForDigits(int digits)
|
||||
{
|
||||
m_digits = digits;
|
||||
updateLineNumberAreaWidth(0);
|
||||
}
|
||||
|
||||
void QtCodeSnippet::annotateText(const TokenLocationFile& locationFile)
|
||||
|
||||
@@ -48,7 +48,9 @@ public:
|
||||
QSize sizeHint() const;
|
||||
|
||||
void lineNumberAreaPaintEvent(QPaintEvent *event);
|
||||
int lineNumberDigits() const;
|
||||
int lineNumberAreaWidth() const;
|
||||
void updateLineNumberAreaWidthForDigits(int digits);
|
||||
|
||||
void annotateText(const TokenLocationFile& locationFile);
|
||||
|
||||
@@ -79,6 +81,7 @@ private:
|
||||
QWidget *m_lineNumberArea;
|
||||
const int m_startLineNumber;
|
||||
const Id m_activeTokenId;
|
||||
int m_digits;
|
||||
|
||||
std::vector<Annotation> m_annotations;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user