Baloo Widgets

filemetadataconfigwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013 Vishesh Handa <me@vhanda.in>
3 SPDX-FileCopyrightText: 2009 Peter Penz <peter.penz@gmx.at>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "filemetadataconfigwidget.h"
9#include "filemetadataprovider.h"
10
11#include <KConfig>
12#include <KConfigGroup>
13
14#include <QDebug>
15#include <QEvent>
16#include <QListWidget>
17#include <QVBoxLayout>
18
19using namespace Baloo;
20
21class Baloo::FileMetaDataConfigWidgetPrivate
22{
23public:
24 explicit FileMetaDataConfigWidgetPrivate(FileMetaDataConfigWidget *parent);
25 ~FileMetaDataConfigWidgetPrivate();
26
27 FileMetaDataConfigWidgetPrivate(const FileMetaDataConfigWidgetPrivate&) = delete;
28 FileMetaDataConfigWidget& operator=(const FileMetaDataConfigWidgetPrivate&) = delete;
29
30 void init();
31 void loadMetaData();
32 void addItem(const QString &property);
33
34 /**
35 * Is invoked after the meta data model has finished the loading of
36 * meta data. The meta data labels will be added to the configuration
37 * list.
38 */
39 void slotLoadingFinished();
40
41 int m_visibleDataTypes;
42 KFileItemList m_fileItems;
43 FileMetaDataProvider *m_provider;
44 QListWidget *m_metaDataList;
45
46private:
47 FileMetaDataConfigWidget *const q;
48};
49
50FileMetaDataConfigWidgetPrivate::FileMetaDataConfigWidgetPrivate(FileMetaDataConfigWidget *parent)
51 : m_visibleDataTypes(0)
52 , m_fileItems()
53 , m_provider(nullptr)
54 , m_metaDataList(nullptr)
55 , q(parent)
56{
57 m_metaDataList = new QListWidget(q);
58 m_metaDataList->setSelectionMode(QAbstractItemView::NoSelection);
59 m_metaDataList->setSortingEnabled(true);
60
61 auto layout = new QVBoxLayout(q);
62 layout->addWidget(m_metaDataList);
63
64 m_provider = new FileMetaDataProvider(q);
65 m_provider->setReadOnly(true);
66 QObject::connect(m_provider, SIGNAL(loadingFinished()), q, SLOT(slotLoadingFinished()));
67}
68
69FileMetaDataConfigWidgetPrivate::~FileMetaDataConfigWidgetPrivate() = default;
70
71void FileMetaDataConfigWidgetPrivate::loadMetaData()
72{
73 m_metaDataList->clear();
74 m_provider->setItems(m_fileItems);
75}
76
77void FileMetaDataConfigWidgetPrivate::addItem(const QString &key)
78{
79 // Meta information provided by Baloo that is already
80 // available from KFileItem as "fixed item" (see above)
81 // should not be shown as second entry.
82 static const char *const hiddenProperties[] = {
83 "comment", // = fixed item kfileitem#comment
84 "contentSize", // = fixed item kfileitem#size
85
86 nullptr // mandatory last entry
87 };
88
89 int i = 0;
90 while (hiddenProperties[i] != nullptr) {
91 if (key == QLatin1String(hiddenProperties[i])) {
92 // the item is hidden
93 return;
94 }
95 ++i;
96 }
97
98 // the item is not hidden, add it to the list
99 KConfig config(QStringLiteral("baloofileinformationrc"), KConfig::NoGlobals);
100 KConfigGroup settings = config.group(QStringLiteral("Show"));
101
102 const QString label = m_provider->label(key);
103
104 auto item = new QListWidgetItem(label, m_metaDataList);
105 item->setData(Qt::UserRole, key);
106 const bool show = settings.readEntry(key, true);
107 item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
108}
109
110void FileMetaDataConfigWidgetPrivate::slotLoadingFinished()
111{
112 // Get all meta information labels that are available for
113 // the currently shown file item and add them to the list.
114 Q_ASSERT(m_provider != nullptr);
115
116 m_metaDataList->clear();
117
118 QVariantMap data = m_provider->data();
119 // Always show these 3
120 data.remove(QStringLiteral("rating"));
121 data.remove(QStringLiteral("tags"));
122 data.remove(QStringLiteral("userComment"));
123
124 QVariantMap::const_iterator it = data.constBegin();
125 while (it != data.constEnd()) {
126 addItem(it.key());
127 ++it;
128 }
129
130 addItem(QStringLiteral("rating"));
131 addItem(QStringLiteral("tags"));
132 addItem(QStringLiteral("userComment"));
133}
134
135FileMetaDataConfigWidget::FileMetaDataConfigWidget(QWidget *parent)
136 : QWidget(parent)
137 , d(new FileMetaDataConfigWidgetPrivate(this))
138{
139}
140
141FileMetaDataConfigWidget::~FileMetaDataConfigWidget() = default;
142
143void FileMetaDataConfigWidget::setItems(const KFileItemList &items)
144{
145 d->m_fileItems = items;
146 d->loadMetaData();
147}
148
149KFileItemList FileMetaDataConfigWidget::items() const
150{
151 return d->m_fileItems;
152}
153
154void FileMetaDataConfigWidget::save()
155{
156 KConfig config(QStringLiteral("baloofileinformationrc"), KConfig::NoGlobals);
157 KConfigGroup showGroup = config.group(QStringLiteral("Show"));
158
159 const int count = d->m_metaDataList->count();
160 for (int i = 0; i < count; ++i) {
161 QListWidgetItem *item = d->m_metaDataList->item(i);
162 const bool show = (item->checkState() == Qt::Checked);
163 const QString key = item->data(Qt::UserRole).toString();
164 showGroup.writeEntry(key, show);
165 }
166
167 showGroup.sync();
168}
169
170bool FileMetaDataConfigWidget::event(QEvent *event)
171{
172 if (event->type() == QEvent::Polish) {
173 qDebug() << "GOT POLISH EVENT!!!";
174 // loadMetaData() must be invoked asynchronously, as the list
175 // must finish it's initialization first
176 QMetaObject::invokeMethod(this, "loadMetaData", Qt::QueuedConnection);
177 }
178 return QWidget::event(event);
179 ;
180}
181
182QSize FileMetaDataConfigWidget::sizeHint() const
183{
184 return d->m_metaDataList->sizeHint();
185}
186
187#include "moc_filemetadataconfigwidget.cpp"
Provides the data for the MetaDataWidget.
void setItems(const KFileItemList &items)
Sets the items, where the meta data should be requested.
virtual QString label(const QString &metaDataLabel) const
KConfigGroup group(const QString &group)
void writeEntry(const char *key, const char *value, WriteConfigFlags pFlags=Normal)
QString readEntry(const char *key, const char *aDefault=nullptr) const
bool sync() override
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QString label(StandardShortcut id)
void clear()
Qt::CheckState checkState() const const
virtual QVariant data(int role) const const
bool invokeMethod(QObject *context, Functor &&function, FunctorReturnType *ret)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QueuedConnection
UserRole
QString toString() const const
virtual bool event(QEvent *event) override
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 12:00:13 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.