File: Database.cpp

package info (click to toggle)
calligra 1%3A2.4.4-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 290,028 kB
  • sloc: cpp: 1,105,019; xml: 24,940; ansic: 11,807; python: 8,457; perl: 2,792; sh: 1,507; yacc: 1,307; ruby: 1,248; sql: 903; lex: 455; makefile: 89
file content (350 lines) | stat: -rw-r--r-- 10,809 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
/* This file is part of the KDE project
   Copyright 2007 Stefan Nikolaus <stefan.nikolaus@kdemail.net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "Database.h"

#include <QString>

#include <KoXmlNS.h>
#include <KoXmlWriter.h>

#include "DatabaseSource.h"
#include "Filter.h"
#include "FilterPopup.h"
#include "Map.h"
#include "Region.h"

using namespace Calligra::Sheets;

class Sort;
class SubtotalRules;

class Database::Private : public QSharedData
{
public:
    Private()
            : source(0)
            , sort(0)
            , filter(new Filter())
            , subtotalRules(0)
            , isSelection(false)
            , onUpdateKeepStyles(false)
            , onUpdateKeepSize(true)
            , hasPersistentData(true)
            , orientation(Row)
            , containsHeader(true)
            , displayFilterButtons(false)
            , refreshDelay(0) {
    }

    Private(const Private& other)
            : QSharedData(other)
            , source(/*other.source ? new DatabaseSource(*other.source) : */0)
            , sort(/*other.sort ? new Sort(*other.sort) : */0)
            , filter(other.filter ? new Filter(*other.filter) : 0)
            , subtotalRules(/*other.subtotalRules ? new SubtotalRules(*other.subtotalRules) : */0)
            , name(other.name)
            , isSelection(other.isSelection)
            , onUpdateKeepStyles(other.onUpdateKeepStyles)
            , onUpdateKeepSize(other.onUpdateKeepSize)
            , hasPersistentData(other.hasPersistentData)
            , orientation(other.orientation)
            , containsHeader(other.containsHeader)
            , displayFilterButtons(other.displayFilterButtons)
            , targetRangeAddress(other.targetRangeAddress)
            , refreshDelay(other.refreshDelay) {
    }

    virtual ~Private() {
//         delete source;
//         delete sort;
        delete filter;
//         delete subtotalRules;
    }

    DatabaseSource* source;
    Sort* sort;
    Filter* filter;
    SubtotalRules* subtotalRules;
    QString name;
    bool isSelection                    : 1;
    bool onUpdateKeepStyles             : 1;
    bool onUpdateKeepSize               : 1;
    bool hasPersistentData              : 1;
    enum { Row, Column } orientation    : 1;
    bool containsHeader                 : 1;
    bool displayFilterButtons           : 1;
    Region targetRangeAddress;
    int refreshDelay;

private:
    void operator=(const Private&);
};

Database::Database()
        : d(new Private)
{
}

Database::Database(const QString& name)
        : d(new Private)
{
    d->name = name;
}

Database::Database(const Database& other)
        : d(other.d)
{
}

Database::~Database()
{
}

bool Database::isEmpty() const
{
    return d->name.isNull(); // it may be empty though
}

const QString& Database::name() const
{
    return d->name;
}

void Database::setName(const QString& name)
{
    d->name = name;
}

Qt::Orientation Database::orientation() const
{
    return d->orientation == Private::Row ? Qt::Vertical : Qt::Horizontal;
}

bool Database::containsHeader() const
{
    return d->containsHeader;
}

void Database::setContainsHeader(bool enable)
{
    d->containsHeader = enable;
}

bool Database::displayFilterButtons() const
{
    return d->displayFilterButtons;
}

void Database::setDisplayFilterButtons(bool enable)
{
    d->displayFilterButtons = enable;
}

const Calligra::Sheets::Region& Database::range() const
{
    return d->targetRangeAddress;
}

void Database::setRange(const Region& region)
{
    Q_ASSERT(region.isContiguous());
    d->targetRangeAddress = region;
}

const Filter& Database::filter() const
{
    return *d->filter;
}

void Database::setFilter(const Filter& filter)
{
    if (*d->filter == filter)
        return;
    delete d->filter;
    d->filter = new Filter(filter);
}

