Some code refactoring regarding the QWaitCondition/QMutex in FileAnalyzer_Task.
[LameXP.git] / src / Model_Progress.cpp
blobbb23541091730e010904e75f382aaedbea93a62c
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2012 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.
9 //
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"
24 #include <QUuid>
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")
40 ProgressModel::~ProgressModel(void)
44 int ProgressModel::columnCount(const QModelIndex &parent) const
46 return 2;
49 int ProgressModel::rowCount(const QModelIndex &parent) const
51 return m_jobList.count();
54 QVariant ProgressModel::data(const QModelIndex &index, int role) const
56 if(index.row() >= 0 && index.row() < m_jobList.count())
58 if(role == Qt::DisplayRole)
60 switch(index.column())
62 case 0:
63 return m_jobName.value(m_jobList.at(index.row()));
64 break;
65 case 1:
66 return m_jobStatus.value(m_jobList.at(index.row()));
67 break;
68 default:
69 return QVariant();
70 break;
73 else if(role == Qt::DecorationRole && index.column() == 0)
75 switch(m_jobState.value(m_jobList.at(index.row())))
77 case JobRunning:
78 return m_iconRunning;
79 break;
80 case JobPaused:
81 return m_iconPaused;
82 break;
83 case JobComplete:
84 return m_iconComplete;
85 break;
86 case JobSystem:
87 return m_iconSystem;
88 break;
89 case JobWarning:
90 return m_iconWarning;
91 break;
92 case JobPerformance:
93 return m_iconPerformance;
94 break;
95 default:
96 return m_iconFailed;
97 break;
100 else if(role == Qt::TextAlignmentRole)
102 return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
106 return QVariant();
109 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
111 if(role == Qt::DisplayRole)
113 if(orientation == Qt::Horizontal)
115 switch(section)
117 case 0:
118 return tr("Job");
119 break;
120 case 1:
121 return tr("Status");
122 break;
123 default:
124 return QVariant();
125 break;
128 if(orientation == Qt::Vertical)
130 return QString::number(section + 1);
134 return QVariant();
137 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
139 if(m_jobIdentifiers.contains(jobId))
141 return;
144 while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
146 beginRemoveRows(QModelIndex(), 0, 0);
147 m_jobListHidden.append(m_jobList.takeFirst());
148 m_jobIndexCache.clear();
149 endRemoveRows();
152 int newIndex = m_jobList.count();
153 beginInsertRows(QModelIndex(), newIndex, newIndex);
155 m_jobList.append(jobId);
156 m_jobName.insert(jobId, jobName);
157 m_jobStatus.insert(jobId, jobInitialStatus);
158 m_jobState.insert(jobId, jobInitialState);
159 m_jobLogFile.insert(jobId, QStringList());
160 m_jobIdentifiers.insert(jobId);
162 endInsertRows();
165 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
167 if(!m_jobIdentifiers.contains(jobId))
169 return;
172 if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
173 if(newState >= 0) m_jobState.insert(jobId, newState);
175 const int row = m_jobIndexCache.value(jobId, -1);
177 if(row >= 0)
179 emit dataChanged(index(row, 0), index(row, 1));
181 else
183 const int tmp = m_jobList.indexOf(jobId);
184 if(tmp >= 0)
186 m_jobIndexCache.insert(jobId, tmp);
187 emit dataChanged(index(tmp, 0), index(tmp, 1));
192 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
194 if(m_jobIdentifiers.contains(jobId))
196 m_jobLogFile[jobId].append(line.split('\n'));
200 const QStringList &ProgressModel::getLogFile(const QModelIndex &index)
202 if(index.row() < m_jobList.count())
204 QUuid id = m_jobList.at(index.row());
205 return m_jobLogFile[id];
208 return *(reinterpret_cast<QStringList*>(NULL));
211 const QUuid &ProgressModel::getJobId(const QModelIndex &index)
213 if(index.row() < m_jobList.count())
215 return m_jobList.at(index.row());
218 return *(reinterpret_cast<QUuid*>(NULL));
221 void ProgressModel::addSystemMessage(const QString &text, int type)
223 const QUuid &jobId = QUuid::createUuid();
225 if(m_jobIdentifiers.contains(jobId))
227 return;
230 while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
232 beginRemoveRows(QModelIndex(), 0, 0);
233 m_jobListHidden.append(m_jobList.takeFirst());
234 m_jobIndexCache.clear();
235 endRemoveRows();
238 int newIndex = m_jobList.count();
239 JobState jobState = JobState(-1);
241 switch(type)
243 case SysMsg_Warning:
244 jobState = JobWarning;
245 break;
246 case SysMsg_Performance:
247 jobState = JobPerformance;
248 break;
249 default:
250 jobState = JobSystem;
251 break;
254 beginInsertRows(QModelIndex(), newIndex, newIndex);
256 m_jobList.append(jobId);
257 m_jobName.insert(jobId, text);
258 m_jobStatus.insert(jobId, QString());
259 m_jobState.insert(jobId, jobState);
260 m_jobLogFile.insert(jobId, QStringList());
261 m_jobIdentifiers.insert(jobId);
263 endInsertRows();
266 void ProgressModel::restoreHiddenItems(void)
268 if(!m_jobListHidden.isEmpty())
270 beginResetModel();
271 while(!m_jobListHidden.isEmpty())
273 m_jobList.prepend(m_jobListHidden.takeLast());
275 m_jobIndexCache.clear();
276 endResetModel();