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