]>
cloud.milkyroute.net Git - dolphin.git/blob - src/tagcloud/resourcetaggingwidget.cpp
2 This file is part of the Nepomuk KDE project.
3 Copyright (C) 2007 Sebastian Trueg <trueg@kde.org>
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.
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.
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.
20 #include "resourcetaggingwidget.h"
22 #include "taggingpopup.h"
24 #include <QtGui/QVBoxLayout>
25 #include <QtGui/QContextMenuEvent>
26 #include <QtGui/QCursor>
27 #include <QtGui/QLabel>
28 #include <QtCore/QSet>
33 inline uint
qHash( const Tag
& res
)
35 return qHash( res
.resourceUri().toString() );
40 class Nepomuk::ResourceTaggingWidget::Private
43 QList
<Nepomuk::Resource
> resources
;
45 TagCloud
* resourceTagCloud
;
48 QList
<Tag
> resourceTags
;
50 void showTaggingPopup( const QPoint
& );
51 void _k_slotShowTaggingPopup();
52 static QList
<Tag
> intersectTags( const QList
<Resource
>& );
56 void Nepomuk::ResourceTaggingWidget::Private::showTaggingPopup( const QPoint
& pos
)
59 resourceTags
= intersectTags( resources
);
60 Q_FOREACH( Tag tag
, resourceTags
) {
61 popup
->setTagSelected( tag
, true );
66 foreach( Resource res
, resources
) {
67 res
.setTags( resourceTags
);
70 resourceTagCloud
->showTags( resourceTags
);
74 void Nepomuk::ResourceTaggingWidget::Private::_k_slotShowTaggingPopup()
76 showTaggingPopup( QCursor::pos() );
80 QList
<Nepomuk::Tag
> Nepomuk::ResourceTaggingWidget::Private::intersectTags( const QList
<Resource
>& res
)
82 if ( res
.count() == 1 ) {
83 return res
.first().tags();
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() ) );
100 Nepomuk::ResourceTaggingWidget::ResourceTaggingWidget( QWidget
* parent
)
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\">" + i18n( "Change tags..." ) + "</a>", this );
109 connect( changeTagsLabel
, SIGNAL( linkActivated( const QString
) ),
110 this, SLOT( _k_slotShowTaggingPopup() ) );
111 layout
->addWidget( changeTagsLabel
);
113 // the popup tag cloud
114 d
->popup
= new TaggingPopup
;
115 d
->popup
->setSelectionEnabled( true );
116 d
->popup
->setNewTagButtonEnabled( true );
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
& ) ) );
123 connect( d
->resourceTagCloud
, SIGNAL( tagClicked( const Nepomuk::Tag
& ) ),
124 this, SIGNAL( tagClicked( const Nepomuk::Tag
& ) ) );
128 Nepomuk::ResourceTaggingWidget::~ResourceTaggingWidget()
135 void Nepomuk::ResourceTaggingWidget::setResource( const Nepomuk::Resource
& res
)
137 setResources( QList
<Resource
>() << res
);
141 void Nepomuk::ResourceTaggingWidget::setResources( const QList
<Nepomuk::Resource
>& resList
)
143 d
->resources
= resList
;
144 d
->resourceTagCloud
->showTags( d
->intersectTags( resList
) );
148 void Nepomuk::ResourceTaggingWidget::slotTagToggled( const Nepomuk::Tag
& tag
, bool enabled
)
151 d
->resourceTags
.append( tag
);
154 d
->resourceTags
.removeAll( tag
);
160 void Nepomuk::ResourceTaggingWidget::slotTagAdded( const Nepomuk::Tag
& tag
)
162 // assign it right away
163 d
->resourceTags
.append( tag
);
164 // d->resource.addTag( tag );
168 void Nepomuk::ResourceTaggingWidget::contextMenuEvent( QContextMenuEvent
* e
)
170 d
->showTaggingPopup( e
->globalPos() );
173 #include "resourcetaggingwidget.moc"