• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • kfile
kdevicelistmodel.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (C) 2006 Michael Larouche <michael.larouche@kdemail.net>
3 2007 Kevin Ottens <ervin@kde.org>
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 version 2 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18
19*/
20#include "kdevicelistmodel.h"
21#include "kdevicelistitem_p.h"
22
23#include <solid/devicenotifier.h>
24#include <solid/device.h>
25#include <solid/deviceinterface.h>
26
27#include <QTimer>
28
29#include <kdebug.h>
30#include <klocale.h>
31#include <kicon.h>
32
33class KDeviceListModel::Private
34{
35public:
36 Private(KDeviceListModel *self) : q(self), rootItem(new KDeviceListItem()) {}
37 ~Private() { delete rootItem; }
38
39
40 KDeviceListModel *q;
41
42 KDeviceListItem *rootItem;
43 QMap<QString, KDeviceListItem*> deviceItems;
44 Solid::Predicate predicate;
45
46
47 void initialize(const Solid::Predicate &p);
48 QModelIndex indexForItem(KDeviceListItem *item) const;
49 void addDevice(const Solid::Device &device);
50 void removeBranch(const QString &udi);
51
52 // Private slots
53 void _k_initDeviceList();
54 void _k_deviceAdded(const QString &udi);
55 void _k_deviceRemoved(const QString &udi);
56};
57
58KDeviceListModel::KDeviceListModel(QObject *parent)
59 : QAbstractItemModel(parent), d(new Private(this))
60{
61 d->deviceItems[QString()] = d->rootItem;
62 d->initialize(Solid::Predicate());
63}
64
65KDeviceListModel::KDeviceListModel(const QString &predicate, QObject *parent)
66 : QAbstractItemModel(parent), d(new Private(this))
67{
68 d->initialize(Solid::Predicate::fromString(predicate));
69}
70
71KDeviceListModel::KDeviceListModel(const Solid::Predicate &predicate, QObject *parent)
72 : QAbstractItemModel(parent), d(new Private(this))
73{
74 d->initialize(predicate);
75}
76
77KDeviceListModel::~KDeviceListModel()
78{
79 delete d;
80}
81
82void KDeviceListModel::Private::initialize(const Solid::Predicate &p)
83{
84 predicate = p;
85
86 // Delay load of hardware list when the event loop start
87 QTimer::singleShot( 0, q, SLOT(_k_initDeviceList()) );
88}
89
90QVariant KDeviceListModel::data(const QModelIndex &index, int role) const
91{
92 if( !index.isValid() )
93 return QVariant();
94
95 KDeviceListItem *deviceItem = static_cast<KDeviceListItem*>(index.internalPointer());
96 Solid::Device device = deviceItem->device();
97
98 QVariant returnData;
99 if (role == Qt::DisplayRole) {
100 returnData = device.product();
101 }
102 // Only display icons in the first column
103 else if (role == Qt::DecorationRole && index.column() == 0) {
104 returnData = KIcon(device.icon());
105 }
106
107 return returnData;
108}
109
110QVariant KDeviceListModel::headerData(int section, Qt::Orientation orientation, int role) const
111{
112 Q_UNUSED(section)
113
114 if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
115 {
116 return i18n("Device name");
117 }
118
119 return QVariant();
120}
121
122QModelIndex KDeviceListModel::index(int row, int column, const QModelIndex &parent) const
123{
124 if (row<0 || column!=0)
125 return QModelIndex();
126
127 KDeviceListItem *parentItem;
128 if (parent.isValid()) {
129 parentItem = static_cast<KDeviceListItem*>(parent.internalPointer());
130 } else {
131 parentItem = d->rootItem;
132 }
133
134 KDeviceListItem *childItem = parentItem->child(row);
135
136 if (childItem) {
137 return createIndex(row, column, childItem);
138 } else {
139 return QModelIndex();
140 }
141}
142
143QModelIndex KDeviceListModel::rootIndex() const
144{
145 return d->indexForItem(d->rootItem);
146}
147
148QModelIndex KDeviceListModel::parent(const QModelIndex &child) const
149{
150 if (!child.isValid())
151 return QModelIndex();
152
153 KDeviceListItem *childItem = static_cast<KDeviceListItem*>(child.internalPointer());
154 KDeviceListItem *parentItem = childItem->parent();
155
156 if (!parentItem)
157 return QModelIndex();
158 else
159 return d->indexForItem(parentItem);
160}
161
162int KDeviceListModel::rowCount(const QModelIndex &parent) const
163{
164 if( !parent.isValid() )
165 return d->rootItem->childCount();
166
167 KDeviceListItem *item = static_cast<KDeviceListItem*>(parent.internalPointer());
168
169 return item->childCount();
170}
171
172int KDeviceListModel::columnCount(const QModelIndex &parent) const
173{
174 Q_UNUSED(parent);
175 // We only know 1 information for a particualiar device.
176 return 1;
177}
178
179Solid::Device KDeviceListModel::deviceForIndex(const QModelIndex& index) const
180{
181 KDeviceListItem *deviceItem = static_cast<KDeviceListItem*>(index.internalPointer());
182 return deviceItem->device();
183}
184
185void KDeviceListModel::Private::_k_initDeviceList()
186{
187 Solid::DeviceNotifier *notifier = Solid::DeviceNotifier::instance();
188
189 connect(notifier, SIGNAL(deviceAdded(QString)),
190 q, SLOT(_k_deviceAdded(QString)));
191 connect(notifier, SIGNAL(deviceRemoved(QString)),
192 q, SLOT(_k_deviceRemoved(QString)));
193
194 // Use allDevices() from the manager if the predicate is not valid
195 // otherwise the returned list is empty
196 const QList<Solid::Device> &deviceList = predicate.isValid()?
197 Solid::Device::listFromQuery(predicate)
198 : Solid::Device::allDevices();
199
200 foreach(const Solid::Device &device, deviceList)
201 {
202 addDevice(device);
203 }
204
205 emit q->modelInitialized();
206}
207
208void KDeviceListModel::Private::addDevice(const Solid::Device &device)
209{
210 // Don't insert invalid devices
211 if (!device.isValid()) return;
212
213 // Don't insert devices that doesn't match the predicate set
214 // (except for the root)
215 if (!device.parentUdi().isEmpty()
216 && predicate.isValid() && !predicate.matches(device)) {
217 return;
218 }
219
220 KDeviceListItem *item;
221 if (deviceItems.contains(device.udi())) { // It was already inserted as a parent
222 item = deviceItems[device.udi()];
223 } else {
224 item = new KDeviceListItem();
225 deviceItems[device.udi()] = item;
226 }
227 item->setDevice(device);
228
229 KDeviceListItem *parent = rootItem;
230
231 if (!deviceItems.contains(device.parentUdi()) ) // The parent was not present, try to insert it in the model
232 addDevice( Solid::Device(device.parentUdi()) );
233
234 if (deviceItems.contains(device.parentUdi())) // Update the parent if the device is now present
235 parent = deviceItems[device.parentUdi()];
236
237 if (item->parent()!=parent) { // If it's already our parent no need to signal the new row
238 q->beginInsertRows(indexForItem(parent), parent->childCount(), parent->childCount());
239 item->setParent(parent);
240 q->endInsertRows();
241 }
242}
243
244void KDeviceListModel::Private::removeBranch(const QString &udi)
245{
246 if (!deviceItems.contains(udi)) return;
247
248 KDeviceListItem *item = deviceItems[udi];
249 KDeviceListItem *parent = item->parent();
250
251 QList<KDeviceListItem*> children = item->children();
252
253 foreach(KDeviceListItem *child, children) {
254 removeBranch(child->device().udi());
255 }
256
257 q->beginRemoveRows(indexForItem(parent),
258 item->row(), item->row());
259
260 item->setParent(0);
261 deviceItems.remove(udi);
262 delete item;
263
264 q->endRemoveRows();
265}
266
267void KDeviceListModel::Private::_k_deviceAdded(const QString &udi)
268{
269 Solid::Device device(udi);
270 addDevice(device);
271}
272
273void KDeviceListModel::Private::_k_deviceRemoved(const QString &udi)
274{
275 removeBranch(udi);
276}
277
278QModelIndex KDeviceListModel::Private::indexForItem(KDeviceListItem *item) const
279{
280 if (item==rootItem) {
281 return QModelIndex();
282 } else {
283 return q->createIndex(item->row(), 0, item);
284 }
285}
286
287#include "kdevicelistmodel.moc"
KDeviceListItem
Item for the Device List model Represent a Solid::Device in the tree device list model.
Definition: kdevicelistitem_p.h:41
KDeviceListItem::device
Solid::Device & device()
Get the Solid::Device reference for this item.
Definition: kdevicelistitem.cpp:102
KDeviceListItem::parent
KDeviceListItem * parent()
Get the parent of this item.
Definition: kdevicelistitem.cpp:92
KDeviceListItem::children
QList< KDeviceListItem * > children()
Get all the children of this item.
Definition: kdevicelistitem.cpp:55
KDeviceListItem::child
KDeviceListItem * child(int row)
Return a child of this item according to the given row.
Definition: kdevicelistitem.cpp:50
KDeviceListItem::setDevice
void setDevice(const Solid::Device &device)
Get the Solid::Device reference for this item.
Definition: kdevicelistitem.cpp:97
KDeviceListItem::childCount
int childCount() const
Helper method to get the numbers of childrens of this item.
Definition: kdevicelistitem.cpp:65
KDeviceListItem::row
int row() const
Get the relative(to parent) row position of this item in the tree.
Definition: kdevicelistitem.cpp:70
KDeviceListItem::setParent
void setParent(KDeviceListItem *parent)
Set the parent of this item.
Definition: kdevicelistitem.cpp:79
KDeviceListModel
Device list model in Qt's Interview framework.
Definition: kdevicelistmodel.h:40
KDeviceListModel::rootIndex
QModelIndex rootIndex() const
Definition: kdevicelistmodel.cpp:143
KDeviceListModel::parent
QModelIndex parent(const QModelIndex &child) const
Get the parent QModelIndex for the given model child.
Definition: kdevicelistmodel.cpp:148
KDeviceListModel::rowCount
int rowCount(const QModelIndex &parent=QModelIndex()) const
Get the number of rows for a model index.
Definition: kdevicelistmodel.cpp:162
KDeviceListModel::headerData
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Get the header data for a given section, orientation and role.
Definition: kdevicelistmodel.cpp:110
KDeviceListModel::KDeviceListModel
KDeviceListModel(QObject *parent=0)
Definition: kdevicelistmodel.cpp:58
KDeviceListModel::~KDeviceListModel
~KDeviceListModel()
Definition: kdevicelistmodel.cpp:77
KDeviceListModel::deviceForIndex
Solid::Device deviceForIndex(const QModelIndex &index) const
Definition: kdevicelistmodel.cpp:179
KDeviceListModel::columnCount
int columnCount(const QModelIndex &parent=QModelIndex()) const
Get the number of columns for a model index.
Definition: kdevicelistmodel.cpp:172
KDeviceListModel::data
QVariant data(const QModelIndex &index, int role) const
Get a visible data based on Qt role for the given index.
Definition: kdevicelistmodel.cpp:90
KDeviceListModel::index
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Get the children model index for the given row and column.
Definition: kdevicelistmodel.cpp:122
KIcon
QAbstractItemModel
QList
QMap
QObject
kdebug.h
kdevicelistitem_p.h
kdevicelistmodel.h
kicon.h
klocale.h
i18n
QString i18n(const char *text)
Solid
Definition: kdevicelistitem_p.h:27
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal