1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
/*
* Copyright (C) 2015 Dominik Haumann <dhaumann@kde.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "QuotaListModel.h"
#include <QDebug>
QuotaListModel::QuotaListModel(QObject *parent)
: QAbstractListModel(parent)
{
}
namespace {
/**
* QML data roles.
*/
enum {
DetailsRole = Qt::UserRole,
IconRole,
FreeStringRole,
UsedStringRole,
MountPointRole,
UsageRole
};
}
QHash<int, QByteArray> QuotaListModel::roleNames() const
{
QHash<int, QByteArray> roles;
roles[DetailsRole] = "details";
roles[IconRole] = "icon";
roles[FreeStringRole] = "free";
roles[UsedStringRole] = "used";
roles[MountPointRole] = "mountPoint";
roles[UsageRole] = "usage";
return roles;
}
QVariant QuotaListModel::data(const QModelIndex &index, int role) const
{
if (! index.isValid() || index.row() >= m_items.size()) {
return QVariant();
}
const auto item = m_items[index.row()];
switch (role) {
case DetailsRole: return item.mountString();
case IconRole: return item.iconName();
case FreeStringRole: return item.freeString();
case UsedStringRole: return item.usedString();
case MountPointRole: return item.mountPoint();
case UsageRole: return item.usage();
}
return QVariant();
}
int QuotaListModel::rowCount(const QModelIndex &index) const
{
if (! index.isValid()) {
return m_items.size();
}
return 0;
}
bool QuotaListModel::setData(const QModelIndex &index, const QVariant &variant, int role)
{
Q_UNUSED(role)
const int row = index.row();
if (index.isValid() && row < m_items.size()) {
const QuotaItem item = variant.value<QuotaItem>();
// This assert makes sure that changing items modify the correct item:
// therefore, the unique identifier 'mountPoint()' is used. If that
// is not the case, the newly inserted row must have an empty mountPoint().
Q_ASSERT(item.mountPoint() == m_items[row].mountPoint()
|| m_items[row].mountPoint().isEmpty());
if (m_items[row] != item) {
m_items[row] = item;
emit dataChanged(index, index);
return true;
}
}
return false;
}
bool QuotaListModel::insertRows(int row, int count, const QModelIndex &parent)
{
// only top-level items are supported
if (parent.isValid()) {
return false;
}
beginInsertRows(QModelIndex(), row, row + count - 1);
m_items.insert(row, count, QuotaItem());
endInsertRows();
return true;
}
bool QuotaListModel::removeRows(int row, int count, const QModelIndex &parent)
{
// only top-level items are valid
if (parent.isValid() || (row + count) >= m_items.size()) {
return false;
}
beginRemoveRows(QModelIndex(), row, row + count - 1);
m_items.remove(row, count);
endRemoveRows();
return true;
}
void QuotaListModel::clear()
{
beginResetModel();
m_items.clear();
endResetModel();
}
namespace {
QStringList mountPoints(const QVector<QuotaItem> &items)
{
QStringList list;
for (auto & item : items) {
list.append(item.mountPoint());
}
return list;
}
int indexOfMountPoint(const QString &mountPoint, const QVector<QuotaItem> &items)
{
for (int i = 0; i < items.size(); ++i) {
if (mountPoint == items[i].mountPoint()) {
return i;
}
}
return -1;
}
}
void QuotaListModel::updateItems(const QVector<QuotaItem> &items)
{
QStringList unusedMountPoints = mountPoints(m_items);
// merge existing and new mount points
for (auto & item : items) {
// remove still used item from unused list
unusedMountPoints.removeOne(item.mountPoint());
// insert or modify m_items
int row = indexOfMountPoint(item.mountPoint(), m_items);
if (row < 0) {
// new item: append on end
row = m_items.size();
insertRow(row);
}
setData(createIndex(row, 0), QVariant::fromValue(item));
}
// remove mount points, that do not exist anymore
for (const auto & mountPoint : unusedMountPoints) {
const int row = indexOfMountPoint(mountPoint, m_items);
Q_ASSERT(row >= 0);
removeRow(row);
}
}
|