File: KoRdfSemanticTree.cpp

package info (click to toggle)
calligra 1%3A2.9.11%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 189,332 kB
  • sloc: cpp: 919,806; xml: 27,759; ansic: 10,472; python: 8,190; perl: 2,724; yacc: 2,557; sh: 1,675; lex: 1,431; java: 1,304; sql: 903; ruby: 734; makefile: 48
file content (158 lines) | stat: -rw-r--r-- 5,538 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
/* This file is part of the KDE project
   Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>
   Copyright (C) 2010 Thomas Zander <zander@kde.org>

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

#include "KoRdfSemanticItemRegistry.h"
#include "KoDocumentRdf.h"
#include "KoRdfSemanticTreeWidgetItem.h"
// KDE
#include <kdebug.h>
#include <klocale.h>
// Qt
#include <QSet>

class KoRdfSemanticTreePrivate : public QSharedData
{
public:
    QTreeWidget *m_tree;
    QHash<QString, QTreeWidgetItem*> m_treeWidgetItems;

    KoRdfSemanticTreePrivate(QTreeWidget *tree)
        {
            m_tree = tree;
            if(m_tree)  {
                tree->sortItems(1, Qt::DescendingOrder);

                foreach (const QString &semanticClass, KoRdfSemanticItemRegistry::instance()->classNames()) {
                    QTreeWidgetItem *treeWidgetItem = new QTreeWidgetItem(tree);
                    treeWidgetItem->setText(0, KoRdfSemanticItemRegistry::instance()->classDisplayName(semanticClass));
                    tree->expandItem(treeWidgetItem);
                    m_treeWidgetItems.insert(semanticClass, treeWidgetItem);
                }
            }
        }
    void update(KoDocumentRdf *rdf, QSharedPointer<Soprano::Model> model);
    /**
     * Add the name of each selected child of parent to the 'ret' set.
     */
    void buildSelectedSet(QTreeWidgetItem *parent, QSet<QString> &ret);
    /**
     * Called from update() to reset the tree to a default state.
     */
    void clear(QTreeWidgetItem *parent);
};

KoRdfSemanticTree::KoRdfSemanticTree()
    : d(new KoRdfSemanticTreePrivate(0))
{
}

KoRdfSemanticTree::KoRdfSemanticTree(QTreeWidget *tree)
    : d(new KoRdfSemanticTreePrivate (tree))
{
}
KoRdfSemanticTree::KoRdfSemanticTree(const KoRdfSemanticTree &orig)
    : d(orig.d)
{
}

KoRdfSemanticTree::~KoRdfSemanticTree()
{
}

KoRdfSemanticTree KoRdfSemanticTree::createTree(QTreeWidget* tree)
{
    KoRdfSemanticTree ret (tree);
    return ret;
}

void KoRdfSemanticTreePrivate::clear(QTreeWidgetItem *parent)
{
    while (parent->childCount()) {
        QTreeWidgetItem* c = parent->child(0);
        parent->removeChild(c);
        delete c;
    }
}

void KoRdfSemanticTreePrivate::buildSelectedSet(QTreeWidgetItem *parent, QSet<QString> &ret)
{
    for (int i = 0; i < parent->childCount(); ++i) {
        QTreeWidgetItem *c = parent->child(i);
        if (c->isSelected()) {
            if (KoRdfSemanticTreeWidgetItem *item = dynamic_cast<KoRdfSemanticTreeWidgetItem*>(c)) {
                ret << item->semanticItem()->name();
            }
        }
    }
}

void KoRdfSemanticTreePrivate::update(KoDocumentRdf *rdf, QSharedPointer<Soprano::Model> model)
{
    QHash<QString, QTreeWidgetItem*>::ConstIterator it = m_treeWidgetItems.constBegin();
    QHash<QString, QTreeWidgetItem*>::ConstIterator end = m_treeWidgetItems.constEnd();
    for( ; it != end; ++it) {
        const QString &semanticClass = it.key();
        QTreeWidgetItem *treeWidgetItem = it.value();

        QSet<QString> selectedItems;
        buildSelectedSet(treeWidgetItem, selectedItems);
        clear(treeWidgetItem);
        treeWidgetItem->setSelected(false);

        // TODO: for locations this unexplained hack is done, find out why and if really needed
        if (model && semanticClass == QLatin1String("Location")) {
            //
            // grab the lat/long triples from m_model into the passed model.
            //
            // geo84
            // <uri:dan84> <http://xmlns.com/foaf/0.1/based_near> _:genid1
            // _:genid1 <http://www.w3.org/2003/01/geo/wgs84_pos#lat> "51.47026" (empty)
            // _:genid1 <http://www.w3.org/2003/01/geo/wgs84_pos#long> "-2.59466" (empty)
            rdf->expandStatementsSubjectPointsTo(model);
            //kDebug(30015) << "expanding lists... old.sz:" << model->statementCount();
            // other geo is an rdf:list, so bring that in too
            rdf->expandStatementsToIncludeRdfLists(model);
            //kDebug(30015) << "expanding lists... new.sz:" << model->statementCount();
        }

        const QList<hKoRdfSemanticItem> semanticItems = rdf->semanticItems(semanticClass, model);
        foreach (hKoRdfSemanticItem semanticItem, semanticItems) {
            KoRdfSemanticTreeWidgetItem *item = semanticItem->createQTreeWidgetItem(treeWidgetItem);
            if (selectedItems.contains(item->semanticItem()->name())) {
                item->setSelected(true);
            }
        }
        m_tree->expandItem(treeWidgetItem);
    }
}

void KoRdfSemanticTree::update(KoDocumentRdf *rdf, QSharedPointer<Soprano::Model> model)
{
    d->update (rdf,model);
}


KoRdfSemanticTree &KoRdfSemanticTree::operator=(const KoRdfSemanticTree &other)
{
    d = other.d;
    return *this;
}