1*bb02e0acSNicolas Bonnefon /* 2*bb02e0acSNicolas Bonnefon * Copyright (C) 2011 Nicolas Bonnefon and other contributors 3*bb02e0acSNicolas Bonnefon * 4*bb02e0acSNicolas Bonnefon * This file is part of glogg. 5*bb02e0acSNicolas Bonnefon * 6*bb02e0acSNicolas Bonnefon * glogg is free software: you can redistribute it and/or modify 7*bb02e0acSNicolas Bonnefon * it under the terms of the GNU General Public License as published by 8*bb02e0acSNicolas Bonnefon * the Free Software Foundation, either version 3 of the License, or 9*bb02e0acSNicolas Bonnefon * (at your option) any later version. 10*bb02e0acSNicolas Bonnefon * 11*bb02e0acSNicolas Bonnefon * glogg is distributed in the hope that it will be useful, 12*bb02e0acSNicolas Bonnefon * but WITHOUT ANY WARRANTY; without even the implied warranty of 13*bb02e0acSNicolas Bonnefon * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*bb02e0acSNicolas Bonnefon * GNU General Public License for more details. 15*bb02e0acSNicolas Bonnefon * 16*bb02e0acSNicolas Bonnefon * You should have received a copy of the GNU General Public License 17*bb02e0acSNicolas Bonnefon * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18*bb02e0acSNicolas Bonnefon */ 19*bb02e0acSNicolas Bonnefon 20*bb02e0acSNicolas Bonnefon #ifndef MARKS_H 21*bb02e0acSNicolas Bonnefon #define MARKS_H 22*bb02e0acSNicolas Bonnefon 23*bb02e0acSNicolas Bonnefon #include <QChar> 24*bb02e0acSNicolas Bonnefon #include <QList> 25*bb02e0acSNicolas Bonnefon 26*bb02e0acSNicolas Bonnefon // Class encapsulating a single mark 27*bb02e0acSNicolas Bonnefon // Contains the line number the mark is identifying. 28*bb02e0acSNicolas Bonnefon class Mark { 29*bb02e0acSNicolas Bonnefon public: 30*bb02e0acSNicolas Bonnefon Mark( int line ) { lineNumber_ = line; }; 31*bb02e0acSNicolas Bonnefon 32*bb02e0acSNicolas Bonnefon // Accessors 33*bb02e0acSNicolas Bonnefon int lineNumber() const { return lineNumber_; } 34*bb02e0acSNicolas Bonnefon 35*bb02e0acSNicolas Bonnefon private: 36*bb02e0acSNicolas Bonnefon int lineNumber_; 37*bb02e0acSNicolas Bonnefon }; 38*bb02e0acSNicolas Bonnefon 39*bb02e0acSNicolas Bonnefon // A list of marks, i.e. line numbers optionally associated to an 40*bb02e0acSNicolas Bonnefon // identifying character. 41*bb02e0acSNicolas Bonnefon class Marks { 42*bb02e0acSNicolas Bonnefon public: 43*bb02e0acSNicolas Bonnefon // Create an empty Marks 44*bb02e0acSNicolas Bonnefon Marks(); 45*bb02e0acSNicolas Bonnefon 46*bb02e0acSNicolas Bonnefon // Add a mark at the given line, optionally identified by the given char 47*bb02e0acSNicolas Bonnefon // If a mark for this char already exist, the previous one is replaced. 48*bb02e0acSNicolas Bonnefon // It will happily add marks anywhere, even at stupid indexes. 49*bb02e0acSNicolas Bonnefon void addMark( qint64 line, QChar mark = QChar() ); 50*bb02e0acSNicolas Bonnefon // Get the (unique) mark identified by the passed char. 51*bb02e0acSNicolas Bonnefon qint64 getMark( QChar mark ) const; 52*bb02e0acSNicolas Bonnefon // Returns wheither the passed line has a mark on it. 53*bb02e0acSNicolas Bonnefon bool isLineMarked( qint64 line ) const; 54*bb02e0acSNicolas Bonnefon // Delete the mark identified by the passed char. 55*bb02e0acSNicolas Bonnefon void deleteMark( QChar mark ); 56*bb02e0acSNicolas Bonnefon // Delete the mark present on the passed line or do nothing if there is 57*bb02e0acSNicolas Bonnefon // none. 58*bb02e0acSNicolas Bonnefon void deleteMark( qint64 line ); 59*bb02e0acSNicolas Bonnefon // Get the line marked identified by the index (in this list) passed. 60*bb02e0acSNicolas Bonnefon qint64 getLineMarkedByIndex( int index ) const 61*bb02e0acSNicolas Bonnefon { return marks_[index].lineNumber(); } 62*bb02e0acSNicolas Bonnefon // Return the total number of marks 63*bb02e0acSNicolas Bonnefon int size() const 64*bb02e0acSNicolas Bonnefon { return marks_.size(); } 65*bb02e0acSNicolas Bonnefon // Completely clear the marks list. 66*bb02e0acSNicolas Bonnefon void clear(); 67*bb02e0acSNicolas Bonnefon 68*bb02e0acSNicolas Bonnefon // Iterator 69*bb02e0acSNicolas Bonnefon // Provide a const_iterator for the client to iterate through the marks. 70*bb02e0acSNicolas Bonnefon class const_iterator { 71*bb02e0acSNicolas Bonnefon public: 72*bb02e0acSNicolas Bonnefon const_iterator( QList<Mark>::const_iterator iter ) 73*bb02e0acSNicolas Bonnefon { internal_iter_ = iter; } 74*bb02e0acSNicolas Bonnefon const_iterator( const const_iterator& original ) 75*bb02e0acSNicolas Bonnefon { internal_iter_ = original.internal_iter_; } 76*bb02e0acSNicolas Bonnefon const Mark& operator*() 77*bb02e0acSNicolas Bonnefon { return *internal_iter_; } 78*bb02e0acSNicolas Bonnefon const Mark* operator->() 79*bb02e0acSNicolas Bonnefon { return &(*internal_iter_); } 80*bb02e0acSNicolas Bonnefon bool operator!=( const const_iterator& other ) const 81*bb02e0acSNicolas Bonnefon { return ( internal_iter_ != other.internal_iter_ ); } 82*bb02e0acSNicolas Bonnefon const_iterator& operator++() 83*bb02e0acSNicolas Bonnefon { ++internal_iter_ ; return *this; } 84*bb02e0acSNicolas Bonnefon 85*bb02e0acSNicolas Bonnefon private: 86*bb02e0acSNicolas Bonnefon QList<Mark>::const_iterator internal_iter_; 87*bb02e0acSNicolas Bonnefon }; 88*bb02e0acSNicolas Bonnefon 89*bb02e0acSNicolas Bonnefon const_iterator begin() const 90*bb02e0acSNicolas Bonnefon { return const_iterator( marks_.begin() ); } 91*bb02e0acSNicolas Bonnefon const_iterator end() const 92*bb02e0acSNicolas Bonnefon { return const_iterator( marks_.end() ); } 93*bb02e0acSNicolas Bonnefon 94*bb02e0acSNicolas Bonnefon private: 95*bb02e0acSNicolas Bonnefon // List of marks. 96*bb02e0acSNicolas Bonnefon QList<Mark> marks_; 97*bb02e0acSNicolas Bonnefon }; 98*bb02e0acSNicolas Bonnefon 99*bb02e0acSNicolas Bonnefon #endif 100