Editor code cleanup
This commit is contained in:
parent
d266882927
commit
e082a5731d
3 changed files with 45 additions and 36 deletions
|
|
@ -31,7 +31,8 @@ int CodeEditor::gutterAreaWidth()
|
||||||
{
|
{
|
||||||
int digits = 1;
|
int digits = 1;
|
||||||
int max = qMax(1, blockCount());
|
int max = qMax(1, blockCount());
|
||||||
while (max >= 10) {
|
while (max >= 10)
|
||||||
|
{
|
||||||
max /= 10;
|
max /= 10;
|
||||||
++digits;
|
++digits;
|
||||||
}
|
}
|
||||||
|
|
@ -136,14 +137,17 @@ void CodeEditor::gutterAreaPaintEvent(QPaintEvent *event)
|
||||||
//
|
//
|
||||||
// We get the top and bottom y-coordinate of the first text block, and adjust these
|
// We get the top and bottom y-coordinate of the first text block, and adjust these
|
||||||
// values by the height of the current text block in each iteration in the loop.
|
// values by the height of the current text block in each iteration in the loop.
|
||||||
while (block.isValid() && top <= event->rect().bottom()) {
|
while (block.isValid() && top <= event->rect().bottom())
|
||||||
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, gutterArea->width(), fontMetrics().height(),
|
painter.drawText(0, top, gutterArea->width(), fontMetrics().height(),
|
||||||
Qt::AlignRight, number);
|
Qt::AlignRight, number);
|
||||||
|
|
||||||
if (errorLines.count(blockNumber) > 0) {
|
if (errorLines.count(blockNumber) > 0)
|
||||||
|
{
|
||||||
int s = fontMetrics().height() - 8;
|
int s = fontMetrics().height() - 8;
|
||||||
painter.setBrush(QBrush(Qt::red));
|
painter.setBrush(QBrush(Qt::red));
|
||||||
painter.drawEllipse(4, top + 4, s, s);
|
painter.drawEllipse(4, top + 4, s, s);
|
||||||
|
|
@ -160,29 +164,32 @@ void CodeEditor::gutterAreaPaintEvent(QPaintEvent *event)
|
||||||
void CodeEditor::keyPressEvent(QKeyEvent *e)
|
void CodeEditor::keyPressEvent(QKeyEvent *e)
|
||||||
{
|
{
|
||||||
auto k = e->key();
|
auto k = e->key();
|
||||||
if (k == Qt::Key_Tab) {
|
switch (k) {
|
||||||
// Function returns false if there was no selection to indent
|
case Qt::Key_Tab:
|
||||||
if (indentSelection(true))
|
if (indentSelection(true))
|
||||||
return;
|
return;
|
||||||
}
|
break;
|
||||||
else if (k == Qt::Key_Backtab) {
|
case Qt::Key_Backtab:
|
||||||
// Function returns false if there was no selection to indent
|
if (indentSelection(false))
|
||||||
if (indentSelection(false))
|
return;
|
||||||
return;
|
break;
|
||||||
}
|
case Qt::Key_Equal:
|
||||||
else if (k == Qt::Key_Equal || k == Qt::Key_Plus) {
|
case Qt::Key_Plus:
|
||||||
if (e->modifiers().testFlag(Qt::ControlModifier)) {
|
if (e->modifiers().testFlag(Qt::ControlModifier))
|
||||||
auto f = font();
|
{
|
||||||
f.setPointSize(f.pointSize() + 1);
|
auto f = font();
|
||||||
setFont(f);
|
f.setPointSize(f.pointSize() + 1);
|
||||||
}
|
setFont(f);
|
||||||
}
|
}
|
||||||
else if (k == Qt::Key_Minus) {
|
break;
|
||||||
if (e->modifiers().testFlag(Qt::ControlModifier)) {
|
case Qt::Key_Minus:
|
||||||
auto f = font();
|
if (e->modifiers().testFlag(Qt::ControlModifier))
|
||||||
f.setPointSize(f.pointSize() - 1);
|
{
|
||||||
setFont(f);
|
auto f = font();
|
||||||
}
|
f.setPointSize(f.pointSize() - 1);
|
||||||
|
setFont(f);
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
QPlainTextEdit::keyPressEvent(e);
|
QPlainTextEdit::keyPressEvent(e);
|
||||||
}
|
}
|
||||||
|
|
@ -211,27 +218,32 @@ bool CodeEditor::indentSelection(bool indent)
|
||||||
cursor.beginEditBlock();
|
cursor.beginEditBlock();
|
||||||
cursor.setPosition(first_pos, QTextCursor::MoveAnchor);
|
cursor.setPosition(first_pos, QTextCursor::MoveAnchor);
|
||||||
const auto block_count = end_block - start_block;
|
const auto block_count = end_block - start_block;
|
||||||
for(int block = 0; block <= block_count; ++block) {
|
for(int block = 0; block <= block_count; ++block)
|
||||||
|
{
|
||||||
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
|
cursor.movePosition(QTextCursor::StartOfBlock, QTextCursor::MoveAnchor);
|
||||||
if (indent) {
|
if (indent)
|
||||||
|
{
|
||||||
if (m_useTab)
|
if (m_useTab)
|
||||||
cursor.insertText("\t");
|
cursor.insertText("\t");
|
||||||
else
|
else
|
||||||
cursor.insertText(QString(m_tabSize, ' '));
|
cursor.insertText(QString(m_tabSize, ' '));
|
||||||
}
|
}
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
// remove tab if there is a tab
|
// remove tab if there is a tab
|
||||||
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
|
cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
|
||||||
const QString text = cursor.selectedText();
|
const QString text = cursor.selectedText();
|
||||||
int index = 0;
|
int index = 0;
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
while (pos < m_tabSize && index < text.length()) {
|
while (pos < m_tabSize && index < text.length())
|
||||||
|
{
|
||||||
QChar c = text[index++];
|
QChar c = text[index++];
|
||||||
if (c == ' ')
|
if (c == ' ')
|
||||||
++pos;
|
++pos;
|
||||||
else if (c == '\t')
|
else if (c == '\t')
|
||||||
pos = ((pos + m_tabSize) / m_tabSize) * m_tabSize;
|
pos = ((pos + m_tabSize) / m_tabSize) * m_tabSize;
|
||||||
else {
|
else
|
||||||
|
{
|
||||||
--index;
|
--index;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,7 @@
|
||||||
EditorGutter::EditorGutter(CodeEditor *editor)
|
EditorGutter::EditorGutter(CodeEditor *editor)
|
||||||
: QWidget(editor)
|
: QWidget(editor)
|
||||||
, codeEditor(editor)
|
, codeEditor(editor)
|
||||||
{
|
{}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
QSize EditorGutter::sizeHint() const
|
QSize EditorGutter::sizeHint() const
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,6 @@
|
||||||
#define EDITORGUTTER_H
|
#define EDITORGUTTER_H
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
//#include <QPlainTextEdit>
|
|
||||||
|
|
||||||
class CodeEditor;
|
class CodeEditor;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue