]> git.sesse.net Git - kdenlive/blob - src/projectitem.cpp
Continue progress on clip jobs: add extract zone action (cuts a clip through ffmpeg...
[kdenlive] / src / projectitem.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
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.                                   *
8  *                                                                         *
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.                          *
13  *                                                                         *
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  ***************************************************************************/
19
20
21 #include "projectitem.h"
22 #include "timecode.h"
23 #include "kdenlivesettings.h"
24 #include "docclipbase.h"
25
26 #include <KDebug>
27 #include <KLocale>
28 #include <KIcon>
29
30 #include <QFile>
31
32 const int DurationRole = Qt::UserRole + 1;
33 const int JobProgressRole = Qt::UserRole + 5;
34 const int JobTypeRole = Qt::UserRole + 6;
35 const int JobCrasMessage = Qt::UserRole + 7;
36 const int itemHeight = 38;
37
38 ProjectItem::ProjectItem(QTreeWidget * parent, DocClipBase *clip) :
39         QTreeWidgetItem(parent, PROJECTCLIPTYPE),
40         m_clip(clip),
41         m_clipId(clip->getId())
42 {
43     buildItem();
44 }
45
46 ProjectItem::ProjectItem(QTreeWidgetItem * parent, DocClipBase *clip) :
47         QTreeWidgetItem(parent, PROJECTCLIPTYPE),
48         m_clip(clip),
49         m_clipId(clip->getId())
50         
51 {
52     buildItem();
53 }
54
55 void ProjectItem::buildItem()
56 {
57     setSizeHint(0, QSize(itemHeight * 3, itemHeight));
58     if (m_clip->isPlaceHolder()) setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDropEnabled);
59     else setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsDropEnabled);
60     QString name = m_clip->getProperty("name");
61     if (name.isEmpty()) name = KUrl(m_clip->getProperty("resource")).fileName();
62     m_clipType = (CLIPTYPE) m_clip->getProperty("type").toInt();
63     setText(0, name);
64     setText(1, m_clip->description());
65     GenTime duration = m_clip->duration();
66     QString durationText;
67     if (duration != GenTime()) {
68         durationText = Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps());
69     }
70     if (m_clipType == PLAYLIST) {
71         // Check if the playlist xml contains a proxy inside, and inform user
72         if (playlistHasProxies(m_clip->fileURL().path())) {
73             durationText.prepend(i18n("Contains proxies") + " / ");
74         }
75     }
76     if (!durationText.isEmpty()) setData(0, DurationRole, durationText);
77 }
78
79 ProjectItem::~ProjectItem()
80 {
81 }
82
83 //static
84 int ProjectItem::itemDefaultHeight()
85 {
86     return itemHeight;
87 }
88
89 int ProjectItem::numReferences() const
90 {
91     if (!m_clip) return 0;
92     return m_clip->numReferences();
93 }
94
95 const QString &ProjectItem::clipId() const
96 {
97     return m_clipId;
98 }
99
100 CLIPTYPE ProjectItem::clipType() const
101 {
102     return m_clipType;
103 }
104
105 int ProjectItem::clipMaxDuration() const
106 {
107     return m_clip->getProperty("duration").toInt();
108 }
109
110 QStringList ProjectItem::names() const
111 {
112     QStringList result;
113     result.append(text(0));
114     result.append(text(1));
115     result.append(text(2));
116     return result;
117 }
118
119 QDomElement ProjectItem::toXml() const
120 {
121     return m_clip->toXML();
122 }
123
124 const KUrl ProjectItem::clipUrl() const
125 {
126     if (m_clipType != COLOR && m_clipType != VIRTUAL && m_clipType != UNKNOWN)
127         return KUrl(m_clip->getProperty("resource"));
128     else return KUrl();
129 }
130
131 void ProjectItem::changeDuration(int frames)
132 {
133     QString itemdata = data(0, DurationRole).toString();
134     if (itemdata.contains('/')) itemdata = itemdata.section('/', 0, 0) + "/ ";
135     else itemdata.clear();
136     setData(0, DurationRole, itemdata + Timecode::getEasyTimecode(GenTime(frames, KdenliveSettings::project_fps()), KdenliveSettings::project_fps()));
137 }
138
139 void ProjectItem::setProperties(QMap <QString, QString> props)
140 {
141     if (m_clip == NULL) return;
142     m_clip->setProperties(props);
143 }
144
145 QString ProjectItem::getClipHash() const
146 {
147     if (m_clip == NULL) return QString();
148     return m_clip->getClipHash();
149 }
150
151 void ProjectItem::setProperty(const QString &key, const QString &value)
152 {
153     if (m_clip == NULL) return;
154     m_clip->setProperty(key, value);
155 }
156
157 void ProjectItem::clearProperty(const QString &key)
158 {
159     if (m_clip == NULL) return;
160     m_clip->clearProperty(key);
161 }
162
163 DocClipBase *ProjectItem::referencedClip()
164 {
165     return m_clip;
166 }
167
168 void ProjectItem::slotSetToolTip()
169 {
170     QString tip;
171     if (m_clip->isPlaceHolder()) tip.append(i18n("Missing") + " | ");
172     int s = data(0, JobProgressRole).toInt();
173     if (s == CREATINGJOB || s > 0) {
174         tip.append(i18n("Building proxy clip") + " | ");
175     }
176     else if (s == JOBWAITING) {
177         tip.append(i18n("Waiting - proxy clip") + " | ");
178     }
179     else if (hasProxy()) {
180         tip.append(i18n("Proxy clip") + " | ");
181     }
182     tip.append("<b>");
183     switch (m_clipType) {
184     case AUDIO:
185         tip.append(i18n("Audio clip") + "</b><br />" + clipUrl().path());
186         break;
187     case VIDEO:
188         tip.append(i18n("Mute video clip") + "</b><br />" + clipUrl().path());
189         break;
190     case AV:
191         tip.append(i18n("Video clip") + "</b><br />" + clipUrl().path());
192         break;
193     case COLOR:
194         tip.append(i18n("Color clip"));
195         break;
196     case IMAGE:
197         tip.append(i18n("Image clip") + "</b><br />" + clipUrl().path());
198         break;
199     case TEXT:
200         if (!clipUrl().isEmpty() && m_clip->getProperty("xmldata").isEmpty()) tip.append(i18n("Template text clip") + "</b><br />" + clipUrl().path());
201         else tip.append(i18n("Text clip") + "</b><br />" + clipUrl().path());
202         break;
203     case SLIDESHOW:
204         tip.append(i18n("Slideshow clip") + "</b><br />" + clipUrl().directory());
205         break;
206     case VIRTUAL:
207         tip.append(i18n("Virtual clip"));
208         break;
209     case PLAYLIST:
210         tip.append(i18n("Playlist clip") + "</b><br />" + clipUrl().path());
211         break;
212     default:
213         tip.append(i18n("Unknown clip"));
214         break;
215     }
216     setToolTip(0, tip);
217 }
218
219
220 void ProjectItem::setProperties(const QMap < QString, QString > &attributes, const QMap < QString, QString > &metadata)
221 {
222     if (m_clip == NULL) return;
223
224     QString prefix;
225     if (m_clipType == UNKNOWN) {
226         QString cliptype = attributes.value("type");
227         if (cliptype == "audio") m_clipType = AUDIO;
228         else if (cliptype == "video") m_clipType = VIDEO;
229         else if (cliptype == "av") m_clipType = AV;
230         else if (cliptype == "playlist") m_clipType = PLAYLIST;
231         else m_clipType = AV;
232
233         m_clip->setClipType(m_clipType);
234         slotSetToolTip();
235         if (m_clipType == PLAYLIST) {
236             // Check if the playlist xml contains a proxy inside, and inform user
237             if (playlistHasProxies(m_clip->fileURL().path())) {
238                 prefix = i18n("Contains proxies") + " / ";
239             }
240         }
241     }
242     if (attributes.contains("duration")) {
243         GenTime duration = GenTime(attributes.value("duration").toInt(), KdenliveSettings::project_fps());
244         QString itemdata = data(0, DurationRole).toString();
245         if (itemdata.contains('/')) itemdata = itemdata.section('/', 0, 0) + "/ ";
246         else itemdata.clear();
247         if (prefix.isEmpty()) prefix = itemdata;
248         setData(0, DurationRole, prefix + Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps()));
249         m_clip->setDuration(duration);
250     } else  {
251         // No duration known, use an arbitrary one until it is.
252     }
253
254     m_clip->setProperties(attributes);
255     m_clip->setMetadata(metadata);
256
257     if (m_clip->description().isEmpty()) {
258         if (metadata.contains("description")) {
259             m_clip->setProperty("description", metadata.value("description"));
260             setText(1, m_clip->description());
261         } else if (metadata.contains("comment")) {
262             m_clip->setProperty("description", metadata.value("comment"));
263             setText(1, m_clip->description());
264         }
265     }
266 }
267
268 void ProjectItem::setJobStatus(CLIPJOBSTATUS status, int progress, JOBTYPE jobType)
269 {
270     if (status == JOBCRASHED) {
271         if (jobType == PROXYJOB) setData(0, JobCrasMessage, i18n("Proxy crashed"));
272         else if (jobType == CUTJOB) setData(0, JobCrasMessage, i18n("Transcoding crashed"));
273     }
274     setData(0, JobTypeRole, jobType);
275     if (progress > 0) setData(0, JobProgressRole, progress);
276     else {
277         setData(0, JobProgressRole, status);
278         slotSetToolTip();
279     }
280 }
281
282 bool ProjectItem::hasProxy() const
283 {
284     if (m_clip == NULL) return false;
285     if (m_clip->getProperty("proxy").isEmpty() || m_clip->getProperty("proxy") == "-" || data(0, JobProgressRole).toInt() == JOBCRASHED) return false;
286     return true;
287 }
288
289 bool ProjectItem::isProxyReady() const
290 {
291      return (data(0, JobProgressRole).toInt() == JOBDONE);
292 }
293
294 bool ProjectItem::isJobRunning() const
295 {
296     int s = data(0, JobProgressRole).toInt();
297     if (s == JOBWAITING || s == CREATINGJOB || s > 0) return true;
298     return false;
299 }
300
301 bool ProjectItem::isProxyRunning() const
302 {
303     int s = data(0, JobProgressRole).toInt();
304     if ((s == CREATINGJOB || s > 0) && data(0, JobTypeRole).toInt() == (int) PROXYJOB) return true;
305     return false;
306 }
307
308 bool ProjectItem::playlistHasProxies(const QString path)
309 {
310     kDebug()<<"// CHECKING FOR PROXIES";
311     QFile file(path);
312     QDomDocument doc;
313     if (!file.open(QIODevice::ReadOnly))
314     return false;
315     if (!doc.setContent(&file)) {
316         file.close();
317         return false;
318     }
319     file.close();
320     QString root = doc.documentElement().attribute("root");
321     QDomNodeList kdenliveProducers = doc.elementsByTagName("kdenlive_producer");
322     for (int i = 0; i < kdenliveProducers.count(); i++) {
323         QString proxy = kdenliveProducers.at(i).toElement().attribute("proxy");
324         if (!proxy.isEmpty() && proxy != "-") return true;
325     }
326     return false;
327 }
328
329
330