bool Database::loadOdf(const KoXmlElement& element, const Map* map)
{
    if (element.hasAttributeNS(KoXmlNS::table, "name"))
        d->name = element.attributeNS(KoXmlNS::table, "name", QString());
    if (element.hasAttributeNS(KoXmlNS::table, "is-selection")) {
        if (element.attributeNS(KoXmlNS::table, "is-selection", "false") == "true")
            d->isSelection = true;
        else
            d->isSelection = false;
    }
    if (element.hasAttributeNS(KoXmlNS::table, "on-update-keep-styles")) {
        if (element.attributeNS(KoXmlNS::table, "on-update-keep-styles", "false") == "true")
            d->onUpdateKeepStyles = true;
        else
            d->onUpdateKeepStyles = false;
    }
    if (element.hasAttributeNS(KoXmlNS::table, "on-update-keep-size")) {
        if (element.attributeNS(KoXmlNS::table, "on-update-keep-size", "true") == "false")
            d->onUpdateKeepSize = false;
        else
            d->onUpdateKeepSize = true;
    }
    if (element.hasAttributeNS(KoXmlNS::table, "has-persistent-data")) {
        if (element.attributeNS(KoXmlNS::table, "has-persistent-data", "true") == "false")
            d->hasPersistentData = false;
        else
            d->hasPersistentData = true;
    }
    if (element.hasAttributeNS(KoXmlNS::table, "orientation")) {
        if (element.attributeNS(KoXmlNS::table, "orientation", "row") == "column")
            d->orientation = Private::Column;
        else
            d->orientation = Private::Row;
    }
    if (element.hasAttributeNS(KoXmlNS::table, "contains-header")) {
        if (element.attributeNS(KoXmlNS::table, "contains-header", "true") == "false")
            d->containsHeader = false;
        else
            d->containsHeader = true;
    }
    if (element.hasAttributeNS(KoXmlNS::table, "display-filter-buttons")) {
        if (element.attributeNS(KoXmlNS::table, "display-filter-buttons", "false") == "true")
            d->displayFilterButtons = true;
        else
            d->displayFilterButtons = false;
    }
    if (element.hasAttributeNS(KoXmlNS::table, "target-range-address")) {
        const QString address = element.attributeNS(KoXmlNS::table, "target-range-address", QString());
        // only absolute addresses allowed; no fallback sheet needed
        d->targetRangeAddress = Region(Region::loadOdf(address), map);
        if (!d->targetRangeAddress.isValid())
            return false;
    }
    if (element.hasAttributeNS(KoXmlNS::table, "refresh-delay")) {
        bool ok = false;
        d->refreshDelay = element.attributeNS(KoXmlNS::table, "refresh-delay", QString()).toInt(&ok);
        if (!ok || d->refreshDelay < 0)
            return false;
    }
    KoXmlElement child;
    forEachElement(child, element) {
        if (child.namespaceURI() != KoXmlNS::table)
            continue;
        if (child.localName() == "database-source-sql") {
            // TODO
        } else if (child.localName() == "database-source-table") {
            // TODO
        } else if (child.localName() == "database-source-query") {
            // TODO
        } else if (child.localName() == "sort") {
            // TODO
        } else if (child.localName() == "filter") {
            d->filter = new Filter();
            if (!d->filter->loadOdf(child, map)) {
                delete d->filter;
                d->filter = 0;
                return false;
            }
        } else if (child.localName() == "subtotal-rules") {
            // TODO
        }
    }
    return true;
}

void Database::saveOdf(KoXmlWriter& xmlWriter) const
{
    if (d->targetRangeAddress.isEmpty())
        return;
    xmlWriter.startElement("table:database-range");
    if (!d->name.isNull())
        xmlWriter.addAttribute("table:name", d->name);
    if (d->isSelection)
        xmlWriter.addAttribute("table:is-selection", "true");
    if (d->onUpdateKeepStyles)
        xmlWriter.addAttribute("table:on-update-keep-styles", "true");
    if (!d->onUpdateKeepSize)
        xmlWriter.addAttribute("table:on-update-keep-size", "false");
    if (!d->hasPersistentData)
        xmlWriter.addAttribute("table:has-persistent-data", "false");
    if (d->orientation == Private::Column)
        xmlWriter.addAttribute("table:orientation", "column");
    if (!d->containsHeader)
        xmlWriter.addAttribute("table:contains-header", "false");
    if (d->displayFilterButtons)
        xmlWriter.addAttribute("table:display-filter-buttons", "true");
    xmlWriter.addAttribute("table:target-range-address", d->targetRangeAddress.saveOdf());
    if (d->refreshDelay)
        xmlWriter.addAttribute("table:refresh-delay", d->refreshDelay);
    // TODO
//     if (d->source)
//         d->source->saveOdf(xmlWriter);
//     if (d->sort)
//         d->sort->saveOdf(xmlWriter);
    if (d->filter)
        d->filter->saveOdf(xmlWriter);
//     if (d->subtotalRules)
//         d->subtotalRules->saveOdf(xmlWriter);
    xmlWriter.endElement();
}

void Database::operator=(const Database & other)
{
    d = other.d;
}

bool Database::operator==(const Database& other) const
{
    // NOTE Stefan: Don't compare targetRangeAddress.
    if (d->name != other.d->name)
        return false;
    if (d->isSelection != other.d->isSelection)
        return false;
    if (d->onUpdateKeepStyles != other.d->onUpdateKeepStyles)
        return false;
    if (d->onUpdateKeepSize != other.d->onUpdateKeepSize)
        return false;
    if (d->hasPersistentData != other.d->hasPersistentData)
        return false;
    if (d->orientation != other.d->orientation)
        return false;
    if (d->containsHeader != other.d->containsHeader)
        return false;
    if (d->displayFilterButtons != other.d->displayFilterButtons)
        return false;
    if (d->refreshDelay != other.d->refreshDelay)
        return false;
//     if (*d->source != *other.d->source)
//         return false;
//     if (*d->sort != *other.d->sort)
//         return false;
    if (*d->filter != *other.d->filter)
        return false;
//     if (*d->subtotalRules != *other.d->subtotalRules)
//         return false;
    return true;
}

bool Database::operator<(const Database& other) const
{
    return (d < other.d);
}

void Database::dump() const
{
    if (d->filter) d->filter->dump();
}