File indexing completed on 2024-06-16 04:50:40

0001 /*
0002     This file is part of Akonadi
0003 
0004     SPDX-FileCopyrightText: 2010 Tobias Koenig <tokoe@kde.org>
0005     SPDX-FileCopyrightText: 2014 Christian Mollekopf <mollekopf@kolabsys.com>
0006     SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-or-later
0009 */
0010 
0011 #include "tagwidget.h"
0012 
0013 #include "changerecorder.h"
0014 #include "tagmodel.h"
0015 #include "tagselectiondialog.h"
0016 
0017 #include <KLocalizedString>
0018 
0019 #include <QContextMenuEvent>
0020 #include <QHBoxLayout>
0021 #include <QLocale>
0022 #include <QMenu>
0023 #include <QToolButton>
0024 
0025 using namespace Akonadi;
0026 
0027 namespace Akonadi
0028 {
0029 class TagView : public QLineEdit
0030 {
0031     Q_OBJECT
0032 public:
0033     explicit TagView(QWidget *parent)
0034         : QLineEdit(parent)
0035     {
0036     }
0037 
0038     void contextMenuEvent(QContextMenuEvent *event) override
0039     {
0040         if (text().isEmpty()) {
0041             return;
0042         }
0043 
0044         QMenu menu;
0045         menu.addAction(i18n("Clear"), this, &TagView::clearTags);
0046         menu.exec(event->globalPos());
0047     }
0048 
0049 Q_SIGNALS:
0050     void clearTags();
0051 };
0052 
0053 } // namespace Akonadi
0054 
0055 // include after defining TagView
0056 #include "ui_tagwidget.h"
0057 
0058 class Akonadi::TagWidgetPrivate
0059 {
0060 public:
0061     Ui::TagWidget ui;
0062     Akonadi::Tag::List mTags;
0063     Akonadi::TagModel *mModel = nullptr;
0064 };
0065 
0066 TagWidget::TagWidget(QWidget *parent)
0067     : QWidget(parent)
0068     , d(new TagWidgetPrivate)
0069 {
0070     auto monitor = new Monitor(this);
0071     monitor->setObjectName(QLatin1StringView("TagWidgetMonitor"));
0072     monitor->setTypeMonitored(Monitor::Tags);
0073     d->mModel = new Akonadi::TagModel(monitor, this);
0074     connect(monitor, &Monitor::tagAdded, this, &TagWidget::updateView);
0075 
0076     d->ui.setupUi(this);
0077     connect(d->ui.tagView, &TagView::clearTags, this, &TagWidget::clearTags);
0078 
0079     connect(d->ui.editButton, &QToolButton::clicked, this, &TagWidget::editTags);
0080     connect(d->mModel, &Akonadi::TagModel::populated, this, &TagWidget::updateView);
0081 }
0082 
0083 TagWidget::~TagWidget() = default;
0084 
0085 void TagWidget::clearTags()
0086 {
0087     if (!d->mTags.isEmpty()) {
0088         d->mTags.clear();
0089         d->ui.tagView->clear();
0090         Q_EMIT selectionChanged(d->mTags);
0091     }
0092 }
0093 
0094 void TagWidget::setSelection(const Akonadi::Tag::List &tags)
0095 {
0096     if (d->mTags != tags) {
0097         d->mTags = tags;
0098         updateView();
0099         Q_EMIT selectionChanged(d->mTags);
0100     }
0101 }
0102 
0103 Akonadi::Tag::List TagWidget::selection() const
0104 {
0105     return d->mTags;
0106 }
0107 
0108 void TagWidget::setReadOnly(bool readOnly)
0109 {
0110     d->ui.editButton->setEnabled(!readOnly);
0111     // d->mTagView is always readOnly => not change it.
0112 }
0113 
0114 void TagWidget::editTags()
0115 {
0116     QScopedPointer<Akonadi::TagSelectionDialog> dlg(new TagSelectionDialog(d->mModel, this));
0117     dlg->setSelection(d->mTags);
0118     if (dlg->exec() == QDialog::Accepted) {
0119         d->mTags = dlg->selection();
0120         updateView();
0121         Q_EMIT selectionChanged(d->mTags);
0122     }
0123 }
0124 
0125 void TagWidget::updateView()
0126 {
0127     QStringList tagsNames;
0128     // Load the real tag names from the model
0129     for (int i = 0; i < d->mModel->rowCount(); ++i) {
0130         const QModelIndex index = d->mModel->index(i, 0);
0131         const auto tag = d->mModel->data(index, Akonadi::TagModel::TagRole).value<Akonadi::Tag>();
0132         if (d->mTags.contains(tag)) {
0133             tagsNames.push_back(tag.name());
0134         }
0135     }
0136     d->ui.tagView->setText(QLocale::system().createSeparatedList(tagsNames));
0137 }
0138 
0139 #include "tagwidget.moc"
0140 
0141 #include "moc_tagwidget.cpp"