File: KoSopranoTableModel.cpp

package info (click to toggle)
calligra 1%3A3.2.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 260,432 kB
  • sloc: cpp: 650,911; xml: 27,662; python: 6,044; perl: 2,724; yacc: 1,817; ansic: 1,325; sh: 1,277; lex: 1,107; ruby: 1,010; javascript: 495; makefile: 24
file content (365 lines) | stat: -rw-r--r-- 11,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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* This file is part of the KDE project
   Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>

   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 "KoSopranoTableModel.h"

#include "KoDocumentRdf.h"
#include "KoRdfPrefixMapping.h"
// KF5
#include <kdebug.h>
#include <klocalizedstring.h>

#include <algorithm>

KoSopranoTableModel::KoSopranoTableModel(KoDocumentRdf *rdf)
        : m_rdf(rdf)
{
    Soprano::StatementIterator siter = model()->listStatements();
    while (siter.next()) {
        m_statementIndex << *siter;
    }
}

QSharedPointer<Soprano::Model> KoSopranoTableModel::model() const
{
    return m_rdf->model();
}

QString KoSopranoTableModel::URItoPrefexedLocalname(const QString &uri) const
{
    return m_rdf->prefixMapping()->URItoPrefexedLocalname(uri);
}
QString KoSopranoTableModel::PrefexedLocalnameToURI(const QString &pname)
{
    return m_rdf->prefixMapping()->PrefexedLocalnameToURI(pname);
}

QVariant KoSopranoTableModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }
    Soprano::Statement st = m_statementIndex[index.row()];
    if (index.column() == ColIsValid && role == Qt::CheckStateRole) {
        return st.isValid();
    }
    if (role == Qt::BackgroundRole) {
        if (!m_statementIndex[index.row()].isValid()) {
            return QColor("#BB0000");
        }
    }
    if (role != Qt::DisplayRole && role != Qt::EditRole) {
        return QVariant();
    }
    switch (index.column()) {
    case ColIsValid:
        return QVariant();
    case ColSubj:
        return URItoPrefexedLocalname(st.subject().toString());
    case ColPred:
        return URItoPrefexedLocalname(st.predicate().toString());
    case ColObj:
        if (st.object().type() == Soprano::Node::ResourceNode)
            return URItoPrefexedLocalname(st.object().toString());
        return st.object().toString();
    case ColObjType:
        switch (st.object().type()) {
        case Soprano::Node::EmptyNode:
            return i18n("Empty");
        case Soprano::Node::ResourceNode:
            return i18n("URI");
        case Soprano::Node::LiteralNode:
            return i18n("Literal");
        case Soprano::Node::BlankNode:
            return i18n("Blank");
        }
        return QString();
    case ColObjXsdType:
        return st.object().dataType().toString();
    case ColCtx: {
        QString ctx = st.context().toString();
        QString RdfPathContextPrefix = m_rdf->RDF_PATH_CONTEXT_PREFIX;
        QString InternalContext = m_rdf->inlineRdfContext().toString();

        kDebug(30015) << "InternalContext:" << InternalContext;
        kDebug(30015) << "ctx:" << ctx;

        if (ctx.startsWith(RdfPathContextPrefix)) {
            ctx.remove(0, RdfPathContextPrefix.size());
        }
        if (isInlineRdf(st)) {
            ctx = "inline";
        }
        return ctx;
    }
    }
    return QVariant();
}

int KoSopranoTableModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return model()->statementCount();
}

int KoSopranoTableModel::columnCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return ColCount;
}

QVariant KoSopranoTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
        case ColIsValid:
            return i18n("Valid");
        case ColSubj:
            return i18n("Subject");
        case ColPred:
            return i18n("Predicate");
        case ColObj:
            return i18n("Object");
        case ColObjType:
            return i18n("Obj Type");
        case ColObjXsdType:
            return i18n("DataType");
        case ColCtx:
            return i18n("Stored In");
        }
    }
    return QVariant();
}

bool KoSopranoTableModel::isInlineRdf(const Soprano::Statement &st) const
{
    return (st.context().toString() == m_rdf->inlineRdfContext().toString());
}

Qt::ItemFlags KoSopranoTableModel::flags(const QModelIndex &index) const
{
    Qt::ItemFlags ret = QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
    if (index.column() == ColIsValid) {
        ret |= Qt::ItemIsUserCheckable;
    }
    if (index.row() >= 0) {
        Soprano::Statement st = m_statementIndex[index.row()];
        if (isInlineRdf(st)) {
            if (index.column() == ColSubj
                    || index.column() == ColObjType
                    || index.column() == ColCtx) {
                ret &= (~Qt::ItemIsEditable);
            }
        }
    }
    return ret;
}

/**
 * You MUST use this method if you want to change a Statement.
 *
 * Used by setData() to remove the old statement and replace it with the new 'n' one.
 * The internal m_statementIndex int->statement is updated
 * as well as the dataChanged signal emitted
 */
bool KoSopranoTableModel::setDataUpdateTriple(const QModelIndex &index, const Soprano::Statement &old, const Soprano::Statement &n)
{
    model()->addStatement(n);
    model()->removeStatement(old);
    m_statementIndex[index.row()] = n;
    emit dataChanged(index, index);
    return true;
}

