1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
21 #include "projectitem.h"
23 #include "kdenlivesettings.h"
24 #include "docclipbase.h"
32 const int DurationRole = Qt::UserRole + 1;
33 const int JobProgressRole = Qt::UserRole + 5;
34 const int JobTypeRole = Qt::UserRole + 6;
35 const int JobStatusMessage = Qt::UserRole + 7;
36 const int itemHeight = 38;
38 ProjectItem::ProjectItem(QTreeWidget * parent, DocClipBase *clip, QSize pixmapSize) :
39 QTreeWidgetItem(parent, PROJECTCLIPTYPE),
41 m_clipId(clip->getId()),
44 buildItem(pixmapSize);
47 ProjectItem::ProjectItem(QTreeWidgetItem * parent, DocClipBase *clip, QSize pixmapSize) :
48 QTreeWidgetItem(parent, PROJECTCLIPTYPE),
50 m_clipId(clip->getId()),
54 buildItem(pixmapSize);
57 void ProjectItem::buildItem(QSize pixmapSize)
59 setSizeHint(0, QSize(itemHeight * 3, itemHeight));
60 if (m_clip->isPlaceHolder()) setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
61 else setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
62 QString name = m_clip->getProperty("name");
63 if (name.isEmpty()) name = KUrl(m_clip->getProperty("resource")).fileName();
64 m_clipType = (CLIPTYPE) m_clip->getProperty("type").toInt();
67 setData(0, Qt::DecorationRole, KIcon("audio-x-generic").pixmap(pixmapSize));
72 setData(0, Qt::DecorationRole, KIcon("image-x-generic").pixmap(pixmapSize));
75 setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(pixmapSize));
77 if (m_clipType != UNKNOWN) slotSetToolTip();
80 setText(1, m_clip->description());
81 GenTime duration = m_clip->duration();
83 if (duration != GenTime()) {
84 durationText = Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps());
86 if (m_clipType == PLAYLIST) {
87 // Check if the playlist xml contains a proxy inside, and inform user
88 if (playlistHasProxies(m_clip->fileURL().path())) {
89 durationText.prepend(i18n("Contains proxies") + " / ");
92 if (!durationText.isEmpty()) setData(0, DurationRole, durationText);
95 ProjectItem::~ProjectItem()
99 bool ProjectItem::hasPixmap() const
104 void ProjectItem::setPixmap(const QPixmap p)
107 setData(0, Qt::DecorationRole, p);
111 int ProjectItem::itemDefaultHeight()
116 int ProjectItem::numReferences() const
118 if (!m_clip) return 0;
119 return m_clip->numReferences();
122 const QString &ProjectItem::clipId() const
127 CLIPTYPE ProjectItem::clipType() const
132 int ProjectItem::clipMaxDuration() const
134 return m_clip->getProperty("duration").toInt();
137 QDomElement ProjectItem::toXml() const
139 return m_clip->toXML();
142 const KUrl ProjectItem::clipUrl() const
144 if (m_clipType != COLOR && m_clipType != VIRTUAL && m_clipType != UNKNOWN)
145 return KUrl(m_clip->getProperty("resource"));
149 void ProjectItem::changeDuration(int frames)
151 QString itemdata = data(0, DurationRole).toString();
152 if (itemdata.contains('/')) itemdata = itemdata.section('/', 0, 0) + "/ ";
153 else itemdata.clear();
154 setData(0, DurationRole, itemdata + Timecode::getEasyTimecode(GenTime(frames, KdenliveSettings::project_fps()), KdenliveSettings::project_fps()));
157 void ProjectItem::setProperties(QMap <QString, QString> props)
159 if (m_clip == NULL) return;
160 m_clip->setProperties(props);
163 QString ProjectItem::getClipHash() const
165 if (m_clip == NULL) return QString();
166 return m_clip->getClipHash();
169 void ProjectItem::setProperty(const QString &key, const QString &value)
171 if (m_clip == NULL) return;
172 m_clip->setProperty(key, value);
175 void ProjectItem::clearProperty(const QString &key)
177 if (m_clip == NULL) return;
178 m_clip->clearProperty(key);
181 DocClipBase *ProjectItem::referencedClip()
186 void ProjectItem::slotSetToolTip()
189 if (m_clip->isPlaceHolder()) tip.append(i18n("Missing") + " | ");
190 QString jobInfo = data(0, JobStatusMessage).toString();
191 if (!jobInfo.isEmpty()) {
192 tip.append(jobInfo + " | ");
194 if (hasProxy() && data(0, JobTypeRole).toInt() != PROXYJOB) {
195 tip.append(i18n("Proxy clip") + " | ");
198 switch (m_clipType) {
200 tip.append(i18n("Audio clip") + "</b><br />" + clipUrl().path());
203 tip.append(i18n("Mute video clip") + "</b><br />" + clipUrl().path());
206 tip.append(i18n("Video clip") + "</b><br />" + clipUrl().path());
209 tip.append(i18n("Color clip"));
212 tip.append(i18n("Image clip") + "</b><br />" + clipUrl().path());
215 if (!clipUrl().isEmpty() && m_clip->getProperty("xmldata").isEmpty()) tip.append(i18n("Template text clip") + "</b><br />" + clipUrl().path());
216 else tip.append(i18n("Text clip") + "</b><br />" + clipUrl().path());
219 tip.append(i18n("Slideshow clip") + "</b><br />" + clipUrl().directory());
222 tip.append(i18n("Virtual clip"));
225 tip.append(i18n("Playlist clip") + "</b><br />" + clipUrl().path());
228 tip.append(i18n("Unknown clip"));
235 void ProjectItem::setProperties(const QMap < QString, QString > &attributes, const QMap < QString, QString > &metadata)
237 if (m_clip == NULL) return;
240 if (m_clipType == UNKNOWN) {
241 QString cliptype = attributes.value("type");
242 if (cliptype == "audio") m_clipType = AUDIO;
243 else if (cliptype == "video") m_clipType = VIDEO;
244 else if (cliptype == "av") m_clipType = AV;
245 else if (cliptype == "playlist") m_clipType = PLAYLIST;
246 else m_clipType = AV;
248 m_clip->setClipType(m_clipType);
250 if (m_clipType == PLAYLIST) {
251 // Check if the playlist xml contains a proxy inside, and inform user
252 if (playlistHasProxies(m_clip->fileURL().path())) {
253 prefix = i18n("Contains proxies") + " / ";
257 if (attributes.contains("duration")) {
258 GenTime duration = GenTime(attributes.value("duration").toInt(), KdenliveSettings::project_fps());
259 QString itemdata = data(0, DurationRole).toString();
260 if (itemdata.contains('/')) itemdata = itemdata.section('/', 0, 0) + "/ ";
261 else itemdata.clear();
262 if (prefix.isEmpty()) prefix = itemdata;
263 setData(0, DurationRole, prefix + Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps()));
264 m_clip->setDuration(duration);
266 // No duration known, use an arbitrary one until it is.
269 m_clip->setProperties(attributes);
270 m_clip->setMetadata(metadata);
272 if (m_clip->description().isEmpty()) {
273 if (metadata.contains("description")) {
274 m_clip->setProperty("description", metadata.value("description"));
275 setText(1, m_clip->description());
276 } else if (metadata.contains("comment")) {
277 m_clip->setProperty("description", metadata.value("comment"));
278 setText(1, m_clip->description());
283 void ProjectItem::setJobStatus(JOBTYPE jobType, CLIPJOBSTATUS status, int progress, const QString &statusMessage)
285 setData(0, JobTypeRole, jobType);
286 if (progress > 0) setData(0, JobProgressRole, qMin(100, progress));
288 setData(0, JobProgressRole, status);
289 if ((status == JOBABORTED || status == JOBCRASHED || status == JOBDONE) || !statusMessage.isEmpty())
290 setData(0, JobStatusMessage, statusMessage);
295 void ProjectItem::setConditionalJobStatus(CLIPJOBSTATUS status, JOBTYPE requestedJobType)
297 if (data(0, JobTypeRole).toInt() == requestedJobType) {
298 setData(0, JobProgressRole, status);
302 bool ProjectItem::hasProxy() const
304 if (m_clip == NULL) return false;
305 if (m_clip->getProperty("proxy").size() < 2 || data(0, JobProgressRole).toInt() == JOBCRASHED) return false;
309 bool ProjectItem::isProxyReady() const
311 return (data(0, JobProgressRole).toInt() == JOBDONE);
314 bool ProjectItem::isJobRunning() const
316 int s = data(0, JobProgressRole).toInt();
317 if (s == JOBWAITING || s == JOBWORKING || s > 0) return true;
321 bool ProjectItem::isProxyRunning() const
323 int s = data(0, JobProgressRole).toInt();
324 if ((s == JOBWORKING || s > 0) && data(0, JobTypeRole).toInt() == (int) PROXYJOB) return true;
328 bool ProjectItem::playlistHasProxies(const QString path)
330 kDebug()<<"// CHECKING FOR PROXIES";
333 if (!file.open(QIODevice::ReadOnly))
335 if (!doc.setContent(&file)) {
340 QString root = doc.documentElement().attribute("root");
341 QDomNodeList kdenliveProducers = doc.elementsByTagName("kdenlive_producer");
342 for (int i = 0; i < kdenliveProducers.count(); i++) {
343 QString proxy = kdenliveProducers.at(i).toElement().attribute("proxy");
344 if (!proxy.isEmpty() && proxy != "-") return true;