xref: /glogg/src/selection.h (revision 821cac888d515a4e41b5d4ba4130c56db4463501)
1 /*
2  * Copyright (C) 2010, 2013 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 SELECTION_H
21 #define SELECTION_H
22 
23 #include <QList>
24 #include <QString>
25 
26 #include "utils.h"
27 
28 class AbstractLogData;
29 
30 class Portion
31 {
32   public:
33     Portion() { line_ = -1; }
34     Portion( int line, int start_column, int end_column )
35     { line_ = line; startColumn_ = start_column; endColumn_ = end_column; }
36 
37     int line() const { return line_; }
38     int startColumn() const { return startColumn_; }
39     int endColumn() const { return endColumn_; }
40 
41     bool isValid() const { return ( line_ != -1 ); }
42 
43   private:
44     int line_;
45     int startColumn_;
46     int endColumn_;
47 };
48 
49 // Represents a selection in an AbstractLogView
50 class Selection
51 {
52   public:
53     // Construct an empty selection
54     Selection();
55 
56     // Clear the selection
57     void clear() { selectedPartial_.line = -1; selectedLine_ = -1; };
58 
59     // Select one line
60     void selectLine( int line )
61       { selectedPartial_.line = -1; selectedRange_.startLine = -1;
62         selectedLine_ = line; };
63     // Select a portion of line (both start and end included)
64     void selectPortion( int line, int start_column, int end_column );
65     void selectPortion( Portion selection )
66       { selectPortion( selection.line(), selection.startColumn(),
67               selection.endColumn() ); }
68     // Select a range of lines (both start and end included)
69     void selectRange( int start_line, int end_line );
70 
71     // Select a range from the previously selected line or beginning
72     // of range (shift+click behaviour)
73     void selectRangeFromPrevious( int line );
74 
75     // Crop selection so that in fit in the range ending with the line passed.
76     void crop( int last_line );
77 
78     // Returns whether the selection is empty
79     bool isEmpty() const
80     { return ( selectedPartial_.line == -1 ) && ( selectedLine_ == -1 ); }
81 
82     // Returns whether the selection is a single line
83     bool isSingleLine() const { return ( selectedLine_ != -1 ); }
84 
85     // Returns whether the selection is a portion of line
86     bool isPortion() const { return ( selectedPartial_.line != -1 ); }
87 
88     // Returns whether a portion is selected or not on the passed line.
89     // If so, returns the portion position.
90     bool getPortionForLine( int line,
91             int* start_column, int* end_column ) const;
92     // Get a list of selected line(s), in order.
93     QList<int> getLines() const;
94 
95     // Returns wether the line passed is selected (entirely).
96     bool isLineSelected( int line ) const;
97 
98     // Returns the line selected or -1 if not a single line selection
99     qint64 selectedLine() const;
100 
101     // Returns the text selected from the passed AbstractLogData
102     QString getSelectedText( const AbstractLogData* logData ) const;
103 
104     // Return the position immediately after the current selection
105     // (used for searches).
106     // This is the next character or the start of the next line.
107     FilePosition getNextPosition() const;
108 
109     // Idem from the position immediately before selection.
110     FilePosition getPreviousPosition() const;
111 
112   private:
113     // Line number currently selected, or -1 if none selected
114     int selectedLine_;
115 
116     struct SelectedPartial {
117         int line;
118         int startColumn;
119         int endColumn;
120     };
121     struct SelectedRange {
122         // The limits of the range, sorted
123         int startLine;
124         int endLine;
125         // The line selected first, used for shift+click
126         int firstLine;
127     };
128     struct SelectedPartial selectedPartial_;
129     struct SelectedRange selectedRange_;
130 };
131 
132 #endif
133