Warning, file /office/calligra/libs/rdf/KoRdfSemanticTree.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>
0003    Copyright (C) 2010 Thomas Zander <zander@kde.org>
0004 
0005    This library is free software; you can redistribute it and/or
0006    modify it under the terms of the GNU Library General Public
0007    License as published by the Free Software Foundation; either
0008    version 2 of the License, or (at your option) any later version.
0009 
0010    This library is distributed in the hope that it will be useful,
0011    but WITHOUT ANY WARRANTY; without even the implied warranty of
0012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013    Library General Public License for more details.
0014 
0015    You should have received a copy of the GNU Library General Public License
0016    along with this library; see the file COPYING.LIB.  If not, write to
0017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019 */
0020 
0021 #include "KoRdfSemanticTree.h"
0022 
0023 #include "KoRdfSemanticItemRegistry.h"
0024 #include "KoDocumentRdf.h"
0025 #include "KoRdfSemanticTreeWidgetItem.h"
0026 // KF5
0027 #include <kdebug.h>
0028 #include <klocalizedstring.h>
0029 // Qt
0030 #include <QSet>
0031 
0032 class KoRdfSemanticTreePrivate : public QSharedData
0033 {
0034 public:
0035     QTreeWidget *m_tree;
0036     QHash<QString, QTreeWidgetItem*> m_treeWidgetItems;
0037 
0038     KoRdfSemanticTreePrivate(QTreeWidget *tree)
0039         {
0040             m_tree = tree;
0041             if(m_tree)  {
0042                 tree->sortItems(1, Qt::DescendingOrder);
0043 
0044                 foreach (const QString &semanticClass, KoRdfSemanticItemRegistry::instance()->classNames()) {
0045                     QTreeWidgetItem *treeWidgetItem = new QTreeWidgetItem(tree);
0046                     treeWidgetItem->setText(0, KoRdfSemanticItemRegistry::instance()->classDisplayName(semanticClass));
0047                     tree->expandItem(treeWidgetItem);
0048                     m_treeWidgetItems.insert(semanticClass, treeWidgetItem);
0049                 }
0050             }
0051         }
0052     void update(KoDocumentRdf *rdf, QSharedPointer<Soprano::Model> model);
0053     /**
0054      * Add the name of each selected child of parent to the 'ret' set.
0055      */
0056     void buildSelectedSet(QTreeWidgetItem *parent, QSet<QString> &ret);
0057     /**
0058      * Called from update() to reset the tree to a default state.
0059      */
0060     void clear(QTreeWidgetItem *parent);
0061 };
0062 
0063 KoRdfSemanticTree::KoRdfSemanticTree()
0064     : d(new KoRdfSemanticTreePrivate(0))
0065 {
0066 }
0067 
0068 KoRdfSemanticTree::KoRdfSemanticTree(QTreeWidget *tree)
0069     : d(new KoRdfSemanticTreePrivate (tree))
0070 {
0071 }
0072 KoRdfSemanticTree::KoRdfSemanticTree(const KoRdfSemanticTree &orig)
0073     : d(orig.d)
0074 {
0075 }
0076 
0077 KoRdfSemanticTree::~KoRdfSemanticTree()
0078 {
0079 }
0080 
0081 KoRdfSemanticTree KoRdfSemanticTree::createTree(QTreeWidget* tree)
0082 {
0083     KoRdfSemanticTree ret (tree);
0084     return ret;
0085 }
0086 
0087 void KoRdfSemanticTreePrivate::clear(QTreeWidgetItem *parent)
0088 {
0089     while (parent->childCount()) {
0090         QTreeWidgetItem* c = parent->child(0);
0091         parent->removeChild(c);
0092         delete c;
0093     }
0094 }
0095 
0096 void KoRdfSemanticTreePrivate::buildSelectedSet(QTreeWidgetItem *parent, QSet<QString> &ret)
0097 {
0098     for (int i = 0; i < parent->childCount(); ++i) {
0099         QTreeWidgetItem *c = parent->child(i);
0100         if (c->isSelected()) {
0101             if (KoRdfSemanticTreeWidgetItem *item = dynamic_cast<KoRdfSemanticTreeWidgetItem*>(c)) {
0102                 ret << item->semanticItem()->name();
0103             }
0104         }
0105     }
0106 }
0107 
0108 void KoRdfSemanticTreePrivate::update(KoDocumentRdf *rdf, QSharedPointer<Soprano::Model> model)
0109 {
0110     QHash<QString, QTreeWidgetItem*>::ConstIterator it = m_treeWidgetItems.constBegin();
0111     QHash<QString, QTreeWidgetItem*>::ConstIterator end = m_treeWidgetItems.constEnd();
0112     for( ; it != end; ++it) {
0113         const QString &semanticClass = it.key();
0114         QTreeWidgetItem *treeWidgetItem = it.value();
0115 
0116         QSet<QString> selectedItems;
0117         buildSelectedSet(treeWidgetItem, selectedItems);
0118         clear(treeWidgetItem);
0119         treeWidgetItem->setSelected(false);
0120 
0121         // TODO: for locations this unexplained hack is done, find out why and if really needed
0122         if (model && semanticClass == QLatin1String("Location")) {
0123             //
0124             // grab the lat/long triples from m_model into the passed model.
0125             //
0126             // geo84
0127             // <uri:dan84> <http://xmlns.com/foaf/0.1/based_near> _:genid1
0128             // _:genid1 <http://www.w3.org/2003/01/geo/wgs84_pos#lat> "51.47026" (empty)
0129             // _:genid1 <http://www.w3.org/2003/01/geo/wgs84_pos#long> "-2.59466" (empty)
0130             rdf->expandStatementsSubjectPointsTo(model);
0131             //kDebug(30015) << "expanding lists... old.sz:" << model->statementCount();
0132             // other geo is an rdf:list, so bring that in too
0133             rdf->expandStatementsToIncludeRdfLists(model);
0134             //kDebug(30015) << "expanding lists... new.sz:" << model->statementCount();
0135         }
0136 
0137         const QList<hKoRdfSemanticItem> semanticItems = KoRdfSemanticItem::fromList(rdf->semanticItems(semanticClass, model));
0138         foreach (hKoRdfSemanticItem semanticItem, semanticItems) {
0139             KoRdfSemanticTreeWidgetItem *item = semanticItem->createQTreeWidgetItem(treeWidgetItem);
0140             if (!item) { //FIXME: add this cuz rdf info dialog is crashing. I don't want to implement QTreeWidgetItem for AuthorSection
0141                 continue;
0142             }
0143 
0144             if (selectedItems.contains(item->semanticItem()->name())) {
0145                 item->setSelected(true);
0146             }
0147         }
0148         m_tree->expandItem(treeWidgetItem);
0149     }
0150 }
0151 
0152 void KoRdfSemanticTree::update(KoDocumentRdf *rdf, QSharedPointer<Soprano::Model> model)
0153 {
0154     d->update(rdf,model);
0155 }
0156 
0157 
0158 KoRdfSemanticTree &KoRdfSemanticTree::operator=(const KoRdfSemanticTree &other)
0159 {
0160     d = other.d;
0161     return *this;
0162 }