Baloo Widgets

tagwidget.cpp
1 /*
2  * This file is part of the Baloo KDE project.
3  * Copyright (C) 2006-2010 Sebastian Trueg <[email protected]>
4  * Copyright (C) 2011-2013 Vishesh Handa <[email protected]>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include "tagwidget.h"
23 #include "tagwidget_p.h"
24 #include "kblocklayout.h"
25 #include "kedittagsdialog_p.h"
26 #include "tagcheckbox.h"
27 
28 #include <KLocalizedString>
29 #include <KJob>
30 
31 #include <QPushButton>
32 #include <QBoxLayout>
33 #include <QLabel>
34 
35 using namespace Baloo;
36 
37 void TagWidgetPrivate::init( TagWidget* parent )
38 {
39  q = parent;
40  m_readOnly = false;
41  m_showAllLinkLabel = nullptr;
42  m_editTagsDialog = nullptr;
43 
44  QGridLayout* mainLayout = new QGridLayout( q );
45  mainLayout->setContentsMargins(0, 0, 0, 0);
46  //TODO spacingHint should be declared. Old code m_flowLayout = new KBlockLayout( 0, KDialog::spacingHint()*3 );
47  m_flowLayout = new KBlockLayout( 0 );
48  mainLayout->addLayout( m_flowLayout, 0, 0, 1, 2 );
49  mainLayout->setColumnStretch( 0, 1 );
50 }
51 
52 
53 void TagWidgetPrivate::rebuild()
54 {
55  buildTagHash( q->selectedTags() );
56 }
57 
58 
59 void TagWidgetPrivate::buildTagHash(const QStringList& tags)
60 {
61  qDeleteAll(m_checkBoxHash);
62  m_checkBoxHash.clear();
63 
64  for (const QString& tag : tags) {
65  getTagCheckBox(tag);
66  }
67 
68  delete m_showAllLinkLabel;
69  m_showAllLinkLabel = nullptr;
70 
71  if (m_readOnly && !tags.isEmpty()) {
72  return;
73  }
74 
75  m_showAllLinkLabel = new QLabel( q );
76  m_flowLayout->addWidget( m_showAllLinkLabel );
77  if (m_readOnly) {
78  m_showAllLinkLabel->setText(QStringLiteral("-"));
79  }
80  else {
81  QFont f(q->font());
82  f.setUnderline(true);
83  m_showAllLinkLabel->setFont(f);
84  m_showAllLinkLabel->setText( QLatin1String("<a href=\"add_tags\">") +
85  ( m_checkBoxHash.isEmpty() ? i18nc("@label", "Add...") : i18nc("@label", "Edit...") ) +
86  QLatin1String("</a>") );
87  q->connect( m_showAllLinkLabel, SIGNAL(linkActivated(QString)), SLOT(slotShowAll()) );
88  }
89 }
90 
91 
92 TagCheckBox* TagWidgetPrivate::getTagCheckBox(const QString& tag)
93 {
94  QMap<QString, TagCheckBox*>::iterator it = m_checkBoxHash.find(tag);
95  if( it == m_checkBoxHash.end() ) {
96  //kDebug() << "Creating checkbox for" << tag.genericLabel();
97  TagCheckBox* checkBox = new TagCheckBox(tag, q);
98  q->connect( checkBox, SIGNAL(tagClicked(QString)), SIGNAL(tagClicked(QString)) );
99  m_checkBoxHash.insert( tag, checkBox );
100  m_flowLayout->addWidget( checkBox );
101  return checkBox;
102  }
103  else {
104  return it.value();
105  }
106 }
107 
108 void TagWidgetPrivate::selectTags(const QStringList& tags)
109 {
110  buildTagHash( tags );
111 }
112 
113 
115  : QWidget( parent ),
116  d( new TagWidgetPrivate() )
117 {
119  d->init(this);
120 }
121 
122 
124 {
125  delete d;
126 }
127 
128 
130 {
131  QStringList tags;
132  QMapIterator<QString, TagCheckBox*> it(d->m_checkBoxHash);
133  while( it.hasNext() ) {
134  tags << it.next().key();
135  }
136  return tags;
137 }
138 
139 
141 {
142  return d->m_flowLayout->alignment();
143 }
144 
146 {
147  return d->m_readOnly;
148 }
149 
151 {
152  d->selectTags(tags);
153 }
154 
156 {
157  d->m_flowLayout->setAlignment( alignment );
158 }
159 
161 {
162  d->m_readOnly = readOnly;
163  d->rebuild();
164 }
165 
166 
167 void TagWidget::slotTagUpdateDone()
168 {
169  setEnabled( true );
170 }
171 
172 void TagWidget::slotShowAll()
173 {
174  d->m_editTagsDialog = new KEditTagsDialog( selectedTags(), this );
175  d->m_editTagsDialog->setWindowModality( Qt::ApplicationModal );
176  connect( d->m_editTagsDialog, SIGNAL(finished(int)), this, SLOT(slotKEditTagDialogFinished(int)) );
177  d->m_editTagsDialog->open();
178 }
179 
180 void TagWidget::slotKEditTagDialogFinished(int result)
181 {
182  if( result == QDialog::Accepted ) {
183  setSelectedTags( d->m_editTagsDialog->tags() );
185  }
186 
187  d->m_editTagsDialog->deleteLater();
188  d->m_editTagsDialog = nullptr;
189 }
190 
void setContentsMargins(int left, int top, int right, int bottom)
bool readOnly() const
If the widget is read only.
Definition: tagwidget.cpp:145
void setUnderline(bool enable)
TagWidget(QWidget *parent=nullptr)
Constructor.
Definition: tagwidget.cpp:114
QPalette::ColorRole foregroundRole() const const
typedef Alignment
void setEnabled(bool)
void setReadyOnly(bool readOnly=true)
Set the TagWidget as read only.
Definition: tagwidget.cpp:160
QMapIterator::Item next()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setForegroundRole(QPalette::ColorRole role)
The KBlockLayout arranges widget in rows and columns like a text editor does.
Definition: kblocklayout.h:36
void selectionChanged(const QStringList &tags)
Emitted whenever the selection of tags changes.
Allows to change a selection of tags.
Definition: tagwidget.h:41
Qt::Alignment alignment() const
The alignment of the tags in the widget.
Definition: tagwidget.cpp:140
void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment)
void setColumnStretch(int column, int stretch)
~TagWidget() override
Destructor.
Definition: tagwidget.cpp:123
ApplicationModal
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setSelectedTags(const QStringList &tags)
Set the list of selected tags.
Definition: tagwidget.cpp:150
void setAlignment(Qt::Alignment alignment)
Set the alignment to use.
Definition: tagwidget.cpp:155
QStringList selectedTags() const
The list of selected tags.
Definition: tagwidget.cpp:129
Q_EMITQ_EMIT
bool hasNext() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Oct 21 2021 23:07:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.