File indexing completed on 2025-01-05 04:47:11

0001 /*
0002   SPDX-FileCopyrightText: 2015-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: LGPL-2.0-or-later
0005   */
0006 
0007 #include "tagselectwidget.h"
0008 #include "monitor.h"
0009 #include "tageditwidget.h"
0010 #include "tagmodel.h"
0011 
0012 #include "shared/akranges.h"
0013 
0014 #include <QHBoxLayout>
0015 
0016 using namespace Akonadi;
0017 using namespace AkRanges;
0018 
0019 class Akonadi::TagSelectWidgetPrivate
0020 {
0021 public:
0022     QScopedPointer<TagEditWidget> mTagEditWidget;
0023 };
0024 
0025 TagSelectWidget::TagSelectWidget(QWidget *parent)
0026     : QWidget(parent)
0027     , d(new TagSelectWidgetPrivate())
0028 {
0029     auto mainLayout = new QHBoxLayout(this);
0030 
0031     auto monitor = new Monitor(this);
0032     monitor->setObjectName(QLatin1StringView("TagSelectWidgetMonitor"));
0033     monitor->setTypeMonitored(Monitor::Tags);
0034 
0035     auto model = new TagModel(monitor, this);
0036     d->mTagEditWidget.reset(new TagEditWidget());
0037     d->mTagEditWidget->setModel(model);
0038     d->mTagEditWidget->setSelectionEnabled(true);
0039     d->mTagEditWidget->setObjectName(QLatin1StringView("tageditwidget"));
0040 
0041     mainLayout->addWidget(d->mTagEditWidget.get());
0042 }
0043 
0044 TagSelectWidget::~TagSelectWidget() = default;
0045 
0046 void TagSelectWidget::setSelection(const Tag::List &tags)
0047 {
0048     d->mTagEditWidget->setSelection(tags);
0049 }
0050 
0051 Tag::List TagSelectWidget::selection() const
0052 {
0053     return d->mTagEditWidget->selection();
0054 }
0055 
0056 QStringList TagSelectWidget::tagToStringList() const
0057 {
0058     return selection() | Views::transform([](const auto &tag) {
0059                return tag.url().url();
0060            })
0061         | Actions::toQList;
0062 }
0063 
0064 void TagSelectWidget::setSelectionFromStringList(const QStringList &lst)
0065 {
0066     setSelection(lst | Views::transform([](const auto &cat) {
0067                      return Tag::fromUrl(QUrl{cat});
0068                  })
0069                  | Actions::toQVector);
0070 }
0071 
0072 #include "moc_tagselectwidget.cpp"