-----------------------------------------------------------------------------
| 12 | |
| 13 | //----------------------------------------------------------------------------- |
| 14 | pqPythonTextArea::pqPythonTextArea(QWidget* parent) |
| 15 | : QWidget(parent) |
| 16 | , TextEdit(new QTextEdit(this)) |
| 17 | , LineNumberArea(new pqPythonLineNumberArea(this, *this->TextEdit)) |
| 18 | , SyntaxHighlighter(new pqPythonSyntaxHighlighter(this, *this->TextEdit)) |
| 19 | , FileIO(new pqPythonFileIO(this, *this->TextEdit)) |
| 20 | { |
| 21 | this->TextEdit->setUndoRedoEnabled(false); |
| 22 | this->TextEdit->setAcceptDrops(false); |
| 23 | this->TextEdit->setAcceptRichText(false); |
| 24 | this->TextEdit->setContextMenuPolicy(Qt::NoContextMenu); |
| 25 | |
| 26 | // Install the Undo/Redo event filter |
| 27 | this->TextEdit->installEventFilter(this); |
| 28 | |
| 29 | // Prepare the HBoxLayout for the text widget and the line area widget |
| 30 | QHBoxLayout* layout = new QHBoxLayout(); |
| 31 | layout->addWidget(this->LineNumberArea); |
| 32 | layout->addWidget(this->TextEdit); |
| 33 | layout->setContentsMargins(0, 0, 0, 0); |
| 34 | layout->setSpacing(0); |
| 35 | this->setLayout(layout); |
| 36 | |
| 37 | this->connect(this->TextEdit.data(), &QTextEdit::cursorPositionChanged, |
| 38 | [this]() { this->LineNumberArea->update(); }); |
| 39 | |
| 40 | this->connect(this->TextEdit->document(), &QTextDocument::blockCountChanged, |
| 41 | [this](int) { this->LineNumberArea->updateGeometry(); }); |
| 42 | |
| 43 | this->connect(this->TextEdit->verticalScrollBar(), &QScrollBar::valueChanged, |
| 44 | [this](int) { this->LineNumberArea->update(); }); |
| 45 | |
| 46 | this->connect(this->TextEdit.data(), &QTextEdit::textChanged, |
| 47 | [this]() |
| 48 | { |
| 49 | const QString text = this->TextEdit->toPlainText(); |
| 50 | const int cursorPosition = this->TextEdit->textCursor().position(); |
| 51 | |
| 52 | // Push a new command to the stack (which calls redo, thus applying the command) |
| 53 | this->UndoStack.push(new pqPythonUndoCommand( |
| 54 | *this->TextEdit, this->SyntaxHighlighter, this->lastEntry, { text, cursorPosition })); |
| 55 | |
| 56 | // Save the last entry |
| 57 | this->lastEntry = { text, cursorPosition }; |
| 58 | |
| 59 | const int vScrollBarValue = this->TextEdit->verticalScrollBar()->value(); |
| 60 | this->TextEdit->verticalScrollBar()->setValue(vScrollBarValue); |
| 61 | |
| 62 | const int hScrollBarValue = this->TextEdit->horizontalScrollBar()->value(); |
| 63 | this->TextEdit->horizontalScrollBar()->setValue(hScrollBarValue); |
| 64 | |
| 65 | this->FileIO->contentChanged(); |
| 66 | }); |
| 67 | |
| 68 | this->connect( |
| 69 | this->FileIO.data(), &pqPythonFileIO::fileSaved, this, &pqPythonTextArea::fileSaved); |
| 70 | this->connect( |
| 71 | this->FileIO.data(), &pqPythonFileIO::fileOpened, this, &pqPythonTextArea::fileOpened); |