OSDN Git Service

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