1/*
2 This file is part of Akonadi
3
4 Copyright (c) 2010 Tobias Koenig <tokoe@kde.org>
5 Copyright (c) 2014 Christian Mollekopf <mollekopf@kolabsys.com>
6
7 This library is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Library General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or (at your
10 option) any later version.
11
12 This library is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15 License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.
21*/
22
23#include "tagwidget.h"
24
25#include "tagmodel.h"
26#include "changerecorder.h"
27#include "tagselectiondialog.h"
28
29#include <kicon.h>
30#include <klocalizedstring.h>
31#include <ksqueezedtextlabel.h>
32
33#include <QHBoxLayout>
34#include <QToolButton>
35
36using namespace Akonadi;
37
38struct TagWidget::Private {
39 QLabel *mTagLabel;
40 Akonadi::Tag::List mTags;
41 Akonadi::TagModel *mModel;
42};
43
44TagWidget::TagWidget(QWidget *parent)
45 : QWidget(parent)
46 , d(new Private)
47{
48 Monitor *monitor = new Monitor(this);
49 monitor->setTypeMonitored(Monitor::Tags);
50 d->mModel = new Akonadi::TagModel(monitor, this);
51
52 QHBoxLayout *layout = new QHBoxLayout(this);
53 d->mTagLabel = new KSqueezedTextLabel;
54 d->mTagLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
55 layout->addWidget(d->mTagLabel);
56
57 QToolButton *editButton = new QToolButton;
58 editButton->setText(i18n("..."));
59 layout->addWidget(editButton, Qt::AlignRight);
60
61 layout->setStretch(0, 10);
62
63 connect(editButton, SIGNAL(clicked()), SLOT(editTags()));
64}
65
66TagWidget::~TagWidget()
67{
68}
69
70void TagWidget::setSelection(const Akonadi::Tag::List &tags)
71{
72 d->mTags = tags;
73 updateView();
74}
75
76Akonadi::Tag::List TagWidget::selection() const
77{
78 return d->mTags;
79}
80
81void TagWidget::editTags()
82{
83 QScopedPointer<Akonadi::TagSelectionDialog> dlg(new TagSelectionDialog(this));
84 dlg->setSelection(d->mTags);
85 if (dlg->exec() == QDialog::Accepted) {
86 d->mTags = dlg->selection();
87 updateView();
88 emit selectionChanged(d->mTags);
89 }
90}
91
92void TagWidget::updateView()
93{
94 QStringList tagsNames;
95 // Load the real tag names from the model
96 for (int i = 0; i < d->mModel->rowCount(); ++i) {
97 const QModelIndex index = d->mModel->index(i, 0);
98 const Akonadi::Tag tag = d->mModel->data(index, Akonadi::TagModel::TagRole).value<Akonadi::Tag>();
99 if (d->mTags.contains(tag)) {
100 tagsNames << tag.name();
101 }
102 }
103 d->mTagLabel->setText(tagsNames.join(QLatin1String(", ")));
104}
105