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