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

akonadi

  • akonadi
tageditwidget.cpp
1 /*
2  This file is part of Akonadi
3 
4  Copyright (c) 2014 Christian Mollekopf <mollekopf@kolabsys.com>
5 
6  This library is free software; you can redistribute it and/or modify it
7  under the terms of the GNU Library General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or (at your
9  option) any later version.
10 
11  This library is distributed in the hope that it will be useful, but WITHOUT
12  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
14  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 the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301, USA.
20 */
21 #include "tageditwidget_p.h"
22 
23 #include <kicon.h>
24 #include <klineedit.h>
25 #include <klocalizedstring.h>
26 #include <kmessagebox.h>
27 #include <kcheckableproxymodel.h>
28 
29 #include <akonadi/changerecorder.h>
30 #include <akonadi/tagcreatejob.h>
31 #include <akonadi/tagdeletejob.h>
32 #include <akonadi/tagfetchscope.h>
33 #include <akonadi/tagattribute.h>
34 #include "tagmodel.h"
35 
36 #include <QEvent>
37 #include <QHBoxLayout>
38 #include <QLabel>
39 #include <QListWidget>
40 #include <QPushButton>
41 #include <QTimer>
42 #include <QVBoxLayout>
43 #include <QWidget>
44 
45 using namespace Akonadi;
46 
47 class TagEditWidget::Private : public QObject
48 {
49  Q_OBJECT
50 public:
51  Private(Akonadi::TagModel *model, QWidget *parent);
52 
53 public Q_SLOTS:
54  void slotTextEdited(const QString &text);
55  void slotItemEntered(const QModelIndex &index);
56  void showDeleteButton();
57  void deleteTag();
58  void slotCreateTag();
59  void slotCreateTagFinished(KJob *job);
60  void onRowsInserted(const QModelIndex &parent, int start, int end);
61 
62 public:
63  void select(const QModelIndex &parent, int start, int end, QItemSelectionModel::SelectionFlag selectionFlag);
64  enum ItemType {
65  UrlTag = Qt::UserRole + 1
66  };
67 
68  QWidget *d;
69  Akonadi::Tag::List m_tags;
70  Akonadi::TagModel *m_model;
71  QListView *m_tagsView;
72  KCheckableProxyModel *m_checkableProxy;
73  QModelIndex m_deleteCandidate;
74  QPushButton *m_newTagButton;
75  KLineEdit *m_newTagEdit;
76 
77  QPushButton *m_deleteButton;
78  QTimer *m_deleteButtonTimer;
79 };
80 
81 TagEditWidget::Private::Private(Akonadi::TagModel *model, QWidget *parent)
82  : QObject(),
83  d(parent),
84  m_model(model),
85  m_tagsView(0),
86  m_newTagButton(0),
87 
88  m_newTagEdit(0),
89  m_deleteButton(0),
90  m_deleteButtonTimer(0)
91 {
92 
93 }
94 
95 void TagEditWidget::Private::select(const QModelIndex &parent, int start, int end, QItemSelectionModel::SelectionFlag selectionFlag)
96 {
97  QItemSelection selection;
98  for (int i = start; i <= end; i++) {
99  const QModelIndex index = m_model->index(i, 0, parent);
100  const Akonadi::Tag insertedTag = index.data(Akonadi::TagModel::TagRole).value<Akonadi::Tag>();
101  if (m_tags.contains(insertedTag)) {
102  selection.select(index, index);
103  }
104  }
105  m_checkableProxy->selectionModel()->select(selection, selectionFlag);
106 }
107 
108 void TagEditWidget::Private::onRowsInserted(const QModelIndex &parent, int start, int end)
109 {
110  select(parent, start, end, QItemSelectionModel::Select);
111 }
112 
113 void TagEditWidget::Private::slotCreateTag()
114 {
115  Akonadi::TagCreateJob *createJob = new Akonadi::TagCreateJob(Akonadi::Tag(m_newTagEdit->text()), this);
116  connect(createJob, SIGNAL(finished(KJob*)),
117  this, SLOT(slotCreateTagFinished(KJob*)));
118 
119  m_newTagEdit->clear();
120  m_newTagEdit->setEnabled(false);
121  m_newTagButton->setEnabled(false);
122 }
123 
124 void TagEditWidget::Private::slotCreateTagFinished(KJob *job)
125 {
126  if (job->error()) {
127  KMessageBox::error(d, i18n("An error occurred while creating a new tag"),
128  i18n("Failed to create a new tag"));
129  }
130 
131  m_newTagEdit->setEnabled(true);
132 }
133 
134 void TagEditWidget::Private::slotTextEdited(const QString &text)
135 {
136  // Remove unnecessary spaces from a new tag is
137  // mandatory, as the user cannot see the difference
138  // between a tag "Test" and "Test ".
139  const QString tagText = text.simplified();
140  if (tagText.isEmpty()) {
141  m_newTagButton->setEnabled(false);
142  return;
143  }
144 
145  // Check whether the new tag already exists
146  const int count = m_model->rowCount();
147  bool exists = false;
148  for (int i = 0; i < count; ++i) {
149  const QModelIndex index = m_model->index(i, 0, QModelIndex());
150  if (index.data(Qt::DisplayRole).toString() == tagText) {
151  exists = true;
152  break;
153  }
154  }
155  m_newTagButton->setEnabled(!exists);
156 }
157 
158 void TagEditWidget::Private::slotItemEntered(const QModelIndex &index)
159 {
160  // align the delete-button to stay on the right border
161  // of the item
162  const QRect rect = m_tagsView->visualRect(index);
163  const int size = rect.height();
164  const int x = rect.right() - size;
165  const int y = rect.top();
166  m_deleteButton->move(x, y);
167  m_deleteButton->resize(size, size);
168 
169  m_deleteCandidate = index;
170  m_deleteButtonTimer->start();
171 }
172 
173 void TagEditWidget::Private::showDeleteButton()
174 {
175  m_deleteButton->show();
176 }
177 
178 void TagEditWidget::Private::deleteTag()
179 {
180  Q_ASSERT(m_deleteCandidate.isValid());
181  const Akonadi::Tag tag = m_deleteCandidate.data(Akonadi::TagModel::TagRole).value<Akonadi::Tag>();
182  const QString text = i18nc("@info",
183  "Do you really want to remove the tag <resource>%1</resource>?",
184  tag.name());
185  const QString caption = i18nc("@title", "Delete tag");
186  const KGuiItem deleteItem(i18nc("@action:button", "Delete"), KIcon(QLatin1String("edit-delete")));
187  const KGuiItem cancelItem(i18nc("@action:button", "Cancel"), KIcon(QLatin1String("dialog-cancel")));
188  if (KMessageBox::warningYesNo(d, text, caption, deleteItem, cancelItem) == KMessageBox::Yes) {
189  new Akonadi::TagDeleteJob(tag, this);
190  }
191 }
192 
193 TagEditWidget::TagEditWidget(Akonadi::TagModel *model, QWidget *parent, bool enableSelection)
194  : QWidget(parent),
195  d(new Private(model, this))
196 {
197  QVBoxLayout *topLayout = new QVBoxLayout(this);
198 
199  QItemSelectionModel *selectionModel = new QItemSelectionModel(d->m_model, this);
200  d->m_checkableProxy = new KCheckableProxyModel(this);
201  d->m_checkableProxy->setSourceModel(d->m_model);
202  d->m_checkableProxy->setSelectionModel(selectionModel);
203  connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d.data(), SLOT(onRowsInserted(QModelIndex,int,int)));
204 
205  d->m_tagsView = new QListView(this);
206  d->m_tagsView->setMouseTracking(true);
207  d->m_tagsView->setSelectionMode(QAbstractItemView::NoSelection);
208  d->m_tagsView->installEventFilter(this);
209  if (enableSelection) {
210  d->m_tagsView->setModel(d->m_checkableProxy);
211  } else {
212  d->m_tagsView->setModel(d->m_model);
213  }
214  connect(d->m_tagsView, SIGNAL(entered(QModelIndex)),
215  d.data(), SLOT(slotItemEntered(QModelIndex)));
216 
217  d->m_newTagEdit = new KLineEdit(this);
218  d->m_newTagEdit->setClearButtonShown(true);
219  connect(d->m_newTagEdit, SIGNAL(textEdited(QString)),
220  d.data(), SLOT(slotTextEdited(QString)));
221 
222  d->m_newTagButton = new QPushButton(i18nc("@label", "Create new tag"));
223  d->m_newTagButton->setEnabled(false);
224  connect(d->m_newTagButton , SIGNAL(clicked(bool)),
225  d.data(), SLOT(slotCreateTag()));
226 
227  QHBoxLayout *newTagLayout = new QHBoxLayout();
228  newTagLayout->addWidget(d->m_newTagEdit, 1);
229  newTagLayout->addWidget(d->m_newTagButton);
230 
231  if (enableSelection) {
232  QLabel *label = new QLabel(i18nc("@label:textbox",
233  "Configure which tags should "
234  "be applied."), this);
235  topLayout->addWidget(label);
236  }
237  topLayout->addWidget(d->m_tagsView);
238  topLayout->addLayout(newTagLayout);
239 
240  setLayout(topLayout);
241 
242  // create the delete button, which is shown when
243  // hovering the items
244  d->m_deleteButton = new QPushButton(d->m_tagsView->viewport());
245  d->m_deleteButton->setIcon(KIcon(QLatin1String("edit-delete")));
246  d->m_deleteButton->setToolTip(i18nc("@info", "Delete tag"));
247  d->m_deleteButton->hide();
248  connect(d->m_deleteButton, SIGNAL(clicked()), d.data(), SLOT(deleteTag()));
249 
250  d->m_deleteButtonTimer = new QTimer(this);
251  d->m_deleteButtonTimer->setSingleShot(true);
252  d->m_deleteButtonTimer->setInterval(500);
253  connect(d->m_deleteButtonTimer, SIGNAL(timeout()), d.data(), SLOT(showDeleteButton()));
254 }
255 
256 TagEditWidget::~TagEditWidget()
257 {
258 
259 }
260 
261 void TagEditWidget::setSelection(const Akonadi::Tag::List &tags)
262 {
263  d->m_tags = tags;
264  d->select(QModelIndex(), 0, d->m_model->rowCount() - 1, QItemSelectionModel::ClearAndSelect);
265 }
266 
267 Akonadi::Tag::List TagEditWidget::selection()
268 {
269  Akonadi::Tag::List list;
270  for (int i = 0; i < d->m_checkableProxy->rowCount(); ++i) {
271  if (d->m_checkableProxy->selectionModel()->isRowSelected(i, QModelIndex())) {
272  const QModelIndex index = d->m_checkableProxy->index(i, 0, QModelIndex());
273  const Akonadi::Tag tag = index.data(Akonadi::TagModel::TagRole).value<Akonadi::Tag>();
274  list << tag;
275  }
276  }
277  return list;
278 }
279 
280 bool TagEditWidget::eventFilter(QObject *watched, QEvent *event)
281 {
282  if ((watched == d->m_tagsView) && (event->type() == QEvent::Leave)) {
283  d->m_deleteButtonTimer->stop();
284  d->m_deleteButton->hide();
285  }
286  return QWidget::eventFilter(watched, event);
287 }
288 
289 
290 #include "tageditwidget.moc"
Akonadi::TagDeleteJob
Job that deletes tags.
Definition: tagdeletejob.h:35
Akonadi::TagCreateJob
Job that creates a new tag in the Akonadi storage.
Definition: tagcreatejob.h:34
Akonadi::Tag
An Akonadi Tag.
Definition: tag.h:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Mon Jul 21 2014 08:03:56 by doxygen 1.8.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

akonadi

Skip menu "akonadi"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • Modules
  • Related Pages

kdepimlibs-4.13.3 API Reference

Skip menu "kdepimlibs-4.13.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