]> cloud.milkyroute.net Git - dolphin.git/blob - src/tagcloud/resourcetaggingwidget.cpp
SVN_SILENT: assure that the position of the methods in the cpp file matchs with the...
[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/QAction>
28
29 #include <KLocale>
30
31
32 class Nepomuk::ResourceTaggingWidget::Private
33 {
34 public:
35 Nepomuk::Resource resource;
36
37 TagCloud* resourceTagCloud;
38 TaggingPopup* popup;
39
40 QList<Tag> resourceTags;
41
42 QAction* changeTagsAction;
43
44 void showTaggingPopup( const QPoint& );
45 void _k_slotShowTaggingPopup();
46 };
47
48
49 void Nepomuk::ResourceTaggingWidget::Private::showTaggingPopup( const QPoint& pos )
50 {
51 popup->showAllTags();
52 resourceTags = resource.tags();
53 Q_FOREACH( Tag tag, resourceTags ) {
54 popup->setTagSelected( tag, true );
55 }
56
57 popup->exec( pos );
58
59 resource.setTags( resourceTags );
60 }
61
62
63 void Nepomuk::ResourceTaggingWidget::Private::_k_slotShowTaggingPopup()
64 {
65 showTaggingPopup( QCursor::pos() );
66 }
67
68
69 Nepomuk::ResourceTaggingWidget::ResourceTaggingWidget( QWidget* parent )
70 : QWidget( parent ),
71 d( new Private() )
72 {
73 QVBoxLayout* layout = new QVBoxLayout( this );
74 layout->setMargin( 0 );
75 d->resourceTagCloud = new TagCloud( this );
76 layout->addWidget( d->resourceTagCloud );
77
78 d->changeTagsAction = new QAction( i18n( "Change tags..." ), this );
79 d->resourceTagCloud->setCustomNewTagAction( d->changeTagsAction );
80
81 // the popup tag cloud
82 d->popup = new TaggingPopup;
83 d->popup->setSelectionEnabled( true );
84 d->popup->setNewTagButtonEnabled( true );
85
86 connect( d->popup, SIGNAL( tagToggled( const Nepomuk::Tag&, bool ) ),
87 this, SLOT( slotTagToggled( const Nepomuk::Tag&, bool ) ) );
88 connect( d->popup, SIGNAL( tagAdded( const Nepomuk::Tag& ) ),
89 this, SLOT( slotTagAdded( const Nepomuk::Tag& ) ) );
90
91 connect( d->changeTagsAction, SIGNAL( activated() ),
92 this, SLOT( _k_slotShowTaggingPopup() ) );
93
94 connect( d->resourceTagCloud, SIGNAL( tagClicked( const Nepomuk::Tag& ) ),
95 this, SIGNAL( tagClicked( const Nepomuk::Tag& ) ) );
96 }
97
98
99 Nepomuk::ResourceTaggingWidget::~ResourceTaggingWidget()
100 {
101 delete d->popup;
102 delete d;
103 }
104
105
106 void Nepomuk::ResourceTaggingWidget::setResource( const Nepomuk::Resource& res )
107 {
108 d->resource = res;
109 d->resourceTagCloud->showResourceTags( res );
110 }
111
112
113 void Nepomuk::ResourceTaggingWidget::slotTagToggled( const Nepomuk::Tag& tag, bool enabled )
114 {
115 if ( enabled ) {
116 d->resourceTags.append( tag );
117 }
118 else {
119 d->resourceTags.removeAll( tag );
120 }
121 d->popup->hide();
122 }
123
124
125 void Nepomuk::ResourceTaggingWidget::slotTagAdded( const Nepomuk::Tag& tag )
126 {
127 // assign it right away
128 d->resourceTags.append( tag );
129 d->resource.addTag( tag );
130 }
131
132
133 void Nepomuk::ResourceTaggingWidget::contextMenuEvent( QContextMenuEvent* e )
134 {
135 d->showTaggingPopup( e->globalPos() );
136 }
137
138 #include "resourcetaggingwidget.moc"