1 ///////////////////////////////////////////////////////////////////////////////
2 // LameXP - Audio Encoder Front-End
3 // Copyright (C) 2004-2011 LoRd_MuldeR <MuldeR2@GMX.de>
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.
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.
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.
19 // http://www.gnu.org/licenses/gpl-2.0.txt
20 ///////////////////////////////////////////////////////////////////////////////
22 #include "Model_Progress.h"
26 #define MAX_DISPLAY_ITEMS 50
28 ProgressModel::ProgressModel(void)
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")
39 ProgressModel::~ProgressModel(void)
43 int ProgressModel::columnCount(const QModelIndex
&parent
) const
48 int ProgressModel::rowCount(const QModelIndex
&parent
) const
50 return m_jobList
.count();
53 QVariant
ProgressModel::data(const QModelIndex
&index
, int role
) const
55 if(index
.row() >= 0 && index
.row() < m_jobList
.count())
57 if(role
== Qt::DisplayRole
)
59 switch(index
.column())
62 return m_jobName
.value(m_jobList
.at(index
.row()));
65 return m_jobStatus
.value(m_jobList
.at(index
.row()));
72 else if(role
== Qt::DecorationRole
&& index
.column() == 0)
74 switch(m_jobState
.value(m_jobList
.at(index
.row())))
83 return m_iconComplete
;
96 else if(role
== Qt::TextAlignmentRole
)
98 return (index
.column() == 1) ? QVariant(Qt::AlignHCenter
| Qt::AlignVCenter
) : QVariant();
105 QVariant
ProgressModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
107 if(role
== Qt::DisplayRole
)
109 if(orientation
== Qt::Horizontal
)
124 if(orientation
== Qt::Vertical
)
126 return QString::number(section
+ 1);
133 void ProgressModel::addJob(const QUuid
&jobId
, const QString
&jobName
, const QString
&jobInitialStatus
, int jobInitialState
)
135 if(m_jobList
.contains(jobId
) || m_jobListHidden
.contains(jobId
))
140 while(m_jobList
.count() >= MAX_DISPLAY_ITEMS
)
142 beginRemoveRows(QModelIndex(), 0, 0);
143 m_jobListHidden
.append(m_jobList
.takeFirst());
147 int newIndex
= m_jobList
.count();
148 beginInsertRows(QModelIndex(), newIndex
, newIndex
);
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());
159 void ProgressModel::updateJob(const QUuid
&jobId
, const QString
&newStatus
, int newState
)
161 int row
= m_jobList
.indexOf(jobId
);
165 if(m_jobListHidden
.indexOf(jobId
) >= 0)
167 if(!newStatus
.isEmpty()) m_jobStatus
.insert(jobId
, newStatus
);
168 if(newState
>= 0) m_jobState
.insert(jobId
, newState
);
173 if(!newStatus
.isEmpty()) m_jobStatus
.insert(jobId
, newStatus
);
174 if(newState
>= 0) m_jobState
.insert(jobId
, newState
);
176 emit
dataChanged(index(row
, 0), index(row
, 1));
179 void ProgressModel::appendToLog(const QUuid
&jobId
, const QString
&line
)
181 if(m_jobList
.contains(jobId
))
183 m_jobLogFile
[jobId
].append(line
.split('\n'));
187 const QStringList
&ProgressModel::getLogFile(const QModelIndex
&index
)
189 if(index
.row() < m_jobList
.count())
191 QUuid id
= m_jobList
.at(index
.row());
192 return m_jobLogFile
[id
];
195 return *(reinterpret_cast<QStringList
*>(NULL
));
198 const QUuid
&ProgressModel::getJobId(const QModelIndex
&index
)
200 if(index
.row() < m_jobList
.count())
202 return m_jobList
.at(index
.row());
205 return *(reinterpret_cast<QUuid
*>(NULL
));
208 void ProgressModel::addSystemMessage(const QString
&text
, bool isWarning
)
210 const QUuid
&jobId
= QUuid::createUuid();
212 if(m_jobList
.contains(jobId
))
217 while(m_jobList
.count() >= MAX_DISPLAY_ITEMS
)
219 beginRemoveRows(QModelIndex(), 0, 0);
220 m_jobListHidden
.append(m_jobList
.takeFirst());
224 int newIndex
= m_jobList
.count();
225 beginInsertRows(QModelIndex(), newIndex
, newIndex
);
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());
236 void ProgressModel::restoreHiddenItems(void)
238 if(!m_jobListHidden
.isEmpty())
241 while(!m_jobListHidden
.isEmpty())
243 m_jobList
.prepend(m_jobListHidden
.takeLast());