Baloo Widgets

tagwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2006-2010 Sebastian Trueg <trueg@kde.org>
3 SPDX-FileCopyrightText: 2011-2013 Vishesh Handa <me@vhanda.in>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "tagwidget.h"
9#include "kblocklayout.h"
10#include "kedittagsdialog_p.h"
11#include "tagcheckbox.h"
12
13#include <KLocalizedString>
14
15#include <QLabel>
16#include <QMap>
17
18using namespace Baloo;
19
20class Baloo::TagWidgetPrivate
21{
22public:
23 void init(TagWidget *parent);
24 void rebuild();
25 void buildTagHash(const QStringList &tags);
26
27 /// create clickable tag label
28 void addTagLabel(const QString &tag);
29
30 /// check the corresponding checkboxes and even
31 /// add missing checkboxes
32 void selectTags(const QStringList &tags);
33
34 bool m_readOnly = false;
35
37 QLabel *m_showAllLinkLabel = nullptr;
38 KBlockLayout *m_flowLayout = nullptr;
39 TagWidget *q;
40
41 void showEditDialog();
42 void kEditTagDialogFinished(int result);
43 KEditTagsDialog *m_editTagsDialog = nullptr;
44};
45
46void TagWidgetPrivate::init(TagWidget *parent)
47{
48 q = parent;
49
50 auto mainLayout = new QGridLayout(q);
51 mainLayout->setContentsMargins(0, 0, 0, 0);
52 // TODO spacingHint should be declared. Old code m_flowLayout = new KBlockLayout( 0, KDialog::spacingHint()*3 );
53 m_flowLayout = new KBlockLayout(0);
54 mainLayout->addLayout(m_flowLayout, 0, 0, 1, 2);
55 mainLayout->setColumnStretch(0, 1);
56}
57
58void TagWidgetPrivate::rebuild()
59{
60 buildTagHash(q->selectedTags());
61}
62
63void TagWidgetPrivate::buildTagHash(const QStringList &tags)
64{
65 qDeleteAll(m_tagLabels);
66 m_tagLabels.clear();
67
68 for (const QString &tag : tags) {
69 addTagLabel(tag);
70 }
71
72 delete m_showAllLinkLabel;
73 m_showAllLinkLabel = nullptr;
74
75 if (m_readOnly && !tags.isEmpty()) {
76 return;
77 }
78
79 m_showAllLinkLabel = new QLabel(q);
80 m_flowLayout->addWidget(m_showAllLinkLabel);
81 if (m_readOnly) {
82 m_showAllLinkLabel->setTextFormat(Qt::PlainText);
83 m_showAllLinkLabel->setText(QStringLiteral("-"));
84 } else {
85 m_showAllLinkLabel->setTextFormat(Qt::RichText);
86 m_showAllLinkLabel->setText(QStringLiteral("<a href=\"add_tags\">%1</a>").arg(m_tagLabels.isEmpty() ? i18nc("@label", "Add...") : i18nc("@label", "Edit...")));
87 q->connect(m_showAllLinkLabel, &QLabel::linkActivated, [this]{showEditDialog();});
88 }
89}
90
91void TagWidgetPrivate::addTagLabel(const QString &tag)
92{
93 const auto it = m_tagLabels.find(tag);
94 if (it == m_tagLabels.end()) {
95 auto label = new TagCheckBox(tag, q);
96 q->connect(label, &TagCheckBox::tagClicked, q, &TagWidget::tagClicked);
97 m_tagLabels.insert(tag, label);
98 m_flowLayout->addWidget(label);
99 }
100}
101
102void TagWidgetPrivate::selectTags(const QStringList &tags)
103{
104 buildTagHash(tags);
105}
106
108 : QWidget(parent)
109 , d(new TagWidgetPrivate())
110{
111 setForegroundRole(parent->foregroundRole());
112 d->init(this);
113}
114
115TagWidget::~TagWidget() = default;
116
118{
119 return d->m_tagLabels.keys();
120}
121
123{
124 return d->m_flowLayout->alignment();
125}
126
128{
129 return d->m_readOnly;
130}
131
133{
134 d->selectTags(tags);
135}
136
138{
139 d->m_flowLayout->setAlignment(alignment);
140}
141
142void TagWidget::setReadyOnly(bool readOnly)
143{
144 d->m_readOnly = readOnly;
145 d->rebuild();
146}
147
148void TagWidgetPrivate::showEditDialog()
149{
150 m_editTagsDialog = new KEditTagsDialog(m_tagLabels.keys(), q);
151 m_editTagsDialog->setWindowModality(Qt::ApplicationModal);
152 q->connect(m_editTagsDialog, &QDialog::finished, q, [this](int result) {kEditTagDialogFinished(result);});
153 m_editTagsDialog->open();
154}
155
156void TagWidgetPrivate::kEditTagDialogFinished(int result)
157{
158 if (result == QDialog::Accepted) {
159 selectTags(m_editTagsDialog->tags());
160 Q_EMIT q->selectionChanged(m_tagLabels.keys());
161 }
162
163 m_editTagsDialog->deleteLater();
164 m_editTagsDialog = nullptr;
165}
166
167#include "moc_tagwidget.cpp"
Allows to change a selection of tags.
Definition tagwidget.h:31
void selectionChanged(const QStringList &tags)
Emitted whenever the selection of tags changes.
void setSelectedTags(const QStringList &tags)
Set the list of selected tags.
TagWidget(QWidget *parent=nullptr)
Constructor.
void tagClicked(const QString &)
This signal is emitted whenever a tag is clicked.
Qt::Alignment alignment() const
The alignment of the tags in the widget.
~TagWidget() override
Destructor.
bool readOnly() const
If the widget is read only.
QStringList selectedTags() const
The list of selected tags.
void setAlignment(Qt::Alignment alignment)
Set the alignment to use.
void setReadyOnly(bool readOnly=true)
Set the TagWidget as read only.
The KBlockLayout arranges widget in rows and columns like a text editor does.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString label(StandardShortcut id)
void finished(int result)
void linkActivated(const QString &link)
void setText(const QString &)
void setTextFormat(Qt::TextFormat)
void addWidget(QWidget *w)
bool isEmpty() const const
void clear()
iterator end()
iterator find(const Key &key)
iterator insert(const Key &key, const T &value)
bool isEmpty() const const
QList< Key > keys() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
typedef Alignment
PlainText
ApplicationModal
void setForegroundRole(QPalette::ColorRole role)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:22 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.