• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.11.3 API Reference
  • KDE Home
  • Contact Us
 

akonadi/contact

  • akonadi
  • contact
  • editor
kedittagsdialog.cpp
1 /*****************************************************************************
2  * Copyright (C) 2009 by Peter Penz <peter.penz@gmx.at> *
3  * *
4  * This library is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU Library General Public *
6  * License version 2 as published by the Free Software Foundation. *
7  * *
8  * This library is distributed in the hope that it will be useful, *
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
11  * Library General Public License for more details. *
12  * *
13  * You should have received a copy of the GNU Library General Public License *
14  * along with this library; see the file COPYING.LIB. If not, write to *
15  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
16  * Boston, MA 02110-1301, USA. *
17  *****************************************************************************/
18 
19 #include "kedittagsdialog_p.h"
20 
21 #include <kicon.h>
22 #include <klineedit.h>
23 #include <klocalizedstring.h>
24 #include <kmessagebox.h>
25 
26 #include <QEvent>
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QListWidget>
30 #include <QPushButton>
31 #include <QTimer>
32 #include <QVBoxLayout>
33 #include <QWidget>
34 
35 KEditTagsDialog::KEditTagsDialog(const QVector<Nepomuk2::Tag>& tags,
36  QWidget* parent,
37  Qt::WindowFlags flags) :
38  KDialog(parent, flags),
39  m_tags(tags),
40  m_tagsList(0),
41  m_newTagItem(0),
42  m_deleteCandidate(0),
43  m_newTagEdit(0),
44  m_deleteButtonTimer(0)
45 {
46 
47  const QString caption = ( tags.count() > 0 ) ?
48  i18nc( "@title:window", "Change Tags" ) :
49  i18nc( "@title:window", "Add Tags" );
50  setCaption( caption );
51  setButtons( KDialog::Ok | KDialog::Cancel );
52  setDefaultButton( KDialog::Ok );
53 
54  QWidget *mainWidget = new QWidget( this );
55  QVBoxLayout *topLayout = new QVBoxLayout( mainWidget );
56 
57  QLabel *label = new QLabel( i18nc( "@label:textbox",
58  "Configure which tags should "
59  "be applied." ), this );
60 
61  m_tagsList = new QListWidget( this );
62  m_tagsList->setMouseTracking( true );
63  m_tagsList->setSortingEnabled( true );
64  m_tagsList->setSelectionMode( QAbstractItemView::NoSelection );
65  m_tagsList->installEventFilter( this );
66  connect( m_tagsList, SIGNAL(itemEntered(QListWidgetItem*)),
67  this, SLOT(slotItemEntered(QListWidgetItem*)) );
68  connect( m_tagsList, SIGNAL(itemEntered(QListWidgetItem*)),
69  this, SLOT(slotItemEntered(QListWidgetItem*)) );
70 
71  QLabel *newTagLabel = new QLabel( i18nc( "@label", "Create new tag:" ) );
72  m_newTagEdit = new KLineEdit( this );
73  m_newTagEdit->setClearButtonShown( true );
74  connect( m_newTagEdit, SIGNAL(textEdited(QString)),
75  this, SLOT(slotTextEdited(QString)) );
76 
77  QHBoxLayout *newTagLayout = new QHBoxLayout();
78  newTagLayout->addWidget( newTagLabel );
79  newTagLayout->addWidget( m_newTagEdit, 1 );
80 
81  topLayout->addWidget( label );
82  topLayout->addWidget( m_tagsList );
83  topLayout->addLayout( newTagLayout );
84 
85  setMainWidget( mainWidget );
86 
87  loadTags();
88 
89  // create the delete button, which is shown when
90  // hovering the items
91  m_deleteButton = new QPushButton( m_tagsList->viewport() );
92  m_deleteButton->setIcon( KIcon( QLatin1String( "edit-delete" ) ) );
93  m_deleteButton->setToolTip( i18nc( "@info", "Delete tag" ) );
94  m_deleteButton->hide();
95  connect( m_deleteButton, SIGNAL(clicked()), this, SLOT(deleteTag()) );
96 
97  m_deleteButtonTimer = new QTimer( this );
98  m_deleteButtonTimer->setSingleShot( true );
99  m_deleteButtonTimer->setInterval( 500 );
100  connect( m_deleteButtonTimer, SIGNAL(timeout()), this, SLOT(showDeleteButton()) );
101  readConfig();
102 }
103 
104 KEditTagsDialog::~KEditTagsDialog()
105 {
106  writeConfig();
107 }
108 
109 void KEditTagsDialog::readConfig()
110 {
111  KConfigGroup group( KGlobal::config(), "KEditTagsDialog" );
112  const QSize sizeDialog = group.readEntry( "Size", QSize(500,400) );
113  if ( sizeDialog.isValid() ) {
114  resize( sizeDialog );
115  }
116 }
117 
118 void KEditTagsDialog::writeConfig()
119 {
120  KConfigGroup group( KGlobal::config(), "KEditTagsDialog" );
121  group.writeEntry( "Size", size() );
122 }
123 
124 QVector<Nepomuk2::Tag> KEditTagsDialog::tags() const
125 {
126  return m_tags;
127 }
128 
129 bool KEditTagsDialog::eventFilter(QObject* watched, QEvent* event)
130 {
131  if ( ( watched == m_tagsList ) && ( event->type() == QEvent::Leave ) ) {
132  m_deleteButtonTimer->stop();
133  m_deleteButton->hide();
134  }
135  return KDialog::eventFilter( watched, event );
136 }
137 
138 void KEditTagsDialog::slotButtonClicked(int button)
139 {
140  if ( button == KDialog::Ok ) {
141  // update m_tags with the checked values, so
142  // that the caller of the KEditTagsDialog can
143  // receive the tags by KEditTagsDialog::tags()
144  m_tags.clear();
145 
146  const int count = m_tagsList->count();
147  for ( int i = 0; i < count; ++i ) {
148  QListWidgetItem* item = m_tagsList->item( i );
149  if ( item->checkState() == Qt::Checked ) {
150  const QString label = item->data( Qt::UserRole ).toString();
151  const QString uri = item->data(UrlTag).toString();
152  if (uri.isEmpty()) {
153  Nepomuk2::Tag tag( label );
154  tag.setLabel( label );
155  m_tags.append( tag );
156  } else {
157  Nepomuk2::Tag tag( uri );
158  m_tags.append( tag );
159  }
160  }
161  }
162 
163  accept();
164  } else {
165  KDialog::slotButtonClicked( button );
166  }
167 }
168 
169 void KEditTagsDialog::slotTextEdited(const QString& text)
170 {
171  // Remove unnecessary spaces from a new tag is
172  // mandatory, as the user cannot see the difference
173  // between a tag "Test" and "Test ".
174  const QString tagText = text.simplified();
175  if ( tagText.isEmpty() ) {
176  removeNewTagItem();
177  return;
178  }
179 
180  // Check whether the new tag already exists. If this
181  // is the case, remove the new tag item.
182  const int count = m_tagsList->count();
183  for ( int i = 0; i < count; ++i ) {
184  const QListWidgetItem* item = m_tagsList->item( i );
185  const bool remove = ( item->text() == tagText ) &&
186  ( ( m_newTagItem == 0 ) || ( m_newTagItem != item ) );
187  if ( remove ) {
188  m_tagsList->scrollToItem( item );
189  removeNewTagItem();
190  return;
191  }
192  }
193 
194  // There is no tag in the list with the passed text.
195  if ( m_newTagItem == 0 ) {
196  m_newTagItem = new QListWidgetItem( tagText, m_tagsList );
197  } else {
198  m_newTagItem->setText( tagText );
199  }
200  m_newTagItem->setData( Qt::UserRole, tagText );
201  m_newTagItem->setCheckState( Qt::Checked );
202  m_tagsList->scrollToItem( m_newTagItem );
203 }
204 
205 void KEditTagsDialog::slotItemEntered(QListWidgetItem* item)
206 {
207  // align the delete-button to stay on the right border
208  // of the item
209  const QRect rect = m_tagsList->visualItemRect( item );
210  const int size = rect.height();
211  const int x = rect.right() - size;
212  const int y = rect.top();
213  m_deleteButton->move( x, y );
214  m_deleteButton->resize( size, size );
215 
216  m_deleteCandidate = item;
217  m_deleteButtonTimer->start();
218 }
219 
220 void KEditTagsDialog::showDeleteButton()
221 {
222  m_deleteButton->show();
223 }
224 
225 void KEditTagsDialog::deleteTag()
226 {
227  Q_ASSERT( m_deleteCandidate != 0 );
228  const QString text = i18nc( "@info",
229  "Should the tag <resource>%1</resource> really be deleted for all files?",
230  m_deleteCandidate->text() );
231  const QString caption = i18nc( "@title", "Delete tag" );
232  const KGuiItem deleteItem( i18nc( "@action:button", "Delete" ), KIcon( QLatin1String( "edit-delete" ) ) );
233  const KGuiItem cancelItem( i18nc( "@action:button", "Cancel" ), KIcon( QLatin1String( "dialog-cancel" ) ) );
234  if ( KMessageBox::warningYesNo( this, text, caption, deleteItem, cancelItem ) == KMessageBox::Yes ) {
235  const QString label = m_deleteCandidate->data( Qt::UserRole ).toString();
236  Nepomuk2::Tag tag( label );
237  tag.remove();
238 
239  // clear list and reload it
240  for ( int i = m_tagsList->count() - 1; i >= 0; --i ) {
241  QListWidgetItem* item = m_tagsList->takeItem( i );
242  delete item;
243  }
244  loadTags();
245  }
246 }
247 
248 void KEditTagsDialog::loadTags()
249 {
250  // load all available tags and mark those tags as checked
251  // that have been passed to the KEditTagsDialog
252  foreach ( const Nepomuk2::Tag& tag, Nepomuk2::Tag::allTags() ) {
253  const QString label = tag.label();
254 
255  QListWidgetItem *item = new QListWidgetItem( label, m_tagsList );
256  item->setData( Qt::UserRole, label );
257  item->setData( UrlTag, tag.uri().toString());
258 
259  bool check = false;
260  foreach ( const Nepomuk2::Tag& selectedTag, m_tags ) {
261  if ( selectedTag.label() == label ) {
262  check = true;
263  break;
264  }
265  }
266  item->setCheckState( check ? Qt::Checked : Qt::Unchecked );
267  }
268 }
269 
270 void KEditTagsDialog::removeNewTagItem()
271 {
272  if ( m_newTagItem != 0 ) {
273  const int row = m_tagsList->row( m_newTagItem );
274  m_tagsList->takeItem( row );
275  delete m_newTagItem;
276  m_newTagItem = 0;
277  }
278 }
279 
280 #include "moc_kedittagsdialog_p.cpp"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Tue Nov 26 2013 09:03:47 by doxygen 1.8.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi/contact

Skip menu "akonadi/contact"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Related Pages

kdepimlibs-4.11.3 API Reference

Skip menu "kdepimlibs-4.11.3 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal