author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
#include <QtGui> |
|
42 |
||
43 |
#include "codeeditor.h" |
|
44 |
||
45 |
//![constructor] |
|
46 |
||
47 |
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent) |
|
48 |
{ |
|
49 |
lineNumberArea = new LineNumberArea(this); |
|
50 |
||
51 |
connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
52 |
connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); |
0 | 53 |
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); |
54 |
||
55 |
updateLineNumberAreaWidth(0); |
|
56 |
highlightCurrentLine(); |
|
57 |
} |
|
58 |
||
59 |
//![constructor] |
|
60 |
||
61 |
//![extraAreaWidth] |
|
62 |
||
63 |
int 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 |
||
81 |
void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) |
|
82 |
{ |
|
83 |
setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); |
|
84 |
} |
|
85 |
||
86 |
//![slotUpdateExtraAreaWidth] |
|
87 |
||
88 |
//![slotUpdateRequest] |
|
89 |
||
90 |
void 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 |
||
105 |
void 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 |
||
117 |
void 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 |
||
140 |
void 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 |