1*bb02e0acSNicolas Bonnefon /* 2*bb02e0acSNicolas Bonnefon * Copyright (C) 2011, 2012 Nicolas Bonnefon and other contributors 3*bb02e0acSNicolas Bonnefon * 4*bb02e0acSNicolas Bonnefon * This file is part of glogg. 5*bb02e0acSNicolas Bonnefon * 6*bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7*bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8*bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9*bb02e0acSNicolas Bonnefon * (at your option) any later version. 10*bb02e0acSNicolas Bonnefon * 11*bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12*bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15*bb02e0acSNicolas Bonnefon * 16*bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17*bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18*bb02e0acSNicolas Bonnefon */ 19*bb02e0acSNicolas Bonnefon 20*bb02e0acSNicolas Bonnefon // This file implements the Overview class. 21*bb02e0acSNicolas Bonnefon // It provides support for drawing the match overview sidebar but 22*bb02e0acSNicolas Bonnefon // the actual drawing is done in AbstractLogView which uses this class. 23*bb02e0acSNicolas Bonnefon 24*bb02e0acSNicolas Bonnefon #include "log.h" 25*bb02e0acSNicolas Bonnefon 26*bb02e0acSNicolas Bonnefon #include "data/logfiltereddata.h" 27*bb02e0acSNicolas Bonnefon 28*bb02e0acSNicolas Bonnefon #include "overview.h" 29*bb02e0acSNicolas Bonnefon 30*bb02e0acSNicolas Bonnefon Overview::Overview() : matchLines_(), markLines_() 31*bb02e0acSNicolas Bonnefon { 32*bb02e0acSNicolas Bonnefon logFilteredData_ = NULL; 33*bb02e0acSNicolas Bonnefon linesInFile_ = 0; 34*bb02e0acSNicolas Bonnefon topLine_ = 0; 35*bb02e0acSNicolas Bonnefon nbLines_ = 0; 36*bb02e0acSNicolas Bonnefon height_ = 0; 37*bb02e0acSNicolas Bonnefon dirty_ = true; 38*bb02e0acSNicolas Bonnefon visible_ = false; 39*bb02e0acSNicolas Bonnefon } 40*bb02e0acSNicolas Bonnefon 41*bb02e0acSNicolas Bonnefon Overview::~Overview() 42*bb02e0acSNicolas Bonnefon { 43*bb02e0acSNicolas Bonnefon } 44*bb02e0acSNicolas Bonnefon 45*bb02e0acSNicolas Bonnefon void Overview::setFilteredData( const LogFilteredData* logFilteredData ) 46*bb02e0acSNicolas Bonnefon { 47*bb02e0acSNicolas Bonnefon logFilteredData_ = logFilteredData; 48*bb02e0acSNicolas Bonnefon } 49*bb02e0acSNicolas Bonnefon 50*bb02e0acSNicolas Bonnefon void Overview::updateData( int totalNbLine ) 51*bb02e0acSNicolas Bonnefon { 52*bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "OverviewWidget::updateData " << totalNbLine; 53*bb02e0acSNicolas Bonnefon 54*bb02e0acSNicolas Bonnefon linesInFile_ = totalNbLine; 55*bb02e0acSNicolas Bonnefon dirty_ = true; 56*bb02e0acSNicolas Bonnefon } 57*bb02e0acSNicolas Bonnefon 58*bb02e0acSNicolas Bonnefon void Overview::updateView( int height ) 59*bb02e0acSNicolas Bonnefon { 60*bb02e0acSNicolas Bonnefon // We don't touch the cache if the height hasn't changed 61*bb02e0acSNicolas Bonnefon if ( ( height != height_ ) || ( dirty_ == true ) ) { 62*bb02e0acSNicolas Bonnefon height_ = height; 63*bb02e0acSNicolas Bonnefon 64*bb02e0acSNicolas Bonnefon recalculatesLines(); 65*bb02e0acSNicolas Bonnefon } 66*bb02e0acSNicolas Bonnefon } 67*bb02e0acSNicolas Bonnefon 68*bb02e0acSNicolas Bonnefon const QVector<Overview::WeightedLine>* Overview::getMatchLines() const 69*bb02e0acSNicolas Bonnefon { 70*bb02e0acSNicolas Bonnefon return &matchLines_; 71*bb02e0acSNicolas Bonnefon } 72*bb02e0acSNicolas Bonnefon 73*bb02e0acSNicolas Bonnefon const QVector<Overview::WeightedLine>* Overview::getMarkLines() const 74*bb02e0acSNicolas Bonnefon { 75*bb02e0acSNicolas Bonnefon return &markLines_; 76*bb02e0acSNicolas Bonnefon } 77*bb02e0acSNicolas Bonnefon 78*bb02e0acSNicolas Bonnefon std::pair<int,int> Overview::getViewLines() const 79*bb02e0acSNicolas Bonnefon { 80*bb02e0acSNicolas Bonnefon int top = 0; 81*bb02e0acSNicolas Bonnefon int bottom = height_ - 1; 82*bb02e0acSNicolas Bonnefon 83*bb02e0acSNicolas Bonnefon if ( linesInFile_ > 0 ) { 84*bb02e0acSNicolas Bonnefon top = (int)((qint64)topLine_ * height_ / linesInFile_); 85*bb02e0acSNicolas Bonnefon bottom = (int)((qint64)top + nbLines_ * height_ / linesInFile_); 86*bb02e0acSNicolas Bonnefon } 87*bb02e0acSNicolas Bonnefon 88*bb02e0acSNicolas Bonnefon return std::pair<int,int>(top, bottom); 89*bb02e0acSNicolas Bonnefon } 90*bb02e0acSNicolas Bonnefon 91*bb02e0acSNicolas Bonnefon int Overview::fileLineFromY( int position ) const 92*bb02e0acSNicolas Bonnefon { 93*bb02e0acSNicolas Bonnefon int line = (int)((qint64)position * linesInFile_ / height_); 94*bb02e0acSNicolas Bonnefon 95*bb02e0acSNicolas Bonnefon return line; 96*bb02e0acSNicolas Bonnefon } 97*bb02e0acSNicolas Bonnefon 98*bb02e0acSNicolas Bonnefon int Overview::yFromFileLine( int file_line ) const 99*bb02e0acSNicolas Bonnefon { 100*bb02e0acSNicolas Bonnefon int position = 0; 101*bb02e0acSNicolas Bonnefon 102*bb02e0acSNicolas Bonnefon if ( linesInFile_ > 0 ) 103*bb02e0acSNicolas Bonnefon position = (int)((qint64)file_line * height_ / linesInFile_); 104*bb02e0acSNicolas Bonnefon 105*bb02e0acSNicolas Bonnefon return position; 106*bb02e0acSNicolas Bonnefon } 107*bb02e0acSNicolas Bonnefon 108*bb02e0acSNicolas Bonnefon // Update the internal cache 109*bb02e0acSNicolas Bonnefon void Overview::recalculatesLines() 110*bb02e0acSNicolas Bonnefon { 111*bb02e0acSNicolas Bonnefon LOG(logDEBUG) << "OverviewWidget::recalculatesLines"; 112*bb02e0acSNicolas Bonnefon 113*bb02e0acSNicolas Bonnefon if ( logFilteredData_ != NULL ) { 114*bb02e0acSNicolas Bonnefon matchLines_.clear(); 115*bb02e0acSNicolas Bonnefon markLines_.clear(); 116*bb02e0acSNicolas Bonnefon 117*bb02e0acSNicolas Bonnefon for ( int i = 0; i < logFilteredData_->getNbLine(); i++ ) { 118*bb02e0acSNicolas Bonnefon LogFilteredData::FilteredLineType line_type = 119*bb02e0acSNicolas Bonnefon logFilteredData_->filteredLineTypeByIndex( i ); 120*bb02e0acSNicolas Bonnefon int line = (int) logFilteredData_->getMatchingLineNumber( i ); 121*bb02e0acSNicolas Bonnefon int position = (int)( (qint64)line * height_ / linesInFile_ ); 122*bb02e0acSNicolas Bonnefon if ( line_type == LogFilteredData::Match ) { 123*bb02e0acSNicolas Bonnefon if ( ( ! matchLines_.isEmpty() ) && matchLines_.last().position() == position ) { 124*bb02e0acSNicolas Bonnefon // If the line is already there, we increase its weight 125*bb02e0acSNicolas Bonnefon matchLines_.last().load(); 126*bb02e0acSNicolas Bonnefon } 127*bb02e0acSNicolas Bonnefon else { 128*bb02e0acSNicolas Bonnefon // If not we just add it 129*bb02e0acSNicolas Bonnefon matchLines_.append( WeightedLine( position ) ); 130*bb02e0acSNicolas Bonnefon } 131*bb02e0acSNicolas Bonnefon } 132*bb02e0acSNicolas Bonnefon else { 133*bb02e0acSNicolas Bonnefon if ( ( ! markLines_.isEmpty() ) && markLines_.last().position() == position ) { 134*bb02e0acSNicolas Bonnefon // If the line is already there, we increase its weight 135*bb02e0acSNicolas Bonnefon markLines_.last().load(); 136*bb02e0acSNicolas Bonnefon } 137*bb02e0acSNicolas Bonnefon else { 138*bb02e0acSNicolas Bonnefon // If not we just add it 139*bb02e0acSNicolas Bonnefon markLines_.append( WeightedLine( position ) ); 140*bb02e0acSNicolas Bonnefon } 141*bb02e0acSNicolas Bonnefon } 142*bb02e0acSNicolas Bonnefon } 143*bb02e0acSNicolas Bonnefon } 144*bb02e0acSNicolas Bonnefon else 145*bb02e0acSNicolas Bonnefon LOG(logERROR) << "Overview::recalculatesLines: logFilteredData_ == NULL"; 146*bb02e0acSNicolas Bonnefon 147*bb02e0acSNicolas Bonnefon dirty_ = false; 148*bb02e0acSNicolas Bonnefon } 149