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