summaryrefslogtreecommitdiff
path: root/utils/themeeditor/gui/codeeditor.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/themeeditor/gui/codeeditor.cpp')
-rw-r--r--utils/themeeditor/gui/codeeditor.cpp149
1 files changed, 149 insertions, 0 deletions
diff --git a/utils/themeeditor/gui/codeeditor.cpp b/utils/themeeditor/gui/codeeditor.cpp
new file mode 100644
index 0000000000..49f441057c
--- /dev/null
+++ b/utils/themeeditor/gui/codeeditor.cpp
@@ -0,0 +1,149 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * This file has been copied from Nokia's Qt Examples, with minor modifications
11 * made available under the LGPL version 2.1, as the original file was licensed
12 *
13 ****************************************************************************
14 ****************************************************************************
15**
16** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
17** All rights reserved.
18** Contact: Nokia Corporation (qt-info@nokia.com)
19**
20** This file is part of the examples of the Qt Toolkit.
21**
22** GNU Lesser General Public License Usage
23** Alternatively, this file may be used under the terms of the GNU Lesser
24** General Public License version 2.1 as published by the Free Software
25** Foundation and appearing in the file LICENSE.LGPL included in the
26** packaging of this file. Please review the following information to
27** ensure the GNU Lesser General Public License version 2.1 requirements
28** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
29**
30** In addition, as a special exception, Nokia gives you certain additional
31** rights. These rights are described in the Nokia Qt LGPL Exception
32** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
33**
34****************************************************************************/
35
36#include <QtGui>
37
38#include "codeeditor.h"
39
40//![constructor]
41
42CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
43{
44 lineNumberArea = new LineNumberArea(this);
45
46 connect(this, SIGNAL(blockCountChanged(int)),
47 this, SLOT(updateLineNumberAreaWidth(int)));
48 connect(this, SIGNAL(updateRequest(QRect,int)),
49 this, SLOT(updateLineNumberArea(QRect,int)));
50
51 updateLineNumberAreaWidth(0);
52}
53
54//![constructor]
55
56//![extraAreaWidth]
57
58int CodeEditor::lineNumberAreaWidth()
59{
60 int digits = 1;
61 int max = qMax(1, blockCount());
62 while (max >= 10) {
63 max /= 10;
64 ++digits;
65 }
66
67 int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
68
69 return space;
70}
71
72//![extraAreaWidth]
73
74//![slotUpdateExtraAreaWidth]
75
76void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
77{
78 setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
79}
80
81//![slotUpdateExtraAreaWidth]
82
83//![slotUpdateRequest]
84
85void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
86{
87 if (dy)
88 lineNumberArea->scroll(0, dy);
89 else
90 lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
91
92 if (rect.contains(viewport()->rect()))
93 updateLineNumberAreaWidth(0);
94}
95
96//![slotUpdateRequest]
97
98//![resizeEvent]
99
100void CodeEditor::resizeEvent(QResizeEvent *e)
101{
102 QPlainTextEdit::resizeEvent(e);
103
104 QRect cr = contentsRect();
105 lineNumberArea->setGeometry(QRect(cr.left(), cr.top(),
106 lineNumberAreaWidth(), cr.height()));
107}
108
109//![resizeEvent]
110
111//![extraAreaPaintEvent_0]
112
113void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
114{
115 QPainter painter(lineNumberArea);
116 painter.fillRect(event->rect(), Qt::lightGray);
117
118//![extraAreaPaintEvent_0]
119
120//![extraAreaPaintEvent_1]
121 QTextBlock block = firstVisibleBlock();
122 int blockNumber = block.blockNumber();
123 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
124 int bottom = top + (int) blockBoundingRect(block).height();
125//![extraAreaPaintEvent_1]
126
127//![extraAreaPaintEvent_2]
128 while (block.isValid() && top <= event->rect().bottom()) {
129 if (block.isVisible() && bottom >= event->rect().top()) {
130 QString number = QString::number(blockNumber + 1);
131 /* Drawing an error circle if necessary */
132 if(errors.contains(blockNumber + 1))
133 {
134 painter.fillRect(QRect(0, top, lineNumberArea->width(),
135 fontMetrics().height()), errorColor);
136 }
137 painter.setPen(Qt::black);
138 painter.drawText(0, top, lineNumberArea->width(),
139 fontMetrics().height(), Qt::AlignRight, number);
140 }
141
142 block = block.next();
143 top = bottom;
144 bottom = top + (int) blockBoundingRect(block).height();
145 ++blockNumber;
146 }
147}
148//![extraAreaPaintEvent_2]
149