source: s10k/CommonLibs/LineNumberDisplay/LineNumberDisplay.cpp@ 1109

Last change on this file since 1109 was 1096, checked in by s10k, 7 years ago

Added zlib, quazip, basicxmlsyntaxhighlighter, conditionalsemaphore and linenumberdisplay libraries. zlib and quazip are pre-compiled, but you can compile them yourself, just delete the dll files (or equivalent binary files to your OS)

File size: 4.9 KB
RevLine 
[1096]1/***************************************************************************
2 File : LineNumberDisplay.cpp
3 Project : QtiPlot
4 --------------------------------------------------------------------
5 Copyright : (C) 2008 by Ion Vasilief
6 Email (use @ for *) : ion_vasilief*yahoo.fr
7 Description : A widget displaying line numbers for a QTextEdit
8
9 ***************************************************************************/
10
11/***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
22 * *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the Free Software *
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26 * Boston, MA 02110-1301 USA *
27 * *
28 ***************************************************************************/
29#include "LineNumberDisplay.h"
30#include <QScrollBar>
31#include <QShowEvent>
32#include <QPainter>
33
34LineNumberDisplay::LineNumberDisplay(QTextEdit *te, QWidget *parent)
35 : QTextEdit(parent), d_text_edit(te)
36{
37 setReadOnly(true);
38 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
39 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
40 setFrameStyle(QFrame::Panel | QFrame::Raised);
41 setMaximumWidth(0);
42 setLineWidth(0);
43 setFocusPolicy(Qt::NoFocus);
44 setCurrentFont(te->currentFont());
45 viewport()->setCursor(Qt::ArrowCursor);
46
47 QPalette palette = this->palette();
48 palette.setColor(QPalette::Highlight, palette.color(QPalette::Base));
49 setPalette(palette);
50
51 if (te){
52 connect(this, SIGNAL(selectionChanged()), this, SLOT(updateDocumentSelection()));
53
54 connect(te->document(), SIGNAL(contentsChanged()), this, SLOT(updateLineNumbers()));
55 connect((QObject *)te->verticalScrollBar(), SIGNAL(valueChanged(int)),
56 (QObject *)verticalScrollBar(), SLOT(setValue(int)));
57 connect(te, SIGNAL(currentCharFormatChanged (const QTextCharFormat &)),
58 this, SLOT(changeCharFormat (const QTextCharFormat &)));
59 }
60
61 // Disable manual user scrolls
62 this->verticalScrollBar()->setEnabled(false);
63 this->horizontalScrollBar()->setEnabled(false);
64}
65
66void LineNumberDisplay::updateDocumentSelection()
67{
68 if (!isVisible() || !d_text_edit)
69 return;
70
71 QTextCursor c = textCursor();
72#if QT_VERSION >= 0x040500
73 int selectionStart = document()->findBlock(c.selectionStart()).firstLineNumber();
74 int selectionEnd = document()->findBlock(c.selectionEnd()).firstLineNumber();
75#else
76 int selectionStart = document()->findBlock(c.selectionStart()).blockNumber();
77 int selectionEnd = document()->findBlock(c.selectionEnd()).blockNumber();
78#endif
79 int selectedLines = abs(selectionEnd - selectionStart);
80
81 QTextCursor cursor(d_text_edit->textCursor());
82 cursor.movePosition(QTextCursor::Start);
83 for (int i = 0; i < selectionStart; i++)
84 cursor.movePosition(QTextCursor::Down);
85
86 for (int i = 0; i < selectedLines; i++)
87 cursor.movePosition(QTextCursor::Down, QTextCursor::KeepAnchor);
88
89 cursor.movePosition(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
90
91 if (selectionEnd == d_text_edit->document()->blockCount() - 1)
92 cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor);
93
94 d_text_edit->setTextCursor(cursor);
95}
96
97void LineNumberDisplay::updateLineNumbers(bool force)
98{
99 if (!isVisible() || !d_text_edit)
100 return;
101
102 int lines = d_text_edit->document()->blockCount();
103 if (document()->blockCount() - 1 == lines && !force)
104 return;
105
106 QString aux;
107 for(int i = 0; i < lines; i++)
108 aux += QString::number(i + 1) + "\n";
109
110 setPlainText(aux);
111
112 QFontMetrics fm(d_text_edit->currentFont(), this);
113 setMaximumWidth(2.5*fm.boundingRect(QString::number(lines)).width()); // 2.5 small fix to display numbers correctly on macos
114 verticalScrollBar()->setValue(d_text_edit->verticalScrollBar()->value());
115}
116
117void LineNumberDisplay::showEvent(QShowEvent *e)
118{
119 e->accept();
120 if (isVisible())
121 updateLineNumbers();
122}
123
124void LineNumberDisplay::changeCharFormat (const QTextCharFormat &f)
125{
126 setCurrentFont(f.font());
127}
Note: See TracBrowser for help on using the repository browser.