Akonadi

tagwidget.cpp
1 /*
2  This file is part of Akonadi
3 
4  SPDX-FileCopyrightText: 2010 Tobias Koenig <[email protected]>
5  SPDX-FileCopyrightText: 2014 Christian Mollekopf <[email protected]>
6  SPDX-FileCopyrightText: 2016-2022 Laurent Montel <[email protected]>
7 
8  SPDX-License-Identifier: LGPL-2.0-or-later
9 */
10 
11 #include "tagwidget.h"
12 
13 #include "changerecorder.h"
14 #include "tagmodel.h"
15 #include "tagselectiondialog.h"
16 
17 #include <KLocalizedString>
18 
19 #include <QContextMenuEvent>
20 #include <QHBoxLayout>
21 #include <QLocale>
22 #include <QMenu>
23 #include <QToolButton>
24 
25 using namespace Akonadi;
26 
27 namespace Akonadi
28 {
29 class TagView : public QLineEdit
30 {
31  Q_OBJECT
32 public:
33  explicit TagView(QWidget *parent)
34  : QLineEdit(parent)
35  {
36  }
37 
38  void contextMenuEvent(QContextMenuEvent *event) override
39  {
40  if (text().isEmpty()) {
41  return;
42  }
43 
44  QMenu menu;
45  menu.addAction(i18n("Clear"), this, &TagView::clearTags);
46  menu.exec(event->globalPos());
47  }
48 
49 Q_SIGNALS:
50  void clearTags();
51 };
52 
53 } // namespace Akonadi
54 
55 // include after defining TagView
56 #include "ui_tagwidget.h"
57 
58 class Akonadi::TagWidgetPrivate
59 {
60 public:
61  Ui::TagWidget ui;
62  Akonadi::Tag::List mTags;
63  Akonadi::TagModel *mModel = nullptr;
64 };
65 
66 TagWidget::TagWidget(QWidget *parent)
67  : QWidget(parent)
68  , d(new TagWidgetPrivate)
69 {
70  auto monitor = new Monitor(this);
71  monitor->setObjectName(QStringLiteral("TagWidgetMonitor"));
72  monitor->setTypeMonitored(Monitor::Tags);
73  d->mModel = new Akonadi::TagModel(monitor, this);
74  connect(monitor, &Monitor::tagAdded, this, &TagWidget::updateView);
75 
76  d->ui.setupUi(this);
77  connect(d->ui.tagView, &TagView::clearTags, this, &TagWidget::clearTags);
78 
79  connect(d->ui.editButton, &QToolButton::clicked, this, &TagWidget::editTags);
80  connect(d->mModel, &Akonadi::TagModel::populated, this, &TagWidget::updateView);
81 }
82 
83 TagWidget::~TagWidget() = default;
84 
85 void TagWidget::clearTags()
86 {
87  if (!d->mTags.isEmpty()) {
88  d->mTags.clear();
89  d->ui.tagView->clear();
90  Q_EMIT selectionChanged(d->mTags);
91  }
92 }
93 
94 void TagWidget::setSelection(const Akonadi::Tag::List &tags)
95 {
96  if (d->mTags != tags) {
97  d->mTags = tags;
98  updateView();
99  Q_EMIT selectionChanged(d->mTags);
100  }
101 }
102 
103 Akonadi::Tag::List TagWidget::selection() const
104 {
105  return d->mTags;
106 }
107 
108 void TagWidget::setReadOnly(bool readOnly)
109 {
110  d->ui.editButton->setEnabled(!readOnly);
111  // d->mTagView is always readOnly => not change it.
112 }
113 
114 void TagWidget::editTags()
115 {
117  dlg->setSelection(d->mTags);
118  if (dlg->exec() == QDialog::Accepted) {
119  d->mTags = dlg->selection();
120  updateView();
121  Q_EMIT selectionChanged(d->mTags);
122  }
123 }
124 
125 void TagWidget::updateView()
126 {
127  QStringList tagsNames;
128  // Load the real tag names from the model
129  for (int i = 0; i < d->mModel->rowCount(); ++i) {
130  const QModelIndex index = d->mModel->index(i, 0);
131  const auto tag = d->mModel->data(index, Akonadi::TagModel::TagRole).value<Akonadi::Tag>();
132  if (d->mTags.contains(tag)) {
133  tagsNames.push_back(tag.name());
134  }
135  }
136  d->ui.tagView->setText(QLocale::system().createSeparatedList(tagsNames));
137 }
138 
139 #include "tagwidget.moc"
Q_OBJECTQ_OBJECT
QString createSeparatedList(const QStringList &list) const const
Q_EMITQ_EMIT
A widget that shows a tag selection and provides means to edit that selection.
An Akonadi Tag.
Definition: tag.h:25
void clicked(bool checked)
void tagAdded(const Akonadi::Tag &tag)
This signal is emitted if a tag has been added to Akonadi storage.
void push_back(const T &value)
Monitors an item or collection for changes.
Definition: monitor.h:71
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QAction * addAction(const QString &text)
QLocale system()
QString i18n(const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
Q_SIGNALSQ_SIGNALS
QAction * exec()
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jun 30 2022 03:51:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.