]> git.sesse.net Git - kdenlive/blob - src/projectitem.cpp
correctly load / save text clips
[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 <QMouseEvent>
22 #include <QStylePainter>
23 #include <QLabel>
24 #include <QLayout>
25
26 #include <KDebug>
27 #include <KLocale>
28 #include <KIcon>
29
30
31 #include "projectitem.h"
32 #include "timecode.h"
33 #include "kdenlivesettings.h"
34 #include "docclipbase.h"
35
36 const int NameRole = Qt::UserRole;
37 const int DurationRole = NameRole + 1;
38 const int UsageRole = NameRole + 2;
39
40
41 ProjectItem::ProjectItem(QTreeWidget * parent, const QStringList & strings, QDomElement xml, int clipId)
42         : QTreeWidgetItem(parent, strings, QTreeWidgetItem::UserType), m_clipType(UNKNOWN), m_clipId(clipId) {
43     QDomElement element = xml.cloneNode().toElement();
44     setSizeHint(0, QSize(65, 45));
45     setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable);
46     if (!element.isNull()) {
47         element.setAttribute("id", clipId);
48         QString cType = element.attribute("type", QString::null);
49         if (!cType.isEmpty()) {
50             m_clipType = (CLIPTYPE) cType.toInt();
51             slotSetToolTip();
52         }
53
54         if (m_clipType == COLOR || m_clipType == IMAGE || m_clipType == SLIDESHOW || m_clipType == TEXT)
55             element.setAttribute("duration", MAXCLIPDURATION);
56         else if (element.attribute("duration").isEmpty() && !element.attribute("out").isEmpty()) {
57             element.setAttribute("duration", element.attribute("out").toInt() - element.attribute("in").toInt());
58         }
59     }
60 }
61
62 ProjectItem::ProjectItem(QTreeWidgetItem * parent, const QStringList & strings, QDomElement xml, int clipId)
63         : QTreeWidgetItem(parent, strings, QTreeWidgetItem::UserType), m_clipType(UNKNOWN), m_clipId(clipId) {
64     QDomElement element = xml.cloneNode().toElement();
65     setSizeHint(0, QSize(65, 45));
66     setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable);
67     if (!element.isNull()) {
68         element.setAttribute("id", clipId);
69         QString cType = element.attribute("type", QString::null);
70         if (!cType.isEmpty()) {
71             m_clipType = (CLIPTYPE) cType.toInt();
72             slotSetToolTip();
73         }
74     }
75 }
76
77 // folder
78 ProjectItem::ProjectItem(QTreeWidget * parent, const QStringList & strings, int clipId)
79         : QTreeWidgetItem(parent, strings), m_clipType(FOLDER), m_groupName(strings.at(1)), m_clipId(clipId), m_clip(NULL) {
80     setSizeHint(0, QSize(65, 45));
81     setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable);
82     setIcon(0, KIcon("folder"));
83     setToolTip(1, "<qt><b>" + i18n("Folder"));
84 }
85
86 ProjectItem::ProjectItem(QTreeWidget * parent, DocClipBase *clip)
87         : QTreeWidgetItem(parent) {
88     setSizeHint(0, QSize(65, 45));
89     setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable);
90     m_clip = clip;
91     m_clipId = clip->getId();
92     QString name = m_clip->getProperty("name");
93     if (name.isEmpty()) name = KUrl(m_clip->getProperty("resource")).fileName();
94     m_clipType = (CLIPTYPE) m_clip->getProperty("type").toInt();
95     setText(1, name);
96     kDebug() << "PROJECT ITE;. ADDING LCIP: " << m_clipId;
97 }
98
99 ProjectItem::ProjectItem(QTreeWidgetItem * parent, DocClipBase *clip)
100         : QTreeWidgetItem(parent) {
101     setSizeHint(0, QSize(65, 45));
102     setFlags(Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled | Qt::ItemIsEditable);
103     m_clip = clip;
104     m_clipId = clip->getId();
105     QString name = m_clip->getProperty("name");
106     if (name.isEmpty()) name = KUrl(m_clip->getProperty("resource")).fileName();
107     m_clipType = (CLIPTYPE) m_clip->getProperty("type").toInt();
108     setText(1, name);
109     kDebug() << "PROJECT ITE;. ADDING LCIP: " << m_clipId;
110 }
111
112
113 ProjectItem::~ProjectItem() {
114 }
115
116 int ProjectItem::numReferences() const {
117     if (!m_clip) return 0;
118     return m_clip->numReferences();
119 }
120
121 int ProjectItem::clipId() const {
122     return m_clipId;
123 }
124
125 CLIPTYPE ProjectItem::clipType() const {
126     return m_clipType;
127 }
128
129 int ProjectItem::clipMaxDuration() const {
130     return m_clip->getProperty("duration").toInt();
131 }
132
133 bool ProjectItem::isGroup() const {
134     return m_clipType == FOLDER;
135 }
136
137 const QString ProjectItem::groupName() const {
138     return m_groupName;
139 }
140
141 void ProjectItem::setGroupName(const QString name) {
142     m_groupName = name;
143 }
144
145 QStringList ProjectItem::names() const {
146     QStringList result;
147     result.append(text(0));
148     result.append(text(1));
149     result.append(text(2));
150     return result;
151 }
152
153 QDomElement ProjectItem::toXml() const {
154     return m_clip->toXML();
155 }
156
157 const KUrl ProjectItem::clipUrl() const {
158     if (m_clipType != COLOR && m_clipType != VIRTUAL && m_clipType != UNKNOWN && m_clipType != FOLDER)
159         return KUrl(m_clip->getProperty("resource"));
160     else return KUrl();
161 }
162
163 void ProjectItem::changeDuration(int frames) {
164     setData(1, DurationRole, Timecode::getEasyTimecode(GenTime(frames, KdenliveSettings::project_fps()), KdenliveSettings::project_fps()));
165 }
166
167 void ProjectItem::setProperties(QMap <QString, QString> props) {
168     m_clip->setProperties(props);
169 }
170
171 DocClipBase *ProjectItem::referencedClip() {
172     return m_clip;
173 }
174
175 void ProjectItem::slotSetToolTip() {
176     QString tip = "<qt><b>";
177     switch (m_clipType) {
178     case 1:
179         tip.append(i18n("Audio clip") + "</b><br />" + clipUrl().path());
180         break;
181     case 2:
182         tip.append(i18n("Mute video clip") + "</b><br />" + clipUrl().path());
183         break;
184     case 3:
185         tip.append(i18n("Video clip") + "</b><br />" + clipUrl().path());
186         break;
187     case 4:
188         tip.append(i18n("Color clip"));
189         setData(1, DurationRole, Timecode::getEasyTimecode(GenTime(m_clip->getProperty("out").toInt(), 25), 25));
190         break;
191     case 5:
192         tip.append(i18n("Image clip") + "</b><br />" + clipUrl().path());
193         break;
194     case 6:
195         tip.append(i18n("Text clip"));
196         break;
197     case 7:
198         tip.append(i18n("Slideshow clip"));
199         break;
200     case 8:
201         tip.append(i18n("Virtual clip"));
202         break;
203     case 9:
204         tip.append(i18n("Playlist clip") + "</b><br />" + clipUrl().path());
205         break;
206     default:
207         tip.append(i18n("Unknown clip"));
208         break;
209     }
210
211     setToolTip(1, tip);
212 }
213
214
215 void ProjectItem::setProperties(const QMap < QString, QString > &attributes, const QMap < QString, QString > &metadata) {
216     if (attributes.contains("duration")) {
217         //if (m_clipType == AUDIO || m_clipType == VIDEO || m_clipType == AV)
218         //m_clip->setProperty("duration", attributes["duration"]);
219         GenTime duration = GenTime(attributes["duration"].toInt(), KdenliveSettings::project_fps());
220         setData(1, DurationRole, Timecode::getEasyTimecode(duration, KdenliveSettings::project_fps()));
221         m_clip->setDuration(duration);
222         //kDebug() << "//// LOADED CLIP, DURATION SET TO: " << duration.frames(KdenliveSettings::project_fps());
223     } else  {
224         // No duration known, use an arbitrary one until it is.
225     }
226
227
228     //extend attributes -reh
229
230     if (m_clipType == UNKNOWN) {
231         if (attributes.contains("type")) {
232             if (attributes["type"] == "audio")
233                 m_clipType = AUDIO;
234             else if (attributes["type"] == "video")
235                 m_clipType = VIDEO;
236             else if (attributes["type"] == "av")
237                 m_clipType = AV;
238             else if (attributes["type"] == "playlist")
239                 m_clipType = PLAYLIST;
240         } else {
241             m_clipType = AV;
242         }
243         m_clip->setClipType(m_clipType);
244     }
245     slotSetToolTip();
246     if (KdenliveSettings::audiothumbnails()) m_clip->slotRequestAudioThumbs();
247
248     m_clip->setProperties(attributes);
249     /*
250          m_metadata = metadata;
251
252          if (m_metadata.contains("description")) {
253              setDescription (m_metadata["description"]);
254          }
255          else if (m_metadata.contains("comment")) {
256              setDescription (m_metadata["comment"]);
257          }
258     */
259
260 }
261
262 #include "projectitem.moc"