OSDN Git Service

Don't display more than 50 table items in the processing window. It seems Qt is getti...
[lamexp/LameXP.git] / src / Model_Progress.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 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 {
37 }
38
39 ProgressModel::~ProgressModel(void)
40 {
41 }
42
43 int ProgressModel::columnCount(const QModelIndex &parent) const
44 {
45         return 2;
46 }
47
48 int ProgressModel::rowCount(const QModelIndex &parent) const
49 {
50         return m_jobList.count();
51 }
52
53 QVariant ProgressModel::data(const QModelIndex &index, int role) const
54 {
55         if(index.row() >= 0 && index.row() < m_jobList.count())
56         {
57                 if(role == Qt::DisplayRole)
58                 {
59                         switch(index.column())
60                         {
61                         case 0:
62                                 return m_jobName.value(m_jobList.at(index.row()));
63                                 break;
64                         case 1:
65                                 return m_jobStatus.value(m_jobList.at(index.row()));
66                                 break;
67                         default:
68                                 return QVariant();
69                                 break;
70                         }
71                 }
72                 else if(role == Qt::DecorationRole && index.column() == 0)
73                 {
74                         switch(m_jobState.value(m_jobList.at(index.row())))
75                         {
76                         case JobRunning:
77                                 return m_iconRunning;
78                                 break;
79                         case JobPaused:
80                                 return m_iconPaused;
81                                 break;
82                         case JobComplete:
83                                 return m_iconComplete;
84                                 break;
85                         case JobSystem:
86                                 return m_iconSystem;
87                                 break;
88                         case JobWarning:
89                                 return m_iconWarning;
90                                 break;
91                         default:
92                                 return m_iconFailed;
93                                 break;
94                         }
95                 }
96                 else if(role == Qt::TextAlignmentRole)
97                 {
98                         return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
99                 }
100         }
101         
102         return QVariant();
103 }
104
105 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
106 {
107         if(role == Qt::DisplayRole)
108         {
109                 if(orientation == Qt::Horizontal)
110                 {
111                         switch(section)
112                         {
113                         case 0:
114                                 return tr("Job");
115                                 break;
116                         case 1:
117                                 return tr("Status");
118                                 break;
119                         default:
120                                 return QVariant();
121                                 break;
122                         }
123                 }
124                 if(orientation == Qt::Vertical)
125                 {
126                         return QString::number(section + 1);
127                 }
128         }
129
130         return QVariant();
131 }
132
133 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
134 {
135         if(m_jobList.contains(jobId) || m_jobListHidden.contains(jobId))
136         {
137                 return;
138         }
139
140         while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
141         {
142                 beginRemoveRows(QModelIndex(), 0, 0);
143                 m_jobListHidden.append(m_jobList.takeFirst());
144                 endRemoveRows();
145         }
146
147         int newIndex = m_jobList.count();
148         beginInsertRows(QModelIndex(), newIndex, newIndex);
149
150         m_jobList.append(jobId);
151         m_jobName.insert(jobId, jobName);
152         m_jobStatus.insert(jobId, jobInitialStatus);
153         m_jobState.insert(jobId, jobInitialState);
154         m_jobLogFile.insert(jobId, QStringList());
155         
156         endInsertRows();
157 }
158
159 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
160 {
161         int row = m_jobList.indexOf(jobId);
162
163         if(row < 0)
164         {
165                 if(m_jobListHidden.indexOf(jobId) >= 0)
166                 {
167                         if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
168                         if(newState >= 0) m_jobState.insert(jobId, newState);
169                 }
170                 return;
171         }
172
173         if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
174         if(newState >= 0) m_jobState.insert(jobId, newState);
175         
176         emit dataChanged(index(row, 0), index(row, 1));
177 }
178
179 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
180 {
181         if(m_jobList.contains(jobId))
182         {
183                 m_jobLogFile[jobId].append(line.split('\n'));
184         }
185 }
186
187 const QStringList &ProgressModel::getLogFile(const QModelIndex &index)
188 {
189         if(index.row() < m_jobList.count())
190         {
191                 QUuid id = m_jobList.at(index.row());
192                 return m_jobLogFile[id];
193         }
194
195         return *(reinterpret_cast<QStringList*>(NULL));
196 }
197
198 const QUuid &ProgressModel::getJobId(const QModelIndex &index)
199 {
200         if(index.row() < m_jobList.count())
201         {
202                 return m_jobList.at(index.row());
203         }
204
205         return *(reinterpret_cast<QUuid*>(NULL));
206 }
207
208 void ProgressModel::addSystemMessage(const QString &text, bool isWarning)
209 {
210         const QUuid &jobId = QUuid::createUuid();
211
212         if(m_jobList.contains(jobId))
213         {
214                 return;
215         }
216
217         while(m_jobList.count() >= MAX_DISPLAY_ITEMS)
218         {
219                 beginRemoveRows(QModelIndex(), 0, 0);
220                 m_jobListHidden.append(m_jobList.takeFirst());
221                 endRemoveRows();
222         }
223
224         int newIndex = m_jobList.count();
225         beginInsertRows(QModelIndex(), newIndex, newIndex);
226
227         m_jobList.append(jobId);
228         m_jobName.insert(jobId, text);
229         m_jobStatus.insert(jobId, QString());
230         m_jobState.insert(jobId, isWarning ? JobWarning : JobSystem);
231         m_jobLogFile.insert(jobId, QStringList());
232         
233         endInsertRows();
234 }
235
236 void ProgressModel::restoreHiddenItems(void)
237 {
238         if(!m_jobListHidden.isEmpty())
239         {
240                 beginResetModel();
241                 while(!m_jobListHidden.isEmpty())
242                 {
243                         m_jobList.prepend(m_jobListHidden.takeLast());
244                 }
245                 endResetModel();
246         }
247 }