xref: /glogg/src/overview.cpp (revision 821cac888d515a4e41b5d4ba4130c56db4463501)
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         for ( int i = 0; i < logFilteredData_->getNbLine(); i++ ) {
118             LogFilteredData::FilteredLineType line_type =
119                 logFilteredData_->filteredLineTypeByIndex( i );
120             int line = (int) logFilteredData_->getMatchingLineNumber( i );
121             int position = (int)( (qint64)line * height_ / linesInFile_ );
122             if ( line_type == LogFilteredData::Match ) {
123                 if ( ( ! matchLines_.isEmpty() ) && matchLines_.last().position() == position ) {
124                     // If the line is already there, we increase its weight
125                     matchLines_.last().load();
126                 }
127                 else {
128                     // If not we just add it
129                     matchLines_.append( WeightedLine( position ) );
130                 }
131             }
132             else {
133                 if ( ( ! markLines_.isEmpty() ) && markLines_.last().position() == position ) {
134                     // If the line is already there, we increase its weight
135                     markLines_.last().load();
136                 }
137                 else {
138                     // If not we just add it
139                     markLines_.append( WeightedLine( position ) );
140                 }
141             }
142         }
143     }
144     else
145         LOG(logERROR) << "Overview::recalculatesLines: logFilteredData_ == NULL";
146 
147     dirty_ = false;
148 }
149