diff options
author | Robert Bieber <robby@bieberphoto.com> | 2010-06-08 07:57:43 +0000 |
---|---|---|
committer | Robert Bieber <robby@bieberphoto.com> | 2010-06-08 07:57:43 +0000 |
commit | 0cc507214ab3d5c35512add1e6150c4042e0aa1c (patch) | |
tree | 6a78263259233b95cf236f3d700372d489c46ae6 /utils/themeeditor/codeeditor.cpp | |
parent | fa99c614b90c17c79f75b54a68e311324eda4184 (diff) | |
download | rockbox-0cc507214ab3d5c35512add1e6150c4042e0aa1c.tar.gz rockbox-0cc507214ab3d5c35512add1e6150c4042e0aa1c.zip |
Theme Editor: Added line numbering in the text editor, thanks to some code from Nokia. Also made newly opened documents scroll to the top of the document after loading
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@26683 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'utils/themeeditor/codeeditor.cpp')
-rw-r--r-- | utils/themeeditor/codeeditor.cpp | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/utils/themeeditor/codeeditor.cpp b/utils/themeeditor/codeeditor.cpp new file mode 100644 index 0000000000..672b210d2e --- /dev/null +++ b/utils/themeeditor/codeeditor.cpp | |||
@@ -0,0 +1,143 @@ | |||
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 | |||
42 | CodeEditor::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 | |||
58 | int 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 | |||
76 | void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) | ||
77 | { | ||
78 | setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); | ||
79 | } | ||
80 | |||
81 | //![slotUpdateExtraAreaWidth] | ||
82 | |||
83 | //![slotUpdateRequest] | ||
84 | |||
85 | void 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 | |||
100 | void 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 | |||
113 | void 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 | painter.setPen(Qt::black); | ||
132 | painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), | ||
133 | Qt::AlignRight, number); | ||
134 | } | ||
135 | |||
136 | block = block.next(); | ||
137 | top = bottom; | ||
138 | bottom = top + (int) blockBoundingRect(block).height(); | ||
139 | ++blockNumber; | ||
140 | } | ||
141 | } | ||
142 | //![extraAreaPaintEvent_2] | ||
143 | |||