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