]> cloud.milkyroute.net Git - dolphin.git/blob - src/tagcloud/resourcetaggingwidget.cpp
Fixed following issue for the information sidebar:
[dolphin.git] / src / tagcloud / resourcetaggingwidget.cpp
1 /*
2 This file is part of the Nepomuk KDE project.
3 Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18 */
19
20 #include "resourcetaggingwidget.h"
21 #include "tagcloud.h"
22 #include "taggingpopup.h"
23
24 #include <QtGui/QVBoxLayout>
25 #include <QtGui/QContextMenuEvent>
26 #include <QtGui/QCursor>
27 #include <QtGui/QLabel>
28 #include <QtCore/QSet>
29
30 #include <KLocale>
31
32 namespace Nepomuk {
33 inline uint qHash( const Tag& res )
34 {
35 return qHash( res.resourceUri().toString() );
36 }
37 }
38
39
40 class Nepomuk::ResourceTaggingWidget::Private
41 {
42 public:
43 QList<Nepomuk::Resource> resources;
44
45 TagCloud* resourceTagCloud;
46 TaggingPopup* popup;
47
48 QList<Tag> resourceTags;
49
50 void showTaggingPopup( const QPoint& );
51 void _k_slotShowTaggingPopup();
52 static QList<Tag> intersectTags( const QList<Resource>& );
53 };
54
55
56 void Nepomuk::ResourceTaggingWidget::Private::showTaggingPopup( const QPoint& pos )
57 {
58 popup->showAllTags();
59 resourceTags = intersectTags( resources );
60 Q_FOREACH( const Tag &tag, resourceTags ) {
61 popup->setTagSelected( tag, true );
62 }
63
64 popup->exec( pos );
65
66 foreach( Resource res, resources ) {
67 res.setTags( resourceTags );
68 }
69
70 resourceTagCloud->showTags( resourceTags );
71 }
72
73
74 void Nepomuk::ResourceTaggingWidget::Private::_k_slotShowTaggingPopup()
75 {
76 showTaggingPopup( QCursor::pos() );
77 }
78
79
80 QList<Nepomuk::Tag> Nepomuk::ResourceTaggingWidget::Private::intersectTags( const QList<Resource>& res )
81 {
82 if ( res.count() == 1 ) {
83 return res.first().tags();
84 }
85 else if ( !res.isEmpty() ) {
86 // determine the tags used for all resources
87 QSet<Tag> tags = QSet<Tag>::fromList( res.first().tags() );
88 QList<Resource>::const_iterator it = res.begin();
89 for ( ++it; it != res.end(); ++it ) {
90 tags.intersect( QSet<Tag>::fromList( (*it).tags() ) );
91 }
92 return tags.values();
93 }
94 else {
95 return QList<Tag>();
96 }
97 }
98
99
100 Nepomuk::ResourceTaggingWidget::ResourceTaggingWidget( QWidget* parent )
101 : QWidget( parent ),
102 d( new Private() )
103 {
104 QVBoxLayout* layout = new QVBoxLayout( this );
105 layout->setMargin( 0 );
106 d->resourceTagCloud = new TagCloud( this );
107 layout->addWidget( d->resourceTagCloud );
108 QLabel* changeTagsLabel = new QLabel( "<p align=center><a style=\"font-size:small;\" href=\"dummy\">" + i18nc( "@label", "Change tags..." ) + "</a>", this );
109 connect( changeTagsLabel, SIGNAL( linkActivated( const QString ) ),
110 this, SLOT( _k_slotShowTaggingPopup() ) );
111 layout->addWidget( changeTagsLabel );
112
113 // the popup tag cloud
114 d->popup = new TaggingPopup;
115 d->popup->setSelectionEnabled( true );
116 d->popup->setNewTagButtonEnabled( true );
117
118 connect( d->popup, SIGNAL( tagToggled( const Nepomuk::Tag&, bool ) ),
119 this, SLOT( slotTagToggled( const Nepomuk::Tag&, bool ) ) );
120 connect( d->popup, SIGNAL( tagAdded( const Nepomuk::Tag& ) ),
121 this, SLOT( slotTagAdded( const Nepomuk::Tag& ) ) );
122
123 connect( d->resourceTagCloud, SIGNAL( tagClicked( const Nepomuk::Tag& ) ),
124 this, SIGNAL( tagClicked( const Nepomuk::Tag& ) ) );
125 }
126
127
128 Nepomuk::ResourceTaggingWidget::~ResourceTaggingWidget()
129 {
130 delete d->popup;
131 delete d;
132 }
133
134
135 void Nepomuk::ResourceTaggingWidget::setResource( const Nepomuk::Resource& res )
136 {
137 setResources( QList<Resource>() << res );
138 }
139
140
141 void Nepomuk::ResourceTaggingWidget::setResources( const QList<Nepomuk::Resource>& resList )
142 {
143 d->resources = resList;
144 d->resourceTagCloud->showTags( d->intersectTags( resList ) );
145 }
146
147
148 void Nepomuk::ResourceTaggingWidget::slotTagToggled( const Nepomuk::Tag& tag, bool enabled )
149 {
150 if ( enabled ) {
151 d->resourceTags.append( tag );
152 }
153 else {
154 d->resourceTags.removeAll( tag );
155 }
156 d->popup->hide();
157 }
158
159
160 void Nepomuk::ResourceTaggingWidget::slotTagAdded( const Nepomuk::Tag& tag )
161 {
162 // assign it right away
163 d->resourceTags.append( tag );
164 // d->resource.addTag( tag );
165 }
166
167
168 void Nepomuk::ResourceTaggingWidget::contextMenuEvent( QContextMenuEvent* e )
169 {
170 d->showTaggingPopup( e->globalPos() );
171 }
172
173 #include "resourcetaggingwidget.moc"