|
| 1 | +#include <QtWidgets> |
| 2 | + |
| 3 | +#include "console.h" |
| 4 | + |
| 5 | +#include <QScrollBar> |
| 6 | + |
| 7 | +//#include <QtCore/QDebug> |
| 8 | + |
| 9 | +Console::Console(QWidget *parent) |
| 10 | + : QPlainTextEdit(parent) |
| 11 | +{ |
| 12 | + lineNumberArea = new LineNumberArea(this); |
| 13 | + |
| 14 | + updateLineNumberAreaWidth(0); |
| 15 | + highlightCurrentLine(); |
| 16 | + |
| 17 | + //document()->setMaximumBlockCount(100); |
| 18 | + document()->setMaximumBlockCount(-1); |
| 19 | + //QPalette p = palette(); |
| 20 | + //p.setColor(QPalette::Base, Qt::black); |
| 21 | + //p.setColor(QPalette::Text, Qt::green); |
| 22 | + //setPalette(p); |
| 23 | + |
| 24 | + QFont font; |
| 25 | + font.setFamily(QStringLiteral("Courier New")); |
| 26 | + this->setFont(font); |
| 27 | + //this->setTextInteractionFlags(Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse); |
| 28 | + |
| 29 | + connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); |
| 30 | + connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); |
| 31 | + connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); |
| 32 | + |
| 33 | + t.start(); |
| 34 | +} |
| 35 | + |
| 36 | +void Console::putData(const QByteArray &data) |
| 37 | +{ |
| 38 | + appendPlainText(data); |
| 39 | + if(localVerticalAutoScrollEnabled == true) |
| 40 | + { |
| 41 | + QScrollBar *bar = verticalScrollBar(); |
| 42 | + bar->setValue(bar->maximum()); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +void Console::setLocalEchoEnabled(bool set) |
| 47 | +{ |
| 48 | + localEchoEnabled = set; |
| 49 | +} |
| 50 | + |
| 51 | +void Console::setLocalVerticalScrollBarMaxEnabled(bool set) |
| 52 | +{ |
| 53 | + localVerticalAutoScrollEnabled = set; |
| 54 | +} |
| 55 | + |
| 56 | +void Console::keyPressEvent(QKeyEvent *e) |
| 57 | +{ |
| 58 | + //Q_UNUSED(e); |
| 59 | + QPlainTextEdit::keyPressEvent(e); |
| 60 | + /* |
| 61 | + switch (e->key()) { |
| 62 | + case Qt::Key_Backspace: |
| 63 | + case Qt::Key_Left: |
| 64 | + case Qt::Key_Right: |
| 65 | + case Qt::Key_Up: |
| 66 | + case Qt::Key_Down: |
| 67 | + break; |
| 68 | + default: |
| 69 | + if (localEchoEnabled) |
| 70 | + QPlainTextEdit::keyPressEvent(e); |
| 71 | + emit getData(e->text().toLocal8Bit()); |
| 72 | + } |
| 73 | + */ |
| 74 | +} |
| 75 | + |
| 76 | +void Console::mousePressEvent(QMouseEvent *e) |
| 77 | +{ |
| 78 | + QPlainTextEdit::mousePressEvent(e); |
| 79 | + //Q_UNUSED(e) |
| 80 | + //setFocus(); |
| 81 | +} |
| 82 | + |
| 83 | +void Console::mouseDoubleClickEvent(QMouseEvent *e) |
| 84 | +{ |
| 85 | + //QPlainTextEdit::mouseDoubleClickEvent(e); |
| 86 | + Q_UNUSED(e) |
| 87 | +} |
| 88 | + |
| 89 | +void Console::contextMenuEvent(QContextMenuEvent *e) |
| 90 | +{ |
| 91 | + QPlainTextEdit::contextMenuEvent(e); |
| 92 | + //Q_UNUSED(e) |
| 93 | +} |
| 94 | + |
| 95 | +void Console::highlightCurrentLine() |
| 96 | +{ |
| 97 | + QList<QTextEdit::ExtraSelection> extraSelections; |
| 98 | + |
| 99 | +/* |
| 100 | + if (!isReadOnly()) |
| 101 | +*/ |
| 102 | + { |
| 103 | + QTextEdit::ExtraSelection selection; |
| 104 | + |
| 105 | + QColor lineColor = QColor(Qt::yellow).lighter(160); |
| 106 | + |
| 107 | + selection.format.setBackground(lineColor); |
| 108 | + selection.format.setProperty(QTextFormat::FullWidthSelection, true); |
| 109 | + selection.cursor = textCursor(); |
| 110 | + selection.cursor.clearSelection(); |
| 111 | + extraSelections.append(selection); |
| 112 | + } |
| 113 | + |
| 114 | + setExtraSelections(extraSelections); |
| 115 | +} |
| 116 | + |
| 117 | +int Console::lineNumberAreaWidth() |
| 118 | +{ |
| 119 | + int digits = 1; |
| 120 | + int max = qMax(1, blockCount()); |
| 121 | + while (max >= 10) { |
| 122 | + max /= 10; |
| 123 | + ++digits; |
| 124 | + } |
| 125 | + |
| 126 | + int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; |
| 127 | + //int space = 80 + fontMetrics().width(QLatin1Char('9')) * digits; |
| 128 | + |
| 129 | + return space; |
| 130 | +} |
| 131 | + |
| 132 | +void Console::updateLineNumberAreaWidth(int /* newBlockCount */) |
| 133 | +{ |
| 134 | + setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); |
| 135 | +} |
| 136 | + |
| 137 | +void Console::updateLineNumberArea(const QRect &rect, int dy) |
| 138 | +{ |
| 139 | + if (dy) |
| 140 | + lineNumberArea->scroll(0, dy); |
| 141 | + else |
| 142 | + lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); |
| 143 | + |
| 144 | + if (rect.contains(viewport()->rect())) |
| 145 | + updateLineNumberAreaWidth(0); |
| 146 | +} |
| 147 | + |
| 148 | +void Console::resizeEvent(QResizeEvent *e) |
| 149 | +{ |
| 150 | + QPlainTextEdit::resizeEvent(e); |
| 151 | + |
| 152 | + QRect cr = contentsRect(); |
| 153 | + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); |
| 154 | +} |
| 155 | + |
| 156 | +void Console::lineNumberAreaPaintEvent(QPaintEvent *event) |
| 157 | +{ |
| 158 | + QPainter painter(lineNumberArea); |
| 159 | + painter.fillRect(event->rect(), Qt::lightGray); |
| 160 | + |
| 161 | + QTextBlock block = firstVisibleBlock(); |
| 162 | + int blockNumber = block.blockNumber(); |
| 163 | + int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); |
| 164 | + int bottom = top + (int) blockBoundingRect(block).height(); |
| 165 | + |
| 166 | + while (block.isValid() && top <= event->rect().bottom()) { |
| 167 | + if (block.isVisible() && bottom >= event->rect().top()) { |
| 168 | + QString number = QString::number(blockNumber + 1); |
| 169 | + //QString number = QString::asprintf("%d %dms", (blockNumber + 1), t.elapsed()); |
| 170 | + painter.setPen(Qt::black); |
| 171 | + painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), |
| 172 | + Qt::AlignRight, number); |
| 173 | + } |
| 174 | + |
| 175 | + block = block.next(); |
| 176 | + top = bottom; |
| 177 | + bottom = top + (int) blockBoundingRect(block).height(); |
| 178 | + ++blockNumber; |
| 179 | + } |
| 180 | +} |
| 181 | + |
0 commit comments