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