2 * This file is part of the KDE project
3 * Copyright (C) 2006-2008 Rafael Fernández López <ereslibre@kde.org>
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.
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.
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.
20 #include "progresslistmodel.h"
22 #include <kwidgetjobtracker.h>
24 ProgressListModel::ProgressListModel(QObject
*parent
)
25 : QAbstractItemModel(parent
)
29 ProgressListModel::~ProgressListModel()
33 QModelIndex
ProgressListModel::parent(const QModelIndex
&) const
38 QVariant
ProgressListModel::data(const QModelIndex
&index
, int role
) const
45 JobInfo jobInfo
= jobInfoMap
[static_cast<UIServer::JobView
*>(index
.internalPointer())];
50 result
= jobInfo
.capabilities
;
53 result
= jobInfo
.applicationName
;
56 result
= jobInfo
.icon
;
59 result
= jobInfo
.sizeTotals
;
62 result
= jobInfo
.sizeProcessed
;
65 result
= jobInfo
.timeTotals
;
68 result
= jobInfo
.timeElapsed
;
71 result
= jobInfo
.speed
;
74 result
= jobInfo
.percent
;
77 result
= jobInfo
.message
;
80 result
= jobInfo
.state
;
83 result
= QVariant::fromValue
<UIServer::JobView
*>(jobInfo
.jobView
);
92 Qt::ItemFlags
ProgressListModel::flags(const QModelIndex
&index
) const
96 return Qt::ItemIsSelectable
| Qt::ItemIsEnabled
;
99 QModelIndex
ProgressListModel::index(int row
, int column
, const QModelIndex
&parent
) const
103 if (row
>= rowCount())
104 return QModelIndex();
107 UIServer::JobView
*jobView
;
108 foreach (UIServer::JobView
*jv
, jobInfoMap
.keys()) {
115 return createIndex(row
, column
, jobView
);
118 QModelIndex
ProgressListModel::indexForJob(UIServer::JobView
*jobView
) const
121 foreach (UIServer::JobView
*jv
, jobInfoMap
.keys()) {
123 return createIndex(i
, 0, jv
);
127 return QModelIndex();
130 int ProgressListModel::columnCount(const QModelIndex
&parent
) const
136 int ProgressListModel::rowCount(const QModelIndex
&parent
) const
138 return parent
.isValid() ? 0 : jobInfoMap
.count();
141 bool ProgressListModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
143 if (!index
.isValid())
146 JobInfo
&jobInfo
= jobInfoMap
[static_cast<UIServer::JobView
*>(index
.internalPointer())];
151 jobInfo
.sizeTotals
= value
.toString();
154 jobInfo
.sizeProcessed
= value
.toString();
157 jobInfo
.timeTotals
= value
.toLongLong();
160 jobInfo
.timeElapsed
= value
.toLongLong();
163 jobInfo
.speed
= value
.toString();
166 jobInfo
.percent
= value
.toInt();
169 jobInfo
.message
= value
.toString();
172 jobInfo
.state
= (JobInfo::State
) value
.toInt();
178 emit
dataChanged(index
, index
);
179 emit
layoutChanged();
184 UIServer::JobView
* ProgressListModel::newJob(const QString
&appName
, const QString
&appIcon
, int capabilities
)
186 insertRow(rowCount());
189 newJob
.jobView
= new UIServer::JobView();
190 newJob
.sizeTotals
.clear();
191 newJob
.sizeProcessed
.clear();
192 newJob
.timeElapsed
= -1;
193 newJob
.timeTotals
= -1;
194 newJob
.speed
.clear();
196 newJob
.message
.clear();
197 newJob
.state
= JobInfo::Running
;
199 newJob
.applicationName
= appName
;
200 newJob
.icon
= appIcon
;
201 newJob
.capabilities
= capabilities
;
202 jobInfoMap
.insert(newJob
.jobView
, newJob
);
204 return newJob
.jobView
;
208 void ProgressListModel::finishJob(UIServer::JobView
*jobView
)
210 beginRemoveRows(QModelIndex(), rowCount() - 1, rowCount() - 1);
212 QModelIndex indexToRemove
= indexForJob(jobView
);
214 if (indexToRemove
.isValid()) {
215 jobInfoMap
.remove(jobView
);
221 QPair
<QString
, QString
> ProgressListModel::getDescriptionField(const QModelIndex
&index
, uint id
)
223 if (!index
.isValid())
224 return QPair
<QString
, QString
>(QString(), QString());
226 JobInfo
&jobInfo
= jobInfoMap
[static_cast<UIServer::JobView
*>(index
.internalPointer())];
228 if (!jobInfo
.descFields
.contains(id
))
229 return QPair
<QString
, QString
>(QString(), QString());
231 return jobInfo
.descFields
[id
];
234 bool ProgressListModel::setDescriptionField(const QModelIndex
&index
, uint id
, const QString
&name
, const QString
&value
)
236 if (!index
.isValid())
239 JobInfo
&jobInfo
= jobInfoMap
[static_cast<UIServer::JobView
*>(index
.internalPointer())];
241 if (jobInfo
.descFields
.contains(id
))
243 jobInfo
.descFields
[id
].first
= name
;
244 jobInfo
.descFields
[id
].second
= value
;
248 QPair
<QString
, QString
> descField(name
, value
);
249 jobInfo
.descFields
.insert(id
, descField
);
255 void ProgressListModel::clearDescriptionField(const QModelIndex
&index
, uint id
)
257 if (!index
.isValid())
260 JobInfo
&jobInfo
= jobInfoMap
[static_cast<UIServer::JobView
*>(index
.internalPointer())];
262 if (jobInfo
.descFields
.contains(id
))
264 jobInfo
.descFields
.remove(id
);
268 JobInfo::State
ProgressListModel::state(const QModelIndex
&index
) const
270 if (index
.isValid()) {
271 return ((JobInfo::State
) data(index
, State
).toInt());
274 return JobInfo::InvalidState
;
277 bool ProgressListModel::setData(int row
, const QVariant
&value
, int role
)
279 return setData(index(row
), value
, role
);
282 #include "progresslistmodel.moc"