]> git.sesse.net Git - kdenlive/blob - src/projectitem.cpp
Merge branch 'feature/pkey' of git://anongit.kde.org/kdenlive into pkey
[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     
78     setText(0, name);
79     setText(1, m_clip->description());
80     GenTime duration = m_clip->duration();
81     QString durationText;
82     if (duration != GenTime()) {
83         durationText = Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps());
84     }
85     if (m_clipType == PLAYLIST) {
86         // Check if the playlist xml contains a proxy inside, and inform user
87         if (playlistHasProxies(m_clip->fileURL().path())) {
88             durationText.prepend(i18n("Contains proxies") + " / ");
89         }
90     }
91     if (!durationText.isEmpty()) setData(0, DurationRole, durationText);
92 }
93
94 ProjectItem::~ProjectItem()
95 {
96 }
97
98 bool ProjectItem::hasPixmap() const
99 {
100     return m_pixmapSet;
101 }
102
103 void ProjectItem::setPixmap(const QPixmap p)
104 {
105     m_pixmapSet = true;
106     setData(0, Qt::DecorationRole, p);
107 }
108
109 //static
110 int ProjectItem::itemDefaultHeight()
111 {
112     return itemHeight;
113 }
114
115 int ProjectItem::numReferences() const
116 {
117     if (!m_clip) return 0;
118     return m_clip->numReferences();
119 }
120
121 const QString &ProjectItem::clipId() const
122 {
123     return m_clipId;
124 }
125
126 CLIPTYPE ProjectItem::clipType() const
127 {
128     return m_clipType;
129 }
130
131 int ProjectItem::clipMaxDuration() const
132 {
133     return m_clip->getProperty("duration").toInt();
134 }
135
136 QDomElement ProjectItem::toXml() const
137 {
138     return m_clip->toXML();
139 }
140
141 const KUrl ProjectItem::clipUrl() const
142 {
143     if (m_clipType != COLOR && m_clipType != VIRTUAL && m_clipType != UNKNOWN)
144         return KUrl(m_clip->getProperty("resource"));
145     else return KUrl();
146 }
147
148 void ProjectItem::changeDuration(int frames)
149 {
150     QString itemdata = data(0, DurationRole).toString();
151     if (itemdata.contains('/')) itemdata = itemdata.section('/', 0, 0) + "/ ";
152     else itemdata.clear();
153     setData(0, DurationRole, itemdata + Timecode::getEasyTimecode(GenTime(frames, KdenliveSettings::project_fps()), KdenliveSettings::project_fps()));
154 }
155
156 void ProjectItem::setProperties(QMap <QString, QString> props)
157 {
158     if (m_clip == NULL) return;
159     m_clip->setProperties(props);
160 }
161
162 QString ProjectItem::getClipHash() const
163 {
164     if (m_clip == NULL) return QString();
165     return m_clip->getClipHash();
166 }
167
168 void ProjectItem::setProperty(const QString &key, const QString &value)
169 {
170     if (m_clip == NULL) return;
171     m_clip->setProperty(key, value);
172 }
173
174 void ProjectItem::clearProperty(const QString &key)
175 {
176     if (m_clip == NULL) return;
177     m_clip->clearProperty(key);
178 }
179
180 DocClipBase *ProjectItem::referencedClip()
181 {
182     return m_clip;
183 }
184
185 void ProjectItem::slotSetToolTip()
186 {
187     QString tip;
188     if (m_clip->isPlaceHolder()) tip.append(i18n("Missing") + " | ");
189     QString jobInfo = data(0, JobStatusMessage).toString();
190     if (!jobInfo.isEmpty()) {
191         tip.append(jobInfo + " | ");
192     }
193     if (hasProxy() && data(0, JobTypeRole).toInt() != PROXYJOB) {
194         tip.append(i18n("Proxy clip") + " | ");
195     }
196     tip.append("<b>");
197     switch (m_clipType) {
198     case AUDIO:
199         tip.append(i18n("Audio clip") + "</b><br />" + clipUrl().path());
200         break;
201     case VIDEO:
202         tip.append(i18n("Mute video clip") + "</b><br />" + clipUrl().path());
203         break;
204     case AV:
205         tip.append(i18n("Video clip") + "</b><br />" + clipUrl().path());
206         break;
207     case COLOR:
208         tip.append(i18n("Color clip"));
209         break;
210     case IMAGE:
211         tip.append(i18n("Image clip") + "</b><br />" + clipUrl().path());
212         break;
213     case TEXT:
214         if (!clipUrl().isEmpty() && m_clip->getProperty("xmldata").isEmpty()) tip.append(i18n("Template text clip") + "</b><br />" + clipUrl().path());
215         else tip.append(i18n("Text clip") + "</b><br />" + clipUrl().path());
216         break;
217     case SLIDESHOW:
218         tip.append(i18n("Slideshow clip") + "</b><br />" + clipUrl().directory());
219         break;
220     case VIRTUAL:
221         tip.append(i18n("Virtual clip"));
222         break;
223     case PLAYLIST:
224         tip.append(i18n("Playlist clip") + "</b><br />" + clipUrl().path());
225         break;
226     default:
227         tip.append(i18n("Unknown clip"));
228         break;
229     }
230     setToolTip(0, tip);
231 }
232
233
234 void ProjectItem::setProperties(const QMap < QString, QString > &attributes, const QMap < QString, QString > &metadata)
235 {
236     if (m_clip == NULL) return;
237
238     QString prefix;
239     if (m_clipType == UNKNOWN) {
240         QString cliptype = attributes.value("type");
241         if (cliptype == "audio") m_clipType = AUDIO;
242         else if (cliptype == "video") m_clipType = VIDEO;
243         else if (cliptype == "av") m_clipType = AV;
244         else if (cliptype == "playlist") m_clipType = PLAYLIST;
245         else m_clipType = AV;
246
247         m_clip->setClipType(m_clipType);
248         slotSetToolTip();
249         if (m_clipType == PLAYLIST) {
250             // Check if the playlist xml contains a proxy inside, and inform user
251             if (playlistHasProxies(m_clip->fileURL().path())) {
252                 prefix = i18n("Contains proxies") + " / ";
253             }
254         }
255     }
256     if (attributes.contains("duration")) {
257         GenTime duration = GenTime(attributes.value("duration").toInt(), KdenliveSettings::project_fps());
258         QString itemdata = data(0, DurationRole).toString();
259         if (itemdata.contains('/')) itemdata = itemdata.section('/', 0, 0) + "/ ";
260         else itemdata.clear();
261         if (prefix.isEmpty()) prefix = itemdata;
262         setData(0, DurationRole, prefix + Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps()));
263         m_clip->setDuration(duration);
264     } else  {
265         // No duration known, use an arbitrary one until it is.
266     }
267
268     m_clip->setProperties(attributes);
269     m_clip->setMetadata(metadata);
270
271     if (m_clip->description().isEmpty()) {
272         if (metadata.contains("description")) {
273             m_clip->setProperty("description", metadata.value("description"));
274             setText(1, m_clip->description());
275         } else if (metadata.contains("comment")) {
276             m_clip->setProperty("description", metadata.value("comment"));
277             setText(1, m_clip->description());
278         }
279     }
280 }
281
282 void ProjectItem::setJobStatus(JOBTYPE jobType, CLIPJOBSTATUS status, int progress, const QString &statusMessage)
283 {
284     setData(0, JobTypeRole, jobType);
285     if (progress > 0) setData(0, JobProgressRole, qMin(100, progress));
286     else {
287         setData(0, JobProgressRole, status);
288         if ((status == JOBABORTED || status == JOBCRASHED  || status == JOBDONE) || !statusMessage.isEmpty())
289             setData(0, JobStatusMessage, statusMessage);
290         slotSetToolTip();
291     }
292 }
293
294 void ProjectItem::setConditionalJobStatus(CLIPJOBSTATUS status, JOBTYPE requestedJobType)
295 {
296     if (data(0, JobTypeRole).toInt() == requestedJobType) {
297         setData(0, JobProgressRole, status);
298     }
299 }
300
301 bool ProjectItem::hasProxy() const
302 {
303     if (m_clip == NULL) return false;
304     if (m_clip->getProperty("proxy").size() < 2 || data(0, JobProgressRole).toInt() == JOBCRASHED) return false;
305     return true;
306 }
307
308 bool ProjectItem::isProxyReady() const
309 {
310      return (data(0, JobProgressRole).toInt() == JOBDONE);
311 }
312
313 bool ProjectItem::isJobRunning() const
314 {
315     int s = data(0, JobProgressRole).toInt();
316     if (s == JOBWAITING || s == JOBWORKING || s > 0) return true;
317     return false;
318 }
319
320 bool ProjectItem::isProxyRunning() const
321 {
322     int s = data(0, JobProgressRole).toInt();
323     if ((s == JOBWORKING || s > 0) && data(0, JobTypeRole).toInt() == (int) PROXYJOB) return true;
324     return false;
325 }
326
327 bool ProjectItem::playlistHasProxies(const QString path)
328 {
329     kDebug()<<"// CHECKING FOR PROXIES";
330     QFile file(path);
331     QDomDocument doc;
332     if (!file.open(QIODevice::ReadOnly))
333     return false;
334     if (!doc.setContent(&file)) {
335         file.close();
336         return false;
337     }
338     file.close();
339     QString root = doc.documentElement().attribute("root");
340     QDomNodeList kdenliveProducers = doc.elementsByTagName("kdenlive_producer");
341     for (int i = 0; i < kdenliveProducers.count(); i++) {
342         QString proxy = kdenliveProducers.at(i).toElement().attribute("proxy");
343         if (!proxy.isEmpty() && proxy != "-") return true;
344     }
345     return false;
346 }
347
348
349