xref: /glogg/src/marks.h (revision c633ced33b4f2c2cf77c0c80a15fa73a7f13ad9f)
1 /*
2  * Copyright (C) 2011 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 MARKS_H
21 #define MARKS_H
22 
23 #include <QChar>
24 #include <QList>
25 
26 // Class encapsulating a single mark
27 // Contains the line number the mark is identifying.
28 class Mark {
29   public:
30     Mark( int line ) { lineNumber_ = line; };
31 
32     // Accessors
33     int lineNumber() const { return lineNumber_; }
34 
35   private:
36     int lineNumber_;
37 };
38 
39 // A list of marks, i.e. line numbers optionally associated to an
40 // identifying character.
41 class Marks {
42   public:
43     // Create an empty Marks
44     Marks();
45 
46     // Add a mark at the given line, optionally identified by the given char
47     // If a mark for this char already exist, the previous one is replaced.
48     // It will happily add marks anywhere, even at stupid indexes.
49     void addMark( qint64 line, QChar mark = QChar() );
50     // Get the (unique) mark identified by the passed char.
51     qint64 getMark( QChar mark ) const;
52     // Returns wheither the passed line has a mark on it.
53     bool isLineMarked( qint64 line ) const;
54     // Delete the mark identified by the passed char.
55     void deleteMark( QChar mark );
56     // Delete the mark present on the passed line or do nothing if there is
57     // none.
58     void deleteMark( qint64 line );
59     // Get the line marked identified by the index (in this list) passed.
60     qint64 getLineMarkedByIndex( int index ) const
61     { return marks_[index].lineNumber(); }
62     // Return the total number of marks
63     int size() const
64     { return marks_.size(); }
65     // Completely clear the marks list.
66     void clear();
67 
68     // Iterator
69     // Provide a const_iterator for the client to iterate through the marks.
70     class const_iterator {
71       public:
72         const_iterator( QList<Mark>::const_iterator iter )
73         { internal_iter_ = iter; }
74         const_iterator( const const_iterator& original )
75         { internal_iter_ = original.internal_iter_; }
76         const Mark& operator*()
77         { return *internal_iter_; }
78         const Mark* operator->()
79         { return &(*internal_iter_); }
80         bool operator!=( const const_iterator& other ) const
81         { return ( internal_iter_ != other.internal_iter_ ); }
82         const_iterator& operator++()
83         { ++internal_iter_ ; return *this; }
84 
85       private:
86         QList<Mark>::const_iterator internal_iter_;
87     };
88 
89     const_iterator begin() const
90     { return const_iterator( marks_.begin() ); }
91     const_iterator end() const
92     { return const_iterator( marks_.end() ); }
93 
94   private:
95     // List of marks.
96     QList<Mark> marks_;
97 };
98 
99 #endif
100