Showing the changes to interface/src/ui/ScriptEditBox.cpp (1 of 8 files). Show all
Unified · Split
| @@ -0,0 +1,107 @@ | |||
| 1 | // | ||
| 2 | // ScriptEditBox.cpp | ||
| 3 | // interface/src/ui | ||
| 4 | // | ||
| 5 | // Created by Thijs Wenker on 4/30/14. | ||
| 6 | // Copyright 2014 High Fidelity, Inc. | ||
| 7 | // | ||
| 8 | // Distributed under the Apache License, Version 2.0. | ||
| 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | ||
| 10 | // | ||
| 11 | |||
| 12 | #include "ScriptEditBox.h" | ||
| 13 | #include "ScriptLineNumberArea.h" | ||
| 14 | #include "Application.h" | ||
| 15 | |||
| 16 | ScriptEditBox::ScriptEditBox(QWidget* parent) : | ||
| 17 | QPlainTextEdit(parent) | ||
| 18 | { | ||
| 19 | _scriptLineNumberArea = new ScriptLineNumberArea(this); | ||
| 20 | |||
| 21 | connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); | ||
| 22 | connect(this, SIGNAL(updateRequest(QRect, int)), this, SLOT(updateLineNumberArea(QRect, int))); | ||
| 23 | connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); | ||
| 24 | |||
| 25 | updateLineNumberAreaWidth(0); | ||
| 26 | highlightCurrentLine(); | ||
| 27 | } | ||
| 28 | |||
| 29 | int ScriptEditBox::lineNumberAreaWidth() { | ||
| 30 | int digits = 1; | ||
| 31 | const int SPACER_PIXELS = 3; | ||
| 32 | const int BASE_TEN = 10; | ||
| 33 | int max = qMax(1, blockCount()); | ||
| 34 | while (max >= BASE_TEN) { | ||
| 35 | max /= BASE_TEN; | ||
| 36 | ++digits; | ||
| 37 | } | ||
| 38 | return SPACER_PIXELS + fontMetrics().width(QLatin1Char('H')) * digits; | ||
| 39 | } | ||
| 40 | |||
| 41 | void ScriptEditBox::updateLineNumberAreaWidth(int) { | ||
| 42 | setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); | ||
| 43 | } | ||
| 44 | |||
| 45 | void ScriptEditBox::updateLineNumberArea(const QRect& rect, int dy) { | ||
| 46 | if (dy) { | ||
| 47 | _scriptLineNumberArea->scroll(0, dy); | ||
| 48 | } else { | ||
| 49 | _scriptLineNumberArea->update(0, rect.y(), _scriptLineNumberArea->width(), rect.height()); | ||
| 50 | } | ||
| 51 | |||
| 52 | if (rect.contains(viewport()->rect())) { | ||
| 53 | updateLineNumberAreaWidth(0); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | void ScriptEditBox::resizeEvent(QResizeEvent* e) { | ||
| 58 | QPlainTextEdit::resizeEvent(e); | ||
| 59 | |||
| 60 | QRect cr = contentsRect(); | ||
| 61 | _scriptLineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); | ||
| 62 | } | ||
| 63 | |||
| 64 | void ScriptEditBox::highlightCurrentLine() { | ||
| 65 | QList<QTextEdit::ExtraSelection> extraSelections; | ||
| 66 | |||
| 67 | if (!isReadOnly()) { | ||
| 68 | QTextEdit::ExtraSelection selection; | ||
| 69 | |||
| 70 | QColor lineColor = QColor(Qt::gray).lighter(); | ||
| 71 | |||
| 72 | selection.format.setBackground(lineColor); | ||
| 73 | selection.format.setProperty(QTextFormat::FullWidthSelection, true); | ||
| 74 | selection.cursor = textCursor(); | ||
| 75 | selection.cursor.clearSelection(); | ||
| 76 | extraSelections.append(selection); | ||
| 77 | } | ||
| 78 | |||
| 79 | setExtraSelections(extraSelections); | ||
| 80 | } | ||
| 81 | |||
| 82 | void ScriptEditBox::lineNumberAreaPaintEvent(QPaintEvent* event) | ||
| 83 | { | ||
| 84 | QPainter painter(_scriptLineNumberArea); | ||
| 85 | painter.fillRect(event->rect(), Qt::lightGray); | ||
| 86 | QTextBlock block = firstVisibleBlock(); | ||
| 87 | int blockNumber = block.blockNumber(); | ||
| 88 | int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top(); | ||
| 89 | int bottom = top + (int) blockBoundingRect(block).height(); | ||
| 90 | |||
| 91 | while (block.isValid() && top <= event->rect().bottom()) { | ||
| 92 | if (block.isVisible() && bottom >= event->rect().top()) { | ||
| 93 | QFont font = painter.font(); | ||
| 94 | font.setBold(this->textCursor().blockNumber() == block.blockNumber()); | ||
| 95 | painter.setFont(font); | ||
| 96 | QString number = QString::number(blockNumber + 1); | ||
| 97 | painter.setPen(Qt::black); | ||
| 98 | painter.drawText(0, top, _scriptLineNumberArea->width(), fontMetrics().height(), | ||
| 99 | Qt::AlignRight, number); | ||
| 100 | } | ||
| 101 | |||
| 102 | block = block.next(); | ||
| 103 | top = bottom; | ||
| 104 | bottom = top + (int) blockBoundingRect(block).height(); | ||
| 105 | ++blockNumber; | ||
| 106 | } | ||
| 107 | } | ||