bool KoSopranoTableModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    Q_UNUSED(role);
    if (!index.isValid()) {
        return false;
    }
    int r = index.row();
    Soprano::Statement st = m_statementIndex[r];
    QString uri = PrefexedLocalnameToURI(value.toString());
    Soprano::Statement n(st.subject(), st.predicate(), st.object(), st.context());
    switch (index.column()) {
    case ColSubj:
        n.setSubject(Soprano::Node(QUrl(uri)));
        return setDataUpdateTriple(index, st, n);
    case ColPred:
        n.setPredicate(Soprano::Node(QUrl(uri)));
        return setDataUpdateTriple(index, st, n);
    case ColObj: {
        if (st.object().isLiteral()) {
            n.setObject(
                Soprano::Node(
                    Soprano::LiteralValue(value.toString())));
        } else {
            n.setObject(Soprano::Node(QUrl(uri)));
        }
        return setDataUpdateTriple(index, st, n);
    }
    case ColObjType: {
        QString v = value.toString();
        if (v == "URI") {
            n.setObject(Soprano::Node(QUrl(st.object().toString())));
        } else if (v == "Literal") {
            n.setObject(
                Soprano::Node(
                    Soprano::LiteralValue(st.object().toString())));
        } else {
            n.setObject(Soprano::Node(QString(st.object().toString())));
        }
        return setDataUpdateTriple(index, st, n);
    }
    case ColCtx: {
        QString v = value.toString();
        if (v == "inline") {
            QString InternalContext = m_rdf->rdfInternalMetadataWithoutSubjectURI();
            n.setContext(Soprano::Node(QUrl(InternalContext)));
        } else {
            if (!v.endsWith(".rdf"))
                v = v + ".rdf";
            n.setContext(Soprano::Node(QUrl(m_rdf->RDF_PATH_CONTEXT_PREFIX + v)));
        }
        return setDataUpdateTriple(index, st, n);
    }
    }
    return false;
}

/**
 * Add the statement 'st' to the model as the new last row.
 */
int KoSopranoTableModel::insertStatement(const Soprano::Statement &st)
{
    QModelIndex parent;
    int newRowNumber = rowCount();
    kDebug(30015) << "insert, newrow:" << newRowNumber << endl;
    beginInsertRows(parent, newRowNumber, newRowNumber);
    model()->addStatement(st);
    m_statementIndex << st;
    endInsertRows();
    return newRowNumber;
}

/**
 * Copy all the triples in srclist to be new rows in the model.
 * Note that the object value is modified to contain a unique
 * postfix so that the new triple copies can be inserted into
 * the Rdf model. It is a copy in a looser sense of the word.
 */
QModelIndexList KoSopranoTableModel::copyTriples(const QModelIndexList &srclist)
{
    QModelIndexList ret;
    int FirstNewRowNumber = rowCount();
    int LastNewRowNumber = FirstNewRowNumber + srclist.size() - 1;
    int currentNewRowNum = FirstNewRowNumber;
    beginInsertRows(QModelIndex(), FirstNewRowNumber, LastNewRowNumber);
    kDebug(30015) << " m_statementIndex.sz:" << m_statementIndex.size();
    kDebug(30015) << " srclist.size():" << srclist.size();
    kDebug(30015) << " first:" << FirstNewRowNumber;
    kDebug(30015) << " last:" << LastNewRowNumber;
    foreach (const QModelIndex &src, srclist) {
        int r = src.row();
        kDebug(30015) << "r:" << r;
        Soprano::Statement st = m_statementIndex[ r ];
        //
        // Append a bnode to the object to ensure the "copy"
        // is unique relative to the original.
        //
        Soprano::Node obj(QUrl(st.object().toString() + '-'
                               + model()->createBlankNode().toString()));
        Soprano::Statement n(st.subject(), st.predicate(),
                             obj, st.context());
        model()->addStatement(n);
        m_statementIndex << n;
        QModelIndex newIdx = index(currentNewRowNum, ColSubj);
        ret << newIdx;
        ++currentNewRowNum;
    }
    endInsertRows();
    return ret;
}

/**
 * Delete all the triples in srclist from the model.
 */
void KoSopranoTableModel::deleteTriples(const QModelIndexList &srclist)
{
    //
    // Because items after a row are shuffled back to fill
    // it's position, it is easiest to remove each item
    // starting at the largest row number and working
    // down by descending row number.
    //
    QList<int> rowsToRemoveDesc;
    foreach (const QModelIndex &src, srclist) {
        int r = src.row();
        rowsToRemoveDesc << r;
    }
    std:sort(rowsToRemoveDesc.begin(), rowsToRemoveDesc.end(), std::greater<int>());
    int r;
    foreach (r, rowsToRemoveDesc) {
        Soprano::Statement st = m_statementIndex[ r ];
        int firstRow =  r;
        int lastRow = r;
        beginRemoveRows(QModelIndex(), firstRow, lastRow);
        model()->removeStatement(st);
        // m_statementIndex[ r ] = Soprano::Statement();
        for (int i = r; i < m_statementIndex.size() - 1; ++i) {
            m_statementIndex[ i ] = m_statementIndex[ i + 1 ];
        }
        m_statementIndex.removeLast();
        endRemoveRows();
    }
}

Soprano::Statement KoSopranoTableModel::statementAtIndex(const QModelIndex &index) const
{
    return m_statementIndex[index.row()];
}

int KoSopranoTableModel::invalidStatementCount() const
{
    return invalidStatementList().size();
}

QModelIndexList KoSopranoTableModel::invalidStatementList() const
{
    QModelIndexList ret;
    for (int r = 0; r < m_statementIndex.size(); ++r)  {
        const Soprano::Statement &s = m_statementIndex.at(r);
        if (!s.isValid()) {
            int col = ColSubj;
            if (!s.subject().isValid()) {
                col = ColSubj;
            }
            if (!s.predicate().isValid()) {
                col = ColPred;
            }
            if (!s.object().isValid()) {
                col = ColObj;
            }
            QModelIndex idx = index(r, col);
            ret << idx;
        }
    }
    return ret;
}