OSDN Git Service

Implemented logging and added a log file view.
[lamexp/LameXP.git] / src / Model_Progress.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2010 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         m_iconRunning(":/icons/media_play.png"),
28         m_iconPaused(":/icons/control_pause_blue.png"),
29         m_iconComplete(":/icons/tick.png"),
30         m_iconFailed(":/icons/exclamation.png")
31 {
32 }
33
34 ProgressModel::~ProgressModel(void)
35 {
36 }
37
38 int ProgressModel::columnCount(const QModelIndex &parent) const
39 {
40         return 2;
41 }
42
43 int ProgressModel::rowCount(const QModelIndex &parent) const
44 {
45         return m_jobList.count();
46 }
47
48 QVariant ProgressModel::data(const QModelIndex &index, int role) const
49 {
50         if(index.row() >= 0 && index.row() < m_jobList.count())
51         {
52                 if(role == Qt::DisplayRole)
53                 {
54                         switch(index.column())
55                         {
56                         case 0:
57                                 return m_jobName.value(m_jobList.at(index.row()));
58                                 break;
59                         case 1:
60                                 return m_jobStatus.value(m_jobList.at(index.row()));
61                                 break;
62                         default:
63                                 return QVariant();
64                                 break;
65                         }
66                 }
67                 else if(role == Qt::DecorationRole && index.column() == 0)
68                 {
69                         switch(m_jobState.value(m_jobList.at(index.row())))
70                         {
71                         case JobRunning:
72                                 return m_iconRunning;
73                                 break;
74                         case JobPaused:
75                                 return m_iconPaused;
76                                 break;
77                         case JobComplete:
78                                 return m_iconComplete;
79                                 break;
80                         default:
81                                 return m_iconFailed;
82                                 break;
83                         }
84                 }
85                 else if(role == Qt::TextAlignmentRole)
86                 {
87                         return (index.column() == 1) ? QVariant(Qt::AlignHCenter | Qt::AlignVCenter) : QVariant();
88                 }
89         }
90         
91         return QVariant();
92 }
93
94 QVariant ProgressModel::headerData(int section, Qt::Orientation orientation, int role) const
95 {
96         if(role == Qt::DisplayRole)
97         {
98                 if(orientation == Qt::Horizontal)
99                 {
100                         switch(section)
101                         {
102                         case 0:
103                                 return "Job";
104                                 break;
105                         case 1:
106                                 return "Status";
107                                 break;
108                         default:
109                                 return QVariant();
110                                 break;
111                         }
112                 }
113                 if(orientation == Qt::Vertical)
114                 {
115                         return QString::number(section + 1);
116                 }
117         }
118
119         return QVariant();
120 }
121
122 void ProgressModel::addJob(const QUuid &jobId, const QString &jobName, const QString &jobInitialStatus, int jobInitialState)
123 {
124         if(m_jobList.contains(jobId))
125         {
126                 return;
127         }
128
129         beginResetModel();
130         m_jobList.append(jobId);
131         m_jobName.insert(jobId, jobName);
132         m_jobStatus.insert(jobId, jobInitialStatus);
133         m_jobState.insert(jobId, jobInitialState);
134         m_jobLogFile.insert(jobId, QStringList());
135         endResetModel();
136 }
137
138 void ProgressModel::updateJob(const QUuid &jobId, const QString &newStatus, int newState)
139 {
140         int row = m_jobList.indexOf(jobId);
141
142         if(row < 0)
143         {
144                 return;
145         }
146
147         if(!newStatus.isEmpty()) m_jobStatus.insert(jobId, newStatus);
148         if(newState >= 0) m_jobState.insert(jobId, newState);
149         
150         emit dataChanged(index(row, 0), index(row, 1));
151 }
152
153 void ProgressModel::appendToLog(const QUuid &jobId, const QString &line)
154 {
155         if(m_jobList.contains(jobId))
156         {
157                 m_jobLogFile[jobId].append(line);
158         }
159 }
160
161 const QStringList &ProgressModel::getLogFile(const QModelIndex &index)
162 {
163         if(index.row() < m_jobList.count())
164         {
165                 QUuid id = m_jobList.at(index.row());
166                 return m_jobLogFile[id];
167         }
168
169         return *(reinterpret_cast<QStringList*>(NULL));
170 }