Akonadi

tagwidget.cpp
1 /*
2  This file is part of Akonadi
3 
4  Copyright (c) 2010 Tobias Koenig <[email protected]>
5  Copyright (c) 2014 Christian Mollekopf <[email protected]>
6  Copyright (C) 2016-2020 Laurent Montel <[email protected]>
7 
8  This library is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Library General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or (at your
11  option) any later version.
12 
13  This library is distributed in the hope that it will be useful, but WITHOUT
14  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
16  License for more details.
17 
18  You should have received a copy of the GNU Library General Public License
19  along with this library; see the file COPYING.LIB. If not, write to the
20  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  02110-1301, USA.
22 */
23 
24 #include "tagwidget.h"
25 
26 #include "tagmodel.h"
27 #include "changerecorder.h"
28 #include "tagselectiondialog.h"
29 
30 #include <KLocalizedString>
31 
32 #include <QHBoxLayout>
33 #include <QToolButton>
34 #include <QMenu>
35 #include <QContextMenuEvent>
36 #include <QLocale>
37 
38 using namespace Akonadi;
39 
40 namespace Akonadi
41 {
42 
43 class Q_DECL_HIDDEN TagView : public QLineEdit
44 {
45  Q_OBJECT
46 public:
47  explicit TagView(QWidget *parent)
48  : QLineEdit(parent)
49  {}
50 
51  void contextMenuEvent(QContextMenuEvent *event) override
52  {
53  if (text().isEmpty()) {
54  return;
55  }
56 
57  QMenu menu;
58  menu.addAction(i18n("Clear"), this, &TagView::clearTags);
59  menu.exec(event->globalPos());
60  }
61 
62 Q_SIGNALS:
63  void clearTags();
64 };
65 
66 }
67 
68 // include after defining TagView
69 #include "ui_tagwidget.h"
70 
71 class Q_DECL_HIDDEN TagWidget::Private
72 {
73 public:
74  Ui::TagWidget ui;
75  Akonadi::Tag::List mTags;
76  Akonadi::TagModel *mModel = nullptr;
77 };
78 
79 TagWidget::TagWidget(QWidget *parent)
80  : QWidget(parent)
81  , d(new Private)
82 {
83  Monitor *monitor = new Monitor(this);
84  monitor->setObjectName(QStringLiteral("TagWidgetMonitor"));
85  monitor->setTypeMonitored(Monitor::Tags);
86  d->mModel = new Akonadi::TagModel(monitor, this);
87  connect(monitor, &Monitor::tagAdded, this, &TagWidget::updateView);
88 
89  d->ui.setupUi(this);
90  connect(d->ui.tagView, &TagView::clearTags, this, &TagWidget::clearTags);
91 
92  connect(d->ui.editButton, &QToolButton::clicked, this, &TagWidget::editTags);
93  connect(d->mModel, &Akonadi::TagModel::populated, this, &TagWidget::updateView);
94 }
95 
96 TagWidget::~TagWidget() = default;
97 
98 void TagWidget::clearTags()
99 {
100  if (!d->mTags.isEmpty()) {
101  d->mTags.clear();
102  d->ui.tagView->clear();
103  Q_EMIT selectionChanged(d->mTags);
104  }
105 }
106 
107 void TagWidget::setSelection(const Akonadi::Tag::List &tags)
108 {
109  if (d->mTags != tags) {
110  d->mTags = tags;
111  updateView();
112  Q_EMIT selectionChanged(d->mTags);
113  }
114 }
115 
116 Akonadi::Tag::List TagWidget::selection() const
117 {
118  return d->mTags;
119 }
120 
121 void TagWidget::setReadOnly(bool readOnly)
122 {
123  d->ui.editButton->setEnabled(!readOnly);
124  //d->mTagView is always readOnly => not change it.
125 }
126 
127 void TagWidget::editTags()
128 {
130  dlg->setSelection(d->mTags);
131  if (dlg->exec() == QDialog::Accepted) {
132  d->mTags = dlg->selection();
133  updateView();
134  Q_EMIT selectionChanged(d->mTags);
135  }
136 }
137 
138 void TagWidget::updateView()
139 {
140  QStringList tagsNames;
141  // Load the real tag names from the model
142  for (int i = 0; i < d->mModel->rowCount(); ++i) {
143  const QModelIndex index = d->mModel->index(i, 0);
144  const Akonadi::Tag tag = d->mModel->data(index, Akonadi::TagModel::TagRole).value<Akonadi::Tag>();
145  if (d->mTags.contains(tag)) {
146  tagsNames.push_back(tag.name());
147  }
148  }
149  d->ui.tagView->setText(QLocale::system().createSeparatedList(tagsNames));
150 }
151 
152 #include "tagwidget.moc"
153 
void push_back(const T &value)
const QPoint & globalPos() const const
QAction * addAction(const QString &text)
void tagAdded(const Akonadi::Tag &tag)
This signal is emitted if a tag has been added to Akonadi storage.
QLocale system()
A widget that shows a tag selection and provides means to edit that selection.
void setObjectName(const QString &name)
void clicked(bool checked)
QAction * exec()
A widget that shows a tag selection and provides means to edit that selection.
Definition: tagwidget.h:41
T * data() const const
void setTypeMonitored(Type type, bool monitored=true)
Sets whether given type (Collection, Item, Tag should be monitored).
Definition: monitor.cpp:149
Monitors an item or collection for changes.
Definition: monitor.h:84
QString i18n(const char *text, const TYPE &arg...)
Definition: item.h:44
Helper integration between Akonadi and Qt.
An Akonadi Tag.
Definition: tag.h:39
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon May 25 2020 22:46:12 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.