OSDN Git Service

Bump version.
[lamexp/LameXP.git] / src / Model_Progress.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2020 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; always including the non-optional
9 // LAMEXP GNU GENERAL PUBLIC LICENSE ADDENDUM. See "License.txt" file!
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 //
20 // http://www.gnu.org/licenses/gpl-2.0.txt
21 ///////////////////////////////////////////////////////////////////////////////
22
23 #include "Model_Progress.h"
24
25 #include <QUuid>
26
27 #define MAX_DISPLAY_ITEMS 64
28
29 ProgressModel::ProgressModel(void)
30 :
31         m_iconRunning(":/icons/media_play.png"),
32         m_iconPaused(":/icons/control_pause_blue.png"),
33         m_iconComplete(":/icons/tick.png"),
34         m_iconFailed(":/icons/exclamation.png"),
35         m_iconSystem(":/icons/computer.png"),
36         m_iconWarning(":/icons/error.png"),
37         m_iconPerformance(":/icons/clock.png"),
38         m_iconSkipped(":/icons/step_over.png"),
39         m_iconUndefined(":/icons/report.png"),
40         m_emptyUuid(0x00000000, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00),
41         m_emptyList("Oups, no data available!")
42 {
43 }
44
45 ProgressModel::~ProgressModel(void)
46 {
47 }
48
49 int ProgressModel::columnCount(const QModelIndex &parent) const
50 {
51         return 2;
52 }
53
54 int ProgressModel::rowCount(const QModelIndex &parent) const
55 {
56         return m_jobList.count();
57 }
58
59 QVariant ProgressModel::data(const QModelIndex &index, int role) const
60 {
61         if(index.row() >= 0 && index.row() < m_jobList.count())
62         {
63                 if(role == Qt::DisplayRole)
64                 {
65                         switch(index.column())
66                         {
67                         case 0:
68                                 return m_jobName.value(m_jobList.at(index.row()));
69                                 break;
70                         case 1:
71                                 return m_jobStatus.value(m_jobList.at(index.row()));
72                                 break;
73                         default:
74                                 return QVariant();
75                                 break;
76                         }
77                 }
78                 else if(role == Qt::DecorationRole && index.column() == 0)
79                 {
80                         const int currentState = m_jobState.value(m_jobList.at(index.row()));
81                         return getIcon(static_cast<const JobState>(currentState));
82                 }
83                 else if(role == Qt::TextAlignmentRole)
84                 {
85                         return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
86                 }
87         }
88         
89         return QVariant();
90 }
91
92 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
93 {
94         if(role == Qt::DisplayRole)
95         {
96                 if(orientation == Qt::Horizontal)
97                 {
98                         switch(section)
99                         {
100                         case 0:
101                                 return tr("Job");
102                                 break;
103                         case 1:
104                                 return tr("Status");
105                                 break;
106                         default:
107                                 return QVariant();
108                                 break;
109                         }
110                 }
111                 if(orientation == Qt::Vertical)
112                 {
113                         return QString::number(section + 1);
114                 }
115         }
116
117         return QVariant();
118 }
119
120 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
121 {
122         if(m_jobIdentifiers.contains(jobId))
123         {
124                 return;
125         }
126
127         while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
128         {
129                 beginRemoveRows(QModelIndex(), 0, 0);
130                 m_jobListHidden.append(m_jobList.takeFirst());
131                 m_jobIndexCache.clear();
132                 endRemoveRows();
133         }
134
135         int newIndex = m_jobList.count();
136         beginInsertRows(QModelIndex(), newIndex, newIndex);
137
138         m_jobList.append(jobId);
139         m_jobName.insert(jobId, jobName);
140         m_jobStatus.insert(jobId, jobInitialStatus);
141         m_jobState.insert(jobId, jobInitialState);
142         m_jobLogFile.insert(jobId, QStringList());
143         m_jobIdentifiers.insert(jobId);
144         
145         endInsertRows();
146 }
147
148 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
149 {
150         if(!m_jobIdentifiers.contains(jobId))
151         {
152                 return;
153         }
154         
155         if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
156         if(newState >= 0) m_jobState.insert(jobId, newState);
157
158         const int row = m_jobIndexCache.value(jobId, -1);
159
160         if(row >= 0)
161         {
162                 emit dataChanged(index(row, 0), index(row, 1));
163         }
164         else
165         {
166                 const int tmp = m_jobList.indexOf(jobId);
167                 if(tmp >= 0)
168                 {
169                         m_jobIndexCache.insert(jobId, tmp);
170                         emit dataChanged(index(tmp, 0), index(tmp, 1));
171                 }
172         }
173 }
174
175 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
176 {
177         if(m_jobIdentifiers.contains(jobId))
178         {
179                 m_jobLogFile[jobId].append(line.split('\n'));
180         }
181 }
182
183 const QStringList &ProgressModel::getLogFile(const QModelIndex &index) const
184 {
185         if(index.row() < m_jobList.count())
186         {
187                 QUuid id = m_jobList.at(index.row());
188                 QHash<QUuid,QStringList>::const_iterator iter = m_jobLogFile.constFind(id);
189                 if(iter != m_jobLogFile.constEnd()) { return iter.value(); }
190         }
191
192         return m_emptyList;
193 }
194
195 const QUuid &ProgressModel::getJobId(const QModelIndex &index) const
196 {
197         if(index.row() < m_jobList.count())
198         {
199                 return m_jobList.at(index.row());
200         }
201
202         return m_emptyUuid;
203 }
204
205 const ProgressModel::JobState ProgressModel::getJobState(const QModelIndex &index) const
206 {
207         if(index.row() < m_jobList.count())
208         {
209                 return static_cast<JobState>(m_jobState.value(m_jobList.at(index.row()), -1));
210         }
211
212         return static_cast<JobState>(-1);
213 }
214
215 void ProgressModel::addSystemMessage(const QString &text, int type)
216 {
217         const QUuid &jobId = QUuid::createUuid();
218
219         if(m_jobIdentifiers.contains(jobId))
220         {
221                 return;
222         }
223
224         while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
225         {
226                 beginRemoveRows(QModelIndex(), 0, 0);
227                 m_jobListHidden.append(m_jobList.takeFirst());
228                 m_jobIndexCache.clear();
229                 endRemoveRows();
230         }
231
232         int newIndex = m_jobList.count();
233         JobState jobState = JobState(-1);
234
235         switch(type)
236         {
237         case SysMsg_Warning:
238                 jobState = JobWarning;
239                 break;
240         case SysMsg_Performance:
241                 jobState = JobPerformance;
242                 break;
243         default:
244                 jobState = JobSystem;
245                 break;
246         }
247
248         beginInsertRows(QModelIndex(), newIndex, newIndex);
249
250         m_jobList.append(jobId);
251         m_jobName.insert(jobId, text);
252         m_jobStatus.insert(jobId, QString());
253         m_jobState.insert(jobId, jobState);
254         m_jobLogFile.insert(jobId, QStringList());
255         m_jobIdentifiers.insert(jobId);
256         
257         endInsertRows();
258 }
259
260 void ProgressModel::restoreHiddenItems(void)
261 {
262         if(!m_jobListHidden.isEmpty())
263         {
264                 beginResetModel();
265                 while(!m_jobListHidden.isEmpty())
266                 {
267                         m_jobList.prepend(m_jobListHidden.takeLast());
268                 }
269                 m_jobIndexCache.clear();
270                 endResetModel();
271         }
272 }
273
274 const QIcon &ProgressModel::getIcon(ProgressModel::JobState state) const
275 {
276         switch(state)
277         {
278         case JobRunning:
279                 return m_iconRunning;
280                 break;
281         case JobPaused:
282                 return m_iconPaused;
283                 break;
284         case JobComplete:
285                 return m_iconComplete;
286                 break;
287         case JobSystem:
288                 return m_iconSystem;
289                 break;
290         case JobWarning:
291                 return m_iconWarning;
292                 break;
293         case JobPerformance:
294                 return m_iconPerformance;
295                 break;
296         case JobSkipped:
297                 return m_iconSkipped;
298                 break;
299         default:
300                 return (state < 0) ? m_iconUndefined : m_iconFailed;
301                 break;
302         }
303 }