25 #include <QtGui/QTextDocument>
26 #include <QtGui/QTextBlock>
27 #include <QtGui/QTextEdit>
28 #include <QtGui/QBoxLayout>
29 #include <QtGui/QScrollBar>
30 #include <QtGui/QPainter>
31 #include <QtGui/QTextObjectInterface>
32 #include <QtGui/QToolTip>
33 #include <QtCore/QDebug>
36 :
QWidget( parent ), edit(0), m_stopLine(-1), m_currentLine(-1), m_bugLine(-1)
38 setFixedWidth( fontMetrics().
width( QString(
"0000") + 10 + 32 ) );
39 stopMarker = QPixmap(
":/images/no.png" );
40 currentMarker = QPixmap(
":/images/next.png" );
41 bugMarker = QPixmap(
":/images/bug.png" );
50 m_currentLine = lineno;
81 connect( edit->document()->documentLayout(), SIGNAL(update(QRectF)),
82 this, SLOT(update()) );
83 connect( edit->verticalScrollBar(), SIGNAL(valueChanged(
int)),
84 this, SLOT(update()) );
89 QAbstractTextDocumentLayout *layout = edit->document()->documentLayout();
90 int contentsY = edit->verticalScrollBar()->value();
91 qreal pageBottom = contentsY + edit->viewport()->height();
92 const QFontMetrics fm = fontMetrics();
93 const int ascent = fontMetrics().ascent() + 1;
100 currentRect = QRect();
102 for ( QTextBlock block = edit->document()->begin();
103 block.isValid(); block = block.next(), ++lineCount ) {
105 const QRectF boundingRect = layout->blockBoundingRect( block );
107 QPointF position = boundingRect.topLeft();
108 if ( position.y() + boundingRect.height() < contentsY )
110 if ( position.y() > pageBottom )
113 const QString txt = QString::number( lineCount );
114 p.drawText(
width() - fm.width(txt), qRound( position.y() ) - contentsY + ascent, txt );
117 if ( m_bugLine == lineCount ) {
118 p.drawPixmap( 1, qRound( position.y() ) - contentsY, bugMarker );
119 bugRect = QRect( 1, qRound( position.y() ) - contentsY, bugMarker.width(), bugMarker.height() );
123 if ( m_stopLine == lineCount ) {
124 p.drawPixmap( 1, qRound( position.y() ) - contentsY, stopMarker );
125 stopRect = QRect( 1, qRound( position.y() ) - contentsY, stopMarker.width(), stopMarker.height() );
129 if ( m_currentLine == lineCount ) {
130 p.drawPixmap( 1, qRound( position.y() ) - contentsY, currentMarker );
131 currentRect = QRect( 1, qRound( position.y() ) - contentsY, currentMarker.width(), currentMarker.height() );
138 if ( event->type() == QEvent::ToolTip ) {
139 QHelpEvent *helpEvent =
static_cast<QHelpEvent *
>(
event);
141 if ( stopRect.contains( helpEvent->pos() ) ) {
142 QToolTip::showText( helpEvent->globalPos(),
"Stop Here" );
144 else if ( currentRect.contains( helpEvent->pos() ) ) {
145 QToolTip::showText( helpEvent->globalPos(),
"Current Line" );
147 else if ( bugRect.contains( helpEvent->pos() ) ) {
148 QToolTip::showText( helpEvent->globalPos(),
"Error Line" );
158 setFrameStyle( QFrame::StyledPanel | QFrame::Sunken );
162 view =
new QTextEdit(
this );
163 view->setFontFamily(
"Monospace" );
164 view->setLineWrapMode( QTextEdit::NoWrap );
165 view->setFrameStyle( QFrame::NoFrame );
166 view->installEventFilter(
this );
168 connect( view->document(), SIGNAL(contentsChange(
int,
int,
int)),
this, SLOT(
textChanged(
int,
int,
int)) );
172 numbers->setTextEdit( view );
174 numbers->setStopLine( 3 );
175 numbers->setBugLine( 1 );
178 box =
new QHBoxLayout(
this );
179 box->setSpacing( 0 );
181 box->addWidget( numbers );
182 box->addWidget( view );
222 return view->toPlainText ();
227 view->setPlainText(text);
235 if ( removed == 0 && added == 0 )
238 QTextBlock block = highlight.block();
239 QTextBlockFormat fmt = block.blockFormat();
240 QColor bg = view->palette().base().color();
241 fmt.setBackground( bg );
242 highlight.setBlockFormat( fmt );
245 for ( QTextBlock block = view->document()->begin();
246 block.isValid(); block = block.next(), ++lineCount ) {
249 fmt = block.blockFormat();
250 QColor bg = view->palette().color(QPalette::Highlight).light( 175 );
251 fmt.setBackground( bg );
253 highlight = QTextCursor( block );
254 highlight.movePosition( QTextCursor::EndOfBlock, QTextCursor::KeepAnchor );
255 highlight.setBlockFormat( fmt );
265 return QFrame::eventFilter(obj, event);
267 if ( event->type() == QEvent::ToolTip ) {
268 QHelpEvent *helpEvent =
static_cast<QHelpEvent *
>(
event);
270 QTextCursor cursor = view->cursorForPosition( helpEvent->pos() );
271 cursor.movePosition( QTextCursor::StartOfWord, QTextCursor::MoveAnchor );
272 cursor.movePosition( QTextCursor::EndOfWord, QTextCursor::KeepAnchor );
274 QString word = cursor.selectedText();
284 #include "numberedtextview.moc"
bool eventFilter(QObject *obj, QEvent *event)
void paintEvent(QPaintEvent *ev)
void setStopLine(int lineno)
Sets the line that should have the stop line indicator.
void setText(const QString &text)
void setBugLine(int lineno)
Sets the line that should have the bug line indicator.
NumberedTextView(QWidget *parent=0)
KJS::JSObject * event(KJS::ExecState *exec, const QEvent *ev)
void mouseHover(const QString &word)
Emitted when the mouse is hovered over the text edit component.
void setBugLine(int lineno)
void setCurrentLine(int lineno)
NumberBar(QWidget *parent)
void setTextEdit(QTextEdit *edit)
void setCurrentLine(int lineno)
Sets the line that should have the current line indicator.
void textChanged(int pos, int added, int removed)
void setStopLine(int lineno)