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