1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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
13 ****************************************************************************
14 ****************************************************************************
16 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
17 ** All rights reserved.
18 ** Contact: Nokia Corporation (qt-info@nokia.com)
20 ** This file is part of the examples of the Qt Toolkit.
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.
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.
34 ****************************************************************************/
38 #include "codeeditor.h"
42 CodeEditor::CodeEditor(QWidget
*parent
) : QPlainTextEdit(parent
)
44 lineNumberArea
= new LineNumberArea(this);
46 connect(this, SIGNAL(blockCountChanged(int)),
47 this, SLOT(updateLineNumberAreaWidth(int)));
48 connect(this, SIGNAL(updateRequest(QRect
,int)),
49 this, SLOT(updateLineNumberArea(QRect
,int)));
51 updateLineNumberAreaWidth(0);
58 int CodeEditor::lineNumberAreaWidth()
61 int max
= qMax(1, blockCount());
67 int space
= 3 + fontMetrics().width(QLatin1Char('9')) * digits
;
74 //![slotUpdateExtraAreaWidth]
76 void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
78 setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
81 //![slotUpdateExtraAreaWidth]
83 //![slotUpdateRequest]
85 void CodeEditor::updateLineNumberArea(const QRect
&rect
, int dy
)
88 lineNumberArea
->scroll(0, dy
);
90 lineNumberArea
->update(0, rect
.y(), lineNumberArea
->width(), rect
.height());
92 if (rect
.contains(viewport()->rect()))
93 updateLineNumberAreaWidth(0);
96 //![slotUpdateRequest]
100 void CodeEditor::resizeEvent(QResizeEvent
*e
)
102 QPlainTextEdit::resizeEvent(e
);
104 QRect cr
= contentsRect();
105 lineNumberArea
->setGeometry(QRect(cr
.left(), cr
.top(),
106 lineNumberAreaWidth(), cr
.height()));
111 //![extraAreaPaintEvent_0]
113 void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent
*event
)
115 QPainter
painter(lineNumberArea
);
116 painter
.fillRect(event
->rect(), Qt::lightGray
);
118 //![extraAreaPaintEvent_0]
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]
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))
134 painter
.fillRect(QRect(0, top
, lineNumberArea
->width(),
135 fontMetrics().height()), errorColor
);
137 painter
.setPen(Qt::black
);
138 painter
.drawText(0, top
, lineNumberArea
->width(),
139 fontMetrics().height(), Qt::AlignRight
, number
);
142 block
= block
.next();
144 bottom
= top
+ (int) blockBoundingRect(block
).height();
148 //![extraAreaPaintEvent_2]