xref: /glogg/src/filterset.h (revision fcf40f1761134a69881dfa6f1d28d01064374a37)
1 /*
2  * Copyright (C) 2009, 2010 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 FILTERSET_H
21 #define FILTERSET_H
22 
23 #include <QRegExp>
24 #include <QColor>
25 #include <QMetaType>
26 
27 #include "persistable.h"
28 
29 // Represents a filter, i.e. a regexp and the colors matching text
30 // should be rendered in.
31 class Filter
32 {
33   public:
34     // Construct an uninitialized Filter (when reading from a config file)
35     Filter();
36     Filter(const QString& pattern, bool ignoreCase,
37             const QString& foreColor, const QString& backColor );
38 
39     // Tests the string passed for a match, returns a value just like
40     // QRegExp::indexIn (i.e. -1 if no match)
41     int indexIn( const QString& string ) const;
42 
43     // Accessor functions
44     QString pattern() const;
45     void setPattern( const QString& pattern );
46     bool ignoreCase() const;
47     void setIgnoreCase( bool ignoreCase );
48     const QString& foreColorName() const;
49     void setForeColor( const QString& foreColorName );
50     const QString& backColorName() const;
51     void setBackColor( const QString& backColorName );
52 
53     // Operators for serialization
54     // (must be kept to migrate filters from <=0.8.2)
55     friend QDataStream& operator<<( QDataStream& out, const Filter& object );
56     friend QDataStream& operator>>( QDataStream& in, Filter& object );
57 
58     // Reads/writes the current config in the QSettings object passed
59     void saveToStorage( QSettings& settings ) const;
60     void retrieveFromStorage( QSettings& settings );
61 
62   private:
63     QRegExp regexp_;
64     QString foreColorName_;
65     QString backColorName_;
66     bool enabled_;
67 };
68 
69 // Represents an ordered set of filters to be applied to each line displayed.
70 class FilterSet : public Persistable
71 {
72   public:
73     // Construct an empty filter set
74     FilterSet();
75 
76     // Returns weither the passed line match a filter of the set,
77     // if so, it returns the fore/back colors the line should use.
78     // Ownership of the colors is transfered to the caller.
79     bool matchLine( const QString& line,
80             QColor* foreColor, QColor* backColor ) const;
81 
82     // Reads/writes the current config in the QSettings object passed
83     virtual void saveToStorage( QSettings& settings ) const;
84     virtual void retrieveFromStorage( QSettings& settings );
85 
86     // Should be private really, but I don't know how to have
87     // it recognised by QVariant then.
88     typedef QList<Filter> FilterList;
89 
90     // Operators for serialization
91     // (must be kept to migrate filters from <=0.8.2)
92     friend QDataStream& operator<<(
93             QDataStream& out, const FilterSet& object );
94     friend QDataStream& operator>>(
95             QDataStream& in, FilterSet& object );
96 
97   private:
98     static const int FILTERSET_VERSION;
99 
100     FilterList filterList;
101 
102     // To simplify this class interface, FilterDialog can access our
103     // internal structure directly.
104     friend class FiltersDialog;
105 };
106 
107 Q_DECLARE_METATYPE(Filter)
108 Q_DECLARE_METATYPE(FilterSet)
109 Q_DECLARE_METATYPE(FilterSet::FilterList)
110 
111 #endif
112