source: trunk/examples/widgets/codeeditor/codeeditor.cpp@ 1165

Last change on this file since 1165 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

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