xref: /glogg/src/filterset.h (revision 4fb0346e73d7caa82d42531c8c8681b5eb607728)
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 <QRegularExpression>
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     bool hasMatch( const QString& string ) const;
40 
41     // Accessor functions
42     QString pattern() const;
43     void setPattern( const QString& pattern );
44     bool ignoreCase() const;
45     void setIgnoreCase( bool ignoreCase );
46     const QString& foreColorName() const;
47     void setForeColor( const QString& foreColorName );
48     const QString& backColorName() const;
49     void setBackColor( const QString& backColorName );
50 
51     // Operators for serialization
52     // (must be kept to migrate filters from <=0.8.2)
53     friend QDataStream& operator<<( QDataStream& out, const Filter& object );
54     friend QDataStream& operator>>( QDataStream& in, Filter& object );
55 
56     // Reads/writes the current config in the QSettings object passed
57     void saveToStorage( QSettings& settings ) const;
58     void retrieveFromStorage( QSettings& settings );
59 
60   private:
61     QRegularExpression regexp_;
62     QString foreColorName_;
63     QString backColorName_;
64     bool enabled_;
65 };
66 
67 // Represents an ordered set of filters to be applied to each line displayed.
68 class FilterSet : public Persistable
69 {
70   public:
71     // Construct an empty filter set
72     FilterSet();
73 
74     // Returns weither the passed line match a filter of the set,
75     // if so, it returns the fore/back colors the line should use.
76     // Ownership of the colors is transfered to the caller.
77     bool matchLine( const QString& line,
78             QColor* foreColor, QColor* backColor ) const;
79 
80     // Reads/writes the current config in the QSettings object passed
81     virtual void saveToStorage( QSettings& settings ) const;
82     virtual void retrieveFromStorage( QSettings& settings );
83 
84     // Should be private really, but I don't know how to have
85     // it recognised by QVariant then.
86     typedef QList<Filter> FilterList;
87 
88     // Operators for serialization
89     // (must be kept to migrate filters from <=0.8.2)
90     friend QDataStream& operator<<(
91             QDataStream& out, const FilterSet& object );
92     friend QDataStream& operator>>(
93             QDataStream& in, FilterSet& object );
94 
95   private:
96     static const int FILTERSET_VERSION;
97 
98     FilterList filterList;
99 
100     // To simplify this class interface, FilterDialog can access our
101     // internal structure directly.
102     friend class FiltersDialog;
103 };
104 
105 Q_DECLARE_METATYPE(Filter)
106 Q_DECLARE_METATYPE(FilterSet)
107 Q_DECLARE_METATYPE(FilterSet::FilterList)
108 
109 #endif
110