Baloo Widgets

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

KDE's Doxygen guidelines are available online.