|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). |
| 4 | +** Contact: http://www.qt-project.org/legal |
| 5 | +** |
| 6 | +** This file is part of the examples of the Qt Toolkit. |
| 7 | +** |
| 8 | +** $QT_BEGIN_LICENSE:BSD$ |
| 9 | +** You may use this file under the terms of the BSD license as follows: |
| 10 | +** |
| 11 | +** "Redistribution and use in source and binary forms, with or without |
| 12 | +** modification, are permitted provided that the following conditions are |
| 13 | +** met: |
| 14 | +** * Redistributions of source code must retain the above copyright |
| 15 | +** notice, this list of conditions and the following disclaimer. |
| 16 | +** * Redistributions in binary form must reproduce the above copyright |
| 17 | +** notice, this list of conditions and the following disclaimer in |
| 18 | +** the documentation and/or other materials provided with the |
| 19 | +** distribution. |
| 20 | +** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names |
| 21 | +** of its contributors may be used to endorse or promote products derived |
| 22 | +** from this software without specific prior written permission. |
| 23 | +** |
| 24 | +** |
| 25 | +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 26 | +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 27 | +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 28 | +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 29 | +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 30 | +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 31 | +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 32 | +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 33 | +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 34 | +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 35 | +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 36 | +** |
| 37 | +** $QT_END_LICENSE$ |
| 38 | +** |
| 39 | +****************************************************************************/ |
| 40 | + |
| 41 | +#include <QtWidgets> |
| 42 | + |
| 43 | +#include "codeeditor.h" |
| 44 | + |
| 45 | +//![constructor] |
| 46 | + |
| 47 | +CodeEditor::CodeEditor(QWidget* parent) |
| 48 | + : QPlainTextEdit(parent) |
| 49 | +{ |
| 50 | + lineNumberArea = new LineNumberArea(this); |
| 51 | + |
| 52 | + connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); |
| 53 | + connect(this, SIGNAL(updateRequest(QRect, int)), this, SLOT(updateLineNumberArea(QRect, int))); |
| 54 | + connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); |
| 55 | + |
| 56 | + updateLineNumberAreaWidth(0); |
| 57 | + highlightCurrentLine(); |
| 58 | +} |
| 59 | + |
| 60 | +//![constructor] |
| 61 | + |
| 62 | +//![extraAreaWidth] |
| 63 | + |
| 64 | +int CodeEditor::lineNumberAreaWidth() |
| 65 | +{ |
| 66 | + int digits = 1; |
| 67 | + int max = qMax(1, blockCount()); |
| 68 | + while (max >= 10) { |
| 69 | + max /= 10; |
| 70 | + ++digits; |
| 71 | + } |
| 72 | + |
| 73 | + int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits; |
| 74 | + |
| 75 | + return space; |
| 76 | +} |
| 77 | + |
| 78 | +//![extraAreaWidth] |
| 79 | + |
| 80 | +//![slotUpdateExtraAreaWidth] |
| 81 | + |
| 82 | +void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) |
| 83 | +{ |
| 84 | + setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); |
| 85 | +} |
| 86 | + |
| 87 | +//![slotUpdateExtraAreaWidth] |
| 88 | + |
| 89 | +//![slotUpdateRequest] |
| 90 | + |
| 91 | +void CodeEditor::updateLineNumberArea(const QRect& rect, int dy) |
| 92 | +{ |
| 93 | + if (dy) |
| 94 | + lineNumberArea->scroll(0, dy); |
| 95 | + else |
| 96 | + lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height()); |
| 97 | + |
| 98 | + if (rect.contains(viewport()->rect())) |
| 99 | + updateLineNumberAreaWidth(0); |
| 100 | +} |
| 101 | + |
| 102 | +//![slotUpdateRequest] |
| 103 | + |
| 104 | +//![resizeEvent] |
| 105 | + |
| 106 | +void CodeEditor::resizeEvent(QResizeEvent* e) |
| 107 | +{ |
| 108 | + QPlainTextEdit::resizeEvent(e); |
| 109 | + |
| 110 | + QRect cr = contentsRect(); |
| 111 | + lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height())); |
| 112 | +} |
| 113 | + |
| 114 | +//![resizeEvent] |
| 115 | + |
| 116 | +//![cursorPositionChanged] |
| 117 | + |
| 118 | +void CodeEditor::highlightCurrentLine() |
| 119 | +{ |
| 120 | + QList<QTextEdit::ExtraSelection> extraSelections; |
| 121 | + |
| 122 | + if (!isReadOnly()) { |
| 123 | + QTextEdit::ExtraSelection selection; |
| 124 | + |
| 125 | + QColor lineColor = QColor(Qt::yellow).lighter(160); |
| 126 | + |
| 127 | + selection.format.setBackground(lineColor); |
| 128 | + selection.format.setProperty(QTextFormat::FullWidthSelection, true); |
| 129 | + selection.cursor = textCursor(); |
| 130 | + selection.cursor.clearSelection(); |
| 131 | + extraSelections.append(selection); |
| 132 | + } |
| 133 | + |
| 134 | + setExtraSelections(extraSelections); |
| 135 | +} |
| 136 | + |
| 137 | +//![cursorPositionChanged] |
| 138 | + |
| 139 | +//![extraAreaPaintEvent_0] |
| 140 | + |
| 141 | +void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent* event) |
| 142 | +{ |
| 143 | + QPainter painter(lineNumberArea); |
| 144 | + painter.fillRect(event->rect(), Qt::lightGray); |
| 145 | + |
| 146 | + //![extraAreaPaintEvent_0] |
| 147 | + |
| 148 | + //![extraAreaPaintEvent_1] |
| 149 | + QTextBlock block = firstVisibleBlock(); |
| 150 | + int blockNumber = block.blockNumber(); |
| 151 | + int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top(); |
| 152 | + int bottom = top + (int)blockBoundingRect(block).height(); |
| 153 | + //![extraAreaPaintEvent_1] |
| 154 | + |
| 155 | + //![extraAreaPaintEvent_2] |
| 156 | + while (block.isValid() && top <= event->rect().bottom()) { |
| 157 | + if (block.isVisible() && bottom >= event->rect().top()) { |
| 158 | + QString number = QString::number(blockNumber + 1); |
| 159 | + painter.setPen(Qt::black); |
| 160 | + painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), |
| 161 | + Qt::AlignRight, number); |
| 162 | + } |
| 163 | + |
| 164 | + block = block.next(); |
| 165 | + top = bottom; |
| 166 | + bottom = top + (int)blockBoundingRect(block).height(); |
| 167 | + ++blockNumber; |
| 168 | + } |
| 169 | +} |
| 170 | +//![extraAreaPaintEvent_2] |
0 commit comments