OSDN Git Service

Updated Russian translation. Thanks to Иван Митин <bardak@inbox.ru>.
[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 50
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_jobList.contains(jobId) || m_jobListHidden.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                 endRemoveRows();
149         }
150
151         int newIndex = m_jobList.count();
152         beginInsertRows(QModelIndex(), newIndex, newIndex);
153
154         m_jobList.append(jobId);
155         m_jobName.insert(jobId, jobName);
156         m_jobStatus.insert(jobId, jobInitialStatus);
157         m_jobState.insert(jobId, jobInitialState);
158         m_jobLogFile.insert(jobId, QStringList());
159         
160         endInsertRows();
161 }
162
163 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
164 {
165         int row = m_jobList.indexOf(jobId);
166
167         if(row < 0)
168         {
169                 if(m_jobListHidden.indexOf(jobId) >= 0)
170                 {
171                         if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
172                         if(newState >= 0) m_jobState.insert(jobId, newState);
173                 }
174                 return;
175         }
176
177         if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
178         if(newState >= 0) m_jobState.insert(jobId, newState);
179         
180         emit dataChanged(index(row, 0), index(row, 1));
181 }
182
183 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
184 {
185         if(m_jobList.contains(jobId))
186         {
187                 m_jobLogFile[jobId].append(line.split('\n'));
188         }
189 }
190
191 const QStringList &ProgressModel::getLogFile(const QModelIndex &index)
192 {
193         if(index.row() < m_jobList.count())
194         {
195                 QUuid id = m_jobList.at(index.row());
196                 return m_jobLogFile[id];
197         }
198
199         return *(reinterpret_cast<QStringList*>(NULL));
200 }
201
202 const QUuid &ProgressModel::getJobId(const QModelIndex &index)
203 {
204         if(index.row() < m_jobList.count())
205         {
206                 return m_jobList.at(index.row());
207         }
208
209         return *(reinterpret_cast<QUuid*>(NULL));
210 }
211
212 void ProgressModel::addSystemMessage(const QString &text, int type)
213 {
214         const QUuid &jobId = QUuid::createUuid();
215
216         if(m_jobList.contains(jobId))
217         {
218                 return;
219         }
220
221         while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
222         {
223                 beginRemoveRows(QModelIndex(), 0, 0);
224                 m_jobListHidden.append(m_jobList.takeFirst());
225                 endRemoveRows();
226         }
227
228         int newIndex = m_jobList.count();
229         JobState jobState = JobState(-1);
230
231         switch(type)
232         {
233         case SysMsg_Warning:
234                 jobState = JobWarning;
235                 break;
236         case SysMsg_Performance:
237                 jobState = JobPerformance;
238                 break;
239         default:
240                 jobState = JobSystem;
241                 break;
242         }
243
244         beginInsertRows(QModelIndex(), newIndex, newIndex);
245
246         m_jobList.append(jobId);
247         m_jobName.insert(jobId, text);
248         m_jobStatus.insert(jobId, QString());
249         m_jobState.insert(jobId, jobState);
250         m_jobLogFile.insert(jobId, QStringList());
251         
252         endInsertRows();
253 }
254
255 void ProgressModel::restoreHiddenItems(void)
256 {
257         if(!m_jobListHidden.isEmpty())
258         {
259                 beginResetModel();
260                 while(!m_jobListHidden.isEmpty())
261                 {
262                         m_jobList.prepend(m_jobListHidden.takeLast());
263                 }
264                 endResetModel();
265         }
266 }