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