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