1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2016 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, but always including the *additional*
9 // restrictions defined in the "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"
27 #define MAX_DISPLAY_ITEMS 48
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
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())
68 return m_jobName
.value(m_jobList
.at(index
.row()));
71 return m_jobStatus
.value(m_jobList
.at(index
.row()));
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();
92 QVariant
ProgressModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
94 if(role
== Qt::DisplayRole
)
96 if(orientation
== Qt::Horizontal
)
111 if(orientation
== Qt::Vertical
)
113 return QString::number(section
+ 1);
120 void ProgressModel::addJob(const QUuid
&jobId
, const QString
&jobName
, const QString
&jobInitialStatus
, int jobInitialState
)
122 if(m_jobIdentifiers
.contains(jobId
))
127 while(m_jobList
.count() >= MAX_DISPLAY_ITEMS
)
129 beginRemoveRows(QModelIndex(), 0, 0);
130 m_jobListHidden
.append(m_jobList
.takeFirst());
131 m_jobIndexCache
.clear();
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
);
148 void ProgressModel::updateJob(const QUuid
&jobId
, const QString
&newStatus
, int newState
)
150 if(!m_jobIdentifiers
.contains(jobId
))
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);
162 emit
dataChanged(index(row
, 0), index(row
, 1));
166 const int tmp
= m_jobList
.indexOf(jobId
);
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(); }
195 const QUuid
&ProgressModel::getJobId(const QModelIndex
&index
) const
197 if(index
.row() < m_jobList
.count())
199 return m_jobList
.at(index
.row());
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
))
224 while(m_jobList
.count() >= MAX_DISPLAY_ITEMS
)
226 beginRemoveRows(QModelIndex(), 0, 0);
227 m_jobListHidden
.append(m_jobList
.takeFirst());
228 m_jobIndexCache
.clear();
232 int newIndex
= m_jobList
.count();
233 JobState jobState
= JobState(-1);
238 jobState
= JobWarning
;
240 case SysMsg_Performance
:
241 jobState
= JobPerformance
;
244 jobState
= JobSystem
;
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
);
260 void ProgressModel::restoreHiddenItems(void)
262 if(!m_jobListHidden
.isEmpty())
265 while(!m_jobListHidden
.isEmpty())
267 m_jobList
.prepend(m_jobListHidden
.takeLast());
269 m_jobIndexCache
.clear();
274 const QIcon
&ProgressModel::getIcon(ProgressModel::JobState state
) const
279 return m_iconRunning
;
285 return m_iconComplete
;
291 return m_iconWarning
;
294 return m_iconPerformance
;
297 return m_iconSkipped
;
300 return (state
< 0) ? m_iconUndefined
: m_iconFailed
;