GUI: Rename private variables

This commit is contained in:
Daniel Marjamäki 2018-06-18 10:10:11 +02:00
parent 68a91b73da
commit 86c5f44d19
2 changed files with 52 additions and 52 deletions

View File

@ -8,8 +8,8 @@ Highlighter::Highlighter(QTextDocument *parent)
{ {
HighlightingRule rule; HighlightingRule rule;
keywordFormat.setForeground(Qt::darkBlue); mKeywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFontWeight(QFont::Bold); mKeywordFormat.setFontWeight(QFont::Bold);
QStringList keywordPatterns; QStringList keywordPatterns;
keywordPatterns << "bool" keywordPatterns << "bool"
<< "break" << "break"
@ -53,51 +53,51 @@ Highlighter::Highlighter(QTextDocument *parent)
<< "while"; << "while";
foreach (const QString &pattern, keywordPatterns) { foreach (const QString &pattern, keywordPatterns) {
rule.pattern = QRegularExpression("\\b" + pattern + "\\b"); rule.pattern = QRegularExpression("\\b" + pattern + "\\b");
rule.format = keywordFormat; rule.format = mKeywordFormat;
highlightingRules.append(rule); mHighlightingRules.append(rule);
} }
classFormat.setFontWeight(QFont::Bold); mClassFormat.setFontWeight(QFont::Bold);
classFormat.setForeground(Qt::darkMagenta); mClassFormat.setForeground(Qt::darkMagenta);
rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b"); rule.pattern = QRegularExpression("\\bQ[A-Za-z]+\\b");
rule.format = classFormat; rule.format = mClassFormat;
highlightingRules.append(rule); mHighlightingRules.append(rule);
quotationFormat.setForeground(Qt::darkGreen); mQuotationFormat.setForeground(Qt::darkGreen);
rule.pattern = QRegularExpression("\".*\""); rule.pattern = QRegularExpression("\".*\"");
rule.format = quotationFormat; rule.format = mQuotationFormat;
highlightingRules.append(rule); mHighlightingRules.append(rule);
singleLineCommentFormat.setForeground(Qt::gray); mSingleLineCommentFormat.setForeground(Qt::gray);
rule.pattern = QRegularExpression("//[^\n]*"); rule.pattern = QRegularExpression("//[^\n]*");
rule.format = singleLineCommentFormat; rule.format = mSingleLineCommentFormat;
highlightingRules.append(rule); mHighlightingRules.append(rule);
highlightingRulesWithSymbols = highlightingRules; mHighlightingRulesWithSymbols = mHighlightingRules;
multiLineCommentFormat.setForeground(Qt::gray); mMultiLineCommentFormat.setForeground(Qt::gray);
symbolFormat.setForeground(Qt::red); mSymbolFormat.setForeground(Qt::red);
symbolFormat.setBackground(QColor(220,220,255)); mSymbolFormat.setBackground(QColor(220,220,255));
commentStartExpression = QRegularExpression("/\\*"); mCommentStartExpression = QRegularExpression("/\\*");
commentEndExpression = QRegularExpression("\\*/"); mCommentEndExpression = QRegularExpression("\\*/");
} }
void Highlighter::setSymbols(const QStringList &symbols) void Highlighter::setSymbols(const QStringList &symbols)
{ {
highlightingRulesWithSymbols = highlightingRules; mHighlightingRulesWithSymbols = mHighlightingRules;
foreach (const QString &sym, symbols) { foreach (const QString &sym, symbols) {
HighlightingRule rule; HighlightingRule rule;
rule.pattern = QRegularExpression("\\b" + sym + "\\b"); rule.pattern = QRegularExpression("\\b" + sym + "\\b");
rule.format = symbolFormat; rule.format = mSymbolFormat;
highlightingRulesWithSymbols.append(rule); mHighlightingRulesWithSymbols.append(rule);
} }
} }
void Highlighter::highlightBlock(const QString &text) void Highlighter::highlightBlock(const QString &text)
{ {
foreach (const HighlightingRule &rule, highlightingRulesWithSymbols) { foreach (const HighlightingRule &rule, mHighlightingRulesWithSymbols) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text); QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) { while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next(); QRegularExpressionMatch match = matchIterator.next();
@ -109,10 +109,10 @@ void Highlighter::highlightBlock(const QString &text)
int startIndex = 0; int startIndex = 0;
if (previousBlockState() != 1) if (previousBlockState() != 1)
startIndex = text.indexOf(commentStartExpression); startIndex = text.indexOf(mCommentStartExpression);
while (startIndex >= 0) { while (startIndex >= 0) {
QRegularExpressionMatch match = commentEndExpression.match(text, startIndex); QRegularExpressionMatch match = mCommentEndExpression.match(text, startIndex);
int endIndex = match.capturedStart(); int endIndex = match.capturedStart();
int commentLength = 0; int commentLength = 0;
if (endIndex == -1) { if (endIndex == -1) {
@ -122,16 +122,16 @@ void Highlighter::highlightBlock(const QString &text)
commentLength = endIndex - startIndex commentLength = endIndex - startIndex
+ match.capturedLength(); + match.capturedLength();
} }
setFormat(startIndex, commentLength, multiLineCommentFormat); setFormat(startIndex, commentLength, mMultiLineCommentFormat);
startIndex = text.indexOf(commentStartExpression, startIndex + commentLength); startIndex = text.indexOf(mCommentStartExpression, startIndex + commentLength);
} }
} }
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
{ {
lineNumberArea = new LineNumberArea(this); mLineNumberArea = new LineNumberArea(this);
highlighter = new Highlighter(this->document()); mHighlighter = new Highlighter(this->document());
mErrorPosition = -1; mErrorPosition = -1;
setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont));
@ -158,7 +158,7 @@ static int getPos(const QString &fileData, int lineNumber)
void CodeEditor::setError(const QString &code, int errorLine, const QStringList &symbols) void CodeEditor::setError(const QString &code, int errorLine, const QStringList &symbols)
{ {
highlighter->setSymbols(symbols); mHighlighter->setSymbols(symbols);
setPlainText(code); setPlainText(code);
@ -192,9 +192,9 @@ void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
void CodeEditor::updateLineNumberArea(const QRect &rect, int dy) void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
{ {
if (dy) if (dy)
lineNumberArea->scroll(0, dy); mLineNumberArea->scroll(0, dy);
else else
lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); mLineNumberArea->update(0, rect.y(), mLineNumberArea->width(), rect.height());
if (rect.contains(viewport()->rect())) if (rect.contains(viewport()->rect()))
updateLineNumberAreaWidth(0); updateLineNumberAreaWidth(0);
@ -204,7 +204,7 @@ void CodeEditor::resizeEvent(QResizeEvent *event)
{ {
QPlainTextEdit::resizeEvent(event); QPlainTextEdit::resizeEvent(event);
QRect cr = contentsRect(); QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); mLineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
} }
void CodeEditor::highlightErrorLine() void CodeEditor::highlightErrorLine()
@ -227,7 +227,7 @@ void CodeEditor::highlightErrorLine()
void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
{ {
QPainter painter(lineNumberArea); QPainter painter(mLineNumberArea);
painter.fillRect(event->rect(), QColor(240,240,240)); painter.fillRect(event->rect(), QColor(240,240,240));
QTextBlock block = firstVisibleBlock(); QTextBlock block = firstVisibleBlock();
@ -239,7 +239,7 @@ void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
if (block.isVisible() && bottom >= event->rect().top()) { if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1); QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black); painter.setPen(Qt::black);
painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), painter.drawText(0, top, mLineNumberArea->width(), fontMetrics().height(),
Qt::AlignRight, number); Qt::AlignRight, number);
} }

