Rewrote FileListModel class to use a QHash map internally. This should speed-up the...
[LameXP.git] / src / Model_Progress.cpp
blob4f7d222e01a4607c57f5dcb4ca52e87482114a5f
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 50
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_jobList.contains(jobId) || m_jobListHidden.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 endRemoveRows();
151 int newIndex = m_jobList.count();
152 beginInsertRows(QModelIndex(), newIndex, newIndex);
154 m_jobList.append(jobId);
155 m_jobName.insert(jobId, jobName);
156 m_jobStatus.insert(jobId, jobInitialStatus);
157 m_jobState.insert(jobId, jobInitialState);
158 m_jobLogFile.insert(jobId, QStringList());
160 endInsertRows();
163 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
165 int row = m_jobList.indexOf(jobId);
167 if(row < 0)
169 if(m_jobListHidden.indexOf(jobId) >= 0)
171 if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
172 if(newState >= 0) m_jobState.insert(jobId, newState);
174 return;
177 if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
178 if(newState >= 0) m_jobState.insert(jobId, newState);
180 emit dataChanged(index(row, 0), index(row, 1));
183 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
185 if(m_jobList.contains(jobId))
187 m_jobLogFile[jobId].append(line.split('\n'));
191 const QStringList &ProgressModel::getLogFile(const QModelIndex &index)
193 if(index.row() < m_jobList.count())
195 QUuid id = m_jobList.at(index.row());
196 return m_jobLogFile[id];
199 return *(reinterpret_cast<QStringList*>(NULL));
202 const QUuid &ProgressModel::getJobId(const QModelIndex &index)
204 if(index.row() < m_jobList.count())
206 return m_jobList.at(index.row());
209 return *(reinterpret_cast<QUuid*>(NULL));
212 void ProgressModel::addSystemMessage(const QString &text, int type)
214 const QUuid &jobId = QUuid::createUuid();
216 if(m_jobList.contains(jobId))
218 return;
221 while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
223 beginRemoveRows(QModelIndex(), 0, 0);
224 m_jobListHidden.append(m_jobList.takeFirst());
225 endRemoveRows();
228 int newIndex = m_jobList.count();
229 JobState jobState = JobState(-1);
231 switch(type)
233 case SysMsg_Warning:
234 jobState = JobWarning;
235 break;
236 case SysMsg_Performance:
237 jobState = JobPerformance;
238 break;
239 default:
240 jobState = JobSystem;
241 break;
244 beginInsertRows(QModelIndex(), newIndex, newIndex);
246 m_jobList.append(jobId);
247 m_jobName.insert(jobId, text);
248 m_jobStatus.insert(jobId, QString());
249 m_jobState.insert(jobId, jobState);
250 m_jobLogFile.insert(jobId, QStringList());
252 endInsertRows();
255 void ProgressModel::restoreHiddenItems(void)
257 if(!m_jobListHidden.isEmpty())
259 beginResetModel();
260 while(!m_jobListHidden.isEmpty())
262 m_jobList.prepend(m_jobListHidden.takeLast());
264 endResetModel();