OSDN Git Service

Implemented a new "disk observer" thread which will constantly check the free diskspa...
[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 ProgressModel::ProgressModel(void)
27 :
28         m_iconRunning(":/icons/media_play.png"),
29         m_iconPaused(":/icons/control_pause_blue.png"),
30         m_iconComplete(":/icons/tick.png"),
31         m_iconFailed(":/icons/exclamation.png"),
32         m_iconSystem(":/icons/computer.png"),
33         m_iconWarning(":/icons/error.png")
34 {
35 }
36
37 ProgressModel::~ProgressModel(void)
38 {
39 }
40
41 int ProgressModel::columnCount(const QModelIndex &parent) const
42 {
43         return 2;
44 }
45
46 int ProgressModel::rowCount(const QModelIndex &parent) const
47 {
48         return m_jobList.count();
49 }
50
51 QVariant ProgressModel::data(const QModelIndex &index, int role) const
52 {
53         if(index.row() >= 0 && index.row() < m_jobList.count())
54         {
55                 if(role == Qt::DisplayRole)
56                 {
57                         switch(index.column())
58                         {
59                         case 0:
60                                 return m_jobName.value(m_jobList.at(index.row()));
61                                 break;
62                         case 1:
63                                 return m_jobStatus.value(m_jobList.at(index.row()));
64                                 break;
65                         default:
66                                 return QVariant();
67                                 break;
68                         }
69                 }
70                 else if(role == Qt::DecorationRole && index.column() == 0)
71                 {
72                         switch(m_jobState.value(m_jobList.at(index.row())))
73                         {
74                         case JobRunning:
75                                 return m_iconRunning;
76                                 break;
77                         case JobPaused:
78                                 return m_iconPaused;
79                                 break;
80                         case JobComplete:
81                                 return m_iconComplete;
82                                 break;
83                         case JobSystem:
84                                 return m_iconSystem;
85                                 break;
86                         case JobWarning:
87                                 return m_iconWarning;
88                                 break;
89                         default:
90                                 return m_iconFailed;
91                                 break;
92                         }
93                 }
94                 else if(role == Qt::TextAlignmentRole)
95                 {
96                         return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
97                 }
98         }
99         
100         return QVariant();
101 }
102
103 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
104 {
105         if(role == Qt::DisplayRole)
106         {
107                 if(orientation == Qt::Horizontal)
108                 {
109                         switch(section)
110                         {
111                         case 0:
112                                 return tr("Job");
113                                 break;
114                         case 1:
115                                 return tr("Status");
116                                 break;
117                         default:
118                                 return QVariant();
119                                 break;
120                         }
121                 }
122                 if(orientation == Qt::Vertical)
123                 {
124                         return QString::number(section + 1);
125                 }
126         }
127
128         return QVariant();
129 }
130
131 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
132 {
133         if(m_jobList.contains(jobId))
134         {
135                 return;
136         }
137
138         int newIndex = m_jobList.count();
139         beginInsertRows(QModelIndex(), newIndex, newIndex);
140
141         m_jobList.append(jobId);
142         m_jobName.insert(jobId, jobName);
143         m_jobStatus.insert(jobId, jobInitialStatus);
144         m_jobState.insert(jobId, jobInitialState);
145         m_jobLogFile.insert(jobId, QStringList());
146         
147         endInsertRows();
148 }
149
150 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
151 {
152         int row = m_jobList.indexOf(jobId);
153
154         if(row < 0)
155         {
156                 return;
157         }
158
159         if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
160         if(newState >= 0) m_jobState.insert(jobId, newState);
161         
162         emit dataChanged(index(row, 0), index(row, 1));
163 }
164
165 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
166 {
167         if(m_jobList.contains(jobId))
168         {
169                 m_jobLogFile[jobId].append(line.split('\n'));
170         }
171 }
172
173 const QStringList &ProgressModel::getLogFile(const QModelIndex &index)
174 {
175         if(index.row() < m_jobList.count())
176         {
177                 QUuid id = m_jobList.at(index.row());
178                 return m_jobLogFile[id];
179         }
180
181         return *(reinterpret_cast<QStringList*>(NULL));
182 }
183
184 const QUuid &ProgressModel::getJobId(const QModelIndex &index)
185 {
186         if(index.row() < m_jobList.count())
187         {
188                 return m_jobList.at(index.row());
189         }
190
191         return *(reinterpret_cast<QUuid*>(NULL));
192 }
193
194 void ProgressModel::addSystemMessage(const QString &text, bool isWarning)
195 {
196         const QUuid &jobId = QUuid::createUuid();
197
198         if(m_jobList.contains(jobId))
199         {
200                 return;
201         }
202
203         int newIndex = m_jobList.count();
204         beginInsertRows(QModelIndex(), newIndex, newIndex);
205
206         m_jobList.append(jobId);
207         m_jobName.insert(jobId, text);
208         m_jobStatus.insert(jobId, QString());
209         m_jobState.insert(jobId, isWarning ? JobWarning : JobSystem);
210         m_jobLogFile.insert(jobId, QStringList());
211         
212         endInsertRows();
213 }