]> git.sesse.net Git - kdenlive/blob - src/projectitem.cpp
ae1eab2688bd7fcf4123a0137795f6fea226efb5
[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 const int DurationRole = Qt::UserRole + 1;
31 const int itemHeight = 38;
32
33 ProjectItem::ProjectItem(QTreeWidget * parent, DocClipBase *clip) :
34         QTreeWidgetItem(parent, PROJECTCLIPTYPE)
35 {
36     setSizeHint(0, QSize(itemHeight * 3, itemHeight));
37     if (clip->isPlaceHolder()) setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
38     else setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable);
39     m_clip = clip;
40     m_clipId = clip->getId();
41     QString name = m_clip->getProperty("name");
42     if (name.isEmpty()) name = KUrl(m_clip->getProperty("resource")).fileName();
43     m_clipType = (CLIPTYPE) m_clip->getProperty("type").toInt();
44     if (m_clipType != UNKNOWN) slotSetToolTip();
45     setText(0, name);
46     setText(1, m_clip->description());
47     m_clip->askForAudioThumbs();
48     GenTime duration = m_clip->duration();
49     if (duration != GenTime()) setData(0, DurationRole, Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps()));
50     //setFlags(Qt::NoItemFlags);
51     //kDebug() << "Constructed with clipId: " << m_clipId;
52 }
53
54 ProjectItem::ProjectItem(QTreeWidgetItem * parent, DocClipBase *clip) :
55         QTreeWidgetItem(parent, PROJECTCLIPTYPE)
56 {
57     setSizeHint(0, QSize(itemHeight * 3, itemHeight));
58     if (clip->isPlaceHolder()) setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);
59     else setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable);
60     m_clip = clip;
61     m_clipId = clip->getId();
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     setText(0, name);
66     setText(1, m_clip->description());
67     m_clip->askForAudioThumbs();
68     GenTime duration = m_clip->duration();
69     if (duration != GenTime()) setData(0, DurationRole, Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps()));
70     //setFlags(Qt::NoItemFlags);
71     //kDebug() << "Constructed with clipId: " << m_clipId;
72 }
73
74
75 ProjectItem::~ProjectItem()
76 {
77 }
78
79 //static
80 int ProjectItem::itemDefaultHeight()
81 {
82     return itemHeight;
83 }
84
85 int ProjectItem::numReferences() const
86 {
87     if (!m_clip) return 0;
88     return m_clip->numReferences();
89 }
90
91 const QString &ProjectItem::clipId() const
92 {
93     return m_clipId;
94 }
95
96 CLIPTYPE ProjectItem::clipType() const
97 {
98     return m_clipType;
99 }
100
101 int ProjectItem::clipMaxDuration() const
102 {
103     return m_clip->getProperty("duration").toInt();
104 }
105
106 QStringList ProjectItem::names() const
107 {
108     QStringList result;
109     result.append(text(0));
110     result.append(text(1));
111     result.append(text(2));
112     return result;
113 }
114
115 QDomElement ProjectItem::toXml() const
116 {
117     return m_clip->toXML();
118 }
119
120 const KUrl ProjectItem::clipUrl() const
121 {
122     if (m_clipType != COLOR && m_clipType != VIRTUAL && m_clipType != UNKNOWN)
123         return KUrl(m_clip->getProperty("resource"));
124     else return KUrl();
125 }
126
127 void ProjectItem::changeDuration(int frames)
128 {
129     setData(0, DurationRole, Timecode::getEasyTimecode(GenTime(frames, KdenliveSettings::project_fps()), KdenliveSettings::project_fps()));
130 }
131
132 void ProjectItem::setProperties(QMap <QString, QString> props)
133 {
134     if (m_clip == NULL) return;
135     m_clip->setProperties(props);
136 }
137
138 QString ProjectItem::getClipHash() const
139 {
140     if (m_clip == NULL) return QString();
141     return m_clip->getClipHash();
142 }
143
144 void ProjectItem::setProperty(const QString &key, const QString &value)
145 {
146     if (m_clip == NULL) return;
147     m_clip->setProperty(key, value);
148 }
149
150 void ProjectItem::clearProperty(const QString &key)
151 {
152     if (m_clip == NULL) return;
153     m_clip->clearProperty(key);
154 }
155
156 DocClipBase *ProjectItem::referencedClip()
157 {
158     return m_clip;
159 }
160
161 void ProjectItem::slotSetToolTip()
162 {
163     QString tip = "<b>";
164     if (m_clip->isPlaceHolder()) tip.append(i18n("Missing") + " | ");
165     switch (m_clipType) {
166     case AUDIO:
167         tip.append(i18n("Audio clip") + "</b><br />" + clipUrl().path());
168         break;
169     case VIDEO:
170         tip.append(i18n("Mute video clip") + "</b><br />" + clipUrl().path());
171         break;
172     case AV:
173         tip.append(i18n("Video clip") + "</b><br />" + clipUrl().path());
174         break;
175     case COLOR:
176         tip.append(i18n("Color clip"));
177         break;
178     case IMAGE:
179         tip.append(i18n("Image clip") + "</b><br />" + clipUrl().path());
180         break;
181     case TEXT:
182         if (!clipUrl().isEmpty() && m_clip->getProperty("xmldata").isEmpty()) tip.append(i18n("Template text clip") + "</b><br />" + clipUrl().path());
183         else tip.append(i18n("Text clip") + "</b><br />" + clipUrl().path());
184         break;
185     case SLIDESHOW:
186         tip.append(i18n("Slideshow clip") + "</b><br />" + clipUrl().directory());
187         break;
188     case VIRTUAL:
189         tip.append(i18n("Virtual clip"));
190         break;
191     case PLAYLIST:
192         tip.append(i18n("Playlist clip") + "</b><br />" + clipUrl().path());
193         break;
194     default:
195         tip.append(i18n("Unknown clip"));
196         break;
197     }
198
199     setToolTip(0, tip);
200 }
201
202
203 void ProjectItem::setProperties(const QMap < QString, QString > &attributes, const QMap < QString, QString > &metadata)
204 {
205     if (m_clip == NULL) return;
206     //setFlags(Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled);
207     if (attributes.contains("duration")) {
208         GenTime duration = GenTime(attributes.value("duration").toInt(), KdenliveSettings::project_fps());
209         setData(0, DurationRole, Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps()));
210         m_clip->setDuration(duration);
211     } else  {
212         // No duration known, use an arbitrary one until it is.
213     }
214
215
216     //extend attributes -reh
217
218     if (m_clipType == UNKNOWN) {
219         QString cliptype = attributes.value("type");
220         if (cliptype == "audio") m_clipType = AUDIO;
221         else if (cliptype == "video") m_clipType = VIDEO;
222         else if (cliptype == "av") m_clipType = AV;
223         else if (cliptype == "playlist") m_clipType = PLAYLIST;
224         else m_clipType = AV;
225
226         m_clip->setClipType(m_clipType);
227         slotSetToolTip();
228     }
229     m_clip->setProperties(attributes);
230     m_clip->setMetadata(metadata);
231     m_clip->askForAudioThumbs();
232
233     if (m_clip->description().isEmpty()) {
234         if (metadata.contains("description")) {
235             m_clip->setProperty("description", metadata.value("description"));
236             setText(1, m_clip->description());
237         } else if (metadata.contains("comment")) {
238             m_clip->setProperty("description", metadata.value("comment"));
239             setText(1, m_clip->description());
240         }
241     }
242 }
243