Bump version.
[LameXP.git] / src / Model_Progress.cpp
blobcda9b04ca530745685a5ce06ccab78f1e7e14271
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2021 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU GENERAL PUBLIC LICENSE as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
23 #include "Model_Progress.h"
25 #include <QUuid>
27 #define MAX_DISPLAY_ITEMS 64
29 ProgressModel::ProgressModel(void)
31 m_iconRunning(":/icons/media_play.png"),
32 m_iconPaused(":/icons/control_pause_blue.png"),
33 m_iconComplete(":/icons/tick.png"),
34 m_iconFailed(":/icons/exclamation.png"),
35 m_iconSystem(":/icons/computer.png"),
36 m_iconWarning(":/icons/error.png"),
37 m_iconPerformance(":/icons/clock.png"),
38 m_iconSkipped(":/icons/step_over.png"),
39 m_iconUndefined(":/icons/report.png"),
40 m_emptyUuid(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
41 m_emptyList("Oups, no data available!")
45 ProgressModel::~ProgressModel(void)
49 int ProgressModel::columnCount(const QModelIndex &parent) const
51 return 2;
54 int ProgressModel::rowCount(const QModelIndex &parent) const
56 return m_jobList.count();
59 QVariant ProgressModel::data(const QModelIndex &index, int role) const
61 if(index.row() >= 0 && index.row() < m_jobList.count())
63 if(role == Qt::DisplayRole)
65 switch(index.column())
67 case 0:
68 return m_jobName.value(m_jobList.at(index.row()));
69 break;
70 case 1:
71 return m_jobStatus.value(m_jobList.at(index.row()));
72 break;
73 default:
74 return QVariant();
75 break;
78 else if(role == Qt::DecorationRole && index.column() == 0)
80 const int currentState = m_jobState.value(m_jobList.at(index.row()));
81 return getIcon(static_cast<const JobState>(currentState));
83 else if(role == Qt::TextAlignmentRole)
85 return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
89 return QVariant();
92 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
94 if(role == Qt::DisplayRole)
96 if(orientation == Qt::Horizontal)
98 switch(section)
100 case 0:
101 return tr("Job");
102 break;
103 case 1:
104 return tr("Status");
105 break;
106 default:
107 return QVariant();
108 break;
111 if(orientation == Qt::Vertical)
113 return QString::number(section + 1);
117 return QVariant();
120 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
122 if(m_jobIdentifiers.contains(jobId))
124 return;
127 while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
129 beginRemoveRows(QModelIndex(), 0, 0);
130 m_jobListHidden.append(m_jobList.takeFirst());
131 m_jobIndexCache.clear();
132 endRemoveRows();
135 int newIndex = m_jobList.count();
136 beginInsertRows(QModelIndex(), newIndex, newIndex);
138 m_jobList.append(jobId);
139 m_jobName.insert(jobId, jobName);
140 m_jobStatus.insert(jobId, jobInitialStatus);
141 m_jobState.insert(jobId, jobInitialState);
142 m_jobLogFile.insert(jobId, QStringList());
143 m_jobIdentifiers.insert(jobId);
145 endInsertRows();
148 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
150 if(!m_jobIdentifiers.contains(jobId))
152 return;
155 if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
156 if(newState >= 0) m_jobState.insert(jobId, newState);
158 const int row = m_jobIndexCache.value(jobId, -1);
160 if(row >= 0)
162 emit dataChanged(index(row, 0), index(row, 1));
164 else
166 const int tmp = m_jobList.indexOf(jobId);
167 if(tmp >= 0)
169 m_jobIndexCache.insert(jobId, tmp);
170 emit dataChanged(index(tmp, 0), index(tmp, 1));
175 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
177 if(m_jobIdentifiers.contains(jobId))
179 m_jobLogFile[jobId].append(line.split('\n'));
183 const QStringList &ProgressModel::getLogFile(const QModelIndex &index) const
185 if(index.row() < m_jobList.count())
187 QUuid id = m_jobList.at(index.row());
188 QHash<QUuid,QStringList>::const_iterator iter = m_jobLogFile.constFind(id);
189 if(iter != m_jobLogFile.constEnd()) { return iter.value(); }
192 return m_emptyList;
195 const QUuid &ProgressModel::getJobId(const QModelIndex &index) const
197 if(index.row() < m_jobList.count())
199 return m_jobList.at(index.row());
202 return m_emptyUuid;
205 const ProgressModel::JobState ProgressModel::getJobState(const QModelIndex &index) const
207 if(index.row() < m_jobList.count())
209 return static_cast<JobState>(m_jobState.value(m_jobList.at(index.row()), -1));
212 return static_cast<JobState>(-1);
215 void ProgressModel::addSystemMessage(const QString &text, int type)
217 const QUuid &jobId = QUuid::createUuid();
219 if(m_jobIdentifiers.contains(jobId))
221 return;
224 while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
226 beginRemoveRows(QModelIndex(), 0, 0);
227 m_jobListHidden.append(m_jobList.takeFirst());
228 m_jobIndexCache.clear();
229 endRemoveRows();
232 int newIndex = m_jobList.count();
233 JobState jobState = JobState(-1);
235 switch(type)
237 case SysMsg_Warning:
238 jobState = JobWarning;
239 break;
240 case SysMsg_Performance:
241 jobState = JobPerformance;
242 break;
243 default:
244 jobState = JobSystem;
245 break;
248 beginInsertRows(QModelIndex(), newIndex, newIndex);
250 m_jobList.append(jobId);
251 m_jobName.insert(jobId, text);
252 m_jobStatus.insert(jobId, QString());
253 m_jobState.insert(jobId, jobState);
254 m_jobLogFile.insert(jobId, QStringList());
255 m_jobIdentifiers.insert(jobId);
257 endInsertRows();
260 void ProgressModel::restoreHiddenItems(void)
262 if(!m_jobListHidden.isEmpty())
264 beginResetModel();
265 while(!m_jobListHidden.isEmpty())
267 m_jobList.prepend(m_jobListHidden.takeLast());
269 m_jobIndexCache.clear();
270 endResetModel();
274 const QIcon &ProgressModel::getIcon(ProgressModel::JobState state) const
276 switch(state)
278 case JobRunning:
279 return m_iconRunning;
280 break;
281 case JobPaused:
282 return m_iconPaused;
283 break;
284 case JobComplete:
285 return m_iconComplete;
286 break;
287 case JobSystem:
288 return m_iconSystem;
289 break;
290 case JobWarning:
291 return m_iconWarning;
292 break;
293 case JobPerformance:
294 return m_iconPerformance;
295 break;
296 case JobSkipped:
297 return m_iconSkipped;
298 break;
299 default:
300 return (state < 0) ? m_iconUndefined : m_iconFailed;
301 break;