1 /* This file is part of the KDE libraries
2 Copyright (C) 2005, 2006 KJSEmbed Authors
3 See included AUTHORS file.
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.
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.
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.
21 --------------------------------------------------------------------------------------
22 Imported into the WPS editor and simplified by Dominik Wenger
27 #include <QTextDocument>
30 #include <QHBoxLayout>
33 #include <QAbstractTextDocumentLayout>
36 #include "numberedtextview.h"
38 NumberBar::NumberBar( QWidget
*parent
)
39 : QWidget( parent
), edit(0), markedLine(-1)
41 // Make room for 4 digits and the breakpoint icon
42 setFixedWidth( fontMetrics().width( QString("0000") + 10 + 32 ) );
43 markerIcon
= QPixmap( "images/marker.png" );
46 NumberBar::~NumberBar()
51 void NumberBar::markLine( int lineno
)
56 void NumberBar::setTextEdit( QTextEdit
*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() ) );
65 void NumberBar::paintEvent( QPaintEvent
* )
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
78 for ( QTextBlock block
= edit
->document()->begin();
79 block
.isValid(); block
= block
.next(), ++lineCount
)
82 const QRectF boundingRect
= layout
->blockBoundingRect( block
);
84 QPointF position
= boundingRect
.topLeft();
85 if ( position
.y() + boundingRect
.height() < contentsY
)
87 if ( position
.y() > pageBottom
)
90 const QString txt
= QString::number( lineCount
);
91 p
.drawText( width() - fm
.width(txt
), qRound( position
.y() ) - contentsY
+ ascent
, txt
);
94 if ( markedLine
== lineCount
)
96 p
.drawPixmap( 1, qRound( position
.y() ) - contentsY
, markerIcon
);
97 markedRect
= QRect( 1, qRound( position
.y() ) - contentsY
, markerIcon
.width(), markerIcon
.height() );
102 NumberedTextView::NumberedTextView( QWidget
*parent
)
105 setFrameStyle( QFrame::StyledPanel
| QFrame::Sunken
);
108 // Setup the main view
109 view
= new QTextEdit( this );
110 //view->setFontFamily( "Courier" );
111 view
->setLineWrapMode( QTextEdit::NoWrap
);
112 view
->setFrameStyle( QFrame::NoFrame
);
114 connect( view
->document(), SIGNAL(contentsChange(int,int,int)), this, SLOT(textChanged(int,int,int)) );
116 // Setup the line number pane
117 numbers
= new NumberBar( this );
118 numbers
->setTextEdit( view
);
124 box
= new QHBoxLayout( this );
125 box
->setSpacing( 0 );
127 box
->addWidget( numbers
);
128 box
->addWidget( view
);
131 NumberedTextView::~NumberedTextView()
135 void NumberedTextView::markLine( int lineno
)
138 numbers
->markLine( lineno
);
142 void NumberedTextView::scrolltoLine( int lineno
)
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
);
150 void NumberedTextView::textChanged( int pos
, int removed
, int added
)
154 if ( removed
== 0 && added
== 0 )
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
);
164 for ( QTextBlock block
= view
->document()->begin();
165 block
.isValid(); block
= block
.next(), ++lineCount
)
167 if ( lineCount
== markedLine
)
169 fmt
= block
.blockFormat();
171 fmt
.setBackground( bg
.light(150) );
173 highlight
= QTextCursor( block
);
174 highlight
.movePosition( QTextCursor::EndOfBlock
, QTextCursor::KeepAnchor
);
175 highlight
.setBlockFormat( fmt
);