delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kuiserver / progresslistmodel.cpp
blob9ed8182eb51dd4778eaa8a890b09de2f5cafe6a8
1 /**
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
35 return QModelIndex();
38 QVariant ProgressListModel::data(const QModelIndex &index, int role) const
40 QVariant result;
42 if (!index.isValid())
43 return result;
45 JobInfo jobInfo = jobInfoMap[static_cast<UIServer::JobView*>(index.internalPointer())];
47 switch (role)
49 case Capabilities:
50 result = jobInfo.capabilities;
51 break;
52 case ApplicationName:
53 result = jobInfo.applicationName;
54 break;
55 case Icon:
56 result = jobInfo.icon;
57 break;
58 case SizeTotals:
59 result = jobInfo.sizeTotals;
60 break;
61 case SizeProcessed:
62 result = jobInfo.sizeProcessed;
63 break;
64 case TimeTotals:
65 result = jobInfo.timeTotals;
66 break;
67 case TimeElapsed:
68 result = jobInfo.timeElapsed;
69 break;
70 case Speed:
71 result = jobInfo.speed;
72 break;
73 case Percent:
74 result = jobInfo.percent;
75 break;
76 case Message:
77 result = jobInfo.message;
78 break;
79 case State:
80 result = jobInfo.state;
81 break;
82 case JobViewRole:
83 result = QVariant::fromValue<UIServer::JobView*>(jobInfo.jobView);
84 break;
85 default:
86 break;
89 return result;
92 Qt::ItemFlags ProgressListModel::flags(const QModelIndex &index) const
94 Q_UNUSED(index);
96 return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
99 QModelIndex ProgressListModel::index(int row, int column, const QModelIndex &parent) const
101 Q_UNUSED(parent);
103 if (row >= rowCount())
104 return QModelIndex();
106 int i = 0;
107 UIServer::JobView *jobView;
108 foreach (UIServer::JobView *jv, jobInfoMap.keys()) {
109 jobView = jv;
110 if (i == row)
111 break;
112 i++;
115 return createIndex(row, column, jobView);
118 QModelIndex ProgressListModel::indexForJob(UIServer::JobView *jobView) const
120 int i = 0;
121 foreach (UIServer::JobView *jv, jobInfoMap.keys()) {
122 if (jv == jobView)
123 return createIndex(i, 0, jv);
124 i++;
127 return QModelIndex();
130 int ProgressListModel::columnCount(const QModelIndex &parent) const
132 Q_UNUSED(parent);
133 return 1;
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())
144 return false;
146 JobInfo &jobInfo = jobInfoMap[static_cast<UIServer::JobView*>(index.internalPointer())];
148 switch (role)
150 case SizeTotals:
151 jobInfo.sizeTotals = value.toString();
152 break;
153 case SizeProcessed:
154 jobInfo.sizeProcessed = value.toString();
155 break;
156 case TimeTotals:
157 jobInfo.timeTotals = value.toLongLong();
158 break;
159 case TimeElapsed:
160 jobInfo.timeElapsed = value.toLongLong();
161 break;
162 case Speed:
163 jobInfo.speed = value.toString();
164 break;
165 case Percent:
166 jobInfo.percent = value.toInt();
167 break;
168 case Message:
169 jobInfo.message = value.toString();
170 break;
171 case State:
172 jobInfo.state = (JobInfo::State) value.toInt();
173 break;
174 default:
175 return false;
178 emit dataChanged(index, index);
179 emit layoutChanged();
181 return true;
184 UIServer::JobView* ProgressListModel::newJob(const QString &appName, const QString &appIcon, int capabilities)
186 insertRow(rowCount());
188 JobInfo newJob;
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();
195 newJob.percent = -1;
196 newJob.message.clear();
197 newJob.state = JobInfo::Running;
198 // given information
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);
218 endRemoveRows();
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())
237 return false;
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;
246 else
248 QPair<QString, QString> descField(name, value);
249 jobInfo.descFields.insert(id, descField);
252 return true;
255 void ProgressListModel::clearDescriptionField(const QModelIndex &index, uint id)
257 if (!index.isValid())
258 return;
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"