OSDN Git Service

bb23541091730e010904e75f382aaedbea93a62c
[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 {
38 }
39
40 ProgressModel::~ProgressModel(void)
41 {
42 }
43
44 int ProgressModel::columnCount(const QModelIndex &parent) const
45 {
46         return 2;
47 }
48
49 int ProgressModel::rowCount(const QModelIndex &parent) const
50 {
51         return m_jobList.count();
52 }
53
54 QVariant ProgressModel::data(const QModelIndex &index, int role) const
55 {
56         if(index.row() >= 0 && index.row() < m_jobList.count())
57         {
58                 if(role == Qt::DisplayRole)
59                 {
60                         switch(index.column())
61                         {
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;
71                         }
72                 }
73                 else if(role == Qt::DecorationRole && index.column() == 0)
74                 {
75                         switch(m_jobState.value(m_jobList.at(index.row())))
76                         {
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;
98                         }
99                 }
100                 else if(role == Qt::TextAlignmentRole)
101                 {
102                         return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
103                 }
104         }
105         
106         return QVariant();
107 }
108
109 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
110 {
111         if(role == Qt::DisplayRole)
112         {
113                 if(orientation == Qt::Horizontal)
114                 {
115                         switch(section)
116                         {
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;
126                         }
127                 }
128                 if(orientation == Qt::Vertical)
129                 {
130                         return QString::number(section + 1);
131                 }
132         }
133
134         return QVariant();
135 }
136
137 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
138 {
139         if(m_jobIdentifiers.contains(jobId))
140         {
141                 return;
142         }
143
144         while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
145         {
146                 beginRemoveRows(QModelIndex(), 0, 0);
147                 m_jobListHidden.append(m_jobList.takeFirst());
148                 m_jobIndexCache.clear();
149                 endRemoveRows();
150         }
151
152         int newIndex = m_jobList.count();
153         beginInsertRows(QModelIndex(), newIndex, newIndex);
154
155         m_jobList.append(jobId);
156         m_jobName.insert(jobId, jobName);
157         m_jobStatus.insert(jobId, jobInitialStatus);
158         m_jobState.insert(jobId, jobInitialState);
159         m_jobLogFile.insert(jobId, QStringList());
160         m_jobIdentifiers.insert(jobId);
161         
162         endInsertRows();
163 }
164
165 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
166 {
167         if(!m_jobIdentifiers.contains(jobId))
168         {
169                 return;
170         }
171         
172         if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
173         if(newState >= 0) m_jobState.insert(jobId, newState);
174
175         const int row = m_jobIndexCache.value(jobId, -1);
176
177         if(row >= 0)
178         {
179                 emit dataChanged(index(row, 0), index(row, 1));
180         }
181         else
182         {
183                 const int tmp = m_jobList.indexOf(jobId);
184                 if(tmp >= 0)
185                 {
186                         m_jobIndexCache.insert(jobId, tmp);
187                         emit dataChanged(index(tmp, 0), index(tmp, 1));
188                 }
189         }
190 }
191
192 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
193 {
194         if(m_jobIdentifiers.contains(jobId))
195         {
196                 m_jobLogFile[jobId].append(line.split('\n'));
197         }
198 }
199
200 const QStringList &ProgressModel::getLogFile(const QModelIndex &index)
201 {
202         if(index.row() < m_jobList.count())
203         {
204                 QUuid id = m_jobList.at(index.row());
205                 return m_jobLogFile[id];
206         }
207
208         return *(reinterpret_cast<QStringList*>(NULL));
209 }
210
211 const QUuid &ProgressModel::getJobId(const QModelIndex &index)
212 {
213         if(index.row() < m_jobList.count())
214         {
215                 return m_jobList.at(index.row());
216         }
217
218         return *(reinterpret_cast<QUuid*>(NULL));
219 }
220
221 void ProgressModel::addSystemMessage(const QString &text, int type)
222 {
223         const QUuid &jobId = QUuid::createUuid();
224
225         if(m_jobIdentifiers.contains(jobId))
226         {
227                 return;
228         }
229
230         while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
231         {
232                 beginRemoveRows(QModelIndex(), 0, 0);
233                 m_jobListHidden.append(m_jobList.takeFirst());
234                 m_jobIndexCache.clear();
235                 endRemoveRows();
236         }
237
238         int newIndex = m_jobList.count();
239         JobState jobState = JobState(-1);
240
241         switch(type)
242         {
243         case SysMsg_Warning:
244                 jobState = JobWarning;
245                 break;
246         case SysMsg_Performance:
247                 jobState = JobPerformance;
248                 break;
249         default:
250                 jobState = JobSystem;
251                 break;
252         }
253
254         beginInsertRows(QModelIndex(), newIndex, newIndex);
255
256         m_jobList.append(jobId);
257         m_jobName.insert(jobId, text);
258         m_jobStatus.insert(jobId, QString());
259         m_jobState.insert(jobId, jobState);
260         m_jobLogFile.insert(jobId, QStringList());
261         m_jobIdentifiers.insert(jobId);
262         
263         endInsertRows();
264 }
265
266 void ProgressModel::restoreHiddenItems(void)
267 {
268         if(!m_jobListHidden.isEmpty())
269         {
270                 beginResetModel();
271                 while(!m_jobListHidden.isEmpty())
272                 {
273                         m_jobList.prepend(m_jobListHidden.takeLast());
274                 }
275                 m_jobIndexCache.clear();
276                 endResetModel();
277         }
278 }