Make strings better translatable by allowing to change the position of the dynamic...
[kugel-rb.git] / utils / wpseditor / gui / src / numberedtextview.cpp
blob81c4208b593ded5ce115043bcc2e976520414c73
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>
28 #include <QTextBlock>
29 #include <QTextEdit>
30 #include <QHBoxLayout>
31 #include <QScrollBar>
32 #include <QPainter>
33 #include <QAbstractTextDocumentLayout>
34 #include <QDebug>
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 )
53 markedLine = lineno;
56 void NumberBar::setTextEdit( QTextEdit *edit )
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() ) );
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
72 int lineCount = 1;
74 QPainter p(this);
76 markedRect = QRect();
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 )
86 continue;
87 if ( position.y() > pageBottom )
88 break;
90 const QString txt = QString::number( lineCount );
91 p.drawText( width() - fm.width(txt), qRound( position.y() ) - contentsY + ascent, txt );
93 // marker
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 )
103 : QFrame( parent )
105 setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
106 setLineWidth( 2 );
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 );
120 // Test
121 markLine(2);
123 //setup layout
124 box = new QHBoxLayout( this );
125 box->setSpacing( 0 );
126 box->setMargin( 0 );
127 box->addWidget( numbers );
128 box->addWidget( view );
131 NumberedTextView::~NumberedTextView()
135 void NumberedTextView::markLine( int lineno )
137 markedLine = lineno;
138 numbers->markLine( lineno );
139 textChanged(1,1,1);
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 )
152 Q_UNUSED( pos );
154 if ( removed == 0 && added == 0 )
155 return;
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 );
163 int lineCount = 1;
164 for ( QTextBlock block = view->document()->begin();
165 block.isValid(); block = block.next(), ++lineCount )
167 if ( lineCount == markedLine )
169 fmt = block.blockFormat();
170 QColor bg = Qt::red;
171 fmt.setBackground( bg.light(150) );
173 highlight = QTextCursor( block );
174 highlight.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor );
175 highlight.setBlockFormat( fmt );
177 break;