File indexing completed on 2024-04-28 05:08:22

0001 /***************************************************************************
0002     Copyright (C) 2003-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #ifndef TELLICO_FILTER_H
0026 #define TELLICO_FILTER_H
0027 
0028 #include "datavectors.h"
0029 
0030 #include <QList>
0031 #include <QString>
0032 #include <QVariant>
0033 
0034 namespace Tellico {
0035   namespace Data {
0036     class Entry;
0037   }
0038 
0039 /**
0040  * @author Robby Stephenson
0041  */
0042 class FilterRule {
0043 
0044 public:
0045   /**
0046    * Operators for comparison of field and contents.
0047    * If you change the order or contents of the enum: do not forget
0048    * to change matches() and @ref FilterRuleWidget::initLists(), too.
0049    */
0050   enum Function {
0051     FuncContains=0, FuncNotContains,
0052     FuncEquals, FuncNotEquals,
0053     FuncRegExp, FuncNotRegExp,
0054     FuncBefore, FuncAfter,
0055     FuncLess, FuncGreater
0056   };
0057 
0058   FilterRule();
0059   FilterRule(const QString& fieldName, const QString& text, Function func);
0060 
0061   /**
0062    * A rule is empty if the pattern text is empty and we're not matching an equal string
0063    */
0064   bool isEmpty() const;
0065   /**
0066    * This is the primary function of the rule.
0067    *
0068    * @return Returns true if the entry is matched by the rule.
0069    */
0070   bool matches(Data::EntryPtr entry) const;
0071 
0072   /**
0073    * Return filter function. This can be any of the operators
0074    * defined in @ref Function.
0075    */
0076   Function function() const { return m_function; }
0077   /**
0078    * Set filter function.
0079    */
0080   void setFunction(Function func);
0081   /**
0082    * Return field name
0083    */
0084   const QString& fieldName() const { return m_fieldName; }
0085   /**
0086    * Set field name
0087    */
0088   void setFieldName(const QString& fieldName) { m_fieldName = fieldName; }
0089   /**
0090    * Return pattern
0091    */
0092   QString pattern() const;
0093   /**
0094    * Set pattern
0095    */
0096 //  void setPattern(const QString& pattern) { m_pattern = pattern; }
0097 
0098 private:
0099   template <typename Func>
0100   bool numberCompare(Tellico::Data::EntryPtr entry, Func f) const;
0101 
0102   bool equals(Data::EntryPtr entry) const;
0103   bool contains(Data::EntryPtr entry) const;
0104   bool matchesRegExp(Data::EntryPtr entry) const;
0105   bool before(Data::EntryPtr entry) const;
0106   bool after(Data::EntryPtr entry) const;
0107   bool lessThan(Data::EntryPtr entry) const;
0108   bool greaterThan(Data::EntryPtr entry) const;
0109   void updatePattern();
0110 
0111   QString m_fieldName;
0112   Function m_function;
0113   QString m_pattern;
0114   QVariant m_patternVariant;
0115 };
0116 
0117 /**
0118  * Borrows from KMSearchPattern by Marc Mutz
0119  *
0120  * @author Robby Stephenson
0121  */
0122 class Filter : public QList<FilterRule*>, public QSharedData {
0123 
0124 public:
0125   enum FilterOp {
0126     MatchAny,
0127     MatchAll
0128   };
0129 
0130   Filter(FilterOp op) : QList<FilterRule*>(), m_op(op) {}
0131   Filter(const Filter& other);
0132   ~Filter();
0133 
0134   void setMatch(FilterOp op) { m_op = op; }
0135   FilterOp op() const { return m_op; }
0136   bool matches(Data::EntryPtr entry) const;
0137 
0138   void setName(const QString& name) { m_name = name; }
0139   const QString& name() const { return m_name; }
0140 
0141   int count() const { return QList<FilterRule*>::count(); } // disambiguate
0142 
0143   bool operator==(const Filter& other) const;
0144 
0145   static void populateQuickFilter(FilterPtr filter, const QString& fieldName, const QString& text, bool allowRegExp);
0146 
0147 private:
0148   Filter& operator=(const Filter& other);
0149 
0150   FilterOp m_op;
0151   QString m_name;
0152 };
0153 
0154 } // end namespace
0155 #endif