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 | |
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')
-rw-r--r-- | utils/themeeditor/codeeditor.cpp | 143 | ||||
-rw-r--r-- | utils/themeeditor/codeeditor.h | 99 | ||||
-rw-r--r-- | utils/themeeditor/skindocument.cpp | 3 | ||||
-rw-r--r-- | utils/themeeditor/skindocument.h | 4 | ||||
-rw-r--r-- | utils/themeeditor/themeeditor.pro | 6 |
5 files changed, 250 insertions, 5 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 | |||
diff --git a/utils/themeeditor/codeeditor.h b/utils/themeeditor/codeeditor.h new file mode 100644 index 0000000000..edbe218ec4 --- /dev/null +++ b/utils/themeeditor/codeeditor.h | |||
@@ -0,0 +1,99 @@ | |||
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 | #ifndef CODEEDITOR_H | ||
37 | #define CODEEDITOR_H | ||
38 | |||
39 | #include <QPlainTextEdit> | ||
40 | #include <QObject> | ||
41 | |||
42 | QT_BEGIN_NAMESPACE | ||
43 | class QPaintEvent; | ||
44 | class QResizeEvent; | ||
45 | class QSize; | ||
46 | class QWidget; | ||
47 | QT_END_NAMESPACE | ||
48 | |||
49 | class LineNumberArea; | ||
50 | |||
51 | //![codeeditordefinition] | ||
52 | |||
53 | class CodeEditor : public QPlainTextEdit | ||
54 | { | ||
55 | Q_OBJECT | ||
56 | |||
57 | public: | ||
58 | CodeEditor(QWidget *parent = 0); | ||
59 | |||
60 | void lineNumberAreaPaintEvent(QPaintEvent *event); | ||
61 | int lineNumberAreaWidth(); | ||
62 | |||
63 | protected: | ||
64 | void resizeEvent(QResizeEvent *event); | ||
65 | |||
66 | private slots: | ||
67 | void updateLineNumberAreaWidth(int newBlockCount); | ||
68 | void updateLineNumberArea(const QRect &, int); | ||
69 | |||
70 | private: | ||
71 | QWidget *lineNumberArea; | ||
72 | }; | ||
73 | |||
74 | //![codeeditordefinition] | ||
75 | //![extraarea] | ||
76 | |||
77 | class LineNumberArea : public QWidget | ||
78 | { | ||
79 | public: | ||
80 | LineNumberArea(CodeEditor *editor) : QWidget(editor) { | ||
81 | codeEditor = editor; | ||
82 | } | ||
83 | |||
84 | QSize sizeHint() const { | ||
85 | return QSize(codeEditor->lineNumberAreaWidth(), 0); | ||
86 | } | ||
87 | |||
88 | protected: | ||
89 | void paintEvent(QPaintEvent *event) { | ||
90 | codeEditor->lineNumberAreaPaintEvent(event); | ||
91 | } | ||
92 | |||
93 | private: | ||
94 | CodeEditor *codeEditor; | ||
95 | }; | ||
96 | |||
97 | //![extraarea] | ||
98 | |||
99 | #endif | ||
diff --git a/utils/themeeditor/skindocument.cpp b/utils/themeeditor/skindocument.cpp index 3da248a30b..f7c902f30f 100644 --- a/utils/themeeditor/skindocument.cpp +++ b/utils/themeeditor/skindocument.cpp | |||
@@ -52,6 +52,7 @@ SkinDocument::SkinDocument(QLabel* statusLabel, QString file, QWidget *parent): | |||
52 | fin.open(QFile::ReadOnly); | 52 | fin.open(QFile::ReadOnly); |
53 | editor->document()->setPlainText(QString(fin.readAll())); | 53 | editor->document()->setPlainText(QString(fin.readAll())); |
54 | saved = editor->document()->toPlainText(); | 54 | saved = editor->document()->toPlainText(); |
55 | editor->setTextCursor(QTextCursor(editor->document()->begin())); | ||
55 | fin.close(); | 56 | fin.close(); |
56 | } | 57 | } |
57 | 58 | ||
@@ -113,7 +114,7 @@ void SkinDocument::setupUI() | |||
113 | { | 114 | { |
114 | /* Setting up the text edit */ | 115 | /* Setting up the text edit */ |
115 | layout = new QHBoxLayout; | 116 | layout = new QHBoxLayout; |
116 | editor = new QPlainTextEdit(this); | 117 | editor = new CodeEditor(this); |
117 | editor->setLineWrapMode(QPlainTextEdit::NoWrap); | 118 | editor->setLineWrapMode(QPlainTextEdit::NoWrap); |
118 | layout->addWidget(editor); | 119 | layout->addWidget(editor); |
119 | 120 | ||
diff --git a/utils/themeeditor/skindocument.h b/utils/themeeditor/skindocument.h index b5eb60b2f4..c8f92955f5 100644 --- a/utils/themeeditor/skindocument.h +++ b/utils/themeeditor/skindocument.h | |||
@@ -25,11 +25,11 @@ | |||
25 | #include <QWidget> | 25 | #include <QWidget> |
26 | #include <QLabel> | 26 | #include <QLabel> |
27 | #include <QHBoxLayout> | 27 | #include <QHBoxLayout> |
28 | #include <QPlainTextEdit> | ||
29 | 28 | ||
30 | #include "skinhighlighter.h" | 29 | #include "skinhighlighter.h" |
31 | #include "parsetreemodel.h" | 30 | #include "parsetreemodel.h" |
32 | #include "preferencesdialog.h" | 31 | #include "preferencesdialog.h" |
32 | #include "codeeditor.h" | ||
33 | 33 | ||
34 | class SkinDocument : public QWidget | 34 | class SkinDocument : public QWidget |
35 | { | 35 | { |
@@ -81,7 +81,7 @@ private: | |||
81 | QTextCharFormat errorColor; | 81 | QTextCharFormat errorColor; |
82 | 82 | ||
83 | QLayout* layout; | 83 | QLayout* layout; |
84 | QPlainTextEdit* editor; | 84 | CodeEditor* editor; |
85 | 85 | ||
86 | SkinHighlighter* highlighter; | 86 | SkinHighlighter* highlighter; |
87 | ParseTreeModel* model; | 87 | ParseTreeModel* model; |
diff --git a/utils/themeeditor/themeeditor.pro b/utils/themeeditor/themeeditor.pro index fe83d6bbf5..e0fcdc09d5 100644 --- a/utils/themeeditor/themeeditor.pro +++ b/utils/themeeditor/themeeditor.pro | |||
@@ -14,7 +14,8 @@ HEADERS += tag_table.h \ | |||
14 | editorwindow.h \ | 14 | editorwindow.h \ |
15 | skinhighlighter.h \ | 15 | skinhighlighter.h \ |
16 | skindocument.h \ | 16 | skindocument.h \ |
17 | preferencesdialog.h | 17 | preferencesdialog.h \ |
18 | codeeditor.h | ||
18 | SOURCES += tag_table.c \ | 19 | SOURCES += tag_table.c \ |
19 | skin_parser.c \ | 20 | skin_parser.c \ |
20 | skin_scan.c \ | 21 | skin_scan.c \ |
@@ -25,7 +26,8 @@ SOURCES += tag_table.c \ | |||
25 | editorwindow.cpp \ | 26 | editorwindow.cpp \ |
26 | skinhighlighter.cpp \ | 27 | skinhighlighter.cpp \ |
27 | skindocument.cpp \ | 28 | skindocument.cpp \ |
28 | preferencesdialog.cpp | 29 | preferencesdialog.cpp \ |
30 | codeeditor.cpp | ||
29 | OTHER_FILES += README \ | 31 | OTHER_FILES += README \ |
30 | resources/windowicon.png \ | 32 | resources/windowicon.png \ |
31 | resources/appicon.xcf \ | 33 | resources/appicon.xcf \ |