Akonadi

tagwidget.cpp
1/*
2 This file is part of Akonadi
3
4 SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
5 SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
6 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
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
25using namespace Akonadi;
26
27namespace Akonadi
28{
29class TagView : public QLineEdit
30{
32public:
33 explicit TagView(QWidget *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 void mousePressEvent(QMouseEvent *event) override
49 {
50 Q_EMIT addTags();
52 }
53
55 void clearTags();
56 void addTags();
57};
58
59} // namespace Akonadi
60
61// include after defining TagView
62#include "ui_tagwidget.h"
63
64class Akonadi::TagWidgetPrivate
65{
66public:
67 Ui::TagWidget ui;
69 Akonadi::TagModel *mModel = nullptr;
70};
71
72TagWidget::TagWidget(QWidget *parent)
73 : QWidget(parent)
74 , d(new TagWidgetPrivate)
75{
76 auto monitor = new Monitor(this);
77 monitor->setObjectName(QLatin1StringView("TagWidgetMonitor"));
78 monitor->setTypeMonitored(Monitor::Tags);
79 d->mModel = new Akonadi::TagModel(monitor, this);
80 connect(monitor, &Monitor::tagAdded, this, &TagWidget::updateView);
81
82 d->ui.setupUi(this);
83 connect(d->ui.tagView, &TagView::clearTags, this, &TagWidget::clearTags);
84 connect(d->ui.tagView, &TagView::addTags, this, &TagWidget::editTags);
85
86 connect(d->ui.editButton, &QToolButton::clicked, this, &TagWidget::editTags);
87 connect(d->mModel, &Akonadi::TagModel::populated, this, &TagWidget::updateView);
88}
89
90TagWidget::~TagWidget() = default;
91
92void TagWidget::clearTags()
93{
94 if (!d->mTags.isEmpty()) {
95 d->mTags.clear();
96 d->ui.tagView->clear();
97 Q_EMIT selectionChanged(d->mTags);
98 }
99}
100
101void TagWidget::setSelection(const Akonadi::Tag::List &tags)
102{
103 if (d->mTags != tags) {
104 d->mTags = tags;
105 updateView();
106 Q_EMIT selectionChanged(d->mTags);
107 }
108}
109
110Akonadi::Tag::List TagWidget::selection() const
111{
112 return d->mTags;
113}
114
115void TagWidget::setReadOnly(bool readOnly)
116{
117 d->ui.editButton->setEnabled(!readOnly);
118 // d->mTagView is always readOnly => not change it.
119}
120
121void TagWidget::editTags()
122{
124 dlg->setSelection(d->mTags);
125 if (dlg->exec() == QDialog::Accepted) {
126 d->mTags = dlg->selection();
127 updateView();
128 Q_EMIT selectionChanged(d->mTags);
129 }
130}
131
132void TagWidget::updateView()
133{
134 QStringList tagsNames;
135 // Load the real tag names from the model
136 for (int i = 0; i < d->mModel->rowCount(); ++i) {
137 const QModelIndex index = d->mModel->index(i, 0);
138 const auto tag = d->mModel->data(index, Akonadi::TagModel::TagRole).value<Akonadi::Tag>();
139 if (d->mTags.contains(tag)) {
140 tagsNames.push_back(tag.name());
141 }
142 }
143 d->ui.tagView->setText(QLocale::system().createSeparatedList(tagsNames));
144}
145
146#include "tagwidget.moc"
147
148#include "moc_tagwidget.cpp"
Monitors an item or collection for changes.
Definition monitor.h:71
void tagAdded(const Akonadi::Tag &tag)
This signal is emitted if a tag has been added to Akonadi storage.
A widget that shows a tag selection and provides means to edit that selection.
An Akonadi Tag.
Definition tag.h:26
QString i18n(const char *text, const TYPE &arg...)
Helper integration between Akonadi and Qt.
void clicked(bool checked)
virtual bool event(QEvent *e) override
virtual void mousePressEvent(QMouseEvent *e) override
void push_back(parameter_type value)
QLocale system()
QAction * addAction(const QIcon &icon, const QString &text, Functor functor, const QKeySequence &shortcut)
QAction * exec()
Q_EMITQ_EMIT
Q_OBJECTQ_OBJECT
Q_SIGNALSQ_SIGNALS
QObject * parent() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.