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 #ifndef OVERVIEW_H
21 #define OVERVIEW_H
22 
23 #include <QList>
24 #include <QVector>
25 
26 class LogFilteredData;
27 
28 // Class implementing the logic behind the matches overview bar.
29 // This class converts the matches found in a LogFilteredData in
30 // a screen dependent set of coloured lines, which is cached.
31 // This class is not a UI class, actual display is left to the client.
32 //
33 // This class is NOT thread-safe.
34 class Overview
35 {
36   public:
37     // A line with a position in pixel and a weight (darkness)
38     class WeightedLine {
39       public:
40         static const int WEIGHT_STEPS = 3;
41 
WeightedLine()42         WeightedLine() { pos_ = 0; weight_ = 0; }
43         // (Necessary for QVector)
WeightedLine(int pos)44         WeightedLine( int pos ) { pos_ = pos; weight_ = 0; }
45 
position()46         int position() const { return pos_; }
weight()47         int weight() const { return weight_; }
48 
load()49         void load() { weight_ = qMin( weight_ + 1, WEIGHT_STEPS - 1 ); }
50 
51       private:
52         int pos_;
53         int weight_;
54     };
55 
56     Overview();
57     ~Overview();
58 
59     // Associate the passed filteredData to this Overview
60     void setFilteredData( const LogFilteredData* logFilteredData );
61     // Signal the overview its attached LogFilteredData has been changed and
62     // the overview must be updated with the provided total number
63     // of line of the file.
64     void updateData( int totalNbLine );
65     // Set the visibility flag of this overview.
setVisible(bool visible)66     void setVisible( bool visible ) { visible_ = visible; dirty_ = visible; }
67 
68     // Update the current position in the file (to draw the view line)
updateCurrentPosition(int firstLine,int lastLine)69     void updateCurrentPosition( int firstLine, int lastLine )
70     { topLine_ = firstLine; nbLines_ = lastLine - firstLine; }
71 
72     // Returns weither this overview is visible.
isVisible()73     bool isVisible() { return visible_; }
74     // Signal the overview the height of the display has changed, triggering
75     // an update of its cache.
76     void updateView( int height );
77     // Returns a list of lines (between 0 and 'height') representing matches.
78     // (pointer returned is valid until next call to update*()
79     const QVector<WeightedLine>* getMatchLines() const;
80     // Returns a list of lines (between 0 and 'height') representing marks.
81     // (pointer returned is valid until next call to update*()
82     const QVector<WeightedLine>* getMarkLines() const;
83     // Return a pair of lines (between 0 and 'height') representing the current view.
84     std::pair<int,int> getViewLines() const;
85 
86     // Return the line number corresponding to the passed overview y coordinate.
87     int fileLineFromY( int y ) const;
88     // Return the y coordinate corresponding to the passed line number.
89     int yFromFileLine( int file_line ) const;
90 
91   private:
92     // List of matches associated with this Overview.
93     const LogFilteredData* logFilteredData_;
94     // Total number of lines in the file.
95     int linesInFile_;
96     // Whether the overview is visible.
97     bool visible_;
98     // First and last line currently viewed.
99     int topLine_;
100     int nbLines_;
101     // Current height of view window.
102     int height_;
103     // Does the cache (matchesLines, markLines) need to be recalculated.
104     int dirty_;
105 
106     // List of lines representing matches and marks (are shared with the client)
107     QVector<WeightedLine> matchLines_;
108     QVector<WeightedLine> markLines_;
109 
110     void recalculatesLines();
111 };
112 
113 #endif
114