1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 LoRd_MuldeR <MuldeR2@GMX.de>
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.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Model_Progress.h"
26 #define MAX_DISPLAY_ITEMS 48
28 ProgressModel::ProgressModel(void)
30 m_iconRunning(":/icons/media_play.png"),
31 m_iconPaused(":/icons/control_pause_blue.png"),
32 m_iconComplete(":/icons/tick.png"),
33 m_iconFailed(":/icons/exclamation.png"),
34 m_iconSystem(":/icons/computer.png"),
35 m_iconWarning(":/icons/error.png"),
36 m_iconPerformance(":/icons/clock.png"),
37 m_iconSkipped(":/icons/step_over.png"),
38 m_iconUndefined(":/icons/report.png"),
39 m_emptyUuid(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
40 m_emptyList("Oups, no data available!")
44 ProgressModel::~ProgressModel(void)
48 int ProgressModel::columnCount(const QModelIndex &parent) const
53 int ProgressModel::rowCount(const QModelIndex &parent) const
55 return m_jobList.count();
58 QVariant ProgressModel::data(const QModelIndex &index, int role) const
60 if(index.row() >= 0 && index.row() < m_jobList.count())
62 if(role == Qt::DisplayRole)
64 switch(index.column())
67 return m_jobName.value(m_jobList.at(index.row()));
70 return m_jobStatus.value(m_jobList.at(index.row()));
77 else if(role == Qt::DecorationRole && index.column() == 0)
79 const int currentState = m_jobState.value(m_jobList.at(index.row()));
80 return getIcon(static_cast<const JobState>(currentState));
82 else if(role == Qt::TextAlignmentRole)
84 return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
91 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
93 if(role == Qt::DisplayRole)
95 if(orientation == Qt::Horizontal)
110 if(orientation == Qt::Vertical)
112 return QString::number(section + 1);
119 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
121 if(m_jobIdentifiers.contains(jobId))
126 while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
128 beginRemoveRows(QModelIndex(), 0, 0);
129 m_jobListHidden.append(m_jobList.takeFirst());
130 m_jobIndexCache.clear();
134 int newIndex = m_jobList.count();
135 beginInsertRows(QModelIndex(), newIndex, newIndex);
137 m_jobList.append(jobId);
138 m_jobName.insert(jobId, jobName);
139 m_jobStatus.insert(jobId, jobInitialStatus);
140 m_jobState.insert(jobId, jobInitialState);
141 m_jobLogFile.insert(jobId, QStringList());
142 m_jobIdentifiers.insert(jobId);
147 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
149 if(!m_jobIdentifiers.contains(jobId))
154 if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
155 if(newState >= 0) m_jobState.insert(jobId, newState);
157 const int row = m_jobIndexCache.value(jobId, -1);
161 emit dataChanged(index(row, 0), index(row, 1));
165 const int tmp = m_jobList.indexOf(jobId);
168 m_jobIndexCache.insert(jobId, tmp);
169 emit dataChanged(index(tmp, 0), index(tmp, 1));
174 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
176 if(m_jobIdentifiers.contains(jobId))
178 m_jobLogFile[jobId].append(line.split('\n'));
182 const QStringList &ProgressModel::getLogFile(const QModelIndex &index) const
184 if(index.row() < m_jobList.count())
186 QUuid id = m_jobList.at(index.row());
187 QHash<QUuid,QStringList>::const_iterator iter = m_jobLogFile.constFind(id);
188 if(iter != m_jobLogFile.constEnd()) { return iter.value(); }
194 const QUuid &ProgressModel::getJobId(const QModelIndex &index) const
196 if(index.row() < m_jobList.count())
198 return m_jobList.at(index.row());
204 const ProgressModel::JobState ProgressModel::getJobState(const QModelIndex &index) const
206 if(index.row() < m_jobList.count())
208 return static_cast<JobState>(m_jobState.value(m_jobList.at(index.row()), -1));
211 return static_cast<JobState>(-1);
214 void ProgressModel::addSystemMessage(const QString &text, int type)
216 const QUuid &jobId = QUuid::createUuid();
218 if(m_jobIdentifiers.contains(jobId))
223 while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
225 beginRemoveRows(QModelIndex(), 0, 0);
226 m_jobListHidden.append(m_jobList.takeFirst());
227 m_jobIndexCache.clear();
231 int newIndex = m_jobList.count();
232 JobState jobState = JobState(-1);
237 jobState = JobWarning;
239 case SysMsg_Performance:
240 jobState = JobPerformance;
243 jobState = JobSystem;
247 beginInsertRows(QModelIndex(), newIndex, newIndex);
249 m_jobList.append(jobId);
250 m_jobName.insert(jobId, text);
251 m_jobStatus.insert(jobId, QString());
252 m_jobState.insert(jobId, jobState);
253 m_jobLogFile.insert(jobId, QStringList());
254 m_jobIdentifiers.insert(jobId);
259 void ProgressModel::restoreHiddenItems(void)
261 if(!m_jobListHidden.isEmpty())
264 while(!m_jobListHidden.isEmpty())
266 m_jobList.prepend(m_jobListHidden.takeLast());
268 m_jobIndexCache.clear();
273 const QIcon &ProgressModel::getIcon(ProgressModel::JobState state) const
278 return m_iconRunning;
284 return m_iconComplete;
290 return m_iconWarning;
293 return m_iconPerformance;
296 return m_iconSkipped;
299 return (state < 0) ? m_iconUndefined : m_iconFailed;