Fixed a typo, thanks to Rub3n CT for reporting.
[LameXP.git] / src / Model_Progress.cpp
blob57da24b29c67264c4a4999230b3fb7540b30ad55
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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")
39 ProgressModel::~ProgressModel(void)
43 int ProgressModel::columnCount(const QModelIndex &parent) const
45 return 2;
48 int ProgressModel::rowCount(const QModelIndex &parent) const
50 return m_jobList.count();
53 QVariant ProgressModel::data(const QModelIndex &index, int role) const
55 if(index.row() >= 0 && index.row() < m_jobList.count())
57 if(role == Qt::DisplayRole)
59 switch(index.column())
61 case 0:
62 return m_jobName.value(m_jobList.at(index.row()));
63 break;
64 case 1:
65 return m_jobStatus.value(m_jobList.at(index.row()));
66 break;
67 default:
68 return QVariant();
69 break;
72 else if(role == Qt::DecorationRole && index.column() == 0)
74 switch(m_jobState.value(m_jobList.at(index.row())))
76 case JobRunning:
77 return m_iconRunning;
78 break;
79 case JobPaused:
80 return m_iconPaused;
81 break;
82 case JobComplete:
83 return m_iconComplete;
84 break;
85 case JobSystem:
86 return m_iconSystem;
87 break;
88 case JobWarning:
89 return m_iconWarning;
90 break;
91 default:
92 return m_iconFailed;
93 break;
96 else if(role == Qt::TextAlignmentRole)
98 return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
102 return QVariant();
105 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
107 if(role == Qt::DisplayRole)
109 if(orientation == Qt::Horizontal)
111 switch(section)
113 case 0:
114 return tr("Job");
115 break;
116 case 1:
117 return tr("Status");
118 break;
119 default:
120 return QVariant();
121 break;
124 if(orientation == Qt::Vertical)
126 return QString::number(section + 1);
130 return QVariant();
133 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
135 if(m_jobList.contains(jobId) || m_jobListHidden.contains(jobId))
137 return;
140 while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
142 beginRemoveRows(QModelIndex(), 0, 0);
143 m_jobListHidden.append(m_jobList.takeFirst());
144 endRemoveRows();
147 int newIndex = m_jobList.count();
148 beginInsertRows(QModelIndex(), newIndex, newIndex);
150 m_jobList.append(jobId);
151 m_jobName.insert(jobId, jobName);
152 m_jobStatus.insert(jobId, jobInitialStatus);
153 m_jobState.insert(jobId, jobInitialState);
154 m_jobLogFile.insert(jobId, QStringList());
156 endInsertRows();
159 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
161 int row = m_jobList.indexOf(jobId);
163 if(row < 0)
165 if(m_jobListHidden.indexOf(jobId) >= 0)
167 if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
168 if(newState >= 0) m_jobState.insert(jobId, newState);
170 return;
173 if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
174 if(newState >= 0) m_jobState.insert(jobId, newState);
176 emit dataChanged(index(row, 0), index(row, 1));
179 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
181 if(m_jobList.contains(jobId))
183 m_jobLogFile[jobId].append(line.split('\n'));
187 const QStringList &ProgressModel::getLogFile(const QModelIndex &index)
189 if(index.row() < m_jobList.count())
191 QUuid id = m_jobList.at(index.row());
192 return m_jobLogFile[id];
195 return *(reinterpret_cast<QStringList*>(NULL));
198 const QUuid &ProgressModel::getJobId(const QModelIndex &index)
200 if(index.row() < m_jobList.count())
202 return m_jobList.at(index.row());
205 return *(reinterpret_cast<QUuid*>(NULL));
208 void ProgressModel::addSystemMessage(const QString &text, bool isWarning)
210 const QUuid &jobId = QUuid::createUuid();
212 if(m_jobList.contains(jobId))
214 return;
217 while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
219 beginRemoveRows(QModelIndex(), 0, 0);
220 m_jobListHidden.append(m_jobList.takeFirst());
221 endRemoveRows();
224 int newIndex = m_jobList.count();
225 beginInsertRows(QModelIndex(), newIndex, newIndex);
227 m_jobList.append(jobId);
228 m_jobName.insert(jobId, text);
229 m_jobStatus.insert(jobId, QString());
230 m_jobState.insert(jobId, isWarning ? JobWarning : JobSystem);
231 m_jobLogFile.insert(jobId, QStringList());
233 endInsertRows();
236 void ProgressModel::restoreHiddenItems(void)
238 if(!m_jobListHidden.isEmpty())
240 beginResetModel();
241 while(!m_jobListHidden.isEmpty())
243 m_jobList.prepend(m_jobListHidden.takeLast());
245 endResetModel();