summaryrefslogtreecommitdiffstats
path: root/src/filter.h
blob: 34e75f5d2d7dc6cb0c27e34ce44aa91a649f19cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/***************************************************************************
    copyright            : (C) 2003-2006 by Robby Stephenson
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of version 2 of the GNU General Public License as  *
 *   published by the Free Software Foundation;                            *
 *                                                                         *
 ***************************************************************************/

#ifndef TELLICO_FILTER_H
#define TELLICO_FILTER_H

#include "datavectors.h"

#include <ksharedptr.h>

#include <tqptrlist.h>
#include <tqstring.h>

namespace Tellico {
  namespace Data {
    class Entry;
  }

/**
 * @author Robby Stephenson
 */
class FilterRule {

public:
  /**
   * Operators for comparison of field and contents.
   * If you change the order or contents of the enum: do not forget
   * to change matches() and @ref FilterRuleWidget::initLists(), too.
   */
  enum Function {
    FuncContains=0, FuncNotContains,
    FuncEquals, FuncNotEquals,
    FuncRegExp, FuncNotRegExp
  };

  FilterRule();
  FilterRule(const TQString& fieldName, const TQString& text, Function func);

  /**
   * A rule is empty if the pattern text is empty
   */
  bool isEmpty() const { return m_pattern.isEmpty(); }
  /**
   * This is the primary function of the rule.
   *
   * @return Returns true if the entry is matched by the rule.
   */
  bool matches(Data::EntryPtr entry) const;

  /**
   * Return filter function. This can be any of the operators
   * defined in @ref Function.
   */
  Function function() const { return m_function; }
  /**
   * Set filter function.
   */
  void setFunction(Function func) { m_function = func; }
  /**
   * Return field name
   */
  const TQString& fieldName() const { return m_fieldName; }
  /**
   * Set field name
   */
  void setFieldName(const TQString& fieldName) { m_fieldName = fieldName; }
  /**
   * Return pattern
   */
  const TQString& pattern() const { return m_pattern; }
  /**
   * Set pattern
   */
//  void setPattern(const TQString& pattern) { m_pattern = pattern; }

private:
  bool equals(Data::EntryPtr entry) const;
  bool contains(Data::EntryPtr entry) const;
  bool matchesRegExp(Data::EntryPtr entry) const;

  TQString m_fieldName;
  Function m_function;
  TQString m_pattern;
};

/**
 * Borrows from KMSearchPattern by Marc Mutz
 *
 * @author Robby Stephenson
 */
class Filter : public TQPtrList<FilterRule>, public KShared {

public:
  enum FilterOp {
    MatchAny,
    MatchAll
  };
  typedef KSharedPtr<Filter> Ptr;

  Filter(FilterOp op) : TQPtrList<FilterRule>(), m_op(op) { setAutoDelete(true); }
  Filter(const Filter& other);

  void setMatch(FilterOp op) { m_op = op; }
  FilterOp op() const { return m_op; }
  bool matches(Data::EntryPtr entry) const;

  void setName(const TQString& name) { m_name = name; }
  const TQString& name() const { return m_name; }

  uint count() const { return TQPtrList<FilterRule>::count(); } // disambiguate

private:
  Filter& operator=(const Filter& other);

  FilterOp m_op;
  TQString m_name;
};

} // end namespace
#endif