OSDN Git Service

Make "Add Job" dialog work + added some useful web-links.
[x264-launcher/x264-launcher.git] / src / model_jobList.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
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_jobList.h"
23 #include "global.h"
24 #include "thread_encode.h"
25
26 #include <QIcon>
27 #include <QFileInfo>
28
29 JobListModel::JobListModel(void)
30 {
31 }
32
33 JobListModel::~JobListModel(void)
34 {
35         while(!m_jobs.isEmpty())
36         {
37                 QUuid id = m_jobs.takeFirst();
38                 EncodeThread *thread = m_threads.value(id, NULL);
39                 LogFileModel *logFile = m_logFile.value(id, NULL);
40                 X264_DELETE(thread);
41                 X264_DELETE(logFile);
42         }
43 }
44
45 ///////////////////////////////////////////////////////////////////////////////
46 // Model interface
47 ///////////////////////////////////////////////////////////////////////////////
48
49 int JobListModel::columnCount(const QModelIndex &parent) const
50 {
51         return 4;
52 }
53
54 int JobListModel::rowCount(const QModelIndex &parent) const
55 {
56         return m_jobs.count();
57 }
58
59 QVariant JobListModel::headerData(int section, Qt::Orientation orientation, int role) const 
60 {
61         if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
62         {
63                 switch(section)
64                 {
65                 case 0:
66                         return QVariant::fromValue<QString>(tr("Job"));
67                         break;
68                 case 1:
69                         return QVariant::fromValue<QString>(tr("Status"));
70                         break;
71                 case 2:
72                         return QVariant::fromValue<QString>(tr("Progress"));
73                         break;
74                 case 3:
75                         return QVariant::fromValue<QString>(tr("Details"));
76                         break;
77                 default:
78                         return QVariant();
79                         break;
80                 }
81         }
82
83         return QVariant();
84 }
85
86 QModelIndex JobListModel::index(int row, int column, const QModelIndex &parent) const
87 {
88         return createIndex(row, column, NULL);
89 }
90
91 QModelIndex JobListModel::parent(const QModelIndex &index) const
92 {
93         return QModelIndex();
94 }
95
96 QVariant JobListModel::data(const QModelIndex &index, int role) const
97 {
98         if(role == Qt::DisplayRole)
99         {
100                 if(index.row() >= 0 && index.row() < m_jobs.count())
101                 {
102                         switch(index.column())
103                         {
104                         case 0:
105                                 return m_name.value(m_jobs.at(index.row()));
106                                 break;
107                         case 1:
108                                 switch(m_status.value(m_jobs.at(index.row())))
109                                 {
110                                 case EncodeThread::JobStatus_Enqueued:
111                                         return QVariant::fromValue<QString>(tr("Enqueued."));
112                                         break;
113                                 case EncodeThread::JobStatus_Starting:
114                                         return QVariant::fromValue<QString>(tr("Starting..."));
115                                         break;
116                                 case EncodeThread::JobStatus_Indexing:
117                                         return QVariant::fromValue<QString>(tr("Indexing..."));
118                                         break;
119                                 case EncodeThread::JobStatus_Running:
120                                         return QVariant::fromValue<QString>(tr("Running..."));
121                                         break;
122                                 case EncodeThread::JobStatus_Running_Pass1:
123                                         return QVariant::fromValue<QString>(tr("Running... (Pass 1)"));
124                                         break;
125                                 case EncodeThread::JobStatus_Running_Pass2:
126                                         return QVariant::fromValue<QString>(tr("Running... (Pass 2)"));
127                                         break;
128                                 case EncodeThread::JobStatus_Completed:
129                                         return QVariant::fromValue<QString>(tr("Completed."));
130                                         break;
131                                 case EncodeThread::JobStatus_Failed:
132                                         return QVariant::fromValue<QString>(tr("Failed!"));
133                                         break;
134                                 case EncodeThread::JobStatus_Aborting:
135                                         return QVariant::fromValue<QString>(tr("Aborting..."));
136                                         break;
137                                 case EncodeThread::JobStatus_Aborted:
138                                         return QVariant::fromValue<QString>(tr("Aborted!"));
139                                         break;
140                                 default:
141                                         return QVariant::fromValue<QString>(tr("(Unknown)"));
142                                         break;
143                                 }
144                                 break;
145                         case 2:
146                                 return QString().sprintf("%d%%", m_progress.value(m_jobs.at(index.row())));
147                                 break;
148                         case 3:
149                                 return m_details.value(m_jobs.at(index.row()));
150                                 break;
151                         default:
152                                 return QVariant();
153                                 break;
154                         }
155                 }
156         }
157         else if(role == Qt::DecorationRole)
158         {
159                 if(index.row() >= 0 && index.row() < m_jobs.count() && index.column() == 0)
160                 {
161                         switch(m_status.value(m_jobs.at(index.row())))
162                         {
163                         case EncodeThread::JobStatus_Enqueued:
164                                 return QIcon(":/buttons/clock_pause.png");
165                                 break;
166                         case EncodeThread::JobStatus_Starting:
167                                 return QIcon(":/buttons/lightning.png");
168                                 break;
169                         case EncodeThread::JobStatus_Indexing:
170                                 return QIcon(":/buttons/find.png");
171                                 break;
172                         case EncodeThread::JobStatus_Running:
173                         case EncodeThread::JobStatus_Running_Pass1:
174                         case EncodeThread::JobStatus_Running_Pass2:
175                                 return QIcon(":/buttons/play.png");
176                                 break;
177                         case EncodeThread::JobStatus_Completed:
178                                 return QIcon(":/buttons/accept.png");
179                                 break;
180                         case EncodeThread::JobStatus_Failed:
181                                 return QIcon(":/buttons/exclamation.png");
182                                 break;
183                         case EncodeThread::JobStatus_Aborting:
184                                 return QIcon(":/buttons/clock_stop.png");
185                                 break;
186                         case EncodeThread::JobStatus_Aborted:
187                                 return QIcon(":/buttons/error.png");
188                                 break;
189                         default:
190                                 return QVariant();
191                                 break;
192                         }
193                 }
194         }
195
196         return QVariant();
197 }
198
199 ///////////////////////////////////////////////////////////////////////////////
200 // Public interface
201 ///////////////////////////////////////////////////////////////////////////////
202
203 QModelIndex JobListModel::insertJob(EncodeThread *thread)
204 {
205         QUuid id = thread->getId();
206         LogFileModel *logFile = NULL;
207
208         if(m_jobs.contains(id))
209         {
210                 return QModelIndex();
211         }
212                 
213         beginInsertRows(QModelIndex(), m_jobs.count(), m_jobs.count());
214         m_jobs.append(id);
215         m_name.insert(id, QFileInfo(thread->sourceFileName()).completeBaseName());
216         m_status.insert(id, EncodeThread::JobStatus_Enqueued);
217         m_progress.insert(id, 0);
218         m_threads.insert(id, thread);
219         m_logFile.insert(id, (logFile = new LogFileModel));
220         m_details.insert(id, tr("Not started yet."));
221         endInsertRows();
222
223         connect(thread, SIGNAL(statusChanged(QUuid, EncodeThread::JobStatus)), this, SLOT(updateStatus(QUuid, EncodeThread::JobStatus)), Qt::QueuedConnection);
224         connect(thread, SIGNAL(progressChanged(QUuid, unsigned int)), this, SLOT(updateProgress(QUuid, unsigned int)), Qt::QueuedConnection);
225         connect(thread, SIGNAL(messageLogged(QUuid, QString)), logFile, SLOT(addLogMessage(QUuid, QString)), Qt::QueuedConnection);
226         connect(thread, SIGNAL(detailsChanged(QUuid, QString)), this, SLOT(updateDetails(QUuid, QString)), Qt::QueuedConnection);
227         
228         return createIndex(m_jobs.count() - 1, 0, NULL);
229 }
230
231 bool JobListModel::startJob(const QModelIndex &index)
232 {
233         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
234         {
235                 QUuid id = m_jobs.at(index.row());
236                 if(m_status.value(id) == EncodeThread::JobStatus_Enqueued)
237                 {
238                         updateStatus(id, EncodeThread::JobStatus_Starting);
239                         updateDetails(id, tr("Starting up, please wait..."));
240                         m_threads.value(id)->start();
241                         return true;
242                 }
243         }
244
245         return false;
246 }
247
248 bool JobListModel::abortJob(const QModelIndex &index)
249 {
250         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
251         {
252                 QUuid id = m_jobs.at(index.row());
253                 if(m_status.value(id) == EncodeThread::JobStatus_Indexing || m_status.value(id) == EncodeThread::JobStatus_Running)
254                 {
255                         updateStatus(id, EncodeThread::JobStatus_Aborting);
256                         m_threads.value(id)->abortJob();
257                         return true;
258                 }
259         }
260
261         return false;
262 }
263
264 LogFileModel *JobListModel::getLogFile(const QModelIndex &index)
265 {
266         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
267         {
268                 return m_logFile.value(m_jobs.at(index.row()));
269         }
270
271         return NULL;
272 }
273
274 EncodeThread::JobStatus JobListModel::getJobStatus(const QModelIndex &index)
275 {
276         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
277         {
278                 return m_status.value(m_jobs.at(index.row()));
279         }
280
281         return static_cast<EncodeThread::JobStatus>(-1);
282 }
283
284 unsigned int JobListModel::getJobProgress(const QModelIndex &index)
285 {
286         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
287         {
288                 return m_progress.value(m_jobs.at(index.row()));
289         }
290
291         return 0;
292 }
293
294 QModelIndex JobListModel::getJobIndexById(const QUuid &id)
295 {
296         if(m_jobs.contains(id))
297         {
298                 return createIndex(m_jobs.indexOf(id), 0);
299         }
300
301         return QModelIndex();
302 }
303
304 ///////////////////////////////////////////////////////////////////////////////
305 // Slots
306 ///////////////////////////////////////////////////////////////////////////////
307
308 void JobListModel::updateStatus(const QUuid &jobId, EncodeThread::JobStatus newStatus)
309 {
310         int index = -1;
311         
312         if((index = m_jobs.indexOf(jobId)) >= 0)
313         {
314                 m_status.insert(jobId, newStatus);
315                 emit dataChanged(createIndex(index, 0), createIndex(index, 1));
316         }
317 }
318
319 void JobListModel::updateProgress(const QUuid &jobId, unsigned int newProgress)
320 {
321         int index = -1;
322
323         if((index = m_jobs.indexOf(jobId)) >= 0)
324         {
325                 m_progress.insert(jobId, newProgress);
326                 emit dataChanged(createIndex(index, 2), createIndex(index, 2));
327         }
328 }
329
330 void JobListModel::updateDetails(const QUuid &jobId, const QString &details)
331 {
332         int index = -1;
333
334         if((index = m_jobs.indexOf(jobId)) >= 0)
335         {
336                 m_details.insert(jobId, details);
337                 emit dataChanged(createIndex(index, 3), createIndex(index, 3));
338         }
339 }