File: filter.cpp

package info (click to toggle)
portabase 2.0%2Bgit20110117-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,692 kB
  • sloc: cpp: 32,047; sh: 2,675; ansic: 2,320; makefile: 343; xml: 20; python: 16; asm: 10
file content (174 lines) | stat: -rw-r--r-- 4,290 bytes parent folder | download | duplicates (2)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * filter.cpp
 *
 * (c) 2002,2008-2010 by Jeremy Bowman <jmbowman@alum.mit.edu>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

/** @file filter.cpp
 * Source file for Filter
 */

#include "condition.h"
#include "database.h"
#include "filter.h"

/**
 * Constructor.
 *
 * @param dbase The database to which this filter belongs
 * @param name The name of this filter
 */
Filter::Filter(Database *dbase, const QString &name)
{
    db = dbase;
    filterName = name;
    int count = db->getConditionCount(name);
    for (int i = 0; i < count; i++) {
        conditions << db->getCondition(name, i);
    }
}

/**
 * Destructor.
 */
Filter::~Filter()
{
    while (!conditions.isEmpty()) {
        delete conditions.takeFirst();
    }
}

/**
 * Get the name of this filter.
 *
 * @return The filter's name
 */
QString Filter::getName() const
{
    return filterName;
}

/**
 * Set the name of this filter.
 *
 * @param newName This filter's new name
 */
void Filter::setName(const QString &newName)
{
    filterName = newName;
}

/**
 * Get the number of conditions that this filter contains.
 */
int Filter::getConditionCount()
{
    return conditions.count();
}

/**
 * Get the Condition with the specified position index.
 *
 * @param index The position index of the Condition to be retrieved
 * @return A pointer to the desired Condition
 */
Condition *Filter::getCondition(int index)
{
    return conditions[index];
}

/**
 * Add a new condition to the filter.  A position can be specified, but this
 * should only be done in cases like file import where it is known that the
 * specified position won't clash with any other indices; no effort is made
 * here to avoid two Conditions having the same position.
 *
 * @param condition A pointer to the Condition to be added
 * @param index The position index at which to insert the new Condition
 */
void Filter::addCondition(Condition *condition, int index)
{
    if (index == -1) {
        conditions << condition;
    }
    else {
        if (index < conditions.count()) {
            conditions[index] = condition;
        }
        else {
            while (conditions.count() < index) {
                conditions << 0;
            }
            conditions << condition;
        }
    }
}

/**
 * Remove the condition at the specified position index from this filter.
 * Adjusts the position indices of later conditions accordingly.
 *
 * @param index The position index of the condition to remove
 */
void Filter::deleteCondition(int index)
{
    Condition *condition = conditions.takeAt(index);
    delete condition;
}

/**
 * Move the condition at the specified position index up by one in the
 * sequence.  Adjusts the positions of other conditions accordingly.
 * Returns false if an invalid operation was specified (like trying to
 * move the first condition up).
 *
 * @param index The position index of the condition to be moved
 * @return True if a change was made, false otherwise
 */
bool Filter::moveConditionUp(int index)
{
    if (index <= 0 || index >= conditions.count()) {
        return false;
    }
    conditions.swap(index, index - 1);
    return true;
}

/**
 * Move the condition at the specified position index down by one in the
 * sequence.  Adjusts the positions of other conditions accordingly.
 * Returns false if an invalid operation was specified (like trying to
 * move the last condition down).
 *
 * @param index The position index of the condition to be moved
 * @return True if a change was made, false otherwise
 */
bool Filter::moveConditionDown(int index)
{
    if (index < 0 || index >= conditions.count() - 1) {
        return false;
    }
    conditions.swap(index, index + 1);
    return true;
}

/**
 * Apply this filter to the provided data table and return the result.
 *
 * @param dbview The data table to be filtered
 * @return The filtered data table
 */
c4_View Filter::apply(c4_View dbview)
{
    int count = conditions.count();
    c4_View result = dbview;
    for (int i = 0; i < count; i++) {
        result = conditions[i]->filter(result);
    }
    return result;
}