retained the AnnotatorTextEdit with some corrections"
[semnotes:semnotes.git] / src / resourcewidget.cpp
1 /*
2  * This file is part of SemNotes Project.
3  *
4  * Copyright (C) 2009 - 2010 Laura Dragan <laura.dragan@deri.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 #include "resourcewidget.h"
22 #include "note.h"
23 #include "filtermanager.h"
24
25 #include <QTreeWidgetItem>
26 #include <KDebug>
27 #include <Nepomuk/Resource>
28 #include <Nepomuk/Thing>
29 #include <Nepomuk/Variant>
30
31 namespace SemNotes
32 {
33
34 ResourceWidget::ResourceWidget(QWidget * parent)
35         : QTreeWidget(parent)
36 {
37     setColumnCount(1);
38     setHeaderHidden(true);
39     setRootIsDecorated(false);
40     m_filterManager = FilterManager::self();
41     connect(this, SIGNAL(itemActivated(QTreeWidgetItem*, int)), this, SLOT(resourceActivated(QTreeWidgetItem*, int)));
42 }
43
44 ResourceWidget::~ResourceWidget()
45 {
46 }
47
48 void ResourceWidget::populate(QList<Note> notes)
49 {
50     clear();
51     QHash<QString, int > referenceCount; // key=resource uri, value=reference count
52     QHash<QString, QSet<QString> > resources; // key=type uri, value=resource uris
53     QHash<QString, QString> labels; // key=resource uri, value=resource label
54
55     foreach(Note note, notes) {
56         QList<Nepomuk::Thing> things = note.isRelated();
57         foreach(Nepomuk::Thing thing, things) {
58             QString uri = thing.uri();
59             QString label = thing.label().isEmpty() ? thing.groundingOccurrences().at(0).label() : thing.label();
60             QList<QUrl> types = thing.types();
61             foreach(QUrl type, types) {
62                 QSet<QString> res = resources.value(type.toString(), QSet<QString>());
63                 res.insert(uri);
64                 resources.insert(type.toString(), res);
65                 Nepomuk::Resource t = Nepomuk::Resource(type);
66                 labels.insert(type.toString(), t.property(QUrl("http://www.w3.org/2000/01/rdf-schema#label")).toString());
67             }
68             referenceCount.insert(uri, referenceCount.value(uri, 0) + 1);
69             labels.insert(uri, label);
70         }
71     }
72
73     QList<QString> types = resources.keys();
74     foreach(QString type, types) {
75         QTreeWidgetItem * typeItem = new QTreeWidgetItem();
76         int count = 0;
77         QSet<QString> instances = resources.value(type);
78         foreach(QString instance, instances) {
79             QTreeWidgetItem * resItem = new QTreeWidgetItem(typeItem);
80             resItem->setData(0, Qt::DisplayRole, QVariant(QString("%1 (%2)").arg(labels.value(instance)).arg(referenceCount.value(instance))));
81             resItem->setData(0, Qt::UserRole, QVariant(instance));
82             if (m_filterManager->isResourceFilterSet(instance)) {
83                 resItem->setBackgroundColor(0, QColor(0, 153, 51, 128));
84             }
85             typeItem->addChild(resItem);
86             count = count + referenceCount.value(instance);
87         }
88         typeItem->setData(0, Qt::DisplayRole, QVariant(QString("%1 (%2)").arg(labels.value(type)).arg(count)));
89         addTopLevelItem(typeItem);
90     }
91     expandAll();
92 }
93
94 void ResourceWidget::resourceActivated(QTreeWidgetItem *item, int column)
95 {
96     Q_UNUSED(column);
97     if (item->childCount() > 0) {
98         // why doesn't this work?
99         if (item->isExpanded()) {
100             kDebug() << "collapsing";
101 //            collapseItem(item);
102             setItemExpanded(item, false);
103         } else {
104             kDebug() << "expanding";
105 //            expandItem(item);
106             setItemExpanded(item , true);
107         }
108     } else {
109         QString uri = item->data(0, Qt::UserRole).toString();
110         if (m_filterManager->isResourceFilterSet(uri)) {
111             m_filterManager->removeResourceFilter(uri);
112         } else {
113             m_filterManager->addResourceFilter(uri);
114         }
115     }
116 }
117
118 }
119
120 #include "resourcewidget.moc"