OSDN Git Service

Code refactoring: Now "Preferences" and "Recently" used models are in separate classe...
[x264-launcher/x264-launcher.git] / src / model_jobList.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Simple x264 Launcher
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 "global.h"
23 #include "model_jobList.h"
24 #include "thread_encode.h"
25 #include "model_options.h"
26 #include "model_preferences.h"
27 #include "resource.h"
28
29 #include <QIcon>
30 #include <QFileInfo>
31
32 #include <Mmsystem.h>
33
34 JobListModel::JobListModel(PreferencesModel *preferences)
35 {
36         m_preferences = preferences;
37 }
38
39 JobListModel::~JobListModel(void)
40 {
41         while(!m_jobs.isEmpty())
42         {
43                 QUuid id = m_jobs.takeFirst();
44                 EncodeThread *thread = m_threads.value(id, NULL);
45                 LogFileModel *logFile = m_logFile.value(id, NULL);
46                 X264_DELETE(thread);
47                 X264_DELETE(logFile);
48         }
49 }
50
51 ///////////////////////////////////////////////////////////////////////////////
52 // Model interface
53 ///////////////////////////////////////////////////////////////////////////////
54
55 int JobListModel::columnCount(const QModelIndex &parent) const
56 {
57         return 4;
58 }
59
60 int JobListModel::rowCount(const QModelIndex &parent) const
61 {
62         return m_jobs.count();
63 }
64
65 QVariant JobListModel::headerData(int section, Qt::Orientation orientation, int role) const 
66 {
67         if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
68         {
69                 switch(section)
70                 {
71                 case 0:
72                         return QVariant::fromValue<QString>(tr("Job"));
73                         break;
74                 case 1:
75                         return QVariant::fromValue<QString>(tr("Status"));
76                         break;
77                 case 2:
78                         return QVariant::fromValue<QString>(tr("Progress"));
79                         break;
80                 case 3:
81                         return QVariant::fromValue<QString>(tr("Details"));
82                         break;
83                 default:
84                         return QVariant();
85                         break;
86                 }
87         }
88
89         return QVariant();
90 }
91
92 QModelIndex JobListModel::index(int row, int column, const QModelIndex &parent) const
93 {
94         return createIndex(row, column, NULL);
95 }
96
97 QModelIndex JobListModel::parent(const QModelIndex &index) const
98 {
99         return QModelIndex();
100 }
101
102 QVariant JobListModel::data(const QModelIndex &index, int role) const
103 {
104         if(role == Qt::DisplayRole)
105         {
106                 if(index.row() >= 0 && index.row() < m_jobs.count())
107                 {
108                         switch(index.column())
109                         {
110                         case 0:
111                                 return m_name.value(m_jobs.at(index.row()));
112                                 break;
113                         case 1:
114                                 switch(m_status.value(m_jobs.at(index.row())))
115                                 {
116                                 case EncodeThread::JobStatus_Enqueued:
117                                         return QVariant::fromValue<QString>(tr("Enqueued."));
118                                         break;
119                                 case EncodeThread::JobStatus_Starting:
120                                         return QVariant::fromValue<QString>(tr("Starting..."));
121                                         break;
122                                 case EncodeThread::JobStatus_Indexing:
123                                         return QVariant::fromValue<QString>(tr("Indexing..."));
124                                         break;
125                                 case EncodeThread::JobStatus_Running:
126                                         return QVariant::fromValue<QString>(tr("Running..."));
127                                         break;
128                                 case EncodeThread::JobStatus_Running_Pass1:
129                                         return QVariant::fromValue<QString>(tr("Running... (Pass 1)"));
130                                         break;
131                                 case EncodeThread::JobStatus_Running_Pass2:
132                                         return QVariant::fromValue<QString>(tr("Running... (Pass 2)"));
133                                         break;
134                                 case EncodeThread::JobStatus_Completed:
135                                         return QVariant::fromValue<QString>(tr("Completed."));
136                                         break;
137                                 case EncodeThread::JobStatus_Failed:
138                                         return QVariant::fromValue<QString>(tr("Failed!"));
139                                         break;
140                                 case EncodeThread::JobStatus_Pausing:
141                                         return QVariant::fromValue<QString>(tr("Pausing..."));
142                                         break;
143                                 case EncodeThread::JobStatus_Paused:
144                                         return QVariant::fromValue<QString>(tr("Paused."));
145                                         break;
146                                 case EncodeThread::JobStatus_Resuming:
147                                         return QVariant::fromValue<QString>(tr("Resuming..."));
148                                         break;
149                                 case EncodeThread::JobStatus_Aborting:
150                                         return QVariant::fromValue<QString>(tr("Aborting..."));
151                                         break;
152                                 case EncodeThread::JobStatus_Aborted:
153                                         return QVariant::fromValue<QString>(tr("Aborted!"));
154                                         break;
155                                 default:
156                                         return QVariant::fromValue<QString>(tr("(Unknown)"));
157                                         break;
158                                 }
159                                 break;
160                         case 2:
161                                 return QString().sprintf("%d%%", m_progress.value(m_jobs.at(index.row())));
162                                 break;
163                         case 3:
164                                 return m_details.value(m_jobs.at(index.row()));
165                                 break;
166                         default:
167                                 return QVariant();
168                                 break;
169                         }
170                 }
171         }
172         else if(role == Qt::DecorationRole)
173         {
174                 if(index.row() >= 0 && index.row() < m_jobs.count() && index.column() == 0)
175                 {
176                         switch(m_status.value(m_jobs.at(index.row())))
177                         {
178                         case EncodeThread::JobStatus_Enqueued:
179                                 return QIcon(":/buttons/hourglass.png");
180                                 break;
181                         case EncodeThread::JobStatus_Starting:
182                                 return QIcon(":/buttons/lightning.png");
183                                 break;
184                         case EncodeThread::JobStatus_Indexing:
185                                 return QIcon(":/buttons/find.png");
186                                 break;
187                         case EncodeThread::JobStatus_Running:
188                         case EncodeThread::JobStatus_Running_Pass1:
189                         case EncodeThread::JobStatus_Running_Pass2:
190                                 return QIcon(":/buttons/play.png");
191                                 break;
192                         case EncodeThread::JobStatus_Completed:
193                                 return QIcon(":/buttons/accept.png");
194                                 break;
195                         case EncodeThread::JobStatus_Failed:
196                                 return QIcon(":/buttons/exclamation.png");
197                                 break;
198                         case EncodeThread::JobStatus_Pausing:
199                                 return QIcon(":/buttons/clock_pause.png");
200                                 break;
201                         case EncodeThread::JobStatus_Paused:
202                                 return QIcon(":/buttons/suspended.png");
203                                 break;
204                         case EncodeThread::JobStatus_Resuming:
205                                 return QIcon(":/buttons/clock_play.png");
206                                 break;
207                         case EncodeThread::JobStatus_Aborting:
208                                 return QIcon(":/buttons/clock_stop.png");
209                                 break;
210                         case EncodeThread::JobStatus_Aborted:
211                                 return QIcon(":/buttons/error.png");
212                                 break;
213                         default:
214                                 return QVariant();
215                                 break;
216                         }
217                 }
218         }
219
220         return QVariant();
221 }
222
223 ///////////////////////////////////////////////////////////////////////////////
224 // Public interface
225 ///////////////////////////////////////////////////////////////////////////////
226
227 QModelIndex JobListModel::insertJob(EncodeThread *thread)
228 {
229         QUuid id = thread->getId();
230
231         if(m_jobs.contains(id))
232         {
233                 return QModelIndex();
234         }
235         
236         QString config = "N/A";
237
238         switch(thread->options()->rcMode())
239         {
240         case OptionsModel::RCMode_CRF:
241                 config = QString("CRF@%1").arg(QString::number(thread->options()->quantizer()));
242                 break;
243         case OptionsModel::RCMode_CQ:
244                 config = QString("CQ@%1").arg(QString::number(qRound(thread->options()->quantizer())));
245                 break;
246         case OptionsModel::RCMode_2Pass:
247                 config = QString("2Pass@%1").arg(QString::number(thread->options()->bitrate()));
248                 break;
249         case OptionsModel::RCMode_ABR:
250                 config = QString("ABR@%1").arg(QString::number(thread->options()->bitrate()));
251                 break;
252         }
253
254         int n = 2;
255         QString jobName = QString("%1 (%2)").arg(QFileInfo(thread->sourceFileName()).completeBaseName().simplified(), config);
256
257         forever
258         {
259                 bool unique = true;
260                 for(int i = 0; i < m_jobs.count(); i++)
261                 {
262                         if(m_name.value(m_jobs.at(i)).compare(jobName, Qt::CaseInsensitive) == 0)
263                         {
264                                 unique = false;
265                                 break;
266                         }
267                 }
268                 if(!unique)
269                 {
270                         jobName = QString("%1 %2 (%3)").arg(QFileInfo(thread->sourceFileName()).completeBaseName().simplified(), QString::number(n++), config);
271                         continue;
272                 }
273                 break;
274         }
275         
276         LogFileModel *logFile = new LogFileModel(thread->sourceFileName(), thread->outputFileName(), config);
277         
278         beginInsertRows(QModelIndex(), m_jobs.count(), m_jobs.count());
279         m_jobs.append(id);
280         m_name.insert(id, jobName);
281         m_status.insert(id, EncodeThread::JobStatus_Enqueued);
282         m_progress.insert(id, 0);
283         m_threads.insert(id, thread);
284         m_logFile.insert(id, logFile);
285         m_details.insert(id, tr("Not started yet."));
286         endInsertRows();
287
288         connect(thread, SIGNAL(statusChanged(QUuid, EncodeThread::JobStatus)), this, SLOT(updateStatus(QUuid, EncodeThread::JobStatus)), Qt::QueuedConnection);
289         connect(thread, SIGNAL(progressChanged(QUuid, unsigned int)), this, SLOT(updateProgress(QUuid, unsigned int)), Qt::QueuedConnection);
290         connect(thread, SIGNAL(messageLogged(QUuid, QString)), logFile, SLOT(addLogMessage(QUuid, QString)), Qt::QueuedConnection);
291         connect(thread, SIGNAL(detailsChanged(QUuid, QString)), this, SLOT(updateDetails(QUuid, QString)), Qt::QueuedConnection);
292         
293         return createIndex(m_jobs.count() - 1, 0, NULL);
294 }
295
296 bool JobListModel::startJob(const QModelIndex &index)
297 {
298         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
299         {
300                 QUuid id = m_jobs.at(index.row());
301                 if(m_status.value(id) == EncodeThread::JobStatus_Enqueued)
302                 {
303                         updateStatus(id, EncodeThread::JobStatus_Starting);
304                         updateDetails(id, tr("Starting up, please wait..."));
305                         m_threads.value(id)->start();
306                         return true;
307                 }
308         }
309
310         return false;
311 }
312
313 bool JobListModel::pauseJob(const QModelIndex &index)
314 {
315         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
316         {
317                 QUuid id = m_jobs.at(index.row());
318                 EncodeThread::JobStatus status = m_status.value(id);
319                 if((status == EncodeThread::JobStatus_Indexing) || (status == EncodeThread::JobStatus_Running) ||
320                         (status == EncodeThread::JobStatus_Running_Pass1) || (status == EncodeThread::JobStatus_Running_Pass2))
321                 {
322                         updateStatus(id, EncodeThread::JobStatus_Pausing);
323                         m_threads.value(id)->pauseJob();
324                         return true;
325                 }
326         }
327
328         return false;
329 }
330
331 bool JobListModel::resumeJob(const QModelIndex &index)
332 {
333         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
334         {
335                 QUuid id = m_jobs.at(index.row());
336                 EncodeThread::JobStatus status = m_status.value(id);
337                 if(status == EncodeThread::JobStatus_Paused)
338                 {
339                         updateStatus(id, EncodeThread::JobStatus_Resuming);
340                         m_threads.value(id)->resumeJob();
341                         return true;
342                 }
343         }
344
345         return false;
346 }
347
348 bool JobListModel::abortJob(const QModelIndex &index)
349 {
350         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
351         {
352                 QUuid id = m_jobs.at(index.row());
353                 if(m_status.value(id) == EncodeThread::JobStatus_Indexing || m_status.value(id) == EncodeThread::JobStatus_Running ||
354                         m_status.value(id) == EncodeThread::JobStatus_Running_Pass1 || EncodeThread::JobStatus_Running_Pass2)
355                 {
356                         updateStatus(id, EncodeThread::JobStatus_Aborting);
357                         m_threads.value(id)->abortJob();
358                         return true;
359                 }
360         }
361
362         return false;
363 }
364
365 bool JobListModel::deleteJob(const QModelIndex &index)
366 {
367         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
368         {
369                 QUuid id = m_jobs.at(index.row());
370                 if(m_status.value(id) == EncodeThread::JobStatus_Completed || m_status.value(id) == EncodeThread::JobStatus_Failed ||
371                         m_status.value(id) == EncodeThread::JobStatus_Aborted || m_status.value(id) == EncodeThread::JobStatus_Enqueued)
372                 {
373                         int idx = index.row();
374                         QUuid id = m_jobs.at(idx);
375                         EncodeThread *thread = m_threads.value(id, NULL);
376                         LogFileModel *logFile = m_logFile.value(id, NULL);
377                         if((thread == NULL) || (!thread->isRunning()))
378                         {
379                                 
380                                 beginRemoveRows(QModelIndex(), idx, idx);
381                                 m_jobs.removeAt(index.row());
382                                 m_name.remove(id);
383                                 m_threads.remove(id);
384                                 m_status.remove(id);
385                                 m_progress.remove(id);
386                                 m_logFile.remove(id);
387                                 m_details.remove(id);
388                                 endRemoveRows();
389                                 X264_DELETE(thread);
390                                 X264_DELETE(logFile);
391                                 return true;
392                         }
393                 }
394         }
395
396         return false;
397 }
398
399 LogFileModel *JobListModel::getLogFile(const QModelIndex &index)
400 {
401         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
402         {
403                 return m_logFile.value(m_jobs.at(index.row()));
404         }
405
406         return NULL;
407 }
408
409 const QString &JobListModel::getJobSourceFile(const QModelIndex &index)
410 {
411         static QString nullStr;
412         
413         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
414         {
415                 EncodeThread *thread = m_threads.value(m_jobs.at(index.row()));
416                 return (thread != NULL) ? thread->sourceFileName() : nullStr;
417         }
418
419         return nullStr;
420 }
421
422 const QString &JobListModel::getJobOutputFile(const QModelIndex &index)
423 {
424         static QString nullStr;
425         
426         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
427         {
428                 EncodeThread *thread = m_threads.value(m_jobs.at(index.row()));
429                 return (thread != NULL) ? thread->outputFileName() : nullStr;
430         }
431
432         return nullStr;
433 }
434
435 EncodeThread::JobStatus JobListModel::getJobStatus(const QModelIndex &index)
436 {
437         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
438         {
439                 return m_status.value(m_jobs.at(index.row()));
440         }
441
442         return static_cast<EncodeThread::JobStatus>(-1);
443 }
444
445 unsigned int JobListModel::getJobProgress(const QModelIndex &index)
446 {
447         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
448         {
449                 return m_progress.value(m_jobs.at(index.row()));
450         }
451
452         return 0;
453 }
454
455 const OptionsModel *JobListModel::getJobOptions(const QModelIndex &index)
456 {
457         static QString nullStr;
458         
459         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
460         {
461                 EncodeThread *thread = m_threads.value(m_jobs.at(index.row()));
462                 return (thread != NULL) ? thread->options() : NULL;
463         }
464
465         return NULL;
466 }
467
468 QModelIndex JobListModel::getJobIndexById(const QUuid &id)
469 {
470         if(m_jobs.contains(id))
471         {
472                 return createIndex(m_jobs.indexOf(id), 0);
473         }
474
475         return QModelIndex();
476 }
477
478 ///////////////////////////////////////////////////////////////////////////////
479 // Slots
480 ///////////////////////////////////////////////////////////////////////////////
481
482 void JobListModel::updateStatus(const QUuid &jobId, EncodeThread::JobStatus newStatus)
483 {
484         int index = -1;
485         
486         if((index = m_jobs.indexOf(jobId)) >= 0)
487         {
488                 m_status.insert(jobId, newStatus);
489                 emit dataChanged(createIndex(index, 0), createIndex(index, 1));
490
491                 if(m_preferences->enableSounds())
492                 {
493                         switch(newStatus)
494                         {
495                         case EncodeThread::JobStatus_Completed:
496                                 PlaySound(MAKEINTRESOURCE(IDR_WAVE4), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC);
497                                 break;
498                         case EncodeThread::JobStatus_Aborted:
499                                 PlaySound(MAKEINTRESOURCE(IDR_WAVE5), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC);
500                                 break;
501                         case EncodeThread::JobStatus_Failed:
502                                 PlaySound(MAKEINTRESOURCE(IDR_WAVE6), GetModuleHandle(NULL), SND_RESOURCE | SND_ASYNC);
503                                 break;
504                         }
505                 }
506         }
507 }
508
509 void JobListModel::updateProgress(const QUuid &jobId, unsigned int newProgress)
510 {
511         int index = -1;
512
513         if((index = m_jobs.indexOf(jobId)) >= 0)
514         {
515                 m_progress.insert(jobId, qBound(0U, newProgress, 100U));
516                 emit dataChanged(createIndex(index, 2), createIndex(index, 2));
517         }
518 }
519
520 void JobListModel::updateDetails(const QUuid &jobId, const QString &details)
521 {
522         int index = -1;
523
524         if((index = m_jobs.indexOf(jobId)) >= 0)
525         {
526                 m_details.insert(jobId, details);
527                 emit dataChanged(createIndex(index, 3), createIndex(index, 3));
528         }
529 }