|
| 1 | +/**************************************************************************** |
| 2 | +** SASM - simple IDE for NASM development |
| 3 | +** Copyright (C) 2013 Dmitriy Manushin |
| 4 | +** Contact: site: https://sites.google.com/site/simpleasmproject/ |
| 5 | + |
| 6 | +** |
| 7 | +** This file is part of SASM. |
| 8 | +** |
| 9 | +** SASM is free software: you can redistribute it and/or modify |
| 10 | +** it under the terms of the GNU General Public License as published by |
| 11 | +** the Free Software Foundation, either version 3 of the License, or |
| 12 | +** (at your option) any later version. |
| 13 | +** |
| 14 | +** SASM is distributed in the hope that it will be useful, |
| 15 | +** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +** GNU General Public License for more details. |
| 18 | +** |
| 19 | +** You should have received a copy of the GNU General Public License |
| 20 | +** along with SASM. If not, see <http://www.gnu.org/licenses/>. |
| 21 | +** |
| 22 | +** Этот файл — часть SASM. |
| 23 | +** |
| 24 | +** SASM - свободная программа: вы можете перераспространять ее и/или |
| 25 | +** изменять ее на условиях Стандартной общественной лицензии GNU в том виде, |
| 26 | +** в каком она была опубликована Фондом свободного программного обеспечения; |
| 27 | +** либо версии 3 лицензии, либо (по вашему выбору) любой более поздней |
| 28 | +** версии. |
| 29 | +** |
| 30 | +** SASM распространяется в надежде, что она будет полезной, |
| 31 | +** но БЕЗО ВСЯКИХ ГАРАНТИЙ; даже без неявной гарантии ТОВАРНОГО ВИДА |
| 32 | +** или ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Подробнее см. в Стандартной |
| 33 | +** общественной лицензии GNU. |
| 34 | +** |
| 35 | +** Вы должны были получить копию Стандартной общественной лицензии GNU |
| 36 | +** вместе с этой программой. Если это не так, см. |
| 37 | +** <http://www.gnu.org/licenses/>.) |
| 38 | +** |
| 39 | +****************************************************************************/ |
| 40 | + |
| 41 | +#include "codeeditor.h" |
| 42 | + |
| 43 | +CodeEditor::CodeEditor(QWidget *parent) : RuQPlainTextEdit(parent) |
| 44 | +{ |
| 45 | + lineNumberArea = new LineNumberArea(this); |
| 46 | + |
| 47 | + connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); |
| 48 | + connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); |
| 49 | + connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); |
| 50 | + |
| 51 | + updateLineNumberAreaWidth(0); |
| 52 | + highlightCurrentLine(); |
| 53 | + currentDebugLine = -1; |
| 54 | +} |
| 55 | + |
| 56 | +int CodeEditor::lineNumberAreaWidth() |
| 57 | +{ |
| 58 | + int digits = 1; |
| 59 | + int max = qMax(1, blockCount()); |
| 60 | + while (max >= 10) { |
| 61 | + max /= 10; |
| 62 | + ++digits; |
| 63 | + } |
| 64 | + |
| 65 | + int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; |
| 66 | + |
| 67 | + return space; |
| 68 | +} |
| 69 | + |
| 70 | +void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) |
| 71 | +{ |
| 72 | + setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); |
| 73 | +} |
| 74 | + |
| 75 | +void CodeEditor::updateLineNumberArea(const QRect &rect, int dy) |
| 76 | +{ |
| 77 | + lastRect = rect; |
| 78 | + if (dy) |
| 79 | + lineNumberArea->scroll(0, dy); |
| 80 | + else |
| 81 | + lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); |
| 82 | + |
| 83 | + if (rect.contains(viewport()->rect())) |
| 84 | + updateLineNumberAreaWidth(0); |
| 85 | +} |
| 86 | + |
| 87 | +void CodeEditor::resizeEvent(QResizeEvent *e) |
| 88 | +{ |
| 89 | + QPlainTextEdit::resizeEvent(e); |
| 90 | + |
| 91 | + QRect cr = contentsRect(); |
| 92 | + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); |
| 93 | +} |
| 94 | + |
| 95 | +void CodeEditor::highlightCurrentLine() |
| 96 | +{ |
| 97 | + QList<QTextEdit::ExtraSelection> extraSelections; |
| 98 | + |
| 99 | + if (!isReadOnly()) { |
| 100 | + QTextEdit::ExtraSelection selection; |
| 101 | + |
| 102 | + QColor lineColor = QColor(232,232,255); |
| 103 | + |
| 104 | + selection.format.setBackground(lineColor); |
| 105 | + selection.format.setProperty(QTextFormat::FullWidthSelection, true); |
| 106 | + selection.cursor = textCursor(); |
| 107 | + selection.cursor.clearSelection(); |
| 108 | + extraSelections.append(selection); |
| 109 | + } |
| 110 | + |
| 111 | + setExtraSelections(extraSelections); |
| 112 | +} |
| 113 | + |
| 114 | +void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event) |
| 115 | +{ |
| 116 | + QPainter painter(lineNumberArea); |
| 117 | + painter.fillRect(event->rect(), QColor(240,240,240)); |
| 118 | + |
| 119 | + QTextBlock block = firstVisibleBlock(); |
| 120 | + int blockNumber = block.blockNumber(); |
| 121 | + int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); |
| 122 | + int bottom = top + (int) blockBoundingRect(block).height(); |
| 123 | + |
| 124 | + while (block.isValid() && top <= event->rect().bottom()) { |
| 125 | + if (block.isVisible() && bottom >= event->rect().top()) { |
| 126 | + QString number = QString::number(blockNumber + 1); |
| 127 | + painter.setPen(Qt::black); |
| 128 | + if (blockNumber + 1 == currentDebugLine) |
| 129 | + painter.setPen(Qt::red); |
| 130 | + painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), |
| 131 | + Qt::AlignRight, number); |
| 132 | + } |
| 133 | + |
| 134 | + block = block.next(); |
| 135 | + top = bottom; |
| 136 | + bottom = top + (int) blockBoundingRect(block).height(); |
| 137 | + ++blockNumber; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +void CodeEditor::updateDebugLine(int number) |
| 142 | +{ |
| 143 | + currentDebugLine = number; |
| 144 | + lineNumberArea->scroll(0, 1); |
| 145 | + lineNumberArea->scroll(0, -1); |
| 146 | +} |
| 147 | + |
| 148 | +void CodeEditor::putTab() |
| 149 | +{ |
| 150 | + QTextCursor cursor = this->textCursor(); |
| 151 | + if (cursor.selectionEnd() - cursor.selectionStart() <= 0) { |
| 152 | + this->insertPlainText(" "); |
| 153 | + } else { |
| 154 | + QString selected = |
| 155 | + cursor.selectedText().replace(QString(QChar::ParagraphSeparator), |
| 156 | + QString(QChar::ParagraphSeparator) + QString(" ")); |
| 157 | + selected.insert(0, QString(" ")); |
| 158 | + cursor.removeSelectedText(); |
| 159 | + cursor.insertText(selected); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +void CodeEditor::deleteTab() |
| 164 | +{ |
| 165 | + QTextCursor cursor = this->textCursor(); |
| 166 | + if (cursor.selectionEnd() - cursor.selectionStart() <= 0) { |
| 167 | + //delete 4 spaces (tab) |
| 168 | + cursor.movePosition(QTextCursor::Left, QTextCursor::KeepAnchor, 4); |
| 169 | + QString selected = cursor.selectedText(); |
| 170 | + if (selected.left(4) == QString(" ")) |
| 171 | + cursor.deletePreviousChar(); |
| 172 | + } else { |
| 173 | + QString selected = |
| 174 | + cursor.selectedText().replace(QString(QChar::ParagraphSeparator) + QString(" "), |
| 175 | + QString(QChar::ParagraphSeparator)); |
| 176 | + cursor.removeSelectedText(); |
| 177 | + if (selected.left(4) == QString(" ")) |
| 178 | + cursor.insertText(selected.right(selected.length() - 4)); |
| 179 | + else |
| 180 | + cursor.insertText(selected); |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +void CodeEditor::keyPressEvent(QKeyEvent *e) |
| 185 | +{ |
| 186 | + QString curString = this->textCursor().block().text(); |
| 187 | + int indentWidth = -1; |
| 188 | + switch (e->key()) { |
| 189 | + case (Qt::Key_Tab) : |
| 190 | + if (e->modifiers() & Qt::ControlModifier) |
| 191 | + deleteTab(); |
| 192 | + else |
| 193 | + putTab(); |
| 194 | + break; |
| 195 | + case (Qt::Key_Enter) : |
| 196 | + case (Qt::Key_Return) : |
| 197 | + while (curString[++indentWidth] == ' '); |
| 198 | + QPlainTextEdit::keyPressEvent(e); |
| 199 | + for (int i = 0; i < indentWidth; i++) |
| 200 | + this->insertPlainText(" "); |
| 201 | + break; |
| 202 | + case (Qt::Key_Q) : |
| 203 | + if ((e->modifiers() & Qt::ShiftModifier) && (e->modifiers() & Qt::ControlModifier)) |
| 204 | + this->commentSelectedCode(); |
| 205 | + else |
| 206 | + QPlainTextEdit::keyPressEvent(e); |
| 207 | + break; |
| 208 | + case (Qt::Key_A) : |
| 209 | + if ((e->modifiers() & Qt::ShiftModifier) && (e->modifiers() & Qt::ControlModifier)) |
| 210 | + this->uncommentSelectedCode(); |
| 211 | + else |
| 212 | + QPlainTextEdit::keyPressEvent(e); |
| 213 | + break; |
| 214 | + default: |
| 215 | + QPlainTextEdit::keyPressEvent(e); |
| 216 | + } |
| 217 | +} |
| 218 | + |
| 219 | +CodeEditor::~CodeEditor() |
| 220 | +{ |
| 221 | + delete lineNumberArea; |
| 222 | +} |
0 commit comments