]> git.sesse.net Git - kdenlive/blob - src/projectitem.cpp
Cleaning code style of Definitions.
[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 <KLocalizedString>
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, const 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, const 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(const 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(const 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(m_clip->shortInfo());
198     setToolTip(0, tip);
199 }
200
201
202 void ProjectItem::setProperties(const QMap < QString, QString > &attributes, const QMap < QString, QString > &metadata)
203 {
204     if (m_clip == NULL) return;
205
206     QString prefix;
207
208     m_clip->setProperties(attributes);
209     m_clip->setMetadata(metadata);
210     
211     if (m_clipType == Unknown) {
212         QString cliptype = attributes.value("type");
213         if (cliptype == "audio") m_clipType = Audio;
214         else if (cliptype == "video") m_clipType = Video;
215         else if (cliptype == "av") m_clipType = AV;
216         else if (cliptype == "playlist") m_clipType = Playlist;
217         else m_clipType = AV;
218
219         m_clip->setClipType(m_clipType);
220         slotSetToolTip();
221         if (m_clipType == Playlist) {
222             // Check if the playlist xml contains a proxy inside, and inform user
223             if (playlistHasProxies(m_clip->fileURL().path())) {
224                 prefix = i18n("Contains proxies") + " / ";
225             }
226         }
227     }
228     else if (attributes.contains("frame_size")) slotSetToolTip();
229     
230     if (attributes.contains("duration")) {
231         GenTime duration = GenTime(attributes.value("duration").toInt(), KdenliveSettings::project_fps());
232         QString itemdata = data(0, DurationRole).toString();
233         if (itemdata.contains('/')) itemdata = itemdata.section('/', 0, 0) + "/ ";
234         else itemdata.clear();
235         if (prefix.isEmpty()) prefix = itemdata;
236         setData(0, DurationRole, prefix + Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps()));
237         m_clip->setDuration(duration);
238     } else  {
239         // No duration known, use an arbitrary one until it is.
240     }
241
242     if (m_clip->description().isEmpty()) {
243         if (metadata.contains("description")) {
244             m_clip->setProperty("description", metadata.value("description"));
245             setText(1, m_clip->description());
246         } else if (metadata.contains("comment")) {
247             m_clip->setProperty("description", metadata.value("comment"));
248             setText(1, m_clip->description());
249         }
250     }
251 }
252
253 void ProjectItem::setJobStatus(JOBTYPE jobType, ClipJobStatus status, int progress, const QString &statusMessage)
254 {
255     setData(0, JobTypeRole, jobType);
256     if (progress > 0) setData(0, JobProgressRole, qMin(100, progress));
257     else {
258         setData(0, JobProgressRole, status);
259         if ((status == JobAborted || status == JobCrashed  || status == JobDone) || !statusMessage.isEmpty())
260             setData(0, JobStatusMessage, statusMessage);
261         slotSetToolTip();
262     }
263 }
264
265 void ProjectItem::setConditionalJobStatus(ClipJobStatus status, JOBTYPE requestedJobType)
266 {
267     if (data(0, JobTypeRole).toInt() == requestedJobType) {
268         setData(0, JobProgressRole, status);
269     }
270 }
271
272 bool ProjectItem::hasProxy() const
273 {
274     if (m_clip == NULL) return false;
275     if (m_clip->getProperty("proxy").size() < 2 || data(0, JobProgressRole).toInt() == JobCrashed) return false;
276     return true;
277 }
278
279 bool ProjectItem::isProxyReady() const
280 {
281      return (data(0, JobProgressRole).toInt() == JobDone);
282 }
283
284 bool ProjectItem::isJobRunning() const
285 {
286     int s = data(0, JobProgressRole).toInt();
287     if (s == JobWaiting || s == JobWorking || s > 0) return true;
288     return false;
289 }
290
291 bool ProjectItem::isProxyRunning() const
292 {
293     int s = data(0, JobProgressRole).toInt();
294     if ((s == JobWorking || s > 0) && data(0, JobTypeRole).toInt() == (int) PROXYJOB) return true;
295     return false;
296 }
297
298 bool ProjectItem::playlistHasProxies(const QString& path)
299 {
300     kDebug()<<"// CHECKING FOR PROXIES";
301     QFile file(path);
302     QDomDocument doc;
303     if (!file.open(QIODevice::ReadOnly))
304     return false;
305     if (!doc.setContent(&file)) {
306         file.close();
307         return false;
308     }
309     file.close();
310     QString root = doc.documentElement().attribute("root");
311     QDomNodeList kdenliveProducers = doc.elementsByTagName("kdenlive_producer");
312     for (int i = 0; i < kdenliveProducers.count(); ++i) {
313         QString proxy = kdenliveProducers.at(i).toElement().attribute("proxy");
314         if (!proxy.isEmpty() && proxy != "-") return true;
315     }
316     return false;
317 }
318
319
320