QSS Solver GUI  4.5.3
codeeditor.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2 
3  This file is part of QSS Solver.
4 
5  QSS Solver is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  QSS Solver is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with QSS Solver. If not, see <http://www.gnu.org/licenses/>.
17 
18  ******************************************************************************/
19 
20 /****************************************************************************
21  **
22  ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
23  ** All rights reserved.
24  ** Contact: Nokia Corporation (qt-info@nokia.com)
25  **
26  ** This file is part of the examples of the Qt Toolkit.
27  **
28  ** $QT_BEGIN_LICENSE:LGPL$
29  ** Commercial Usage
30  ** Licensees holding valid Qt Commercial licenses may use this file in
31  ** accordance with the Qt Commercial License Agreement provided with the
32  ** Software or, alternatively, in accordance with the terms contained in
33  ** a written agreement between you and Nokia.
34  **
35  ** GNU Lesser General Public License Usage
36  ** Alternatively, this file may be used under the terms of the GNU Lesser
37  ** General Public License version 2.1 as published by the Free Software
38  ** Foundation and appearing in the file LICENSE.LGPL included in the
39  ** packaging of this file. Please review the following information to
40  ** ensure the GNU Lesser General Public License version 2.1 requirements
41  ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
42  **
43  ** In addition, as a special exception, Nokia gives you certain additional
44  ** rights. These rights are described in the Nokia Qt LGPL Exception
45  ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
46  **
47  ** GNU General Public License Usage
48  ** Alternatively, this file may be used under the terms of the GNU
49  ** General Public License version 3.0 as published by the Free Software
50  ** Foundation and appearing in the file LICENSE.GPL included in the
51  ** packaging of this file. Please review the following information to
52  ** ensure the GNU General Public License version 3.0 requirements will be
53  ** met: http://www.gnu.org/copyleft/gpl.html.
54  **
55  ** If you have questions regarding the use of this file, please contact
56  ** Nokia at qt-info@nokia.com.
57  ** $QT_END_LICENSE$
58  **
59  ****************************************************************************/
60 
61 #include <QtGui>
62 
63 #include "codeeditor.hpp"
64 
65 CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit(parent)
66 {
67  lineNumberArea = new LineNumberArea(this);
68 
69  connect(this, &CodeEditor::blockCountChanged, this, &CodeEditor::updateLineNumberAreaWidth);
70  connect(this, &CodeEditor::updateRequest, this, &CodeEditor::updateLineNumberArea);
71  connect(this, &CodeEditor::cursorPositionChanged, this, &CodeEditor::highlightCurrentLine);
72 
75 }
76 
78 {
79  int digits = 1;
80  int max = qMax(1, blockCount());
81  while (max >= 10) {
82  max /= 10;
83  ++digits;
84  }
85 
86  int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
87 
88  return space;
89 }
90 
91 void CodeEditor::updateLineNumberAreaWidth(int /* newBlockCount */) { setViewportMargins(lineNumberAreaWidth(), 0, 0, 0); }
92 
93 void CodeEditor::updateLineNumberArea(const QRect &rect, int dy)
94 {
95  if (dy) {
96  lineNumberArea->scroll(0, dy);
97  } else {
98  lineNumberArea->update(0, rect.y(), lineNumberArea->width(), rect.height());
99  }
100 
101  if (rect.contains(viewport()->rect())) {
103  }
104 }
105 
106 void CodeEditor::resizeEvent(QResizeEvent *e)
107 {
108  QPlainTextEdit::resizeEvent(e);
109  QRect cr = contentsRect();
110  lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
111 }
112 
114 {
115  QList<QTextEdit::ExtraSelection> extraSelections;
116  if (!isReadOnly()) {
117  QTextEdit::ExtraSelection selection;
118 
119  QColor lineColor = QColor(Qt::lightGray).lighter(120);
120  selection.format.setBackground(lineColor);
121  selection.format.setProperty(QTextFormat::FullWidthSelection, true);
122  selection.cursor = textCursor();
123  selection.cursor.clearSelection();
124  extraSelections.append(selection);
125  }
126 
127  setExtraSelections(extraSelections);
128 }
129 
130 void CodeEditor::lineNumberAreaPaintEvent(QPaintEvent *event)
131 {
132  QPainter painter(lineNumberArea);
133  painter.fillRect(event->rect(), Qt::lightGray);
134 
135  QTextBlock block = firstVisibleBlock();
136  int blockNumber = block.blockNumber();
137  int top = (int)blockBoundingGeometry(block).translated(contentOffset()).top();
138  int bottom = top + (int)blockBoundingRect(block).height();
139 
140  while (block.isValid() && top <= event->rect().bottom()) {
141  if (block.isVisible() && bottom >= event->rect().top()) {
142  QString number = QString::number(blockNumber + 1);
143  painter.setPen(Qt::black);
144  painter.drawText(0, top, lineNumberArea->width(), fontMetrics().height(), Qt::AlignRight, number);
145  }
146 
147  block = block.next();
148  top = bottom;
149  bottom = top + (int)blockBoundingRect(block).height();
150  ++blockNumber;
151  }
152 }
CodeEditor::lineNumberAreaWidth
int lineNumberAreaWidth()
Definition: codeeditor.cpp:77
CodeEditor::resizeEvent
void resizeEvent(QResizeEvent *event)
Definition: codeeditor.cpp:106
LineNumberArea
Definition: codeeditor.hpp:119
codeeditor.hpp
CodeEditor::lineNumberAreaPaintEvent
void lineNumberAreaPaintEvent(QPaintEvent *event)
Definition: codeeditor.cpp:130
CodeEditor::updateLineNumberArea
void updateLineNumberArea(const QRect &, int)
Definition: codeeditor.cpp:93
CodeEditor::updateLineNumberAreaWidth
void updateLineNumberAreaWidth(int newBlockCount)
Definition: codeeditor.cpp:91
CodeEditor::CodeEditor
CodeEditor(QWidget *parent=0)
Definition: codeeditor.cpp:65
CodeEditor::lineNumberArea
QWidget * lineNumberArea
Definition: codeeditor.hpp:113
CodeEditor::highlightCurrentLine
void highlightCurrentLine()
Definition: codeeditor.cpp:113