xref: /glogg/src/abstractlogview.cpp (revision 819e4a32eed04ec3288b42e92217b9d36f93cfd9)
1 /*
2  * Copyright (C) 2009, 2010, 2011, 2012, 2013, 2015 Nicolas Bonnefon
3  * and other contributors
4  *
5  * This file is part of glogg.
6  *
7  * glogg is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * glogg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with glogg.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 // This file implements the AbstractLogView base class.
22 // Most of the actual drawing and event management common to the two views
23 // is implemented in this class.  The class only calls protected virtual
24 // functions when view specific behaviour is desired, using the template
25 // pattern.
26 
27 #include <iostream>
28 #include <cassert>
29 
30 #include <QApplication>
31 #include <QClipboard>
32 #include <QFile>
33 #include <QRect>
34 #include <QPaintEvent>
35 #include <QPainter>
36 #include <QFontMetrics>
37 #include <QScrollBar>
38 #include <QMenu>
39 #include <QAction>
40 #include <QtCore>
41 #include <QGestureEvent>
42 
43 #include "log.h"
44 
45 #include "persistentinfo.h"
46 #include "filterset.h"
47 #include "logmainview.h"
48 #include "quickfind.h"
49 #include "quickfindpattern.h"
50 #include "overview.h"
51 #include "configuration.h"
52 
53 namespace {
54 int mapPullToFollowLength( int length );
55 };
56 
57 namespace {
58 
59 int countDigits( quint64 n )
60 {
61     if (n == 0)
62         return 1;
63 
64     // We must force the compiler to not store intermediate results
65     // in registers because this causes incorrect result on some
66     // systems under optimizations level >0. For the skeptical:
67     //
68     // #include <math.h>
69     // #include <stdlib.h>
70     // int main(int argc, char **argv) {
71     //     (void)argc;
72     //     long long int n = atoll(argv[1]);
73     //     return floor( log( n ) / log( 10 ) + 1 );
74     // }
75     //
76     // This is on Thinkpad T60 (Genuine Intel(R) CPU T2300).
77     // $ g++ --version
78     // g++ (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
79     // $ g++ -O0 -Wall -W -o math math.cpp -lm; ./math 10; echo $?
80     // 2
81     // $ g++ -O1 -Wall -W -o math math.cpp -lm; ./math 10; echo $?
82     // 1
83     //
84     // A fix is to (1) explicitly place intermediate results in
85     // variables *and* (2) [A] mark them as 'volatile', or [B] pass
86     // -ffloat-store to g++ (note that approach [A] is more portable).
87 
88     volatile qreal ln_n  = qLn( n );
89     volatile qreal ln_10 = qLn( 10 );
90     volatile qreal lg_n = ln_n / ln_10;
91     volatile qreal lg_n_1 = lg_n + 1;
92     volatile qreal fl_lg_n_1 = qFloor( lg_n_1 );
93 
94     return fl_lg_n_1;
95 }
96 
97 } // anon namespace
98 
99 
100 LineChunk::LineChunk( int first_col, int last_col, ChunkType type )
101 {
102     // LOG(logDEBUG) << "new LineChunk: " << first_col << " " << last_col;
103 
104     start_ = first_col;
105     end_   = last_col;
106     type_  = type;
107 }
108 
109 QList<LineChunk> LineChunk::select( int sel_start, int sel_end ) const
110 {
111     QList<LineChunk> list;
112 
113     if ( ( sel_start < start_ ) && ( sel_end < start_ ) ) {
114         // Selection BEFORE this chunk: no change
115         list << LineChunk( *this );
116     }
117     else if ( sel_start > end_ ) {
118         // Selection AFTER this chunk: no change
119         list << LineChunk( *this );
120     }
121     else /* if ( ( sel_start >= start_ ) && ( sel_end <= end_ ) ) */
122     {
123         // We only want to consider what's inside THIS chunk
124         sel_start = qMax( sel_start, start_ );
125         sel_end   = qMin( sel_end, end_ );
126 
127         if ( sel_start > start_ )
128             list << LineChunk( start_, sel_start - 1, type_ );
129         list << LineChunk( sel_start, sel_end, Selected );
130         if ( sel_end < end_ )
131             list << LineChunk( sel_end + 1, end_, type_ );
132     }
133 
134     return list;
135 }
136 
137 inline void LineDrawer::addChunk( int first_col, int last_col,
138         QColor fore, QColor back )
139 {
140     if ( first_col < 0 )
141         first_col = 0;
142     int length = last_col - first_col + 1;
143     if ( length > 0 ) {
144         list << Chunk ( first_col, length, fore, back );
145     }
146 }
147 
148 inline void LineDrawer::addChunk( const LineChunk& chunk,
149         QColor fore, QColor back )
150 {
151     int first_col = chunk.start();
152     int last_col  = chunk.end();
153 
154     addChunk( first_col, last_col, fore, back );
155 }
156 
157 inline void LineDrawer::draw( QPainter& painter,
158         int initialXPos, int initialYPos,
159         int line_width, const QString& line,
160         int leftExtraBackgroundPx )
161 {
162     QFontMetrics fm = painter.fontMetrics();
163     const int fontHeight = fm.height();
164     const int fontAscent = fm.ascent();
165     // For some reason on Qt 4.8.2 for Win, maxWidth() is wrong but the
166     // following give the right result, not sure why:
167     const int fontWidth = fm.width( QChar('a') );
168 
169     int xPos = initialXPos;
170     int yPos = initialYPos;
171 
172     foreach ( Chunk chunk, list ) {
173         // Draw each chunk
174         // LOG(logDEBUG) << "Chunk: " << chunk.start() << " " << chunk.length();
175         QString cutline = line.mid( chunk.start(), chunk.length() );
176         const int chunk_width = cutline.length() * fontWidth;
177         if ( xPos == initialXPos ) {
178             // First chunk, we extend the left background a bit,
179             // it looks prettier.
180             painter.fillRect( xPos - leftExtraBackgroundPx, yPos,
181                     chunk_width + leftExtraBackgroundPx,
182                     fontHeight, chunk.backColor() );
183         }
184         else {
185             // other chunks...
186             painter.fillRect( xPos, yPos, chunk_width,
187                     fontHeight, chunk.backColor() );
188         }
189         painter.setPen( chunk.foreColor() );
190         painter.drawText( xPos, yPos + fontAscent, cutline );
191         xPos += chunk_width;
192     }
193 
194     // Draw the empty block at the end of the line
195     int blank_width = line_width - xPos;
196 
197     if ( blank_width > 0 )
198         painter.fillRect( xPos, yPos, blank_width, fontHeight, backColor_ );
199 }
200 
201 const int DigitsBuffer::timeout_ = 2000;
202 
203 DigitsBuffer::DigitsBuffer() : QObject()
204 {
205 }
206 
207 void DigitsBuffer::reset()
208 {
209     LOG(logDEBUG) << "DigitsBuffer::reset()";
210 
211     timer_.stop();
212     digits_.clear();
213 }
214 
215 void DigitsBuffer::add( char character )
216 {
217     LOG(logDEBUG) << "DigitsBuffer::add()";
218 
219     digits_.append( QChar( character ) );
220     timer_.start( timeout_ , this );
221 }
222 
223 int DigitsBuffer::content()
224 {
225     int result = digits_.toInt();
226     reset();
227 
228     return result;
229 }
230 
231 void DigitsBuffer::timerEvent( QTimerEvent* event )
232 {
233     if ( event->timerId() == timer_.timerId() ) {
234         reset();
235     }
236     else {
237         QObject::timerEvent( event );
238     }
239 }
240 
241 AbstractLogView::AbstractLogView(const AbstractLogData* newLogData,
242         const QuickFindPattern* const quickFindPattern, QWidget* parent) :
243     QAbstractScrollArea( parent ),
244     followElasticHook_( HOOK_THRESHOLD ),
245     lineNumbersVisible_( false ),
246     selectionStartPos_(),
247     selectionCurrentEndPos_(),
248     autoScrollTimer_(),
249     selection_(),
250     quickFindPattern_( quickFindPattern ),
251     quickFind_( newLogData, &selection_, quickFindPattern )
252 {
253     logData = newLogData;
254 
255     followMode_ = false;
256 
257     selectionStarted_ = false;
258     markingClickInitiated_ = false;
259 
260     firstLine = 0;
261     firstCol = 0;
262 
263     overview_ = NULL;
264     overviewWidget_ = NULL;
265 
266     // Display
267     leftMarginPx_ = 0;
268 
269     // Fonts
270     charWidth_ = 1;
271     charHeight_ = 1;
272 
273     // Create the viewport QWidget
274     setViewport( 0 );
275 
276     // Hovering
277     setMouseTracking( true );
278     lastHoveredLine_ = -1;
279 
280     // Init the popup menu
281     createMenu();
282 
283     // Signals
284     connect( quickFindPattern_, SIGNAL( patternUpdated() ),
285             this, SLOT ( handlePatternUpdated() ) );
286     connect( &quickFind_, SIGNAL( notify( const QFNotification& ) ),
287             this, SIGNAL( notifyQuickFind( const QFNotification& ) ) );
288     connect( &quickFind_, SIGNAL( clearNotification() ),
289             this, SIGNAL( clearQuickFindNotification() ) );
290     connect( &followElasticHook_, SIGNAL( lengthChanged() ),
291             this, SLOT( repaint() ) );
292     connect( &followElasticHook_, SIGNAL( hooked( bool ) ),
293             this, SIGNAL( followModeChanged( bool ) ) );
294 }
295 
296 AbstractLogView::~AbstractLogView()
297 {
298 }
299 
300 
301 //
302 // Received events
303 //
304 
305 void AbstractLogView::changeEvent( QEvent* changeEvent )
306 {
307     QAbstractScrollArea::changeEvent( changeEvent );
308 
309     // Stop the timer if the widget becomes inactive
310     if ( changeEvent->type() == QEvent::ActivationChange ) {
311         if ( ! isActiveWindow() )
312             autoScrollTimer_.stop();
313     }
314     viewport()->update();
315 }
316 
317 void AbstractLogView::mousePressEvent( QMouseEvent* mouseEvent )
318 {
319     static std::shared_ptr<Configuration> config =
320         Persistent<Configuration>( "settings" );
321 
322     if ( mouseEvent->button() == Qt::LeftButton )
323     {
324         int line = convertCoordToLine( mouseEvent->y() );
325 
326         if ( mouseEvent->modifiers() & Qt::ShiftModifier )
327         {
328             selection_.selectRangeFromPrevious( line );
329             emit updateLineNumber( line );
330             update();
331         }
332         else
333         {
334             if ( mouseEvent->x() < bulletZoneWidthPx_ ) {
335                 // Mark a line if it is clicked in the left margin
336                 // (only if click and release in the same area)
337                 markingClickInitiated_ = true;
338                 markingClickLine_ = line;
339             }
340             else {
341                 // Select the line, and start a selection
342                 if ( line < logData->getNbLine() ) {
343                     selection_.selectLine( line );
344                     emit updateLineNumber( line );
345                     emit newSelection( line );
346                 }
347 
348                 // Remember the click in case we're starting a selection
349                 selectionStarted_ = true;
350                 selectionStartPos_ = convertCoordToFilePos( mouseEvent->pos() );
351                 selectionCurrentEndPos_ = selectionStartPos_;
352             }
353         }
354 
355         // Invalidate our cache
356         textAreaCache_.invalid_ = true;
357     }
358     else if ( mouseEvent->button() == Qt::RightButton )
359     {
360         // Prepare the popup depending on selection type
361         if ( selection_.isSingleLine() ) {
362             copyAction_->setText( "&Copy this line" );
363         }
364         else {
365             copyAction_->setText( "&Copy" );
366             copyAction_->setStatusTip( tr("Copy the selection") );
367         }
368 
369         if ( selection_.isPortion() ) {
370             findNextAction_->setEnabled( true );
371             findPreviousAction_->setEnabled( true );
372             addToSearchAction_->setEnabled( true );
373         }
374         else {
375             findNextAction_->setEnabled( false );
376             findPreviousAction_->setEnabled( false );
377             addToSearchAction_->setEnabled( false );
378         }
379 
380         // "Add to search" only makes sense in regexp mode
381         if ( config->mainRegexpType() != ExtendedRegexp )
382             addToSearchAction_->setEnabled( false );
383 
384         // Display the popup (blocking)
385         popupMenu_->exec( QCursor::pos() );
386     }
387 
388     emit activity();
389 }
390 
391 void AbstractLogView::mouseMoveEvent( QMouseEvent* mouseEvent )
392 {
393     // Selection implementation
394     if ( selectionStarted_ )
395     {
396         // Invalidate our cache
397         textAreaCache_.invalid_ = true;
398 
399         QPoint thisEndPos = convertCoordToFilePos( mouseEvent->pos() );
400         if ( thisEndPos != selectionCurrentEndPos_ )
401         {
402             // Are we on a different line?
403             if ( selectionStartPos_.y() != thisEndPos.y() )
404             {
405                 if ( thisEndPos.y() != selectionCurrentEndPos_.y() )
406                 {
407                     // This is a 'range' selection
408                     selection_.selectRange( selectionStartPos_.y(),
409                             thisEndPos.y() );
410                     emit updateLineNumber( thisEndPos.y() );
411                     update();
412                 }
413             }
414             // So we are on the same line. Are we moving horizontaly?
415             else if ( thisEndPos.x() != selectionCurrentEndPos_.x() )
416             {
417                 // This is a 'portion' selection
418                 selection_.selectPortion( thisEndPos.y(),
419                         selectionStartPos_.x(), thisEndPos.x() );
420                 update();
421             }
422             // On the same line, and moving vertically then
423             else
424             {
425                 // This is a 'line' selection
426                 selection_.selectLine( thisEndPos.y() );
427                 emit updateLineNumber( thisEndPos.y() );
428                 update();
429             }
430             selectionCurrentEndPos_ = thisEndPos;
431 
432             // Do we need to scroll while extending the selection?
433             QRect visible = viewport()->rect();
434             if ( visible.contains( mouseEvent->pos() ) )
435                 autoScrollTimer_.stop();
436             else if ( ! autoScrollTimer_.isActive() )
437                 autoScrollTimer_.start( 100, this );
438         }
439     }
440     else {
441         considerMouseHovering( mouseEvent->x(), mouseEvent->y() );
442     }
443 }
444 
445 void AbstractLogView::mouseReleaseEvent( QMouseEvent* mouseEvent )
446 {
447     if ( markingClickInitiated_ ) {
448         markingClickInitiated_ = false;
449         int line = convertCoordToLine( mouseEvent->y() );
450         if ( line == markingClickLine_ ) {
451             // Invalidate our cache
452             textAreaCache_.invalid_ = true;
453 
454             emit markLine( line );
455         }
456     }
457     else {
458         selectionStarted_ = false;
459         if ( autoScrollTimer_.isActive() )
460             autoScrollTimer_.stop();
461         updateGlobalSelection();
462     }
463 }
464 
465 void AbstractLogView::mouseDoubleClickEvent( QMouseEvent* mouseEvent )
466 {
467     if ( mouseEvent->button() == Qt::LeftButton )
468     {
469         // Invalidate our cache
470         textAreaCache_.invalid_ = true;
471 
472         const QPoint pos = convertCoordToFilePos( mouseEvent->pos() );
473         selectWordAtPosition( pos );
474     }
475 
476     emit activity();
477 }
478 
479 void AbstractLogView::timerEvent( QTimerEvent* timerEvent )
480 {
481     if ( timerEvent->timerId() == autoScrollTimer_.timerId() ) {
482         QRect visible = viewport()->rect();
483         const QPoint globalPos = QCursor::pos();
484         const QPoint pos = viewport()->mapFromGlobal( globalPos );
485         QMouseEvent ev( QEvent::MouseMove, pos, globalPos, Qt::LeftButton,
486                 Qt::LeftButton, Qt::NoModifier );
487         mouseMoveEvent( &ev );
488         int deltaX = qMax( pos.x() - visible.left(),
489                 visible.right() - pos.x() ) - visible.width();
490         int deltaY = qMax( pos.y() - visible.top(),
491                 visible.bottom() - pos.y() ) - visible.height();
492         int delta = qMax( deltaX, deltaY );
493 
494         if ( delta >= 0 ) {
495             if ( delta < 7 )
496                 delta = 7;
497             int timeout = 4900 / ( delta * delta );
498             autoScrollTimer_.start( timeout, this );
499 
500             if ( deltaX > 0 )
501                 horizontalScrollBar()->triggerAction(
502                         pos.x() <visible.center().x() ?
503                         QAbstractSlider::SliderSingleStepSub :
504                         QAbstractSlider::SliderSingleStepAdd );
505 
506             if ( deltaY > 0 )
507                 verticalScrollBar()->triggerAction(
508                         pos.y() <visible.center().y() ?
509                         QAbstractSlider::SliderSingleStepSub :
510                         QAbstractSlider::SliderSingleStepAdd );
511         }
512     }
513     QAbstractScrollArea::timerEvent( timerEvent );
514 }
515 
516 void AbstractLogView::keyPressEvent( QKeyEvent* keyEvent )
517 {
518     LOG(logDEBUG4) << "keyPressEvent received";
519     bool controlModifier = (keyEvent->modifiers() & Qt::ControlModifier) == Qt::ControlModifier;
520     bool shiftModifier = (keyEvent->modifiers() & Qt::ShiftModifier) == Qt::ShiftModifier;
521 
522     if ( keyEvent->key() == Qt::Key_Left )
523         horizontalScrollBar()->triggerAction(QScrollBar::SliderPageStepSub);
524     else if ( keyEvent->key() == Qt::Key_Right )
525         horizontalScrollBar()->triggerAction(QScrollBar::SliderPageStepAdd);
526     else if ( keyEvent->key() == Qt::Key_Home && !controlModifier)
527         jumpToStartOfLine();
528     else if ( keyEvent->key() == Qt::Key_End  && !controlModifier)
529         jumpToRightOfScreen();
530     else if ( (keyEvent->key() == Qt::Key_PageDown && controlModifier)
531            || (keyEvent->key() == Qt::Key_End && controlModifier) )
532     {
533         disableFollow(); // duplicate of 'G' action.
534         selection_.selectLine( logData->getNbLine() - 1 );
535         emit updateLineNumber( logData->getNbLine() - 1 );
536         jumpToBottom();
537     }
538     else if ( (keyEvent->key() == Qt::Key_PageUp && controlModifier)
539            || (keyEvent->key() == Qt::Key_Home && controlModifier) )
540     {
541         disableFollow(); // like 'g' but 0 input first line action.
542         selectAndDisplayLine( 0 );
543         emit updateLineNumber( 0 );
544     }
545     else if ( keyEvent->key() == Qt::Key_F3 && !shiftModifier )
546         searchNext(); // duplicate of 'n' action.
547     else if ( keyEvent->key() == Qt::Key_F3 && shiftModifier )
548         searchPrevious(); // duplicate of 'N' action.
549     else {
550         const char character = (keyEvent->text())[0].toLatin1();
551 
552         if ( keyEvent->modifiers() == Qt::NoModifier &&
553                 ( character >= '0' ) && ( character <= '9' ) ) {
554             // Adds the digit to the timed buffer
555             digitsBuffer_.add( character );
556         }
557         else {
558             switch ( (keyEvent->text())[0].toLatin1() ) {
559                 case 'j':
560                     {
561                         int delta = qMax( 1, digitsBuffer_.content() );
562                         disableFollow();
563                         //verticalScrollBar()->triggerAction(
564                         //QScrollBar::SliderSingleStepAdd);
565                         moveSelection( delta );
566                         break;
567                     }
568                 case 'k':
569                     {
570                         int delta = qMin( -1, - digitsBuffer_.content() );
571                         disableFollow();
572                         //verticalScrollBar()->triggerAction(
573                         //QScrollBar::SliderSingleStepSub);
574                         moveSelection( delta );
575                         break;
576                     }
577                 case 'h':
578                     horizontalScrollBar()->triggerAction(
579                             QScrollBar::SliderSingleStepSub);
580                     break;
581                 case 'l':
582                     horizontalScrollBar()->triggerAction(
583                             QScrollBar::SliderSingleStepAdd);
584                     break;
585                 case '0':
586                     jumpToStartOfLine();
587                     break;
588                 case '$':
589                     jumpToEndOfLine();
590                     break;
591                 case 'g':
592                     {
593                         int newLine = qMax( 0, digitsBuffer_.content() - 1 );
594                         if ( newLine >= logData->getNbLine() )
595                             newLine = logData->getNbLine() - 1;
596                         disableFollow();
597                         selectAndDisplayLine( newLine );
598                         emit updateLineNumber( newLine );
599                         break;
600                     }
601                 case 'G':
602                     disableFollow();
603                     selection_.selectLine( logData->getNbLine() - 1 );
604                     emit updateLineNumber( logData->getNbLine() - 1 );
605                     jumpToBottom();
606                     break;
607                 case 'n':
608                     emit searchNext();
609                     break;
610                 case 'N':
611                     emit searchPrevious();
612                     break;
613                 case '*':
614                     // Use the selected 'word' and search forward
615                     findNextSelected();
616                     break;
617                 case '#':
618                     // Use the selected 'word' and search backward
619                     findPreviousSelected();
620                     break;
621                 default:
622                     keyEvent->ignore();
623             }
624         }
625     }
626 
627     if ( keyEvent->isAccepted() ) {
628         emit activity();
629     }
630     else {
631         QAbstractScrollArea::keyPressEvent( keyEvent );
632     }
633 }
634 
635 void AbstractLogView::wheelEvent( QWheelEvent* wheelEvent )
636 {
637     emit activity();
638 
639     // LOG(logDEBUG) << "wheelEvent";
640 
641     // This is to handle the case where follow mode is on, but the user
642     // has moved using the scroll bar. We take them back to the bottom.
643     if ( followMode_ )
644         jumpToBottom();
645 
646     int y_delta = 0;
647     if ( verticalScrollBar()->value() == verticalScrollBar()->maximum() ) {
648         // First see if we need to block the elastic (on Mac)
649         if ( wheelEvent->phase() == Qt::ScrollBegin )
650             followElasticHook_.hold();
651         else if ( wheelEvent->phase() == Qt::ScrollEnd )
652             followElasticHook_.release();
653 
654         auto pixel_delta = wheelEvent->pixelDelta();
655 
656         if ( pixel_delta.isNull() ) {
657             y_delta = wheelEvent->angleDelta().y() / 1.4;
658         }
659         else {
660             y_delta = pixel_delta.y();
661         }
662 
663         // LOG(logDEBUG) << "Elastic " << y_delta;
664         followElasticHook_.move( - y_delta );
665     }
666 
667     // LOG(logDEBUG) << "Length = " << followElasticHook_.length();
668     if ( followElasticHook_.length() == 0 && !followElasticHook_.isHooked() ) {
669         QAbstractScrollArea::wheelEvent( wheelEvent );
670     }
671 }
672 
673 void AbstractLogView::resizeEvent( QResizeEvent* )
674 {
675     if ( logData == NULL )
676         return;
677 
678     LOG(logDEBUG) << "resizeEvent received";
679 
680     updateDisplaySize();
681 }
682 
683 bool AbstractLogView::event( QEvent* e )
684 {
685     LOG(logDEBUG4) << "Event! Type: " << e->type();
686 
687     // Make sure we ignore the gesture events as
688     // they seem to be accepted by default.
689     if ( e->type() == QEvent::Gesture ) {
690         auto gesture_event = dynamic_cast<QGestureEvent*>( e );
691         if ( gesture_event ) {
692             foreach( QGesture* gesture, gesture_event->gestures() ) {
693                 LOG(logDEBUG4) << "Gesture: " << gesture->gestureType();
694                 gesture_event->ignore( gesture );
695             }
696 
697             // Ensure the event is sent up to parents who might care
698             return false;
699         }
700     }
701 
702     return QAbstractScrollArea::event( e );
703 }
704 
705 void AbstractLogView::scrollContentsBy( int dx, int dy )
706 {
707     LOG(logDEBUG) << "scrollContentsBy received " << dy;
708 
709     firstLine = std::max( static_cast<int32_t>( firstLine ) - dy, 0 );
710     firstCol  = (firstCol - dx) > 0 ? firstCol - dx : 0;
711     LineNumber last_line  = firstLine + getNbVisibleLines();
712 
713     // Update the overview if we have one
714     if ( overview_ != NULL )
715         overview_->updateCurrentPosition( firstLine, last_line );
716 
717     // Are we hovering over a new line?
718     const QPoint mouse_pos = mapFromGlobal( QCursor::pos() );
719     considerMouseHovering( mouse_pos.x(), mouse_pos.y() );
720 
721     // Redraw
722     update();
723 }
724 
725 void AbstractLogView::paintEvent( QPaintEvent* paintEvent )
726 {
727     const QRect invalidRect = paintEvent->rect();
728     if ( (invalidRect.isEmpty()) || (logData == NULL) )
729         return;
730 
731     LOG(logDEBUG4) << "paintEvent received, firstLine=" << firstLine
732         << " rect: " << invalidRect.topLeft().x() <<
733         ", " << invalidRect.topLeft().y() <<
734         ", " << invalidRect.bottomRight().x() <<
735         ", " << invalidRect.bottomRight().y();
736 
737 #ifdef GLOGG_PERF_MEASURE_FPS
738     static uint32_t maxline = logData->getNbLine();
739     if ( ! perfCounter_.addEvent() && logData->getNbLine() > maxline ) {
740         LOG(logWARNING) << "Redraw per second: " << perfCounter_.readAndReset()
741             << " lines: " << logData->getNbLine();
742         perfCounter_.addEvent();
743         maxline = logData->getNbLine();
744     }
745 #endif
746 
747     auto start = std::chrono::system_clock::now();
748 
749     // Can we use our cache?
750     int32_t delta_y = textAreaCache_.first_line_ - firstLine;
751 
752     if ( textAreaCache_.invalid_ || ( textAreaCache_.first_column_ != firstCol ) ) {
753         // Force a full redraw
754         delta_y = INT32_MAX;
755     }
756 
757     if ( delta_y != 0 ) {
758         // Full or partial redraw
759         drawTextArea( &textAreaCache_.pixmap_, delta_y );
760 
761         textAreaCache_.invalid_      = false;
762         textAreaCache_.first_line_   = firstLine;
763         textAreaCache_.first_column_ = firstCol;
764 
765         LOG(logDEBUG) << "End of writing " <<
766             std::chrono::duration_cast<std::chrono::microseconds>
767             ( std::chrono::system_clock::now() - start ).count();
768     }
769     else {
770         // Use the cache as is: nothing to do!
771     }
772 
773     // Height including the potentially invisible last line
774     const int wholeHeight = getNbVisibleLines() * charHeight_;
775     // Height in pixels of the "pull to follow" bottom bar.
776     pullToFollowHeight_ = mapPullToFollowLength( followElasticHook_.length() )
777         + ( followElasticHook_.isHooked() ?
778                 ( wholeHeight - viewport()->height() ) + PULL_TO_FOLLOW_HOOKED_HEIGHT : 0 );
779 
780     if ( pullToFollowHeight_
781             && ( pullToFollowCache_.nb_columns_ != getNbVisibleCols() ) ) {
782         LOG(logDEBUG) << "Drawing pull to follow bar";
783         pullToFollowCache_.pixmap_ = drawPullToFollowBar(
784                 viewport()->width(), viewport()->devicePixelRatio() );
785         pullToFollowCache_.nb_columns_ = getNbVisibleCols();
786     }
787 
788     QPainter devicePainter( viewport() );
789     int drawingTopPosition = - pullToFollowHeight_;
790 
791     // This is to cover the special case where there is less than a screenful
792     // worth of data, we want to see the document from the top, rather than
793     // pushing the first couple of lines above the viewport.
794     if ( followElasticHook_.isHooked() && ( logData->getNbLine() < getNbVisibleLines() ) )
795         drawingTopPosition += ( wholeHeight - viewport()->height() + PULL_TO_FOLLOW_HOOKED_HEIGHT );
796 
797     devicePainter.drawPixmap( 0, drawingTopPosition, textAreaCache_.pixmap_ );
798 
799     // Draw the "pull to follow" zone if needed
800     if ( pullToFollowHeight_ ) {
801         devicePainter.drawPixmap( 0,
802                 getNbVisibleLines() * charHeight_ - pullToFollowHeight_,
803                 pullToFollowCache_.pixmap_ );
804     }
805 
806     LOG(logDEBUG) << "End of repaint " <<
807         std::chrono::duration_cast<std::chrono::microseconds>
808         ( std::chrono::system_clock::now() - start ).count();
809 }
810 
811 // These two functions are virtual and this implementation is clearly
812 // only valid for a non-filtered display.
813 // We count on the 'filtered' derived classes to override them.
814 qint64 AbstractLogView::displayLineNumber( int lineNumber ) const
815 {
816     return lineNumber + 1; // show a 1-based index
817 }
818 
819 qint64 AbstractLogView::maxDisplayLineNumber() const
820 {
821     return logData->getNbLine();
822 }
823 
824 void AbstractLogView::setOverview( Overview* overview,
825        OverviewWidget* overview_widget )
826 {
827     overview_ = overview;
828     overviewWidget_ = overview_widget;
829 
830     if ( overviewWidget_ ) {
831         connect( overviewWidget_, SIGNAL( lineClicked ( int ) ),
832                 this, SIGNAL( followDisabled() ) );
833         connect( overviewWidget_, SIGNAL( lineClicked ( int ) ),
834                 this, SLOT( jumpToLine( int ) ) );
835     }
836     refreshOverview();
837 }
838 
839 void AbstractLogView::searchUsingFunction(
840         qint64 (QuickFind::*search_function)() )
841 {
842     disableFollow();
843 
844     int line = (quickFind_.*search_function)();
845     if ( line >= 0 ) {
846         LOG(logDEBUG) << "search " << line;
847         displayLine( line );
848         emit updateLineNumber( line );
849     }
850 }
851 
852 void AbstractLogView::searchForward()
853 {
854     searchUsingFunction( &QuickFind::searchForward );
855 }
856 
857 void AbstractLogView::searchBackward()
858 {
859     searchUsingFunction( &QuickFind::searchBackward );
860 }
861 
862 void AbstractLogView::incrementallySearchForward()
863 {
864     searchUsingFunction( &QuickFind::incrementallySearchForward );
865 }
866 
867 void AbstractLogView::incrementallySearchBackward()
868 {
869     searchUsingFunction( &QuickFind::incrementallySearchBackward );
870 }
871 
872 void AbstractLogView::incrementalSearchAbort()
873 {
874     quickFind_.incrementalSearchAbort();
875     emit changeQuickFind(
876             "",
877             QuickFindMux::Forward );
878 }
879 
880 void AbstractLogView::incrementalSearchStop()
881 {
882     quickFind_.incrementalSearchStop();
883 }
884 
885 void AbstractLogView::followSet( bool checked )
886 {
887     followMode_ = checked;
888     followElasticHook_.hook( checked );
889     update();
890     if ( checked )
891         jumpToBottom();
892 }
893 
894 void AbstractLogView::refreshOverview()
895 {
896     assert( overviewWidget_ );
897 
898     // Create space for the Overview if needed
899     if ( ( getOverview() != NULL ) && getOverview()->isVisible() ) {
900         setViewportMargins( 0, 0, OVERVIEW_WIDTH, 0 );
901         overviewWidget_->show();
902     }
903     else {
904         setViewportMargins( 0, 0, 0, 0 );
905         overviewWidget_->hide();
906     }
907 }
908 
909 // Reset the QuickFind when the pattern is changed.
910 void AbstractLogView::handlePatternUpdated()
911 {
912     LOG(logDEBUG) << "AbstractLogView::handlePatternUpdated()";
913 
914     quickFind_.resetLimits();
915     update();
916 }
917 
918 // OR the current with the current search expression
919 void AbstractLogView::addToSearch()
920 {
921     if ( selection_.isPortion() ) {
922         LOG(logDEBUG) << "AbstractLogView::addToSearch()";
923         emit addToSearch( selection_.getSelectedText( logData ) );
924     }
925     else {
926         LOG(logERROR) << "AbstractLogView::addToSearch called for a wrong type of selection";
927     }
928 }
929 
930 // Find next occurence of the selected text (*)
931 void AbstractLogView::findNextSelected()
932 {
933     // Use the selected 'word' and search forward
934     if ( selection_.isPortion() ) {
935         emit changeQuickFind(
936                 selection_.getSelectedText( logData ),
937                 QuickFindMux::Forward );
938         emit searchNext();
939     }
940 }
941 
942 // Find next previous of the selected text (#)
943 void AbstractLogView::findPreviousSelected()
944 {
945     if ( selection_.isPortion() ) {
946         emit changeQuickFind(
947                 selection_.getSelectedText( logData ),
948                 QuickFindMux::Backward );
949         emit searchNext();
950     }
951 }
952 
953 // Copy the selection to the clipboard
954 void AbstractLogView::copy()
955 {
956     static QClipboard* clipboard = QApplication::clipboard();
957 
958     clipboard->setText( selection_.getSelectedText( logData ) );
959 }
960 
961 //
962 // Public functions
963 //
964 
965 void AbstractLogView::updateData()
966 {
967     LOG(logDEBUG) << "AbstractLogView::updateData";
968 
969     // Check the top Line is within range
970     if ( firstLine >= logData->getNbLine() ) {
971         firstLine = 0;
972         firstCol = 0;
973         verticalScrollBar()->setValue( 0 );
974         horizontalScrollBar()->setValue( 0 );
975     }
976 
977     // Crop selection if it become out of range
978     selection_.crop( logData->getNbLine() - 1 );
979 
980     // Adapt the scroll bars to the new content
981     updateScrollBars();
982 
983     // Calculate the index of the last line shown
984     LineNumber last_line = std::min( static_cast<int64_t>( logData->getNbLine() ),
985             static_cast<int64_t>( firstLine + getNbVisibleLines() ) );
986 
987     // Reset the QuickFind in case we have new stuff to search into
988     quickFind_.resetLimits();
989 
990     if ( followMode_ )
991         jumpToBottom();
992 
993     // Update the overview if we have one
994     if ( overview_ != NULL )
995         overview_->updateCurrentPosition( firstLine, last_line );
996 
997     // Invalidate our cache
998     textAreaCache_.invalid_ = true;
999 
1000     // Repaint!
1001     update();
1002 }
1003 
1004 void AbstractLogView::updateDisplaySize()
1005 {
1006     // Font is assumed to be mono-space (is restricted by options dialog)
1007     QFontMetrics fm = fontMetrics();
1008     charHeight_ = fm.height();
1009     // For some reason on Qt 4.8.2 for Win, maxWidth() is wrong but the
1010     // following give the right result, not sure why:
1011     charWidth_ = fm.width( QChar('a') );
1012 
1013     // Update the scroll bars
1014     updateScrollBars();
1015     verticalScrollBar()->setPageStep( getNbVisibleLines() );
1016 
1017     if ( followMode_ )
1018         jumpToBottom();
1019 
1020     LOG(logDEBUG) << "viewport.width()=" << viewport()->width();
1021     LOG(logDEBUG) << "viewport.height()=" << viewport()->height();
1022     LOG(logDEBUG) << "width()=" << width();
1023     LOG(logDEBUG) << "height()=" << height();
1024 
1025     if ( overviewWidget_ )
1026         overviewWidget_->setGeometry( viewport()->width() + 2, 1,
1027                 OVERVIEW_WIDTH - 1, viewport()->height() );
1028 
1029     // Our text area cache is now invalid
1030     textAreaCache_.invalid_ = true;
1031     textAreaCache_.pixmap_  = QPixmap {
1032         viewport()->width() * viewport()->devicePixelRatio(),
1033         static_cast<int32_t>( getNbVisibleLines() ) * charHeight_ * viewport()->devicePixelRatio() };
1034     textAreaCache_.pixmap_.setDevicePixelRatio( viewport()->devicePixelRatio() );
1035 }
1036 
1037 int AbstractLogView::getTopLine() const
1038 {
1039     return firstLine;
1040 }
1041 
1042 QString AbstractLogView::getSelection() const
1043 {
1044     return selection_.getSelectedText( logData );
1045 }
1046 
1047 void AbstractLogView::selectAll()
1048 {
1049     selection_.selectRange( 0, logData->getNbLine() - 1 );
1050     update();
1051 }
1052 
1053 void AbstractLogView::selectAndDisplayLine( int line )
1054 {
1055     disableFollow();
1056     selection_.selectLine( line );
1057     displayLine( line );
1058     emit updateLineNumber( line );
1059 }
1060 
1061 // The difference between this function and displayLine() is quite
1062 // subtle: this one always jump, even if the line passed is visible.
1063 void AbstractLogView::jumpToLine( int line )
1064 {
1065     // Put the selected line in the middle if possible
1066     int newTopLine = line - ( getNbVisibleLines() / 2 );
1067     if ( newTopLine < 0 )
1068         newTopLine = 0;
1069 
1070     // This will also trigger a scrollContents event
1071     verticalScrollBar()->setValue( newTopLine );
1072 }
1073 
1074 void AbstractLogView::setLineNumbersVisible( bool lineNumbersVisible )
1075 {
1076     lineNumbersVisible_ = lineNumbersVisible;
1077 }
1078 
1079 //
1080 // Private functions
1081 //
1082 
1083 // Returns the number of lines visible in the viewport
1084 LineNumber AbstractLogView::getNbVisibleLines() const
1085 {
1086     return static_cast<LineNumber>( viewport()->height() / charHeight_ + 1 );
1087 }
1088 
1089 // Returns the number of columns visible in the viewport
1090 int AbstractLogView::getNbVisibleCols() const
1091 {
1092     return ( viewport()->width() - leftMarginPx_ ) / charWidth_ + 1;
1093 }
1094 
1095 // Converts the mouse x, y coordinates to the line number in the file
1096 int AbstractLogView::convertCoordToLine(int yPos) const
1097 {
1098     int line = firstLine + ( yPos + pullToFollowHeight_ ) / charHeight_;
1099 
1100     return line;
1101 }
1102 
1103 // Converts the mouse x, y coordinates to the char coordinates (in the file)
1104 // This function ensure the pos exists in the file.
1105 QPoint AbstractLogView::convertCoordToFilePos( const QPoint& pos ) const
1106 {
1107     int line = firstLine + ( pos.y() + pullToFollowHeight_ ) / charHeight_;
1108     if ( line >= logData->getNbLine() )
1109         line = logData->getNbLine() - 1;
1110     if ( line < 0 )
1111         line = 0;
1112 
1113     // Determine column in screen space and convert it to file space
1114     int column = firstCol + ( pos.x() - leftMarginPx_ ) / charWidth_;
1115 
1116     QString this_line = logData->getExpandedLineString( line );
1117     const int length = this_line.length();
1118 
1119     if ( column >= length )
1120         column = length - 1;
1121     if ( column < 0 )
1122         column = 0;
1123 
1124     LOG(logDEBUG4) << "AbstractLogView::convertCoordToFilePos col="
1125         << column << " line=" << line;
1126     QPoint point( column, line );
1127 
1128     return point;
1129 }
1130 
1131 // Makes the widget adjust itself to display the passed line.
1132 // Doing so, it will throw itself a scrollContents event.
1133 void AbstractLogView::displayLine( LineNumber line )
1134 {
1135     // If the line is already the screen
1136     if ( ( line >= firstLine ) &&
1137          ( line < ( firstLine + getNbVisibleLines() ) ) ) {
1138         // Invalidate our cache
1139         textAreaCache_.invalid_ = true;
1140 
1141         // ... don't scroll and just repaint
1142         update();
1143     } else {
1144         jumpToLine( line );
1145     }
1146 }
1147 
1148 // Move the selection up and down by the passed number of lines
1149 void AbstractLogView::moveSelection( int delta )
1150 {
1151     LOG(logDEBUG) << "AbstractLogView::moveSelection delta=" << delta;
1152 
1153     QList<int> selection = selection_.getLines();
1154     int new_line;
1155 
1156     // If nothing is selected, do as if line -1 was.
1157     if ( selection.isEmpty() )
1158         selection.append( -1 );
1159 
1160     if ( delta < 0 )
1161         new_line = selection.first() + delta;
1162     else
1163         new_line = selection.last() + delta;
1164 
1165     if ( new_line < 0 )
1166         new_line = 0;
1167     else if ( new_line >= logData->getNbLine() )
1168         new_line = logData->getNbLine() - 1;
1169 
1170     // Select and display the new line
1171     selection_.selectLine( new_line );
1172     displayLine( new_line );
1173     emit updateLineNumber( new_line );
1174 }
1175 
1176 // Make the start of the lines visible
1177 void AbstractLogView::jumpToStartOfLine()
1178 {
1179     horizontalScrollBar()->setValue( 0 );
1180 }
1181 
1182 // Make the end of the lines in the selection visible
1183 void AbstractLogView::jumpToEndOfLine()
1184 {
1185     QList<int> selection = selection_.getLines();
1186 
1187     // Search the longest line in the selection
1188     int max_length = 0;
1189     foreach ( int line, selection ) {
1190         int length = logData->getLineLength( line );
1191         if ( length > max_length )
1192             max_length = length;
1193     }
1194 
1195     horizontalScrollBar()->setValue( max_length - getNbVisibleCols() );
1196 }
1197 
1198 // Make the end of the lines on the screen visible
1199 void AbstractLogView::jumpToRightOfScreen()
1200 {
1201     QList<int> selection = selection_.getLines();
1202 
1203     // Search the longest line on screen
1204     int max_length = 0;
1205     for ( auto i = firstLine; i <= ( firstLine + getNbVisibleLines() ); i++ ) {
1206         int length = logData->getLineLength( i );
1207         if ( length > max_length )
1208             max_length = length;
1209     }
1210 
1211     horizontalScrollBar()->setValue( max_length - getNbVisibleCols() );
1212 }
1213 
1214 // Jump to the first line
1215 void AbstractLogView::jumpToTop()
1216 {
1217     // This will also trigger a scrollContents event
1218     verticalScrollBar()->setValue( 0 );
1219     update();       // in case the screen hasn't moved
1220 }
1221 
1222 // Jump to the last line
1223 void AbstractLogView::jumpToBottom()
1224 {
1225     const int new_top_line =
1226         qMax( logData->getNbLine() - getNbVisibleLines() + 1, 0LL );
1227 
1228     // This will also trigger a scrollContents event
1229     verticalScrollBar()->setValue( new_top_line );
1230     update();       // in case the screen hasn't moved
1231 }
1232 
1233 // Returns whether the character passed is a 'word' character
1234 inline bool AbstractLogView::isCharWord( char c )
1235 {
1236     if ( ( ( c >= 'A' ) && ( c <= 'Z' ) ) ||
1237          ( ( c >= 'a' ) && ( c <= 'z' ) ) ||
1238          ( ( c >= '0' ) && ( c <= '9' ) ) ||
1239          ( ( c == '_' ) ) )
1240         return true;
1241     else
1242         return false;
1243 }
1244 
1245 // Select the word under the given position
1246 void AbstractLogView::selectWordAtPosition( const QPoint& pos )
1247 {
1248     const int x = pos.x();
1249     const QString line = logData->getExpandedLineString( pos.y() );
1250 
1251     if ( isCharWord( line[x].toLatin1() ) ) {
1252         // Search backward for the first character in the word
1253         int currentPos = x;
1254         for ( ; currentPos > 0; currentPos-- )
1255             if ( ! isCharWord( line[currentPos].toLatin1() ) )
1256                 break;
1257         // Exclude the first char of the line if needed
1258         if ( ! isCharWord( line[currentPos].toLatin1() ) )
1259             currentPos++;
1260         int start = currentPos;
1261 
1262         // Now search for the end
1263         currentPos = x;
1264         for ( ; currentPos < line.length() - 1; currentPos++ )
1265             if ( ! isCharWord( line[currentPos].toLatin1() ) )
1266                 break;
1267         // Exclude the last char of the line if needed
1268         if ( ! isCharWord( line[currentPos].toLatin1() ) )
1269             currentPos--;
1270         int end = currentPos;
1271 
1272         selection_.selectPortion( pos.y(), start, end );
1273         updateGlobalSelection();
1274         update();
1275     }
1276 }
1277 
1278 // Update the system global (middle click) selection (X11 only)
1279 void AbstractLogView::updateGlobalSelection()
1280 {
1281     static QClipboard* const clipboard = QApplication::clipboard();
1282 
1283     // Updating it only for "non-trivial" (range or portion) selections
1284     if ( ! selection_.isSingleLine() )
1285         clipboard->setText( selection_.getSelectedText( logData ),
1286                 QClipboard::Selection );
1287 }
1288 
1289 // Create the pop-up menu
1290 void AbstractLogView::createMenu()
1291 {
1292     copyAction_ = new QAction( tr("&Copy"), this );
1293     // No text as this action title depends on the type of selection
1294     connect( copyAction_, SIGNAL(triggered()), this, SLOT(copy()) );
1295 
1296     // For '#' and '*', shortcuts doesn't seem to work but
1297     // at least it displays them in the menu, we manually handle those keys
1298     // as keys event anyway (in keyPressEvent).
1299     findNextAction_ = new QAction(tr("Find &next"), this);
1300     findNextAction_->setShortcut( Qt::Key_Asterisk );
1301     findNextAction_->setStatusTip( tr("Find the next occurence") );
1302     connect( findNextAction_, SIGNAL(triggered()),
1303             this, SLOT( findNextSelected() ) );
1304 
1305     findPreviousAction_ = new QAction( tr("Find &previous"), this );
1306     findPreviousAction_->setShortcut( tr("#")  );
1307     findPreviousAction_->setStatusTip( tr("Find the previous occurence") );
1308     connect( findPreviousAction_, SIGNAL(triggered()),
1309             this, SLOT( findPreviousSelected() ) );
1310 
1311     addToSearchAction_ = new QAction( tr("&Add to search"), this );
1312     addToSearchAction_->setStatusTip(
1313             tr("Add the selection to the current search") );
1314     connect( addToSearchAction_, SIGNAL( triggered() ),
1315             this, SLOT( addToSearch() ) );
1316 
1317     popupMenu_ = new QMenu( this );
1318     popupMenu_->addAction( copyAction_ );
1319     popupMenu_->addSeparator();
1320     popupMenu_->addAction( findNextAction_ );
1321     popupMenu_->addAction( findPreviousAction_ );
1322     popupMenu_->addAction( addToSearchAction_ );
1323 }
1324 
1325 void AbstractLogView::considerMouseHovering( int x_pos, int y_pos )
1326 {
1327     int line = convertCoordToLine( y_pos );
1328     if ( ( x_pos < leftMarginPx_ )
1329             && ( line >= 0 )
1330             && ( line < logData->getNbLine() ) ) {
1331         // Mouse moved in the margin, send event up
1332         // (possibly to highlight the overview)
1333         if ( line != lastHoveredLine_ ) {
1334             LOG(logDEBUG) << "Mouse moved in margin line: " << line;
1335             emit mouseHoveredOverLine( line );
1336             lastHoveredLine_ = line;
1337         }
1338     }
1339     else {
1340         if ( lastHoveredLine_ != -1 ) {
1341             emit mouseLeftHoveringZone();
1342             lastHoveredLine_ = -1;
1343         }
1344     }
1345 }
1346 
1347 void AbstractLogView::updateScrollBars()
1348 {
1349     verticalScrollBar()->setRange( 0, std::max( 0LL,
1350             logData->getNbLine() - getNbVisibleLines() ) );
1351 
1352     const int hScrollMaxValue = std::max( 0,
1353             logData->getMaxLength() - getNbVisibleCols() + 1 );
1354     horizontalScrollBar()->setRange( 0, hScrollMaxValue );
1355 }
1356 
1357 void AbstractLogView::drawTextArea( QPaintDevice* paint_device, int32_t delta_y )
1358 {
1359     // LOG( logDEBUG ) << "devicePixelRatio: " << viewport()->devicePixelRatio();
1360     // LOG( logDEBUG ) << "viewport size: " << viewport()->size().width();
1361     // LOG( logDEBUG ) << "pixmap size: " << textPixmap.width();
1362     // Repaint the viewport
1363     QPainter painter( paint_device );
1364     // LOG( logDEBUG ) << "font: " << viewport()->font().family().toStdString();
1365     // LOG( logDEBUG ) << "font painter: " << painter.font().family().toStdString();
1366 
1367     painter.setFont( this->font() );
1368 
1369     const int fontHeight = charHeight_;
1370     const int fontAscent = painter.fontMetrics().ascent();
1371     const int nbCols = getNbVisibleCols();
1372     const int paintDeviceHeight = paint_device->height() / viewport()->devicePixelRatio();
1373     const int paintDeviceWidth = paint_device->width() / viewport()->devicePixelRatio();
1374     const QPalette& palette = viewport()->palette();
1375     std::shared_ptr<const FilterSet> filterSet =
1376         Persistent<FilterSet>( "filterSet" );
1377     QColor foreColor, backColor;
1378 
1379     static const QBrush normalBulletBrush = QBrush( Qt::white );
1380     static const QBrush matchBulletBrush = QBrush( Qt::red );
1381     static const QBrush markBrush = QBrush( "dodgerblue" );
1382 
1383     static const int SEPARATOR_WIDTH = 1;
1384     static const qreal BULLET_AREA_WIDTH = 11;
1385     static const int CONTENT_MARGIN_WIDTH = 1;
1386     static const int LINE_NUMBER_PADDING = 3;
1387 
1388     // First check the lines to be drawn are within range (might not be the case if
1389     // the file has just changed)
1390     const int64_t lines_in_file = logData->getNbLine();
1391 
1392     if ( firstLine > lines_in_file )
1393         firstLine = lines_in_file - 1;
1394 
1395     const int64_t nbLines = std::min(
1396             static_cast<int64_t>( getNbVisibleLines() ), lines_in_file - firstLine );
1397 
1398     const int bottomOfTextPx = nbLines * fontHeight;
1399 
1400     LOG(logDEBUG) << "drawing lines from " << firstLine << " (" << nbLines << " lines)";
1401     LOG(logDEBUG) << "bottomOfTextPx: " << bottomOfTextPx;
1402     LOG(logDEBUG) << "Height: " << paintDeviceHeight;
1403 
1404     // Lines to write
1405     const QStringList lines = logData->getExpandedLines( firstLine, nbLines );
1406 
1407     // First draw the bullet left margin
1408     painter.setPen(palette.color(QPalette::Text));
1409     painter.fillRect( 0, 0,
1410                       BULLET_AREA_WIDTH, paintDeviceHeight,
1411                       Qt::darkGray );
1412 
1413     // Column at which the content should start (pixels)
1414     qreal contentStartPosX = BULLET_AREA_WIDTH + SEPARATOR_WIDTH;
1415 
1416     // This is also the bullet zone width, used for marking clicks
1417     bulletZoneWidthPx_ = contentStartPosX;
1418 
1419     // Update the length of line numbers
1420     const int nbDigitsInLineNumber = countDigits( maxDisplayLineNumber() );
1421 
1422     // Draw the line numbers area
1423     int lineNumberAreaStartX = 0;
1424     if ( lineNumbersVisible_ ) {
1425         int lineNumberWidth = charWidth_ * nbDigitsInLineNumber;
1426         int lineNumberAreaWidth =
1427             2 * LINE_NUMBER_PADDING + lineNumberWidth;
1428         lineNumberAreaStartX = contentStartPosX;
1429 
1430         painter.setPen(palette.color(QPalette::Text));
1431         /* Not sure if it looks good...
1432         painter.drawLine( contentStartPosX + lineNumberAreaWidth,
1433                           0,
1434                           contentStartPosX + lineNumberAreaWidth,
1435                           viewport()->height() );
1436         */
1437         painter.fillRect( contentStartPosX - SEPARATOR_WIDTH, 0,
1438                           lineNumberAreaWidth + SEPARATOR_WIDTH, paintDeviceHeight,
1439                           Qt::lightGray );
1440 
1441         // Update for drawing the actual text
1442         contentStartPosX += lineNumberAreaWidth;
1443     }
1444     else {
1445         painter.fillRect( contentStartPosX - SEPARATOR_WIDTH, 0,
1446                           SEPARATOR_WIDTH + 1, paintDeviceHeight,
1447                           Qt::lightGray );
1448         // contentStartPosX += SEPARATOR_WIDTH;
1449     }
1450 
1451     painter.drawLine( BULLET_AREA_WIDTH, 0,
1452                       BULLET_AREA_WIDTH, paintDeviceHeight - 1 );
1453 
1454     // This is the total width of the 'margin' (including line number if any)
1455     // used for mouse calculation etc...
1456     leftMarginPx_ = contentStartPosX + SEPARATOR_WIDTH;
1457 
1458     // Then draw each line
1459     for (int i = 0; i < nbLines; i++) {
1460         const LineNumber line_index = i + firstLine;
1461 
1462         // Position in pixel of the base line of the line to print
1463         const int yPos = i * fontHeight;
1464         const int xPos = contentStartPosX + CONTENT_MARGIN_WIDTH;
1465 
1466         // string to print, cut to fit the length and position of the view
1467         const QString line = lines[i];
1468         const QString cutLine = line.mid( firstCol, nbCols );
1469 
1470         if ( selection_.isLineSelected( line_index ) ) {
1471             // Reverse the selected line
1472             foreColor = palette.color( QPalette::HighlightedText );
1473             backColor = palette.color( QPalette::Highlight );
1474             painter.setPen(palette.color(QPalette::Text));
1475         }
1476         else if ( filterSet->matchLine( logData->getLineString( line_index ),
1477                     &foreColor, &backColor ) ) {
1478             // Apply a filter to the line
1479         }
1480         else {
1481             // Use the default colors
1482             foreColor = palette.color( QPalette::Text );
1483             backColor = palette.color( QPalette::Base );
1484         }
1485 
1486         // Is there something selected in the line?
1487         int sel_start, sel_end;
1488         bool isSelection =
1489             selection_.getPortionForLine( line_index, &sel_start, &sel_end );
1490         // Has the line got elements to be highlighted
1491         QList<QuickFindMatch> qfMatchList;
1492         bool isMatch =
1493             quickFindPattern_->matchLine( line, qfMatchList );
1494 
1495         if ( isSelection || isMatch ) {
1496             // We use the LineDrawer and its chunks because the
1497             // line has to be somehow highlighted
1498             LineDrawer lineDrawer( backColor );
1499 
1500             // First we create a list of chunks with the highlights
1501             QList<LineChunk> chunkList;
1502             int column = 0; // Current column in line space
1503             foreach( const QuickFindMatch match, qfMatchList ) {
1504                 int start = match.startColumn() - firstCol;
1505                 int end = start + match.length();
1506                 // Ignore matches that are *completely* outside view area
1507                 if ( ( start < 0 && end < 0 ) || start >= nbCols )
1508                     continue;
1509                 if ( start > column )
1510                     chunkList << LineChunk( column, start - 1, LineChunk::Normal );
1511                 column = qMin( start + match.length() - 1, nbCols );
1512                 chunkList << LineChunk( qMax( start, 0 ), column,
1513                         LineChunk::Highlighted );
1514                 column++;
1515             }
1516             if ( column <= cutLine.length() - 1 )
1517                 chunkList << LineChunk( column, cutLine.length() - 1, LineChunk::Normal );
1518 
1519             // Then we add the selection if needed
1520             QList<LineChunk> newChunkList;
1521             if ( isSelection ) {
1522                 sel_start -= firstCol; // coord in line space
1523                 sel_end   -= firstCol;
1524 
1525                 foreach ( const LineChunk chunk, chunkList ) {
1526                     newChunkList << chunk.select( sel_start, sel_end );
1527                 }
1528             }
1529             else
1530                 newChunkList = chunkList;
1531 
1532             foreach ( const LineChunk chunk, newChunkList ) {
1533                 // Select the colours
1534                 QColor fore;
1535                 QColor back;
1536                 switch ( chunk.type() ) {
1537                     case LineChunk::Normal:
1538                         fore = foreColor;
1539                         back = backColor;
1540                         break;
1541                     case LineChunk::Highlighted:
1542                         fore = QColor( "black" );
1543                         back = QColor( "yellow" );
1544                         // fore = highlightForeColor;
1545                         // back = highlightBackColor;
1546                         break;
1547                     case LineChunk::Selected:
1548                         fore = palette.color( QPalette::HighlightedText ),
1549                              back = palette.color( QPalette::Highlight );
1550                         break;
1551                 }
1552                 lineDrawer.addChunk ( chunk, fore, back );
1553             }
1554 
1555             lineDrawer.draw( painter, xPos, yPos,
1556                     viewport()->width(), cutLine,
1557                     CONTENT_MARGIN_WIDTH );
1558         }
1559         else {
1560             // Nothing to be highlighted, we print the whole line!
1561             painter.fillRect( xPos - CONTENT_MARGIN_WIDTH, yPos,
1562                     viewport()->width(), fontHeight, backColor );
1563             // (the rectangle is extended on the left to cover the small
1564             // margin, it looks better (LineDrawer does the same) )
1565             painter.setPen( foreColor );
1566             painter.drawText( xPos, yPos + fontAscent, cutLine );
1567         }
1568 
1569         // Then draw the bullet
1570         painter.setPen( palette.color( QPalette::Text ) );
1571         const qreal circleSize = 3;
1572         const qreal arrowHeight = 4;
1573         const qreal middleXLine = BULLET_AREA_WIDTH / 2;
1574         const qreal middleYLine = yPos + (fontHeight / 2);
1575 
1576         const LineType line_type = lineType( line_index );
1577         if ( line_type == Marked ) {
1578             // A pretty arrow if the line is marked
1579             const QPointF points[7] = {
1580                 QPointF(1, middleYLine - 2),
1581                 QPointF(middleXLine, middleYLine - 2),
1582                 QPointF(middleXLine, middleYLine - arrowHeight),
1583                 QPointF(BULLET_AREA_WIDTH - 1, middleYLine),
1584                 QPointF(middleXLine, middleYLine + arrowHeight),
1585                 QPointF(middleXLine, middleYLine + 2),
1586                 QPointF(1, middleYLine + 2 ),
1587             };
1588 
1589             painter.setBrush( markBrush );
1590             painter.drawPolygon( points, 7 );
1591         }
1592         else {
1593             // For pretty circles
1594             painter.setRenderHint( QPainter::Antialiasing );
1595 
1596             if ( lineType( line_index ) == Match )
1597                 painter.setBrush( matchBulletBrush );
1598             else
1599                 painter.setBrush( normalBulletBrush );
1600             painter.drawEllipse( middleXLine - circleSize,
1601                     middleYLine - circleSize,
1602                     circleSize * 2, circleSize * 2 );
1603         }
1604 
1605         // Draw the line number
1606         if ( lineNumbersVisible_ ) {
1607             static const QString lineNumberFormat( "%1" );
1608             const QString& lineNumberStr =
1609                 lineNumberFormat.arg( displayLineNumber( line_index ),
1610                         nbDigitsInLineNumber );
1611             painter.setPen( palette.color( QPalette::Text ) );
1612             painter.drawText( lineNumberAreaStartX + LINE_NUMBER_PADDING,
1613                     yPos + fontAscent, lineNumberStr );
1614         }
1615     } // For each line
1616 
1617     if ( bottomOfTextPx < paintDeviceHeight ) {
1618         // The lines don't cover the whole device
1619         painter.fillRect( contentStartPosX, bottomOfTextPx,
1620                 paintDeviceWidth - contentStartPosX,
1621                 paintDeviceHeight, palette.color( QPalette::Window ) );
1622     }
1623 }
1624 
1625 // Draw the "pull to follow" bar and return a pixmap.
1626 // The width is passed in "logic" pixels.
1627 QPixmap AbstractLogView::drawPullToFollowBar( int width, float pixel_ratio )
1628 {
1629     static constexpr int barWidth = 40;
1630     QPixmap pixmap ( static_cast<float>( width ) * pixel_ratio, barWidth * 6.0 );
1631     pixmap.setDevicePixelRatio( pixel_ratio );
1632     pixmap.fill( this->palette().color( this->backgroundRole() ) );
1633     const int nbBars = width / (barWidth * 2) + 1;
1634 
1635     QPainter painter( &pixmap );
1636     painter.setPen( QPen( QColor( 0, 0, 0, 0 ) ) );
1637     painter.setBrush( QBrush( QColor( "lightyellow" ) ) );
1638 
1639     for ( int i = 0; i < nbBars; ++i ) {
1640         QPoint points[4] = {
1641             { (i*2+1)*barWidth, 0 },
1642             { 0, (i*2+1)*barWidth },
1643             { 0, (i+1)*2*barWidth },
1644             { (i+1)*2*barWidth, 0 }
1645         };
1646         painter.drawConvexPolygon( points, 4 );
1647     }
1648 
1649     return pixmap;
1650 }
1651 
1652 void AbstractLogView::disableFollow()
1653 {
1654     emit followModeChanged( false );
1655     followElasticHook_.hook( false );
1656 }
1657 
1658 namespace {
1659 
1660 // Convert the length of the pull to follow bar to pixels
1661 int mapPullToFollowLength( int length )
1662 {
1663     return length / 14;
1664 }
1665 
1666 };
1667