OSDN Git Service

Updated Monkey's Audio binary to v4.11 (2013-01-20), including STDERR flush fix.
[lamexp/LameXP.git] / src / Model_Progress.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2013 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.
14 //
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.
18 //
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
21
22 #include "Model_Progress.h"
23
24 #include <QUuid>
25
26 #define MAX_DISPLAY_ITEMS 48
27
28 ProgressModel::ProgressModel(void)
29 :
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!")
41 {
42 }
43
44 ProgressModel::~ProgressModel(void)
45 {
46 }
47
48 int ProgressModel::columnCount(const QModelIndex &parent) const
49 {
50         return 2;
51 }
52
53 int ProgressModel::rowCount(const QModelIndex &parent) const
54 {
55         return m_jobList.count();
56 }
57
58 QVariant ProgressModel::data(const QModelIndex &index, int role) const
59 {
60         if(index.row() >= 0 && index.row() < m_jobList.count())
61         {
62                 if(role == Qt::DisplayRole)
63                 {
64                         switch(index.column())
65                         {
66                         case 0:
67                                 return m_jobName.value(m_jobList.at(index.row()));
68                                 break;
69                         case 1:
70                                 return m_jobStatus.value(m_jobList.at(index.row()));
71                                 break;
72                         default:
73                                 return QVariant();
74                                 break;
75                         }
76                 }
77                 else if(role == Qt::DecorationRole && index.column() == 0)
78                 {
79                         const int currentState = m_jobState.value(m_jobList.at(index.row()));
80                         return getIcon(static_cast<const JobState>(currentState));
81                 }
82                 else if(role == Qt::TextAlignmentRole)
83                 {
84                         return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
85                 }
86         }
87         
88         return QVariant();
89 }
90
91 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
92 {
93         if(role == Qt::DisplayRole)
94         {
95                 if(orientation == Qt::Horizontal)
96                 {
97                         switch(section)
98                         {
99                         case 0:
100                                 return tr("Job");
101                                 break;
102                         case 1:
103                                 return tr("Status");
104                                 break;
105                         default:
106                                 return QVariant();
107                                 break;
108                         }
109                 }
110                 if(orientation == Qt::Vertical)
111                 {
112                         return QString::number(section + 1);
113                 }
114         }
115
116         return QVariant();
117 }
118
119 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
120 {
121         if(m_jobIdentifiers.contains(jobId))
122         {
123                 return;
124         }
125
126         while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
127         {
128                 beginRemoveRows(QModelIndex(), 0, 0);
129                 m_jobListHidden.append(m_jobList.takeFirst());
130                 m_jobIndexCache.clear();
131                 endRemoveRows();
132         }
133
134         int newIndex = m_jobList.count();
135         beginInsertRows(QModelIndex(), newIndex, newIndex);
136
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);
143         
144         endInsertRows();
145 }
146
147 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
148 {
149         if(!m_jobIdentifiers.contains(jobId))
150         {
151                 return;
152         }
153         
154         if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
155         if(newState >= 0) m_jobState.insert(jobId, newState);
156
157         const int row = m_jobIndexCache.value(jobId, -1);
158
159         if(row >= 0)
160         {
161                 emit dataChanged(index(row, 0), index(row, 1));
162         }
163         else
164         {
165                 const int tmp = m_jobList.indexOf(jobId);
166                 if(tmp >= 0)
167                 {
168                         m_jobIndexCache.insert(jobId, tmp);
169                         emit dataChanged(index(tmp, 0), index(tmp, 1));
170                 }
171         }
172 }
173
174 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
175 {
176         if(m_jobIdentifiers.contains(jobId))
177         {
178                 m_jobLogFile[jobId].append(line.split('\n'));
179         }
180 }
181
182 const QStringList &ProgressModel::getLogFile(const QModelIndex &index) const
183 {
184         if(index.row() < m_jobList.count())
185         {
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(); }
189         }
190
191         return m_emptyList;
192 }
193
194 const QUuid &ProgressModel::getJobId(const QModelIndex &index) const
195 {
196         if(index.row() < m_jobList.count())
197         {
198                 return m_jobList.at(index.row());
199         }
200
201         return m_emptyUuid;
202 }
203
204 const ProgressModel::JobState ProgressModel::getJobState(const QModelIndex &index) const
205 {
206         if(index.row() < m_jobList.count())
207         {
208                 return static_cast<JobState>(m_jobState.value(m_jobList.at(index.row()), -1));
209         }
210
211         return static_cast<JobState>(-1);
212 }
213
214 void ProgressModel::addSystemMessage(const QString &text, int type)
215 {
216         const QUuid &jobId = QUuid::createUuid();
217
218         if(m_jobIdentifiers.contains(jobId))
219         {
220                 return;
221         }
222
223         while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
224         {
225                 beginRemoveRows(QModelIndex(), 0, 0);
226                 m_jobListHidden.append(m_jobList.takeFirst());
227                 m_jobIndexCache.clear();
228                 endRemoveRows();
229         }
230
231         int newIndex = m_jobList.count();
232         JobState jobState = JobState(-1);
233
234         switch(type)
235         {
236         case SysMsg_Warning:
237                 jobState = JobWarning;
238                 break;
239         case SysMsg_Performance:
240                 jobState = JobPerformance;
241                 break;
242         default:
243                 jobState = JobSystem;
244                 break;
245         }
246
247         beginInsertRows(QModelIndex(), newIndex, newIndex);
248
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);
255         
256         endInsertRows();
257 }
258
259 void ProgressModel::restoreHiddenItems(void)
260 {
261         if(!m_jobListHidden.isEmpty())
262         {
263                 beginResetModel();
264                 while(!m_jobListHidden.isEmpty())
265                 {
266                         m_jobList.prepend(m_jobListHidden.takeLast());
267                 }
268                 m_jobIndexCache.clear();
269                 endResetModel();
270         }
271 }
272
273 const QIcon &ProgressModel::getIcon(ProgressModel::JobState state) const
274 {
275         switch(state)
276         {
277         case JobRunning:
278                 return m_iconRunning;
279                 break;
280         case JobPaused:
281                 return m_iconPaused;
282                 break;
283         case JobComplete:
284                 return m_iconComplete;
285                 break;
286         case JobSystem:
287                 return m_iconSystem;
288                 break;
289         case JobWarning:
290                 return m_iconWarning;
291                 break;
292         case JobPerformance:
293                 return m_iconPerformance;
294                 break;
295         case JobSkipped:
296                 return m_iconSkipped;
297                 break;
298         default:
299                 return (state < 0) ? m_iconUndefined : m_iconFailed;
300                 break;
301         }
302 }