summaryrefslogtreecommitdiff
path: root/utils/wpseditor/gui/src/numberedtextview.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utils/wpseditor/gui/src/numberedtextview.cpp')
-rw-r--r--utils/wpseditor/gui/src/numberedtextview.cpp181
1 files changed, 181 insertions, 0 deletions
diff --git a/utils/wpseditor/gui/src/numberedtextview.cpp b/utils/wpseditor/gui/src/numberedtextview.cpp
new file mode 100644
index 0000000000..81c4208b59
--- /dev/null
+++ b/utils/wpseditor/gui/src/numberedtextview.cpp
@@ -0,0 +1,181 @@
1/* This file is part of the KDE libraries
2 Copyright (C) 2005, 2006 KJSEmbed Authors
3 See included AUTHORS file.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library 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 GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19
20
21 --------------------------------------------------------------------------------------
22 Imported into the WPS editor and simplified by Dominik Wenger
23
24*/
25
26
27#include <QTextDocument>
28#include <QTextBlock>
29#include <QTextEdit>
30#include <QHBoxLayout>
31#include <QScrollBar>
32#include <QPainter>
33#include <QAbstractTextDocumentLayout>
34#include <QDebug>
35
36#include "numberedtextview.h"
37
38NumberBar::NumberBar( QWidget *parent )
39 : QWidget( parent ), edit(0), markedLine(-1)
40{
41 // Make room for 4 digits and the breakpoint icon
42 setFixedWidth( fontMetrics().width( QString("0000") + 10 + 32 ) );
43 markerIcon = QPixmap( "images/marker.png" );
44}
45
46NumberBar::~NumberBar()
47{
48}
49
50
51void NumberBar::markLine( int lineno )
52{
53 markedLine = lineno;
54}
55
56void NumberBar::setTextEdit( QTextEdit *edit )
57{
58 this->edit = edit;
59 connect( edit->document()->documentLayout(), SIGNAL( update(const QRectF &) ),
60 this, SLOT( update() ) );
61 connect( edit->verticalScrollBar(), SIGNAL(valueChanged(int) ),
62 this, SLOT( update() ) );
63}
64
65void NumberBar::paintEvent( QPaintEvent * )
66{
67 QAbstractTextDocumentLayout *layout = edit->document()->documentLayout();
68 int contentsY = edit->verticalScrollBar()->value();
69 qreal pageBottom = contentsY + edit->viewport()->height();
70 const QFontMetrics fm = fontMetrics();
71 const int ascent = fontMetrics().ascent() + 1; // height = ascent + descent + 1
72 int lineCount = 1;
73
74 QPainter p(this);
75
76 markedRect = QRect();
77
78 for ( QTextBlock block = edit->document()->begin();
79 block.isValid(); block = block.next(), ++lineCount )
80 {
81
82 const QRectF boundingRect = layout->blockBoundingRect( block );
83
84 QPointF position = boundingRect.topLeft();
85 if ( position.y() + boundingRect.height() < contentsY )
86 continue;
87 if ( position.y() > pageBottom )
88 break;
89
90 const QString txt = QString::number( lineCount );
91 p.drawText( width() - fm.width(txt), qRound( position.y() ) - contentsY + ascent, txt );
92
93 // marker
94 if ( markedLine == lineCount )
95 {
96 p.drawPixmap( 1, qRound( position.y() ) - contentsY, markerIcon );
97 markedRect = QRect( 1, qRound( position.y() ) - contentsY, markerIcon.width(), markerIcon.height() );
98 }
99 }
100}
101
102NumberedTextView::NumberedTextView( QWidget *parent )
103 : QFrame( parent )
104{
105 setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
106 setLineWidth( 2 );
107
108 // Setup the main view
109 view = new QTextEdit( this );
110 //view->setFontFamily( "Courier" );
111 view->setLineWrapMode( QTextEdit::NoWrap );
112 view->setFrameStyle( QFrame::NoFrame );
113
114 connect( view->document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(textChanged(int,int,int)) );
115
116 // Setup the line number pane
117 numbers = new NumberBar( this );
118 numbers->setTextEdit( view );
119
120 // Test
121 markLine(2);
122
123 //setup layout
124 box = new QHBoxLayout( this );
125 box->setSpacing( 0 );
126 box->setMargin( 0 );
127 box->addWidget( numbers );
128 box->addWidget( view );
129}
130
131NumberedTextView::~NumberedTextView()
132{
133}
134
135void NumberedTextView::markLine( int lineno )
136{
137 markedLine = lineno;
138 numbers->markLine( lineno );
139 textChanged(1,1,1);
140}
141
142void NumberedTextView::scrolltoLine( int lineno )
143{
144 int max = view->verticalScrollBar()->maximum();
145 int min = view->verticalScrollBar()->minimum();
146 int lines = view->document()->blockCount();
147 view->verticalScrollBar()->setValue( (max*lineno)/lines+min );
148}
149
150void NumberedTextView::textChanged( int pos, int removed, int added )
151{
152 Q_UNUSED( pos );
153
154 if ( removed == 0 && added == 0 )
155 return;
156
157 QTextBlock block = highlight.block();
158 QTextBlockFormat fmt = block.blockFormat();
159 QColor bg = view->palette().base().color();
160 fmt.setBackground( bg );
161 highlight.setBlockFormat( fmt );
162
163 int lineCount = 1;
164 for ( QTextBlock block = view->document()->begin();
165 block.isValid(); block = block.next(), ++lineCount )
166 {
167 if ( lineCount == markedLine )
168 {
169 fmt = block.blockFormat();
170 QColor bg = Qt::red;
171 fmt.setBackground( bg.light(150) );
172
173 highlight = QTextCursor( block );
174 highlight.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor );
175 highlight.setBlockFormat( fmt );
176
177 break;
178 }
179 }
180}
181