File: condition.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 (433 lines) | stat: -rw-r--r-- 11,608 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * condition.cpp
 *
 * (c) 2002-2004,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 condition.cpp
 * Source file for Condition
 */

#include <QLocale>
#include <mk4.h>
#include "condition.h"
#include "database.h"
#include "datatypes.h"
#include "formatting.h"

/**
 * Constructor.
 *
 * @param dbase The database in use
 */
Condition::Condition(Database *dbase) : QObject(), colName("_anytext"), operation(Contains), constant(""), caseSensitive(false), description("")
{
    db = dbase;
}

/**
 * Get the name of the column that this condition applies to.
 *
 * @return The column name
 */
QString Condition::getColName()
{
    return colName;
}

/**
 * Set the name of the column that this condition applies to.
 *
 * @param newName The column name
 */
void Condition::setColName(const QString &newName)
{
    colName = newName;
}

/**
 * Get the ID of the operator used in this condition.
 *
 * @return The operator ID
 */
Condition::Operator Condition::getOperator()
{
    return operation;
}

/**
 * Set the ID of the operator used in this condition.
 *
 * @param op The operator ID
 */
void Condition::setOperator(Operator op)
{
    operation = op;
}

/**
 * Get the string representation of the constant value which is
 * being used in this condition.
 *
 * @return The string form of the constant in use
 */
QString Condition::getConstant()
{
    return constant;
}

/**
 * Set the constant value to use in this condition.
 *
 * @param newConstant The string representation of the constant to use
 */
void Condition::setConstant(const QString &newConstant)
{
    constant = newConstant;
}

/**
 * Determine if this condition uses a case sensitive text comparison or not.
 *
 * @return True for case sensitive text comparisons, false otherwise
 */
bool Condition::isCaseSensitive()
{
    return caseSensitive;
}

/**
 * Specifiy if this condition's comparison is to be performed in a case
 * sensitive manner or not.
 *
 * @param y True for case sensitive text comparisons, false otherwise
 */
void Condition::setCaseSensitive(bool y)
{
    caseSensitive = y;
}

/**
 * Filter the provided data so that only rows which match this condition
 * are retained.
 *
 * @param dbview The database view which is to be filtered
 * @return The subset of the data which matches this condition
 */
c4_View Condition::filter(c4_View dbview)
{
    if (colName == "_anytext") {
        c4_View result = dbview.Clone();
        int count = textCols.count();
        if (count == 0) {
            QStringList colNames = db->listColumns();
            int numCols = colNames.count();
            for (int i = 0; i < numCols; i++) {
                QString name = colNames[i];
                int type = db->getType(name);
                if (type == STRING || type == NOTE) {
                    textCols.append(name);
                }
            }
            count = textCols.count();
        }
        for (int i = 0; i < count; i++) {
            setColName(textCols[i]);
            result = result.Union(filterString(dbview));
        }
        setColName("_anytext");
        return result;
    }
    int type = db->getType(colName);
    if (type == INTEGER || type == BOOLEAN || type == DATE || type == TIME
            || type == SEQUENCE) {
        QString colId = db->getColId(colName);
        int value = QLocale::c().toInt(constant);
        return filterInt(dbview, colId, value);
    }
    else if (type >= FIRST_ENUM) {
        QString colId = db->getColId(colName, INTEGER);
        QStringList options = db->listEnumOptions(type);
        int value = options.indexOf(constant);
        return filterInt(dbview, colId, value);
    }
    else if (type == FLOAT || type == CALC) {
        return filterFloat(dbview);
    }
    else {
        return filterString(dbview);
    }
}

/**
 * Filter implementation for conditions involving integer columns.  Called by
 * filter() when appropriate.
 *
 * @param dbview The database view which is to be filtered
 * @param colId The ID string used for the column of interest in the data table
 * @param value The integer constant being compared against
 * @return The subset of the data which matches this condition
 */
c4_View Condition::filterInt(c4_View dbview, const QString &colId, int value)
{
    c4_IntProp prop(colId.toUtf8().data());
    c4_View result = dbview.Clone();
    int size = dbview.GetSize();
    if (operation == Equals) {
        result = dbview.Select(prop [value]);
    }
    else if (operation == NotEqual) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) != value) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == LessThan) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) < value) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == GreaterThan) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) > value) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == LessEqual) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) <= value) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == GreaterEqual) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) >= value) {
                result.Add(dbview[i]);
            }
        }
    }
    return result;
}

/**
 * Filter implementation for conditions involving floating point columns.
 * Called by filter() when appropriate.
 *
 * @param dbview The database view which is to be filtered
 * @return The subset of the data which matches this condition
 */
