Baloo Widgets

filemetadataconfigwidget.cpp
1 /*****************************************************************************
2  * Copyright (C) 2013 by Vishesh Handa <[email protected]> *
3  * Copyright (C) 2009 by Peter Penz <[email protected]> *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Library General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Library General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Library General Public License *
16  * along with this library; see the file COPYING.LIB. If not, write to *
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18  * Boston, MA 02110-1301, USA. *
19  *****************************************************************************/
20 
21 #include "filemetadataconfigwidget.h"
22 #include "filemetadataprovider.h"
23 
24 #include <KConfig>
25 #include <KConfigGroup>
26 
27 #include <QEvent>
28 #include <QListWidget>
29 #include <QVBoxLayout>
30 #include <QDebug>
31 
32 using namespace Baloo;
33 
34 class FileMetaDataConfigWidget::Private
35 {
36 public:
38  ~Private();
39 
40  void init();
41  void loadMetaData();
42  void addItem(const QString& property);
43 
44  /**
45  * Is invoked after the meta data model has finished the loading of
46  * meta data. The meta data labels will be added to the configuration
47  * list.
48  */
49  void slotLoadingFinished();
50 
51  int m_visibleDataTypes;
52  KFileItemList m_fileItems;
53  FileMetaDataProvider* m_provider;
54  QListWidget* m_metaDataList;
55 
56 private:
57  FileMetaDataConfigWidget* const q;
58 };
59 
60 
61 FileMetaDataConfigWidget::Private::Private(FileMetaDataConfigWidget* parent) :
62  m_visibleDataTypes(0),
63  m_fileItems(),
64  m_provider(nullptr),
65  m_metaDataList(nullptr),
66  q(parent)
67 {
68  m_metaDataList = new QListWidget(q);
69  m_metaDataList->setSelectionMode(QAbstractItemView::NoSelection);
70  m_metaDataList->setSortingEnabled(true);
71 
72  QVBoxLayout* layout = new QVBoxLayout(q);
73  layout->addWidget(m_metaDataList);
74 
75  m_provider = new FileMetaDataProvider(q);
76  m_provider->setReadOnly(true);
77  connect(m_provider, SIGNAL(loadingFinished()), q, SLOT(slotLoadingFinished()));
78 }
79 
80 FileMetaDataConfigWidget::Private::~Private()
81 {
82 }
83 
84 void FileMetaDataConfigWidget::Private::loadMetaData()
85 {
86  m_metaDataList->clear();
87  m_provider->setItems(m_fileItems);
88 }
89 
90 void FileMetaDataConfigWidget::Private::addItem(const QString& key)
91 {
92  // Meta information provided by Baloo that is already
93  // available from KFileItem as "fixed item" (see above)
94  // should not be shown as second entry.
95  static const char* const hiddenProperties[] = {
96  "comment", // = fixed item kfileitem#comment
97  "contentSize", // = fixed item kfileitem#size
98 
99  nullptr // mandatory last entry
100  };
101 
102  int i = 0;
103  while (hiddenProperties[i] != nullptr) {
104  if (key == QLatin1String(hiddenProperties[i])) {
105  // the item is hidden
106  return;
107  }
108  ++i;
109  }
110 
111  // the item is not hidden, add it to the list
112  KConfig config(QStringLiteral("baloofileinformationrc"), KConfig::NoGlobals);
113  KConfigGroup settings = config.group("Show");
114 
115  const QString label = m_provider->label(key);
116 
117  QListWidgetItem* item = new QListWidgetItem(label, m_metaDataList);
118  item->setData(Qt::UserRole, key);
119  const bool show = settings.readEntry(key, true);
120  item->setCheckState(show ? Qt::Checked : Qt::Unchecked);
121 }
122 
123 void FileMetaDataConfigWidget::Private::slotLoadingFinished()
124 {
125  // Get all meta information labels that are available for
126  // the currently shown file item and add them to the list.
127  Q_ASSERT(m_provider != nullptr);
128 
129  m_metaDataList->clear();
130 
131  QVariantMap data = m_provider->data();
132  // Always show these 3
133  data.remove(QStringLiteral("rating"));
134  data.remove(QStringLiteral("tags"));
135  data.remove(QStringLiteral("userComment"));
136 
137  QVariantMap::const_iterator it = data.constBegin();
138  while (it != data.constEnd()) {
139  addItem(it.key());
140  ++it;
141  }
142 
143  addItem(QStringLiteral("rating"));
144  addItem(QStringLiteral("tags"));
145  addItem(QStringLiteral("userComment"));
146 }
147 
148 FileMetaDataConfigWidget::FileMetaDataConfigWidget(QWidget* parent) :
149  QWidget(parent),
150  d(new Private(this))
151 {
152 }
153 
154 FileMetaDataConfigWidget::~FileMetaDataConfigWidget()
155 {
156  delete d;
157 }
158 
160 {
161  d->m_fileItems = items;
162  d->loadMetaData();
163 }
164 
165 KFileItemList FileMetaDataConfigWidget::items() const
166 {
167  return d->m_fileItems;
168 }
169 
171 {
172  KConfig config(QStringLiteral("baloofileinformationrc"), KConfig::NoGlobals);
173  KConfigGroup showGroup = config.group("Show");
174 
175  const int count = d->m_metaDataList->count();
176  for (int i = 0; i < count; ++i) {
177  QListWidgetItem* item = d->m_metaDataList->item(i);
178  const bool show = (item->checkState() == Qt::Checked);
179  const QString key = item->data(Qt::UserRole).toString();
180  showGroup.writeEntry(key, show);
181  }
182 
183  showGroup.sync();
184 }
185 
186 bool FileMetaDataConfigWidget::event(QEvent* event)
187 {
188  if (event->type() == QEvent::Polish) {
189  qDebug() << "GOT POLISH EVENT!!!";
190  // loadMetaData() must be invoked asynchronously, as the list
191  // must finish it's initialization first
192  QMetaObject::invokeMethod(this, "loadMetaData", Qt::QueuedConnection);
193  }
194  return QWidget::event(event);;
195 }
196 
198 {
199  return d->m_metaDataList->sizeHint();
200 }
201 
202 #include "moc_filemetadataconfigwidget.cpp"
bool sync() override
QEvent::Type type() const const
Qt::CheckState checkState() const const
Provides the data for the MetaDataWidget.
void writeEntry(const QString &key, const QVariant &value, WriteConfigFlags pFlags=Normal)
void setItems(const KFileItemList &items)
Sets the items, for which the visibility of the meta data should be configured.
KSharedConfigPtr config()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QVariant property(const char *name) const const
QString label(StandardShortcut id)
UserRole
virtual QVariant data(int role) const const
void setCheckState(Qt::CheckState state)
virtual void setData(int role, const QVariant &value)
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)
QCA_EXPORT void init()
void save()
Saves the modified configuration.
KConfigGroup group(const QString &group)
QueuedConnection
QObject * parent() const const
QString toString() const const
virtual bool event(QEvent *event) override
T readEntry(const QString &key, const T &aDefault) const
Widget which allows to configure which meta data should be shown in the FileMetadataWidget.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 18 2021 23:07:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.