]> git.sesse.net Git - kdenlive/blob - src/projectitem.cpp
Fix 1 frame offset in image / color clips, fix changing duration of image/color clips
[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 JobStatusMessage = Qt::UserRole + 7;
36 const int itemHeight = 38;
37
38 ProjectItem::ProjectItem(QTreeWidget * parent, DocClipBase *clip, QSize pixmapSize) :
39         QTreeWidgetItem(parent, PROJECTCLIPTYPE),
40         m_clip(clip),
41         m_clipId(clip->getId()),
42         m_pixmapSet(false)
43 {
44     buildItem(pixmapSize);
45 }
46
47 ProjectItem::ProjectItem(QTreeWidgetItem * parent, DocClipBase *clip, QSize pixmapSize) :
48         QTreeWidgetItem(parent, PROJECTCLIPTYPE),
49         m_clip(clip),
50         m_clipId(clip->getId()),
51         m_pixmapSet(false)
52         
53 {
54     buildItem(pixmapSize);
55 }
56
57 void ProjectItem::buildItem(QSize pixmapSize)
58 {
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();
65     switch(m_clipType) {
66         case AUDIO:
67             setData(0, Qt::DecorationRole, KIcon("audio-x-generic").pixmap(pixmapSize));
68             m_pixmapSet = true;
69             break;
70         case IMAGE:
71         case SLIDESHOW:
72             setData(0, Qt::DecorationRole, KIcon("image-x-generic").pixmap(pixmapSize));
73             break;
74         default:
75             setData(0, Qt::DecorationRole, KIcon("video-x-generic").pixmap(pixmapSize));
76     }
77     if (m_clipType != UNKNOWN) slotSetToolTip();
78     
79     setText(0, name);
80     setText(1, m_clip->description());
81     GenTime duration = m_clip->duration();
82     QString durationText;
83     if (duration != GenTime()) {
84         durationText = Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps());
85     }
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") + " / ");
90         }
91     }
92     if (!durationText.isEmpty()) setData(0, DurationRole, durationText);
93 }
94
95 ProjectItem::~ProjectItem()
96 {
97 }
98
99 bool ProjectItem::hasPixmap() const
100 {
101     return m_pixmapSet;
102 }
103
104 void ProjectItem::setPixmap(const QPixmap p)
105 {
106     m_pixmapSet = true;
107     setData(0, Qt::DecorationRole, p);
108 }
109
110 //static
111 int ProjectItem::itemDefaultHeight()
112 {
113     return itemHeight;
114 }
115
116 int ProjectItem::numReferences() const
117 {
118     if (!m_clip) return 0;
119     return m_clip->numReferences();
120 }
121
122 const QString &ProjectItem::clipId() const
123 {
124     return m_clipId;
125 }
126
127 CLIPTYPE ProjectItem::clipType() const
128 {
129     return m_clipType;
130 }
131
132 int ProjectItem::clipMaxDuration() const
133 {
134     return m_clip->getProperty("duration").toInt();
135 }
136
137 QDomElement ProjectItem::toXml() const
138 {
139     return m_clip->toXML();
140 }
141
142 const KUrl ProjectItem::clipUrl() const
143 {
144     if (m_clipType != COLOR && m_clipType != VIRTUAL && m_clipType != UNKNOWN)
145         return KUrl(m_clip->getProperty("resource"));
146     else return KUrl();
147 }
148
149 void ProjectItem::changeDuration(int frames)
150 {
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()));
155 }
156
157 void ProjectItem::setProperties(QMap <QString, QString> props)
158 {
159     if (m_clip == NULL) return;
160     m_clip->setProperties(props);
161 }
162
163 QString ProjectItem::getClipHash() const
164 {
165     if (m_clip == NULL) return QString();
166     return m_clip->getClipHash();
167 }
168
169 void ProjectItem::setProperty(const QString &key, const QString &value)
170 {
171     if (m_clip == NULL) return;
172     m_clip->setProperty(key, value);
173 }
174
175 void ProjectItem::clearProperty(const QString &key)
176 {
177     if (m_clip == NULL) return;
178     m_clip->clearProperty(key);
179 }
180
181 DocClipBase *ProjectItem::referencedClip()
182 {
183     return m_clip;
184 }
185
186 void ProjectItem::slotSetToolTip()
187 {
188     QString tip;
189     if (m_clip->isPlaceHolder()) tip.append(i18n("Missing") + " | ");
190     QString jobInfo = data(0, JobStatusMessage).toString();
191     if (!jobInfo.isEmpty()) {
192         tip.append(jobInfo + " | ");
193     }
194     if (hasProxy() && data(0, JobTypeRole).toInt() != PROXYJOB) {
195         tip.append(i18n("Proxy clip") + " | ");
196     }
197     tip.append("<b>");
198     switch (m_clipType) {
199     case AUDIO:
200         tip.append(i18n("Audio clip") + "</b><br />" + clipUrl().path());
201         break;
202     case VIDEO:
203         tip.append(i18n("Mute video clip") + "</b><br />" + clipUrl().path());
204         break;
205     case AV:
206         tip.append(i18n("Video clip") + "</b><br />" + clipUrl().path());
207         break;
208     case COLOR:
209         tip.append(i18n("Color clip"));
210         break;
211     case IMAGE:
212         tip.append(i18n("Image clip") + "</b><br />" + clipUrl().path());
213         break;
214     case TEXT:
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());
217         break;
218     case SLIDESHOW:
219         tip.append(i18n("Slideshow clip") + "</b><br />" + clipUrl().directory());
220         break;
221     case VIRTUAL:
222         tip.append(i18n("Virtual clip"));
223         break;
224     case PLAYLIST:
225         tip.append(i18n("Playlist clip") + "</b><br />" + clipUrl().path());
226         break;
227     default:
228         tip.append(i18n("Unknown clip"));
229         break;
230     }
231     setToolTip(0, tip);
232 }
233
234
235 void ProjectItem::setProperties(const QMap < QString, QString > &attributes, const QMap < QString, QString > &metadata)
236 {
237     if (m_clip == NULL) return;
238
239     QString prefix;
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;
247
248         m_clip->setClipType(m_clipType);
249         slotSetToolTip();
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") + " / ";
254             }
255         }
256     }
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);
265     } else  {
266         // No duration known, use an arbitrary one until it is.
267     }
268
269     m_clip->setProperties(attributes);
270     m_clip->setMetadata(metadata);
271
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());
279         }
280     }
281 }
282
283 void ProjectItem::setJobStatus(JOBTYPE jobType, CLIPJOBSTATUS status, int progress, const QString &statusMessage)
284 {
285     setData(0, JobTypeRole, jobType);
286     if (progress > 0) setData(0, JobProgressRole, qMin(100, progress));
287     else {
288         setData(0, JobProgressRole, status);
289         if ((status == JOBABORTED || status == JOBCRASHED  || status == JOBDONE) || !statusMessage.isEmpty())
290             setData(0, JobStatusMessage, statusMessage);
291         slotSetToolTip();
292     }
293 }
294
295 void ProjectItem::setConditionalJobStatus(CLIPJOBSTATUS status, JOBTYPE requestedJobType)
296 {
297     if (data(0, JobTypeRole).toInt() == requestedJobType) {
298         setData(0, JobProgressRole, status);
299     }
300 }
301
302 bool ProjectItem::hasProxy() const
303 {
304     if (m_clip == NULL) return false;
305     if (m_clip->getProperty("proxy").size() < 2 || data(0, JobProgressRole).toInt() == JOBCRASHED) return false;
306     return true;
307 }
308
309 bool ProjectItem::isProxyReady() const
310 {
311      return (data(0, JobProgressRole).toInt() == JOBDONE);
312 }
313
314 bool ProjectItem::isJobRunning() const
315 {
316     int s = data(0, JobProgressRole).toInt();
317     if (s == JOBWAITING || s == JOBWORKING || s > 0) return true;
318     return false;
319 }
320
321 bool ProjectItem::isProxyRunning() const
322 {
323     int s = data(0, JobProgressRole).toInt();
324     if ((s == JOBWORKING || s > 0) && data(0, JobTypeRole).toInt() == (int) PROXYJOB) return true;
325     return false;
326 }
327
328 bool ProjectItem::playlistHasProxies(const QString path)
329 {
330     kDebug()<<"// CHECKING FOR PROXIES";
331     QFile file(path);
332     QDomDocument doc;
333     if (!file.open(QIODevice::ReadOnly))
334     return false;
335     if (!doc.setContent(&file)) {
336         file.close();
337         return false;
338     }
339     file.close();
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;
345     }
346     return false;
347 }
348
349
350