OSDN Git Service

Added log file support.
[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 "thread_encode.h"
24
25 #include <QIcon>
26
27 JobListModel::JobListModel(void)
28 {
29 }
30
31 JobListModel::~JobListModel(void)
32 {
33 }
34
35 ///////////////////////////////////////////////////////////////////////////////
36 // Model interface
37 ///////////////////////////////////////////////////////////////////////////////
38
39 int JobListModel::columnCount(const QModelIndex &parent) const
40 {
41         return 3;
42 }
43
44 int JobListModel::rowCount(const QModelIndex &parent) const
45 {
46         return m_jobs.count();
47 }
48
49 QVariant JobListModel::headerData(int section, Qt::Orientation orientation, int role) const 
50 {
51         if((orientation == Qt::Horizontal) && (role == Qt::DisplayRole))
52         {
53                 switch(section)
54                 {
55                 case 0:
56                         return QVariant::fromValue<QString>(tr("Job"));
57                         break;
58                 case 1:
59                         return QVariant::fromValue<QString>(tr("Status"));
60                         break;
61                 case 2:
62                         return QVariant::fromValue<QString>(tr("Progress"));
63                         break;
64                 default:
65                         return QVariant();
66                         break;
67                 }
68         }
69
70         return QVariant();
71 }
72
73 QModelIndex JobListModel::index(int row, int column, const QModelIndex &parent) const
74 {
75         return createIndex(row, column, NULL);
76 }
77
78 QModelIndex JobListModel::parent(const QModelIndex &index) const
79 {
80         return QModelIndex();
81 }
82
83 QVariant JobListModel::data(const QModelIndex &index, int role) const
84 {
85         if(role == Qt::DisplayRole)
86         {
87                 if(index.row() >= 0 && index.row() < m_jobs.count())
88                 {
89                         switch(index.column())
90                         {
91                         case 0:
92                                 return m_jobs.at(index.row()).toString();
93                                 break;
94                         case 1:
95                                 switch(m_status.value(m_jobs.at(index.row())))
96                                 {
97                                 case EncodeThread::JobStatus_Enqueued:
98                                         return QVariant::fromValue<QString>(tr("Enqueued."));
99                                         break;
100                                 case EncodeThread::JobStatus_Starting:
101                                         return QVariant::fromValue<QString>(tr("Starting..."));
102                                         break;
103                                 case EncodeThread::JobStatus_Indexing:
104                                         return QVariant::fromValue<QString>(tr("Indexing..."));
105                                         break;
106                                 case EncodeThread::JobStatus_Running:
107                                         return QVariant::fromValue<QString>(tr("Running..."));
108                                         break;
109                                 case EncodeThread::JobStatus_Completed:
110                                         return QVariant::fromValue<QString>(tr("Completed."));
111                                         break;
112                                 case EncodeThread::JobStatus_Failed:
113                                         return QVariant::fromValue<QString>(tr("Failed!"));
114                                         break;
115                                 case EncodeThread::JobStatus_Aborted:
116                                         return QVariant::fromValue<QString>(tr("Aborted!"));
117                                         break;
118                                 default:
119                                         return QVariant::fromValue<QString>(tr("(Unknown)"));
120                                         break;
121                                 }
122                                 break;
123                         case 2:
124                                 return QString().sprintf("%d%%", m_progress.value(m_jobs.at(index.row())));
125                                 break;
126                         default:
127                                 return QVariant();
128                                 break;
129                         }
130                 }
131         }
132         else if(role == Qt::DecorationRole)
133         {
134                 if(index.row() >= 0 && index.row() < m_jobs.count() && index.column() == 0)
135                 {
136                         switch(m_status.value(m_jobs.at(index.row())))
137                         {
138                         case EncodeThread::JobStatus_Enqueued:
139                                 return QIcon(":/buttons/clock_pause.png");
140                                 break;
141                         case EncodeThread::JobStatus_Starting:
142                                 return QIcon(":/buttons/lightning.png");
143                                 break;
144                         case EncodeThread::JobStatus_Indexing:
145                                 return QIcon(":/buttons/find.png");
146                                 break;
147                         case EncodeThread::JobStatus_Running:
148                                 return QIcon(":/buttons/play.png");
149                                 break;
150                         case EncodeThread::JobStatus_Completed:
151                                 return QIcon(":/buttons/accept.png");
152                                 break;
153                         case EncodeThread::JobStatus_Failed:
154                                 return QIcon(":/buttons/exclamation.png");
155                                 break;
156                         case EncodeThread::JobStatus_Aborted:
157                                 return QIcon(":/buttons/error.png");
158                                 break;
159                         default:
160                                 return QVariant();
161                                 break;
162                         }
163                 }
164         }
165
166         return QVariant();
167 }
168
169 ///////////////////////////////////////////////////////////////////////////////
170 // Public interface
171 ///////////////////////////////////////////////////////////////////////////////
172
173 bool JobListModel::insertJob(EncodeThread *thread)
174 {
175         QUuid id = thread->getId();
176         LogFileModel *logFile = NULL;
177
178         if(m_jobs.contains(id))
179         {
180                 return false;
181         }
182                 
183         beginInsertRows(QModelIndex(), m_jobs.count(), m_jobs.count());
184         m_jobs.append(id);
185         m_status.insert(id, EncodeThread::JobStatus_Enqueued);
186         m_progress.insert(id, 0);
187         m_threads.insert(id, thread);
188         m_logFile.insert(id, (logFile = new LogFileModel));
189         endInsertRows();
190
191         connect(thread, SIGNAL(statusChanged(QUuid, EncodeThread::JobStatus)), this, SLOT(updateStatus(QUuid, EncodeThread::JobStatus)), Qt::QueuedConnection);
192         connect(thread, SIGNAL(progressChanged(QUuid, unsigned int)), this, SLOT(updateProgress(QUuid, unsigned int)), Qt::QueuedConnection);
193         connect(thread, SIGNAL(messageLogged(QUuid, QString)), logFile, SLOT(addLogMessage(QUuid, QString)), Qt::QueuedConnection);
194         
195         return true;
196 }
197
198 LogFileModel *JobListModel::getLogFile(const QModelIndex &index)
199 {
200         if(index.isValid() && index.row() >= 0 && index.row() < m_jobs.count())
201         {
202                 return m_logFile.value(m_jobs.at(index.row()));
203         }
204 }
205
206 ///////////////////////////////////////////////////////////////////////////////
207 // Slots
208 ///////////////////////////////////////////////////////////////////////////////
209
210 void JobListModel::updateStatus(const QUuid &jobId, EncodeThread::JobStatus newStatus)
211 {
212         int index = -1;
213         
214         if((index = m_jobs.indexOf(jobId)) >= 0)
215         {
216                 m_status.insert(jobId, newStatus);
217                 emit dataChanged(createIndex(index, 0), createIndex(index, 1));
218         }
219 }
220
221 void JobListModel::updateProgress(const QUuid &jobId, unsigned int newProgress)
222 {
223         int index = -1;
224
225         if((index = m_jobs.indexOf(jobId)) >= 0)
226         {
227                 m_progress.insert(jobId, newProgress);
228                 emit dataChanged(createIndex(index, 2), createIndex(index, 2));
229         }
230 }