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