View File

@ -30,18 +30,18 @@ private:
QRegularExpression pattern; QRegularExpression pattern;
QTextCharFormat format; QTextCharFormat format;
}; };
QVector<HighlightingRule> highlightingRules; QVector<HighlightingRule> mHighlightingRules;
QVector<HighlightingRule> highlightingRulesWithSymbols; QVector<HighlightingRule> mHighlightingRulesWithSymbols;
QRegularExpression commentStartExpression; QRegularExpression mCommentStartExpression;
QRegularExpression commentEndExpression; QRegularExpression mCommentEndExpression;
QTextCharFormat keywordFormat; QTextCharFormat mKeywordFormat;
QTextCharFormat classFormat; QTextCharFormat mClassFormat;
QTextCharFormat singleLineCommentFormat; QTextCharFormat mSingleLineCommentFormat;
QTextCharFormat multiLineCommentFormat; QTextCharFormat mMultiLineCommentFormat;
QTextCharFormat quotationFormat; QTextCharFormat mQuotationFormat;
QTextCharFormat symbolFormat; QTextCharFormat mSymbolFormat;
}; };
class CodeEditor : public QPlainTextEdit { class CodeEditor : public QPlainTextEdit {
@ -66,8 +66,8 @@ private slots:
void updateLineNumberArea(const QRect &, int); void updateLineNumberArea(const QRect &, int);
private: private:
QWidget *lineNumberArea; QWidget *mLineNumberArea;
Highlighter *highlighter; Highlighter *mHighlighter;
int mErrorPosition; int mErrorPosition;
}; };
@ -75,20 +75,20 @@ private:
class LineNumberArea : public QWidget { class LineNumberArea : public QWidget {
public: public:
explicit LineNumberArea(CodeEditor *editor) : QWidget(editor) { explicit LineNumberArea(CodeEditor *editor) : QWidget(editor) {
codeEditor = editor; mCodeEditor = editor;
} }
QSize sizeHint() const override { QSize sizeHint() const override {
return QSize(codeEditor->lineNumberAreaWidth(), 0); return QSize(mCodeEditor->lineNumberAreaWidth(), 0);
} }
protected: protected:
void paintEvent(QPaintEvent *event) override { void paintEvent(QPaintEvent *event) override {
codeEditor->lineNumberAreaPaintEvent(event); mCodeEditor->lineNumberAreaPaintEvent(event);
} }
private: private:
CodeEditor *codeEditor; CodeEditor *mCodeEditor;
}; };
#endif // CODEEDITOR_H #endif // CODEEDITOR_H