c4_View Condition::filterFloat(c4_View dbview)
{
    double value = Formatting::parseDouble(constant);
    c4_FloatProp prop(db->getColId(colName).toLatin1().data());
    c4_View result = dbview.Clone();
    int size = dbview.GetSize();
    if (operation == Equals) {
        result = dbview.Select(prop [value]);
    }
    else if (operation == NotEqual) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) != value) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == LessThan) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) < value) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == GreaterThan) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) > value) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == LessEqual) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) <= value) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == GreaterEqual) {
        for (int i = 0; i < size; i++) {
            if (prop (dbview[i]) >= value) {
                result.Add(dbview[i]);
            }
        }
    }
    return result;
}

/**
 * Filter implementation for conditions involving text columns. Called by
 * filter() when appropriate.
 *
 * @param dbview The database view which is to be filtered
 * @return The subset of the data which matches this condition
 */
c4_View Condition::filterString(c4_View dbview)
{
    c4_StringProp prop(db->getColId(colName).toLatin1().data());
    c4_View result = dbview.Clone();
    int size = dbview.GetSize();
    QString target = constant;
    if (!caseSensitive) {
        target = target.toLower();
    }
    if (operation == Equals) {
        if (caseSensitive) {
            result = dbview.Select(prop [target.toUtf8()]);
        }
        else {
            for (int i = 0; i < size; i++) {
                QString value = QString::fromUtf8(prop (dbview[i]));
                if (value.toLower() == target) {
                    result.Add(dbview[i]);
                }
            }
        }
    }
    else if (operation == Contains) {
        for (int i = 0; i < size; i++) {
            QString value = QString::fromUtf8(prop (dbview[i]));
            Qt::CaseSensitivity cs = caseSensitive ? Qt::CaseSensitive
                                                   : Qt::CaseInsensitive;
            if (value.contains(constant, cs) > 0) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == StartsWith) {
        for (int i = 0; i < size; i++) {
            QString value = QString::fromUtf8(prop (dbview[i]));
            if (!caseSensitive) {
                value = value.toLower();
            }
            if (value.startsWith(target)) {
                result.Add(dbview[i]);
            }
        }
    }
    else if (operation == NotEqual) {
        for (int i = 0; i < size; i++) {
            QString value = QString::fromUtf8(prop (dbview[i]));
            if (!caseSensitive) {
                value = value.toLower();
            }
            if (value != target) {
                result.Add(dbview[i]);
            }
        }
    }
    return result;
}

/**
 * Get the text description of this condition, as shown in the Filter UI.
 *
 * @return A text description of this condition
 */
QString Condition::getDescription()
{
    if (description.isEmpty()) {
        updateDescription();
    }
    return description;
}

/**
 * Update the text description of this condition in order to reflect its
 * current definition.
 */
void Condition::updateDescription()
{
    int type = STRING;
    QString arg1;
    QString arg2;
    if (colName == "_anytext") {
        arg1 = tr("Any text column");
    }
    else {
        arg1 = colName;
        type = db->getType(colName);
    }
    if (type == BOOLEAN) {
        if (constant == "1") {
            description = tr("%1 is checked").arg(arg1);
        }
        else {
            description = tr("%1 is not checked").arg(arg1);
        }
        return;
    }
    if (type == STRING || type == NOTE || type >= FIRST_ENUM) {
        arg2 = "\"" + constant + "\"";
    }
    else if (type == DATE) {
        arg2 = Formatting::dateToString(constant.toInt());
    }
    else if (type == TIME) {
        arg2 = Formatting::timeToString(constant.toInt());
    }
    else if (type == INTEGER || type == SEQUENCE) {
        arg2 = QLocale::system().toString(constant.toInt());
    }
    else if (type == FLOAT) {
        arg2 = Formatting::toLocalDouble(constant);
    }
    else {
        arg2 = constant;
    }
    description = getOperatorText(operation).arg(arg1).arg(arg2);
}

/**
 * Get the description template appropriate for the given operator ID.
 *
 * @param op The ID of the operator to get a description template for
 * @return The appropriate description template
 */
QString Condition::getOperatorText(Operator op)
{
    if (op == Equals) {
        return "%1 = %2";
    }
    else if (op == Contains) {
        return tr("%1 contains %2");
    }
    else if (op == StartsWith) {
        return tr("%1 starts with %2");
    }
    else if (op == LessThan) {
        return "%1 < %2";
    }
    else if (op == GreaterThan) {
        return "%1 > %2";
    }
    else if (op == LessEqual) {
        return "%1 <= %2";
    }
    else if (op == GreaterEqual) {
        return "%1 >= %2";
    }
    else if (op == NotEqual) {
        return "%1 != %2";
    }
    // shouldn't get here
    return "";
}