Baloo Widgets

tagwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2006-2010 Sebastian Trueg <[email protected]>
3  SPDX-FileCopyrightText: 2011-2013 Vishesh Handa <[email protected]>
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 #include "tagwidget_p.h"
13 
14 #include <KJob>
15 #include <KLocalizedString>
16 
17 #include <QBoxLayout>
18 #include <QLabel>
19 #include <QPushButton>
20 
21 using namespace Baloo;
22 
23 void TagWidgetPrivate::init(TagWidget *parent)
24 {
25  q = parent;
26  m_readOnly = false;
27  m_showAllLinkLabel = nullptr;
28  m_editTagsDialog = nullptr;
29 
30  auto mainLayout = new QGridLayout(q);
31  mainLayout->setContentsMargins(0, 0, 0, 0);
32  // TODO spacingHint should be declared. Old code m_flowLayout = new KBlockLayout( 0, KDialog::spacingHint()*3 );
33  m_flowLayout = new KBlockLayout(0);
34  mainLayout->addLayout(m_flowLayout, 0, 0, 1, 2);
35  mainLayout->setColumnStretch(0, 1);
36 }
37 
38 void TagWidgetPrivate::rebuild()
39 {
40  buildTagHash(q->selectedTags());
41 }
42 
43 void TagWidgetPrivate::buildTagHash(const QStringList &tags)
44 {
45  qDeleteAll(m_checkBoxHash);
46  m_checkBoxHash.clear();
47 
48  for (const QString &tag : tags) {
49  getTagCheckBox(tag);
50  }
51 
52  delete m_showAllLinkLabel;
53  m_showAllLinkLabel = nullptr;
54 
55  if (m_readOnly && !tags.isEmpty()) {
56  return;
57  }
58 
59  m_showAllLinkLabel = new QLabel(q);
60  m_flowLayout->addWidget(m_showAllLinkLabel);
61  if (m_readOnly) {
62  m_showAllLinkLabel->setText(QStringLiteral("-"));
63  } else {
64  QFont f(q->font());
65  f.setUnderline(true);
66  m_showAllLinkLabel->setFont(f);
67  m_showAllLinkLabel->setText(QLatin1String("<a href=\"add_tags\">") + (m_checkBoxHash.isEmpty() ? i18nc("@label", "Add...") : i18nc("@label", "Edit..."))
68  + QLatin1String("</a>"));
69  q->connect(m_showAllLinkLabel, SIGNAL(linkActivated(QString)), SLOT(slotShowAll()));
70  }
71 }
72 
73 TagCheckBox *TagWidgetPrivate::getTagCheckBox(const QString &tag)
74 {
75  QMap<QString, TagCheckBox *>::iterator it = m_checkBoxHash.find(tag);
76  if (it == m_checkBoxHash.end()) {
77  // kDebug() << "Creating checkbox for" << tag.genericLabel();
78  auto checkBox = new TagCheckBox(tag, q);
79  q->connect(checkBox, SIGNAL(tagClicked(QString)), SIGNAL(tagClicked(QString)));
80  m_checkBoxHash.insert(tag, checkBox);
81  m_flowLayout->addWidget(checkBox);
82  return checkBox;
83  } else {
84  return it.value();
85  }
86 }
87 
88 void TagWidgetPrivate::selectTags(const QStringList &tags)
89 {
90  buildTagHash(tags);
91 }
92 
94  : QWidget(parent)
95  , d(new TagWidgetPrivate())
96 {
97  setForegroundRole(parent->foregroundRole());
98  d->init(this);
99 }
100 
101 TagWidget::~TagWidget() = default;
102 
104 {
105  QStringList tags;
106  QMapIterator<QString, TagCheckBox *> it(d->m_checkBoxHash);
107  while (it.hasNext()) {
108  tags << it.next().key();
109  }
110  return tags;
111 }
112 
114 {
115  return d->m_flowLayout->alignment();
116 }
117 
119 {
120  return d->m_readOnly;
121 }
122 
124 {
125  d->selectTags(tags);
126 }
127 
129 {
130  d->m_flowLayout->setAlignment(alignment);
131 }
132 
133 void TagWidget::setReadyOnly(bool readOnly)
134 {
135  d->m_readOnly = readOnly;
136  d->rebuild();
137 }
138 
139 void TagWidget::slotTagUpdateDone()
140 {
141  setEnabled(true);
142 }
143 
144 void TagWidget::slotShowAll()
145 {
146  d->m_editTagsDialog = new KEditTagsDialog(selectedTags(), this);
147  d->m_editTagsDialog->setWindowModality(Qt::ApplicationModal);
148  connect(d->m_editTagsDialog, SIGNAL(finished(int)), this, SLOT(slotKEditTagDialogFinished(int)));
149  d->m_editTagsDialog->open();
150 }
151 
152 void TagWidget::slotKEditTagDialogFinished(int result)
153 {
154  if (result == QDialog::Accepted) {
155  setSelectedTags(d->m_editTagsDialog->tags());
157  }
158 
159  d->m_editTagsDialog->deleteLater();
160  d->m_editTagsDialog = nullptr;
161 }
typedef Alignment
QStringList selectedTags() const
The list of selected tags.
Definition: tagwidget.cpp:103
bool readOnly() const
If the widget is read only.
Definition: tagwidget.cpp:118
Q_EMITQ_EMIT
ApplicationModal
void setForegroundRole(QPalette::ColorRole role)
~TagWidget() override
Destructor.
bool hasNext() const const
void selectionChanged(const QStringList &tags)
Emitted whenever the selection of tags changes.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
TagWidget(QWidget *parent=nullptr)
Constructor.
Definition: tagwidget.cpp:93
void setEnabled(bool)
QMapIterator::Item next()
void setSelectedTags(const QStringList &tags)
Set the list of selected tags.
Definition: tagwidget.cpp:123
The KBlockLayout arranges widget in rows and columns like a text editor does.
Definition: kblocklayout.h:20
void setAlignment(Qt::Alignment alignment)
Set the alignment to use.
Definition: tagwidget.cpp:128
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setReadyOnly(bool readOnly=true)
Set the TagWidget as read only.
Definition: tagwidget.cpp:133
Allows to change a selection of tags.
Definition: tagwidget.h:30
QObject * parent() const const
Qt::Alignment alignment() const
The alignment of the tags in the widget.
Definition: tagwidget.cpp:113
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 19 2022 04:12:56 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.