KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
codeeditor.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41#include <QPlainTextEdit>
42#include <QPainter>
43#include <QTextBlock>
44
45#include "codeeditor_p.h"
46
47
48CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
49{
50 lineNumberArea = new LineNumberArea(this);
51
52 connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth(int))); // clazy:exclude=old-style-connect
53 connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int))); // clazy:exclude=old-style-connect
54 connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(highlightCurrentLine())); // clazy:exclude=old-style-connect
55
56 QFont font(QStringLiteral("Monospace"));
57 font.setStyleHint(QFont::TypeWriter);
58 setFont(font);
59
60 updateLineNumberAreaWidth(0);
61 highlightCurrentLine();
62}
63
64
65
67{
68 int digits = 1;
69 int max = qMax(1, blockCount());
70 while (max >= 10) {
71 max /= 10;
72 ++digits;
73 }
74
75 const int space = 3 + (fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits);
76
77 return space;
78}
79
80
81
82void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */)
83{
84 setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
85}
86
87
88
89void CodeEditor::updateLineNumberArea(QRect rect, int dy)
90{
91 if (dy)
92 lineNumberArea->scroll(0, dy);
93 else
94 lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
95
96 if (rect.contains(viewport()->rect()))
97 updateLineNumberAreaWidth(0);
98}
99
100
101
102void CodeEditor::resizeEvent(QResizeEvent *event)
103{
104 QPlainTextEdit::resizeEvent(event);
105
106 const QRect cr = contentsRect();
107 lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
108}
109
110void CodeEditor::focusOutEvent(QFocusEvent *event)
111{
112 Q_UNUSED(event);
113 Q_EMIT editingFinished(toPlainText());
114}
115
116void CodeEditor::highlightCurrentLine()
117{
118 QList<QTextEdit::ExtraSelection> extraSelections;
119
120 if (!isReadOnly()) {
121 QTextEdit::ExtraSelection selection;
122
123 const QColor lineColor = QColor(Qt::yellow).lighter(160);
124
125 selection.format.setBackground(lineColor);
126 selection.format.setProperty(QTextFormat::FullWidthSelection, true);
127 selection.cursor = textCursor();
128 selection.cursor.clearSelection();
129 extraSelections.append(selection);
130 }
131
132 setExtraSelections(extraSelections);
133}
134
135
136
138{
139 QPainter painter(lineNumberArea);
140 painter.fillRect(event->rect(), Qt::lightGray);
141
142
143 QTextBlock block = firstVisibleBlock();
144 int blockNumber = block.blockNumber();
145 int top = (int) blockBoundingGeometry(block).translated(contentOffset()).top();
146 int bottom = top + (int) blockBoundingRect(block).height();
147
148 while (block.isValid() && top <= event->rect().bottom()) {
149 if (block.isVisible() && bottom >= event->rect().top()) {
150 const QString number = QString::number(blockNumber + 1);
151 painter.setPen(Qt::black);
152 painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(),
153 Qt::AlignRight, number);
154 }
155
156 block = block.next();
157 top = bottom;
158 bottom = top + (int) blockBoundingRect(block).height();
159 ++blockNumber;
160 }
161}
void focusOutEvent(QFocusEvent *event) override
CodeEditor(QWidget *parent=nullptr)
void lineNumberAreaPaintEvent(QPaintEvent *event)
void resizeEvent(QResizeEvent *event) override
void editingFinished(QString)
int lineNumberAreaWidth()

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDStateMachineEditor
Create Qt State Machine metacode using a graphical user interface
https://github.com/KDAB/KDStateMachineEditor
Generated on Tue Jul 15 2025 15:21:47 for KDStateMachineEditor API Documentation by doxygen 1.9.8