Plasma5Support

placesproxymodel.cpp
1/*
2 SPDX-FileCopyrightText: 2014 Marco Martin <mart@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.1-or-later
5*/
6
7#include "placesproxymodel.h"
8
9#include <QDebug>
10#include <QIcon>
11#include <QStorageInfo>
12
13PlacesProxyModel::PlacesProxyModel(QObject *parent, KFilePlacesModel *model)
14 : QIdentityProxyModel(parent)
15 , m_placesModel(model)
16{
17 setSourceModel(model);
18}
19
20QHash<int, QByteArray> PlacesProxyModel::roleNames() const
21{
23 roles.insert(Qt::DisplayRole, "display");
24 roles.insert(Qt::DecorationRole, "decoration");
26 roles.insert(KFilePlacesModel::HiddenRole, "hidden");
27 roles.insert(KFilePlacesModel::SetupNeededRole, "setupNeeded");
28 roles.insert(KFilePlacesModel::FixedDeviceRole, "fixedDevice");
29 roles.insert(KFilePlacesModel::CapacityBarRecommendedRole, "capacityBarRecommended");
30 roles.insert(PlaceIndexRole, "placeIndex");
31 roles.insert(IsDeviceRole, "isDevice");
32 roles.insert(PathRole, "path");
33 roles.insert(SizeRole, "size");
34 roles.insert(UsedRole, "used");
35 roles.insert(AvailableRole, "available");
36 return roles;
37}
38
39QVariant PlacesProxyModel::data(const QModelIndex &index, int role) const
40{
41 switch (role) {
42 case PlaceIndexRole:
43 return index.row();
44 case IsDeviceRole:
45 return m_placesModel->deviceForIndex(index).isValid();
46 case PathRole:
47 return m_placesModel->url(index).path();
48
49 case SizeRole: {
50 const QString path = m_placesModel->url(index).path();
51 QStorageInfo info{path};
52 return info.isValid() && info.isReady() ? info.bytesTotal() : QVariant{};
53 }
54 case UsedRole: {
55 const QString path = m_placesModel->url(index).path();
56 QStorageInfo info{path};
57 return info.isValid() && info.isReady() ? info.bytesTotal() - info.bytesAvailable() : QVariant{};
58 }
59 case AvailableRole: {
60 const QString path = m_placesModel->url(index).path();
61 QStorageInfo info{path};
62 return info.isValid() && info.isReady() ? info.bytesAvailable() : QVariant{};
63 }
64 default:
65 return QIdentityProxyModel::data(index, role);
66 }
67}
Q_INVOKABLE QUrl url(const QModelIndex &index) const
Solid::Device deviceForIndex(const QModelIndex &index) const
bool isValid() const
QString path(const QString &relativePath)
virtual QVariant data(const QModelIndex &index, int role) const const=0
iterator insert(const Key &key, const T &value)
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
int row() const const
bool isValid() const const
DisplayRole
QString path(ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:57 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.