]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
Show name, resource and comment in tooltip for timeline clips (http://kdenlive.org...
[kdenlive] / src / clipitem.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 "clipitem.h"
22 #include "customtrackview.h"
23 #include "customtrackscene.h"
24 #include "renderer.h"
25 #include "docclipbase.h"
26 #include "transition.h"
27 #include "kdenlivesettings.h"
28 #include "kthumb.h"
29 #include "profilesdialog.h"
30
31 #include <KDebug>
32 #include <KIcon>
33
34 #include <QPainter>
35 #include <QTimer>
36 #include <QStyleOptionGraphicsItem>
37 #include <QGraphicsScene>
38 #include <QMimeData>
39
40 ClipItem::ClipItem(DocClipBase *clip, ItemInfo info, double fps, double speed, int strobe, bool generateThumbs) :
41         AbstractClipItem(info, QRectF(), fps),
42         m_clip(clip),
43         m_startFade(0),
44         m_endFade(0),
45         m_audioOnly(false),
46         m_videoOnly(false),
47         m_startPix(QPixmap()),
48         m_endPix(QPixmap()),
49         m_hasThumbs(false),
50         m_selectedEffect(-1),
51         m_timeLine(0),
52         m_startThumbRequested(false),
53         m_endThumbRequested(false),
54         //m_hover(false),
55         m_speed(speed),
56         m_strobe(strobe),
57         m_framePixelWidth(0)
58 {
59     setZValue(2);
60     setRect(0, 0, (info.endPos - info.startPos).frames(fps) - 0.02, (double)(KdenliveSettings::trackheight() - 2));
61     setPos(info.startPos.frames(fps), (double)(info.track * KdenliveSettings::trackheight()) + 1);
62
63     // set speed independant info
64     m_speedIndependantInfo = m_info;
65     m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
66     m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
67
68     m_videoPix = KIcon("kdenlive-show-video").pixmap(QSize(16, 16));
69     m_audioPix = KIcon("kdenlive-show-audio").pixmap(QSize(16, 16));
70
71     if (m_speed == 1.0) m_clipName = m_clip->name();
72     else {
73         m_clipName = m_clip->name() + " - " + QString::number(m_speed * 100, 'f', 0) + '%';
74     }
75     m_producer = m_clip->getId();
76     m_clipType = m_clip->clipType();
77     //m_cropStart = info.cropStart;
78     m_maxDuration = m_clip->maxDuration();
79     setAcceptDrops(true);
80     m_audioThumbReady = m_clip->audioThumbCreated();
81     //setAcceptsHoverEvents(true);
82     connect(this , SIGNAL(prepareAudioThumb(double, int, int, int)) , this, SLOT(slotPrepareAudioThumb(double, int, int, int)));
83
84     if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
85         m_baseColor = QColor(141, 166, 215);
86         if (!m_clip->isPlaceHolder()) {
87             m_hasThumbs = true;
88             m_startThumbTimer.setSingleShot(true);
89             connect(&m_startThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetStartThumb()));
90             m_endThumbTimer.setSingleShot(true);
91             connect(&m_endThumbTimer, SIGNAL(timeout()), this, SLOT(slotGetEndThumb()));
92
93             connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
94
95             connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
96             connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
97             if (generateThumbs) QTimer::singleShot(200, this, SLOT(slotFetchThumbs()));
98         }
99
100     } else if (m_clipType == COLOR) {
101         QString colour = m_clip->getProperty("colour");
102         colour = colour.replace(0, 2, "#");
103         m_baseColor = QColor(colour.left(7));
104     } else if (m_clipType == IMAGE || m_clipType == TEXT) {
105         m_baseColor = QColor(141, 166, 215);
106         if (m_clipType == TEXT) {
107             connect(this, SIGNAL(getThumb(int, int)), m_clip->thumbProducer(), SLOT(extractImage(int, int)));
108             connect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
109         }
110         //m_startPix = KThumb::getImage(KUrl(clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
111     } else if (m_clipType == AUDIO) {
112         m_baseColor = QColor(141, 215, 166);
113         connect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
114     }
115 }
116
117
118 ClipItem::~ClipItem()
119 {
120     blockSignals(true);
121     if (scene()) scene()->removeItem(this);
122     if (m_clipType == VIDEO || m_clipType == AV || m_clipType == SLIDESHOW || m_clipType == PLAYLIST) {
123         //disconnect(m_clip->thumbProducer(), SIGNAL(thumbReady(int, QImage)), this, SLOT(slotThumbReady(int, QImage)));
124         //disconnect(m_clip, SIGNAL(gotAudioData()), this, SLOT(slotGotAudioData()));
125     }
126     delete m_timeLine;
127 }
128
129 ClipItem *ClipItem::clone(ItemInfo info) const
130 {
131     ClipItem *duplicate = new ClipItem(m_clip, info, m_fps, m_speed, m_strobe);
132     if (m_clipType == IMAGE || m_clipType == TEXT) duplicate->slotSetStartThumb(m_startPix);
133     else if (m_clipType != COLOR) {
134         if (info.cropStart == m_info.cropStart) duplicate->slotSetStartThumb(m_startPix);
135         if (info.cropStart + (info.endPos - info.startPos) == m_info.cropStart + (m_info.endPos - m_info.startPos)) duplicate->slotSetEndThumb(m_endPix);
136     }
137     //kDebug() << "// CLoning clip: " << (info.cropStart + (info.endPos - info.startPos)).frames(m_fps) << ", CURRENT end: " << (cropStart() + duration()).frames(m_fps);
138     duplicate->setEffectList(m_effectList);
139     duplicate->setVideoOnly(m_videoOnly);
140     duplicate->setAudioOnly(m_audioOnly);
141     //duplicate->setSpeed(m_speed);
142     return duplicate;
143 }
144
145 void ClipItem::setEffectList(const EffectsList effectList)
146 {
147     m_effectList.clone(effectList);
148     m_effectNames = m_effectList.effectNames().join(" / ");
149     if (!m_effectList.isEmpty()) setSelectedEffect(0);
150 }
151
152 const EffectsList ClipItem::effectList() const
153 {
154     return m_effectList;
155 }
156
157 int ClipItem::selectedEffectIndex() const
158 {
159     return m_selectedEffect;
160 }
161
162 void ClipItem::initEffect(QDomElement effect, int diff)
163 {
164     // the kdenlive_ix int is used to identify an effect in mlt's playlist, should
165     // not be changed
166     if (effect.attribute("kdenlive_ix").toInt() == 0)
167         effect.setAttribute("kdenlive_ix", QString::number(effectsCounter()));
168
169     if (effect.attribute("id") == "freeze" && diff > 0) {
170         EffectsList::setParameter(effect, "frame", QString::number(diff));
171     }
172
173     // Init parameter value & keyframes if required
174     QDomNodeList params = effect.elementsByTagName("parameter");
175     for (int i = 0; i < params.count(); i++) {
176         QDomElement e = params.item(i).toElement();
177         kDebug() << "// init eff: " << e.attribute("name");
178
179         // Check if this effect has a variable parameter
180         if (e.attribute("default").startsWith('%')) {
181             double evaluatedValue = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("default"));
182             e.setAttribute("default", evaluatedValue);
183             if (e.hasAttribute("value") && e.attribute("value").startsWith('%')) {
184                 e.setAttribute("value", evaluatedValue);
185             }
186         }
187
188         if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
189             QString def = e.attribute("default");
190             // Effect has a keyframe type parameter, we need to set the values
191             if (e.attribute("keyframes").isEmpty()) {
192                 e.setAttribute("keyframes", QString::number(cropStart().frames(m_fps)) + ':' + def + ';' + QString::number((cropStart() + cropDuration()).frames(m_fps) - 1) + ':' + def);
193                 kDebug() << "///// EFFECT KEYFRAMES INITED: " << e.attribute("keyframes");
194                 //break;
195             }
196         }
197     }
198     if (effect.attribute("tag") == "volume" || effect.attribute("tag") == "brightness") {
199         if (effect.attribute("id") == "fadeout" || effect.attribute("id") == "fade_to_black") {
200             int end = (cropDuration() + cropStart()).frames(m_fps);
201             int start = end;
202             if (effect.attribute("id") == "fadeout") {
203                 if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
204                     int effectDuration = EffectsList::parameter(effect, "in").toInt();
205                     if (effectDuration > cropDuration().frames(m_fps)) {
206                         effectDuration = cropDuration().frames(m_fps) / 2;
207                     }
208                     start -= effectDuration;
209                 } else {
210                     QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
211                     start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
212                 }
213             } else if (effect.attribute("id") == "fade_to_black") {
214                 if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
215                     int effectDuration = EffectsList::parameter(effect, "in").toInt();
216                     if (effectDuration > cropDuration().frames(m_fps)) {
217                         effectDuration = cropDuration().frames(m_fps) / 2;
218                     }
219                     start -= effectDuration;
220                 } else {
221                     QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
222                     start -= EffectsList::parameter(fadeout, "out").toInt() - EffectsList::parameter(fadeout, "in").toInt();
223                 }
224             }
225             EffectsList::setParameter(effect, "in", QString::number(start));
226             EffectsList::setParameter(effect, "out", QString::number(end));
227         } else if (effect.attribute("id") == "fadein" || effect.attribute("id") == "fade_from_black") {
228             int start = cropStart().frames(m_fps);
229             int end = start;
230             if (effect.attribute("id") == "fadein") {
231                 if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
232                     int effectDuration = EffectsList::parameter(effect, "out").toInt();
233                     if (effectDuration > cropDuration().frames(m_fps)) {
234                         effectDuration = cropDuration().frames(m_fps) / 2;
235                     }
236                     end += effectDuration;
237                 } else
238                     end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fade_from_black"), "out").toInt();
239             } else if (effect.attribute("id") == "fade_from_black") {
240                 if (m_effectList.hasEffect(QString(), "fadein") == -1) {
241                     int effectDuration = EffectsList::parameter(effect, "out").toInt();
242                     if (effectDuration > cropDuration().frames(m_fps)) {
243                         effectDuration = cropDuration().frames(m_fps) / 2;
244                     }
245                     end += effectDuration;
246                 } else
247                     end += EffectsList::parameter(m_effectList.getEffectByTag(QString(), "fadein"), "out").toInt();
248             }
249             EffectsList::setParameter(effect, "in", QString::number(start));
250             EffectsList::setParameter(effect, "out", QString::number(end));
251         }
252     }
253 }
254
255 bool ClipItem::checkKeyFrames()
256 {
257     bool clipEffectsModified = false;
258     for (int ix = 0; ix < m_effectList.count(); ix ++) {
259         QString kfr = keyframes(ix);
260         if (!kfr.isEmpty()) {
261             const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
262             QStringList newKeyFrames;
263             bool cutKeyFrame = false;
264             bool modified = false;
265             int lastPos = -1;
266             double lastValue = -1;
267             int start = cropStart().frames(m_fps);
268             int end = (cropStart() + cropDuration()).frames(m_fps);
269             foreach(const QString &str, keyframes) {
270                 int pos = str.section(':', 0, 0).toInt();
271                 double val = str.section(':', 1, 1).toDouble();
272                 if (pos - start < 0) {
273                     // a keyframe is defined before the start of the clip
274                     cutKeyFrame = true;
275                 } else if (cutKeyFrame) {
276                     // create new keyframe at clip start, calculate interpolated value
277                     if (pos > start) {
278                         int diff = pos - lastPos;
279                         double ratio = (double)(start - lastPos) / diff;
280                         double newValue = lastValue + (val - lastValue) * ratio;
281                         newKeyFrames.append(QString::number(start) + ':' + QString::number(newValue));
282                         modified = true;
283                     }
284                     cutKeyFrame = false;
285                 }
286                 if (!cutKeyFrame) {
287                     if (pos > end) {
288                         // create new keyframe at clip end, calculate interpolated value
289                         int diff = pos - lastPos;
290                         if (diff != 0) {
291                             double ratio = (double)(end - lastPos) / diff;
292                             double newValue = lastValue + (val - lastValue) * ratio;
293                             newKeyFrames.append(QString::number(end) + ':' + QString::number(newValue));
294                             modified = true;
295                         }
296                         break;
297                     } else {
298                         newKeyFrames.append(QString::number(pos) + ':' + QString::number(val));
299                     }
300                 }
301                 lastPos = pos;
302                 lastValue = val;
303             }
304             if (modified) {
305                 // update KeyFrames
306                 setKeyframes(ix, newKeyFrames.join(";"));
307                 clipEffectsModified = true;
308             }
309         }
310     }
311     return clipEffectsModified;
312 }
313
314 void ClipItem::setKeyframes(const int ix, const QString keyframes)
315 {
316     QDomElement effect = getEffectAt(ix);
317     if (effect.attribute("disable") == "1") return;
318     QDomNodeList params = effect.elementsByTagName("parameter");
319     for (int i = 0; i < params.count(); i++) {
320         QDomElement e = params.item(i).toElement();
321         if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
322             e.setAttribute("keyframes", keyframes);
323             if (ix == m_selectedEffect) {
324                 m_keyframes.clear();
325                 double max = e.attribute("max").toDouble();
326                 double min = e.attribute("min").toDouble();
327                 m_keyframeFactor = 100.0 / (max - min);
328                 m_keyframeDefault = e.attribute("default").toDouble();
329                 m_selectedKeyframe = 0;
330                 // parse keyframes
331                 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
332                 foreach(const QString &str, keyframes) {
333                     int pos = str.section(':', 0, 0).toInt();
334                     double val = str.section(':', 1, 1).toDouble();
335                     m_keyframes[pos] = val;
336                 }
337                 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
338                 if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
339                 update();
340                 return;
341             }
342             break;
343         }
344     }
345 }
346
347
348 void ClipItem::setSelectedEffect(const int ix)
349 {
350     m_selectedEffect = ix;
351     QDomElement effect = effectAt(m_selectedEffect);
352     if (effect.isNull() == false) {
353         QDomNodeList params = effect.elementsByTagName("parameter");
354         if (effect.attribute("disable") != "1")
355             for (int i = 0; i < params.count(); i++) {
356                 QDomElement e = params.item(i).toElement();
357                 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
358                     m_keyframes.clear();
359                     double max = e.attribute("max").toDouble();
360                     double min = e.attribute("min").toDouble();
361                     m_keyframeFactor = 100.0 / (max - min);
362                     m_keyframeDefault = e.attribute("default").toDouble();
363                     m_selectedKeyframe = 0;
364
365                     // parse keyframes
366                     const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
367                     foreach(const QString &str, keyframes) {
368                         int pos = str.section(':', 0, 0).toInt();
369                         double val = str.section(':', 1, 1).toDouble();
370                         m_keyframes[pos] = val;
371                     }
372                     if (m_keyframes.find(m_editedKeyframe) == m_keyframes.end()) m_editedKeyframe = -1;
373                     update();
374                     return;
375                 }
376             }
377     }
378     if (!m_keyframes.isEmpty()) {
379         m_keyframes.clear();
380         update();
381     }
382 }
383
384 QString ClipItem::keyframes(const int index)
385 {
386     QString result;
387     QDomElement effect = effectAt(index);
388     QDomNodeList params = effect.elementsByTagName("parameter");
389
390     for (int i = 0; i < params.count(); i++) {
391         QDomElement e = params.item(i).toElement();
392         if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
393             result = e.attribute("keyframes");
394             break;
395         }
396     }
397     return result;
398 }
399
400 void ClipItem::updateKeyframeEffect()
401 {
402     // regenerate xml parameter from the clip keyframes
403     QDomElement effect = getEffectAt(m_selectedEffect);
404     if (effect.attribute("disable") == "1") return;
405     QDomNodeList params = effect.elementsByTagName("parameter");
406
407     for (int i = 0; i < params.count(); i++) {
408         QDomElement e = params.item(i).toElement();
409         if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
410             QString keyframes;
411             if (m_keyframes.count() > 0) {
412                 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
413                 while (i != m_keyframes.constEnd()) {
414                     keyframes.append(QString::number(i.key()) + ':' + QString::number(i.value()) + ';');
415                     ++i;
416                 }
417             }
418             // Effect has a keyframe type parameter, we need to set the values
419             //kDebug() << ":::::::::::::::   SETTING EFFECT KEYFRAMES: " << keyframes;
420             e.setAttribute("keyframes", keyframes);
421             break;
422         }
423     }
424 }
425
426 QDomElement ClipItem::selectedEffect()
427 {
428     if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
429     return effectAt(m_selectedEffect);
430 }
431
432 void ClipItem::resetThumbs(bool clearExistingThumbs)
433 {
434     if (clearExistingThumbs) {
435         m_startPix = QPixmap();
436         m_endPix = QPixmap();
437         m_audioThumbCachePic.clear();
438     }
439     slotFetchThumbs();
440 }
441
442
443 void ClipItem::refreshClip(bool checkDuration)
444 {
445     if (checkDuration && (m_maxDuration != m_clip->maxDuration())) {
446         m_maxDuration = m_clip->maxDuration();
447         if (m_clipType != IMAGE && m_clipType != TEXT && m_clipType != COLOR) {
448             if (m_maxDuration != GenTime() && m_info.cropStart + m_info.cropDuration > m_maxDuration) {
449                 // Clip duration changed, make sure to stay in correct range
450                 if (m_info.cropStart > m_maxDuration) {
451                     m_info.cropStart = GenTime();
452                     m_info.cropDuration = qMin(m_info.cropDuration, m_maxDuration);
453                     updateRectGeometry();
454                 } else {
455                     m_info.cropDuration = m_maxDuration;
456                     updateRectGeometry();
457                 }
458             }
459         }
460     }
461     if (m_clipType == COLOR) {
462         QString colour = m_clip->getProperty("colour");
463         colour = colour.replace(0, 2, "#");
464         m_baseColor = QColor(colour.left(7));
465     } else resetThumbs(checkDuration);
466 }
467
468 void ClipItem::slotFetchThumbs()
469 {
470     if (scene() == NULL || m_clipType == AUDIO || m_clipType == COLOR) return;
471     if (m_clipType == IMAGE) {
472         if (m_startPix.isNull()) {
473             m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
474             update();
475         }
476         return;
477     }
478
479     if (m_clipType == TEXT) {
480         if (m_startPix.isNull()) slotGetStartThumb();
481         return;
482     }
483
484     if (m_endPix.isNull() && m_startPix.isNull()) {
485         m_startThumbRequested = true;
486         m_endThumbRequested = true;
487         emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
488     } else {
489         if (m_endPix.isNull()) {
490             slotGetEndThumb();
491         }
492         if (m_startPix.isNull()) {
493             slotGetStartThumb();
494         }
495     }
496 }
497
498 void ClipItem::slotGetStartThumb()
499 {
500     m_startThumbRequested = true;
501     emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
502 }
503
504 void ClipItem::slotGetEndThumb()
505 {
506     m_endThumbRequested = true;
507     emit getThumb(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
508 }
509
510
511 void ClipItem::slotSetStartThumb(QImage img)
512 {
513     if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
514         QPixmap pix = QPixmap::fromImage(img);
515         m_startPix = pix;
516         QRectF r = sceneBoundingRect();
517         r.setRight(pix.width() + 2);
518         update(r);
519     }
520 }
521
522 void ClipItem::slotSetEndThumb(QImage img)
523 {
524     if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
525         QPixmap pix = QPixmap::fromImage(img);
526         m_endPix = pix;
527         QRectF r = sceneBoundingRect();
528         r.setLeft(r.right() - pix.width() - 2);
529         update(r);
530     }
531 }
532
533 void ClipItem::slotThumbReady(int frame, QImage img)
534 {
535     if (scene() == NULL) return;
536     QRectF r = boundingRect();
537     QPixmap pix = QPixmap::fromImage(img);
538     double width = pix.width() / projectScene()->scale().x();
539     if (m_startThumbRequested && frame == m_speedIndependantInfo.cropStart.frames(m_fps)) {
540         m_startPix = pix;
541         m_startThumbRequested = false;
542         update(r.left(), r.top(), width, pix.height());
543         if (m_clipType == IMAGE || m_clipType == TEXT) {
544             update(r.right() - width, r.top(), width, pix.height());
545         }
546     } else if (m_endThumbRequested && frame == (m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1) {
547         m_endPix = pix;
548         m_endThumbRequested = false;
549         update(r.right() - width, r.top(), width, pix.height());
550     }
551 }
552
553 void ClipItem::slotSetStartThumb(const QPixmap pix)
554 {
555     m_startPix = pix;
556 }
557
558 void ClipItem::slotSetEndThumb(const QPixmap pix)
559 {
560     m_endPix = pix;
561 }
562
563 QPixmap ClipItem::startThumb() const
564 {
565     return m_startPix;
566 }
567
568 QPixmap ClipItem::endThumb() const
569 {
570     return m_endPix;
571 }
572
573 void ClipItem::slotGotAudioData()
574 {
575     m_audioThumbReady = true;
576     if (m_clipType == AV && !isAudioOnly()) {
577         QRectF r = boundingRect();
578         r.setTop(r.top() + r.height() / 2 - 1);
579         update(r);
580     } else update();
581 }
582
583 int ClipItem::type() const
584 {
585     return AVWIDGET;
586 }
587
588 DocClipBase *ClipItem::baseClip() const
589 {
590     return m_clip;
591 }
592
593 QDomElement ClipItem::xml() const
594 {
595     QDomElement xml = m_clip->toXML();
596     if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
597     if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
598     if (m_audioOnly) xml.setAttribute("audio_only", 1);
599     else if (m_videoOnly) xml.setAttribute("video_only", 1);
600     return xml;
601 }
602
603 int ClipItem::clipType() const
604 {
605     return m_clipType;
606 }
607
608 QString ClipItem::clipName() const
609 {
610     return m_clipName;
611 }
612
613 void ClipItem::setClipName(const QString &name)
614 {
615     m_clipName = name;
616 }
617
618 const QString ClipItem::clipProducer() const
619 {
620     return m_producer;
621 }
622
623 void ClipItem::flashClip()
624 {
625     if (m_timeLine == 0) {
626         m_timeLine = new QTimeLine(750, this);
627         m_timeLine->setUpdateInterval(80);
628         m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
629         m_timeLine->setFrameRange(0, 100);
630         connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
631     }
632     //m_timeLine->start();
633 }
634
635 void ClipItem::animate(qreal /*value*/)
636 {
637     QRectF r = boundingRect();
638     r.setHeight(20);
639     update(r);
640 }
641
642 // virtual
643 void ClipItem::paint(QPainter *painter,
644                      const QStyleOptionGraphicsItem *option,
645                      QWidget *)
646 {
647     QColor paintColor;
648     if (parentItem()) paintColor = QColor(255, 248, 149);
649     else paintColor = m_baseColor;
650     if (isSelected() || (parentItem() && parentItem()->isSelected())) paintColor = paintColor.darker();
651
652     painter->setMatrixEnabled(false);
653     const QRectF mapped = painter->matrix().mapRect(rect()).adjusted(0.5, 0, 0.5, 0);
654     const QRectF exposed = option->exposedRect;
655     painter->setClipRect(mapped);
656     painter->fillRect(mapped, paintColor);
657
658     // draw thumbnails
659     if (KdenliveSettings::videothumbnails() && !isAudioOnly()) {
660         QPen pen = painter->pen();
661         pen.setColor(QColor(255, 255, 255, 150));
662         painter->setPen(pen);
663         if ((m_clipType == IMAGE || m_clipType == TEXT) && !m_startPix.isNull()) {
664             const QPointF top = mapped.topRight() - QPointF(m_startPix.width() - 1, 0);
665             painter->drawPixmap(top, m_startPix);
666             QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
667             painter->drawLine(l2);
668         } else if (!m_endPix.isNull()) {
669             const QPointF top = mapped.topRight() - QPointF(m_endPix.width() - 1, 0);
670             painter->drawPixmap(top, m_endPix);
671             QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
672             painter->drawLine(l2);
673         }
674         if (!m_startPix.isNull()) {
675             painter->drawPixmap(mapped.topLeft(), m_startPix);
676             QLineF l2(mapped.left() + m_startPix.width(), mapped.top(), mapped.left() + m_startPix.width(), mapped.bottom());
677             painter->drawLine(l2);
678         }
679         painter->setPen(Qt::black);
680     }
681
682     // draw audio thumbnails
683     if (KdenliveSettings::audiothumbnails() && m_speed == 1.0 && !isVideoOnly() && ((m_clipType == AV && (exposed.bottom() > (rect().height() / 2) || isAudioOnly())) || m_clipType == AUDIO) && m_audioThumbReady) {
684
685         double startpixel = exposed.left();
686         if (startpixel < 0)
687             startpixel = 0;
688         double endpixel = exposed.right();
689         if (endpixel < 0)
690             endpixel = 0;
691         //kDebug()<<"///  REPAINTING AUDIO THMBS ZONE: "<<startpixel<<"x"<<endpixel;
692
693         /*QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;*/
694         QRectF mappedRect;
695         if (m_clipType == AV && !isAudioOnly()) {
696             mappedRect = mapped;
697             mappedRect.setTop(mappedRect.bottom() - mapped.height() / 2);
698         } else mappedRect = mapped;
699
700         double scale = painter->matrix().m11();
701         int channels = 0;
702         if (isEnabled() && m_clip) channels = m_clip->getProperty("channels").toInt();
703         if (scale != m_framePixelWidth)
704             m_audioThumbCachePic.clear();
705         double cropLeft = m_info.cropStart.frames(m_fps);
706         const int clipStart = mappedRect.x();
707         const int mappedStartPixel =  painter->matrix().map(QPointF(startpixel + cropLeft, 0)).x() - clipStart;
708         const int mappedEndPixel =  painter->matrix().map(QPointF(endpixel + cropLeft, 0)).x() - clipStart;
709         cropLeft = cropLeft * scale;
710
711         if (channels >= 1) {
712             emit prepareAudioThumb(scale, mappedStartPixel, mappedEndPixel, channels);
713         }
714
715         for (int startCache = mappedStartPixel - (mappedStartPixel) % 100; startCache < mappedEndPixel; startCache += 100) {
716             if (m_audioThumbCachePic.contains(startCache) && !m_audioThumbCachePic[startCache].isNull())
717                 painter->drawPixmap(clipStart + startCache - cropLeft, mappedRect.y(),  m_audioThumbCachePic[startCache]);
718         }
719     }
720
721     // Draw effects names
722     if (!m_effectNames.isEmpty() && mapped.width() > 40) {
723         QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
724         QColor bgColor;
725         if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
726             qreal value = m_timeLine->currentValue();
727             txtBounding.setWidth(txtBounding.width() * value);
728             bgColor.setRgb(50 + 200 *(1.0 - value), 50, 50, 100 + 50 * value);
729         } else bgColor.setRgb(50, 50, 90, 180);
730
731         QPainterPath rounded;
732         rounded.moveTo(txtBounding.bottomRight());
733         rounded.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
734         rounded.lineTo(txtBounding.topLeft());
735         rounded.lineTo(txtBounding.bottomLeft());
736         painter->fillPath(rounded, bgColor);
737         painter->setPen(Qt::lightGray);
738         painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_effectNames);
739     }
740
741     // Draw clip name
742     QColor frameColor(paintColor.darker());
743     if (isSelected() || (parentItem() && parentItem()->isSelected())) {
744         frameColor = QColor(Qt::red);
745     }
746     frameColor.setAlpha(160);
747
748     const QRectF txtBounding2 = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + m_clipName + ' ');
749     //painter->fillRect(txtBounding2, frameColor);
750     painter->setBrush(frameColor);
751     painter->setPen(Qt::NoPen);
752     painter->drawRoundedRect(txtBounding2, 3, 3);
753     painter->setBrush(QBrush(Qt::NoBrush));
754
755     //painter->setPen(QColor(0, 0, 0, 180));
756     //painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
757     if (m_videoOnly) {
758         painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_videoPix);
759     } else if (m_audioOnly) {
760         painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_audioPix);
761     }
762     painter->setPen(Qt::white);
763     painter->drawText(txtBounding2, Qt::AlignCenter, m_clipName);
764
765
766     // draw markers
767     if (isEnabled() && m_clip) {
768         QList < CommentedTime > markers = m_clip->commentedSnapMarkers();
769         QList < CommentedTime >::Iterator it = markers.begin();
770         GenTime pos;
771         double framepos;
772         QBrush markerBrush(QColor(120, 120, 0, 140));
773         QPen pen = painter->pen();
774         pen.setColor(QColor(255, 255, 255, 200));
775         pen.setStyle(Qt::DotLine);
776
777         for (; it != markers.end(); ++it) {
778             pos = GenTime((int)((*it).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
779             if (pos > GenTime()) {
780                 if (pos > cropDuration()) break;
781                 QLineF l(rect().x() + pos.frames(m_fps), rect().y(), rect().x() + pos.frames(m_fps), rect().bottom());
782                 QLineF l2 = painter->matrix().map(l);
783                 painter->setPen(pen);
784                 painter->drawLine(l2);
785                 if (KdenliveSettings::showmarkers()) {
786                     framepos = rect().x() + pos.frames(m_fps);
787                     const QRectF r1(framepos + 0.04, 10, rect().width() - framepos - 2, rect().height() - 10);
788                     const QRectF r2 = painter->matrix().mapRect(r1);
789                     const QRectF txtBounding3 = painter->boundingRect(r2, Qt::AlignLeft | Qt::AlignTop, ' ' + (*it).comment() + ' ');
790                     painter->setBrush(markerBrush);
791                     painter->setPen(Qt::NoPen);
792                     painter->drawRoundedRect(txtBounding3, 3, 3);
793                     painter->setBrush(QBrush(Qt::NoBrush));
794                     painter->setPen(Qt::white);
795                     painter->drawText(txtBounding3, Qt::AlignCenter, (*it).comment());
796                 }
797                 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
798             }
799         }
800     }
801
802     // draw start / end fades
803     QBrush fades;
804     if (isSelected()) {
805         fades = QBrush(QColor(200, 50, 50, 150));
806     } else fades = QBrush(QColor(200, 200, 200, 200));
807
808     if (m_startFade != 0) {
809         QPainterPath fadeInPath;
810         fadeInPath.moveTo(0, 0);
811         fadeInPath.lineTo(0, rect().height());
812         fadeInPath.lineTo(m_startFade, 0);
813         fadeInPath.closeSubpath();
814         QPainterPath f1 = painter->matrix().map(fadeInPath);
815         painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
816         /*if (isSelected()) {
817             QLineF l(m_startFade * scale, 0, 0, itemHeight);
818             painter->drawLine(l);
819         }*/
820     }
821     if (m_endFade != 0) {
822         QPainterPath fadeOutPath;
823         fadeOutPath.moveTo(rect().width(), 0);
824         fadeOutPath.lineTo(rect().width(), rect().height());
825         fadeOutPath.lineTo(rect().width() - m_endFade, 0);
826         fadeOutPath.closeSubpath();
827         QPainterPath f1 = painter->matrix().map(fadeOutPath);
828         painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
829         /*if (isSelected()) {
830             QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
831             painter->drawLine(l);
832         }*/
833     }
834
835
836     painter->setPen(QPen(Qt::lightGray));
837     // draw effect or transition keyframes
838     if (mapped.width() > 20) drawKeyFrames(painter, exposed);
839
840     //painter->setMatrixEnabled(true);
841
842     // draw clip border
843     // expand clip rect to allow correct painting of clip border
844     QPen pen1(frameColor);
845     painter->setPen(pen1);
846     painter->setClipping(false);
847     painter->drawRect(painter->matrix().mapRect(rect()));
848 }
849
850
851 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
852 {
853     if (isItemLocked()) return NONE;
854     const double scale = projectScene()->scale().x();
855     double maximumOffset = 6 / scale;
856     if (isSelected() || (parentItem() && parentItem()->isSelected())) {
857         int kf = mouseOverKeyFrames(pos, maximumOffset);
858         if (kf != -1) {
859             m_editedKeyframe = kf;
860             return KEYFRAME;
861         }
862     }
863     QRectF rect = sceneBoundingRect();
864     int addtransitionOffset = 10;
865     // Don't allow add transition if track height is very small
866     if (rect.height() < 30) addtransitionOffset = 0;
867
868     if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset  && qAbs((int)(pos.y() - rect.y())) < 6) {
869         if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
870         // xgettext:no-c-format
871         else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
872         return FADEIN;
873     } else if (pos.x() - rect.x() < maximumOffset && (rect.bottom() - pos.y() > addtransitionOffset)) {
874         // xgettext:no-c-format
875         setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
876         return RESIZESTART;
877     } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - m_endFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
878         if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
879         // xgettext:no-c-format
880         else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
881         return FADEOUT;
882     } else if ((rect.right() - pos.x() < maximumOffset) && (rect.bottom() - pos.y() > addtransitionOffset)) {
883         // xgettext:no-c-format
884         setToolTip(i18n("Clip duration: %1s", cropDuration().seconds()));
885         return RESIZEEND;
886     } else if ((pos.x() - rect.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
887         setToolTip(i18n("Add transition"));
888         return TRANSITIONSTART;
889     } else if ((rect.right() - pos.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
890         setToolTip(i18n("Add transition"));
891         return TRANSITIONEND;
892     }
893     QString tooltip = "<b>" + m_clipName + "</b>";
894     if (!baseClip()->fileURL().isEmpty())
895         tooltip.append("<br />" + baseClip()->fileURL().path());
896     if (!baseClip()->description().isEmpty())
897         tooltip.append("<br />" + baseClip()->description());
898     setToolTip(tooltip);
899     return MOVE;
900 }
901
902 QList <GenTime> ClipItem::snapMarkers() const
903 {
904     QList < GenTime > snaps;
905     QList < GenTime > markers = baseClip()->snapMarkers();
906     GenTime pos;
907
908     for (int i = 0; i < markers.size(); i++) {
909
910         pos = GenTime((int)(markers.at(i).frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
911         if (pos > GenTime()) {
912             if (pos > cropDuration()) break;
913             else snaps.append(pos + startPos());
914         }
915     }
916     return snaps;
917 }
918
919 QList <CommentedTime> ClipItem::commentedSnapMarkers() const
920 {
921     QList < CommentedTime > snaps;
922     QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
923     GenTime pos;
924
925     for (int i = 0; i < markers.size(); i++) {
926         pos = GenTime((int)(markers.at(i).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
927         if (pos > GenTime()) {
928             if (pos > cropDuration()) break;
929             else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
930         }
931     }
932     return snaps;
933 }
934
935 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, int startpixel, int endpixel, int channels)
936 {
937     QRectF re =  sceneBoundingRect();
938     if (m_clipType == AV && !isAudioOnly()) re.setTop(re.y() + re.height() / 2);
939
940     //kDebug() << "// PREP AUDIO THMB FRMO : scale:" << pixelForOneFrame<< ", from: " << startpixel << ", to: " << endpixel;
941     //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
942
943     for (int startCache = startpixel - startpixel % 100; startCache < endpixel; startCache += 100) {
944         //kDebug() << "creating " << startCache;
945         //if (framePixelWidth!=pixelForOneFrame  ||
946         if (m_framePixelWidth == pixelForOneFrame && m_audioThumbCachePic.contains(startCache))
947             continue;
948         if (m_audioThumbCachePic[startCache].isNull() || m_framePixelWidth != pixelForOneFrame) {
949             m_audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
950             m_audioThumbCachePic[startCache].fill(QColor(180, 180, 200, 140));
951         }
952         bool fullAreaDraw = pixelForOneFrame < 10;
953         QMap<int, QPainterPath > positiveChannelPaths;
954         QMap<int, QPainterPath > negativeChannelPaths;
955         QPainter pixpainter(&m_audioThumbCachePic[startCache]);
956         QPen audiopen;
957         audiopen.setWidth(0);
958         pixpainter.setPen(audiopen);
959         //pixpainter.setRenderHint(QPainter::Antialiasing,true);
960         //pixpainter.drawLine(0,0,100,re.height());
961         // Bail out, if caller provided invalid data
962         if (channels <= 0) {
963             kWarning() << "Unable to draw image with " << channels << "number of channels";
964             return;
965         }
966
967         int channelHeight = m_audioThumbCachePic[startCache].height() / channels;
968
969         for (int i = 0; i < channels; i++) {
970
971             positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
972             negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
973         }
974
975         for (int samples = 0; samples <= 100; samples++) {
976             double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
977             int sample = (int)((frame - (int)(frame)) * 20);   // AUDIO_FRAME_SIZE
978             if (frame < 0 || sample < 0 || sample > 19)
979                 continue;
980             QMap<int, QByteArray> frame_channel_data = baseClip()->m_audioFrameCache[(int)frame];
981
982             for (int channel = 0; channel < channels && frame_channel_data[channel].size() > 0; channel++) {
983
984                 int y = channelHeight * channel + channelHeight / 2;
985                 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2)  * channelHeight / 64;
986                 if (fullAreaDraw) {
987                     positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
988                     negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
989                 } else {
990                     positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
991                     negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
992                 }
993             }
994             for (int channel = 0; channel < channels ; channel++)
995                 if (fullAreaDraw && samples == 100) {
996                     positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
997                     negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
998                     positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
999                     negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
1000                 }
1001
1002         }
1003         pixpainter.setPen(QPen(QColor(0, 0, 0)));
1004         pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
1005
1006         for (int i = 0; i < channels; i++) {
1007             if (fullAreaDraw) {
1008                 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
1009                 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
1010             } else
1011                 pixpainter.drawPath(positiveChannelPaths[i]);
1012         }
1013     }
1014     //audioThumbWasDrawn=true;
1015     m_framePixelWidth = pixelForOneFrame;
1016
1017     //}
1018 }
1019
1020 int ClipItem::fadeIn() const
1021 {
1022     return m_startFade;
1023 }
1024
1025 int ClipItem::fadeOut() const
1026 {
1027     return m_endFade;
1028 }
1029
1030
1031 void ClipItem::setFadeIn(int pos)
1032 {
1033     if (pos == m_startFade) return;
1034     int oldIn = m_startFade;
1035     if (pos < 0) pos = 0;
1036     if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1037     m_startFade = pos;
1038     QRectF rect = boundingRect();
1039     update(rect.x(), rect.y(), qMax(oldIn, pos), rect.height());
1040 }
1041
1042 void ClipItem::setFadeOut(int pos)
1043 {
1044     if (pos == m_endFade) return;
1045     int oldOut = m_endFade;
1046     if (pos < 0) pos = 0;
1047     if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1048     m_endFade = pos;
1049     QRectF rect = boundingRect();
1050     update(rect.x() + rect.width() - qMax(oldOut, pos), rect.y(), qMax(oldOut, pos), rect.height());
1051
1052 }
1053
1054 /*
1055 //virtual
1056 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
1057 {
1058     //if (e->pos().x() < 20) m_hover = true;
1059     return;
1060     if (isItemLocked()) return;
1061     m_hover = true;
1062     QRectF r = boundingRect();
1063     double width = 35 / projectScene()->scale().x();
1064     double height = r.height() / 2;
1065     //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1066     update(r.x(), r.y() + height, width, height);
1067     update(r.right() - width, r.y() + height, width, height);
1068 }
1069
1070 //virtual
1071 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
1072 {
1073     if (isItemLocked()) return;
1074     m_hover = false;
1075     QRectF r = boundingRect();
1076     double width = 35 / projectScene()->scale().x();
1077     double height = r.height() / 2;
1078     //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1079     update(r.x(), r.y() + height, width, height);
1080     update(r.right() - width, r.y() + height, width, height);
1081 }
1082 */
1083
1084 void ClipItem::resizeStart(int posx)
1085 {
1086     const int min = (startPos() - cropStart()).frames(m_fps);
1087     if (posx < min) posx = min;
1088     if (posx == startPos().frames(m_fps)) return;
1089     const int previous = cropStart().frames(m_fps);
1090     AbstractClipItem::resizeStart(posx);
1091
1092     // set speed independant info
1093     m_speedIndependantInfo = m_info;
1094     m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1095     m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1096
1097     if ((int) cropStart().frames(m_fps) != previous) {
1098         if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1099             m_startThumbTimer.start(150);
1100         }
1101     }
1102 }
1103
1104 void ClipItem::resizeEnd(int posx)
1105 {
1106     const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps);
1107     if (posx > max && maxDuration() != GenTime()) posx = max;
1108     if (posx == endPos().frames(m_fps)) return;
1109     //kDebug() << "// NEW POS: " << posx << ", OLD END: " << endPos().frames(m_fps);
1110     const int previous = cropDuration().frames(m_fps);
1111     AbstractClipItem::resizeEnd(posx);
1112
1113     // set speed independant info
1114     m_speedIndependantInfo = m_info;
1115     m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1116     m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1117
1118     if ((int) cropDuration().frames(m_fps) != previous) {
1119         if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1120             m_endThumbTimer.start(150);
1121         }
1122     }
1123 }
1124
1125
1126 bool ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart)
1127 {
1128     bool modified = false;
1129     for (int i = 0; i < m_effectList.count(); i++) {
1130         QDomElement effect = m_effectList.at(i);
1131         QDomNodeList params = effect.elementsByTagName("parameter");
1132         for (int j = 0; j < params.count(); j++) {
1133             QDomElement e = params.item(i).toElement();
1134             if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
1135                 // parse keyframes and adjust values
1136                 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1137                 QMap <int, double> kfr;
1138                 int pos;
1139                 double val;
1140                 foreach(const QString &str, keyframes) {
1141                     pos = str.section(':', 0, 0).toInt();
1142                     val = str.section(':', 1, 1).toDouble();
1143                     if (pos == previous) {
1144                         kfr[current] = val;
1145                         modified = true;
1146                     } else {
1147                         if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
1148                             kfr[pos] = val;
1149                             modified = true;
1150                         }
1151                     }
1152                 }
1153                 if (modified) {
1154                     QString newkfr;
1155                     QMap<int, double>::const_iterator k = kfr.constBegin();
1156                     while (k != kfr.constEnd()) {
1157                         newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
1158                         ++k;
1159                     }
1160                     e.setAttribute("keyframes", newkfr);
1161                     break;
1162                 }
1163             }
1164         }
1165     }
1166     if (modified && m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
1167     return modified;
1168 }
1169
1170 //virtual
1171 QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
1172 {
1173     if (change == QGraphicsItem::ItemSelectedChange) {
1174         if (value.toBool()) setZValue(10);
1175         else setZValue(2);
1176     }
1177     if (change == ItemPositionChange && scene()) {
1178         // calculate new position.
1179         //if (parentItem()) return pos();
1180         QPointF newPos = value.toPointF();
1181         //kDebug() << "/// MOVING CLIP ITEM.------------\n++++++++++";
1182         int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
1183         xpos = qMax(xpos, 0);
1184         newPos.setX(xpos);
1185         int newTrack = newPos.y() / KdenliveSettings::trackheight();
1186         newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
1187         newTrack = qMax(newTrack, 0);
1188         newPos.setY((int)(newTrack  * KdenliveSettings::trackheight() + 1));
1189         // Only one clip is moving
1190         QRectF sceneShape = rect();
1191         sceneShape.translate(newPos);
1192         QList<QGraphicsItem*> items;
1193         if (projectScene()->editMode() == NORMALEDIT)
1194             items = scene()->items(sceneShape, Qt::IntersectsItemShape);
1195         items.removeAll(this);
1196         bool forwardMove = newPos.x() > pos().x();
1197         int offset = 0;
1198         if (!items.isEmpty()) {
1199             for (int i = 0; i < items.count(); i++) {
1200                 if (!items.at(i)->isEnabled()) continue;
1201                 if (items.at(i)->type() == type()) {
1202                     // Collision!
1203                     QPointF otherPos = items.at(i)->pos();
1204                     if ((int) otherPos.y() != (int) pos().y()) {
1205                         return pos();
1206                     }
1207                     if (forwardMove) {
1208                         offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
1209                     } else {
1210                         offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
1211                     }
1212
1213                     if (offset > 0) {
1214                         if (forwardMove) {
1215                             sceneShape.translate(QPointF(-offset, 0));
1216                             newPos.setX(newPos.x() - offset);
1217                         } else {
1218                             sceneShape.translate(QPointF(offset, 0));
1219                             newPos.setX(newPos.x() + offset);
1220                         }
1221                         QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
1222                         subitems.removeAll(this);
1223                         for (int j = 0; j < subitems.count(); j++) {
1224                             if (!subitems.at(j)->isEnabled()) continue;
1225                             if (subitems.at(j)->type() == type()) {
1226                                 // move was not successful, revert to previous pos
1227                                 m_info.startPos = GenTime((int) pos().x(), m_fps);
1228                                 return pos();
1229                             }
1230                         }
1231                     }
1232
1233                     m_info.track = newTrack;
1234                     m_info.startPos = GenTime((int) newPos.x(), m_fps);
1235
1236                     return newPos;
1237                 }
1238             }
1239         }
1240         m_info.track = newTrack;
1241         m_info.startPos = GenTime((int) newPos.x(), m_fps);
1242         //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
1243         return newPos;
1244     }
1245     return QGraphicsItem::itemChange(change, value);
1246 }
1247
1248 // virtual
1249 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
1250 }*/
1251
1252 int ClipItem::effectsCounter()
1253 {
1254     return effectsCount() + 1;
1255 }
1256
1257 int ClipItem::effectsCount()
1258 {
1259     return m_effectList.count();
1260 }
1261
1262 int ClipItem::hasEffect(const QString &tag, const QString &id) const
1263 {
1264     return m_effectList.hasEffect(tag, id);
1265 }
1266
1267 QStringList ClipItem::effectNames()
1268 {
1269     return m_effectList.effectNames();
1270 }
1271
1272 QDomElement ClipItem::effectAt(int ix) const
1273 {
1274     if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1275     return m_effectList.at(ix).cloneNode().toElement();
1276 }
1277
1278 QDomElement ClipItem::getEffectAt(int ix) const
1279 {
1280     if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1281     return m_effectList.at(ix);
1282 }
1283
1284 void ClipItem::setEffectAt(int ix, QDomElement effect)
1285 {
1286     if (ix < 0 || ix > (m_effectList.count() - 1) || effect.isNull()) {
1287         kDebug() << "Invalid effect index: " << ix;
1288         return;
1289     }
1290     //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
1291     effect.setAttribute("kdenlive_ix", ix + 1);
1292     m_effectList.replace(ix, effect);
1293     m_effectNames = m_effectList.effectNames().join(" / ");
1294     QString id = effect.attribute("id");
1295     if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
1296         update();
1297     else {
1298         QRectF r = boundingRect();
1299         r.setHeight(20);
1300         update(r);
1301     }
1302 }
1303
1304 EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animate*/)
1305 {
1306     bool needRepaint = false;
1307     int ix;
1308     if (!effect.hasAttribute("kdenlive_ix")) {
1309         ix = effectsCounter();
1310     } else ix = effect.attribute("kdenlive_ix").toInt();
1311     if (!m_effectList.isEmpty() && ix <= m_effectList.count()) {
1312         needRepaint = true;
1313         m_effectList.insert(ix - 1, effect);
1314         for (int i = ix; i < m_effectList.count(); i++) {
1315             int index = m_effectList.item(i).attribute("kdenlive_ix").toInt();
1316             if (index >= ix) m_effectList.item(i).setAttribute("kdenlive_ix", index + 1);
1317         }
1318     } else m_effectList.append(effect);
1319     EffectsParameterList parameters;
1320     parameters.addParam("tag", effect.attribute("tag"));
1321     parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1322     if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1323     if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1324
1325
1326     QString effectId = effect.attribute("id");
1327     if (effectId.isEmpty()) effectId = effect.attribute("tag");
1328     parameters.addParam("id", effectId);
1329
1330     QDomNodeList params = effect.elementsByTagName("parameter");
1331     int fade = 0;
1332     for (int i = 0; i < params.count(); i++) {
1333         QDomElement e = params.item(i).toElement();
1334         if (!e.isNull()) {
1335             if (e.attribute("type") == "simplekeyframe") {
1336                 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1337                 double factor = e.attribute("factor", "1").toDouble();
1338                 if (factor != 1) {
1339                     for (int j = 0; j < values.count(); j++) {
1340                         QString pos = values.at(j).section(":", 0, 0);
1341                         double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1342                         values[j] = pos + "=" + QString::number(val);
1343                     }
1344                 }
1345                 parameters.addParam(e.attribute("name"), values.join(";"));
1346                 /*parameters.addParam("max", e.attribute("max"));
1347                 parameters.addParam("min", e.attribute("min"));
1348                 parameters.addParam("factor", );*/
1349             } else if (e.attribute("type") == "keyframe") {
1350                 parameters.addParam("keyframes", e.attribute("keyframes"));
1351                 parameters.addParam("max", e.attribute("max"));
1352                 parameters.addParam("min", e.attribute("min"));
1353                 parameters.addParam("factor", e.attribute("factor", "1"));
1354                 parameters.addParam("starttag", e.attribute("starttag", "start"));
1355                 parameters.addParam("endtag", e.attribute("endtag", "end"));
1356             } else if (e.attribute("factor", "1") == "1") {
1357                 parameters.addParam(e.attribute("name"), e.attribute("value"));
1358
1359                 // check if it is a fade effect
1360                 if (effectId == "fadein") {
1361                     needRepaint = true;
1362                     if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
1363                         if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1364                         else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1365                     } else {
1366                         QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
1367                         if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1368                         else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1369                     }
1370                 } else if (effectId == "fade_from_black") {
1371                     needRepaint = true;
1372                     if (m_effectList.hasEffect(QString(), "fadein") == -1) {
1373                         if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1374                         else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1375                     } else {
1376                         QDomElement fadein = m_effectList.getEffectByTag(QString(), "fadein");
1377                         if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1378                         else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1379                     }
1380                 } else if (effectId == "fadeout") {
1381                     needRepaint = true;
1382                     if (m_effectList.hasEffect(QString(), "fade_to_black") == -1) {
1383                         if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1384                         else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1385                     } else {
1386                         QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
1387                         if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1388                         else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1389                     }
1390                 } else if (effectId == "fade_to_black") {
1391                     needRepaint = true;
1392                     if (m_effectList.hasEffect(QString(), "fadeout") == -1) {
1393                         if (e.attribute("name") == "out") fade -= e.attribute("value").toInt();
1394                         else if (e.attribute("name") == "in") fade += e.attribute("value").toInt();
1395                     } else {
1396                         QDomElement fadeout = m_effectList.getEffectByTag(QString(), "fadeout");
1397                         if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1398                         else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1399                     }
1400                 }
1401             } else {
1402                 double fact;
1403                 if (e.attribute("factor").startsWith('%')) {
1404                     fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1405                 } else fact = e.attribute("factor", "1").toDouble();
1406                 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1407             }
1408         }
1409     }
1410     m_effectNames = m_effectList.effectNames().join(" / ");
1411     if (fade > 0) m_startFade = fade;
1412     else if (fade < 0) m_endFade = -fade;
1413
1414     if (m_selectedEffect == -1) {
1415         setSelectedEffect(0);
1416     } else if (m_selectedEffect == ix - 1) setSelectedEffect(m_selectedEffect);
1417     if (needRepaint) update(boundingRect());
1418     /*if (animate) {
1419         flashClip();
1420     } */
1421     else { /*if (!needRepaint) */
1422         QRectF r = boundingRect();
1423         r.setHeight(20);
1424         update(r);
1425     }
1426     return parameters;
1427 }
1428
1429 EffectsParameterList ClipItem::getEffectArgs(const QDomElement effect)
1430 {
1431     EffectsParameterList parameters;
1432     parameters.addParam("tag", effect.attribute("tag"));
1433     parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1434     parameters.addParam("id", effect.attribute("id"));
1435     if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1436     if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
1437
1438     QDomNodeList params = effect.elementsByTagName("parameter");
1439     for (int i = 0; i < params.count(); i++) {
1440         QDomElement e = params.item(i).toElement();
1441         //kDebug() << "/ / / /SENDING EFFECT PARAM: " << e.attribute("type") << ", NAME_ " << e.attribute("tag");
1442         if (e.attribute("type") == "simplekeyframe") {
1443
1444             QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1445             double factor = e.attribute("factor", "1").toDouble();
1446             for (int j = 0; j < values.count(); j++) {
1447                 QString pos = values.at(j).section(":", 0, 0);
1448                 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1449                 values[j] = pos + "=" + QString::number(val);
1450             }
1451             // kDebug() << "/ / / /SENDING KEYFR:" << values;
1452             parameters.addParam(e.attribute("name"), values.join(";"));
1453             /*parameters.addParam(e.attribute("name"), e.attribute("keyframes").replace(":", "="));
1454             parameters.addParam("max", e.attribute("max"));
1455             parameters.addParam("min", e.attribute("min"));
1456             parameters.addParam("factor", e.attribute("factor", "1"));*/
1457         } else if (e.attribute("type") == "keyframe") {
1458             kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
1459             parameters.addParam("keyframes", e.attribute("keyframes"));
1460             parameters.addParam("max", e.attribute("max"));
1461             parameters.addParam("min", e.attribute("min"));
1462             parameters.addParam("factor", e.attribute("factor", "1"));
1463             parameters.addParam("starttag", e.attribute("starttag", "start"));
1464             parameters.addParam("endtag", e.attribute("endtag", "end"));
1465         } else if (e.attribute("namedesc").contains(';')) {
1466             QString format = e.attribute("format");
1467             QStringList separators = format.split("%d", QString::SkipEmptyParts);
1468             QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
1469             QString neu;
1470             QTextStream txtNeu(&neu);
1471             if (values.size() > 0)
1472                 txtNeu << (int)values[0].toDouble();
1473             for (int i = 0; i < separators.size() && i + 1 < values.size(); i++) {
1474                 txtNeu << separators[i];
1475                 txtNeu << (int)(values[i+1].toDouble());
1476             }
1477             parameters.addParam("start", neu);
1478         } else {
1479             if (e.attribute("factor", "1") != "1") {
1480                 double fact;
1481                 if (e.attribute("factor").startsWith('%')) {
1482                     fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1483                 } else fact = e.attribute("factor", "1").toDouble();
1484                 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1485             } else {
1486                 parameters.addParam(e.attribute("name"), e.attribute("value"));
1487             }
1488         }
1489     }
1490     return parameters;
1491 }
1492
1493 void ClipItem::deleteEffect(QString index)
1494 {
1495     bool needRepaint = false;
1496     QString ix;
1497
1498     for (int i = 0; i < m_effectList.count(); ++i) {
1499         ix = m_effectList.at(i).attribute("kdenlive_ix");
1500         if (ix == index) {
1501             QString effectId = m_effectList.at(i).attribute("id");
1502             if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
1503                     (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
1504                 m_startFade = 0;
1505                 needRepaint = true;
1506             } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
1507                        (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
1508                 m_endFade = 0;
1509                 needRepaint = true;
1510             } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
1511             m_effectList.removeAt(i);
1512             i--;
1513         } else if (ix.toInt() > index.toInt()) {
1514             m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
1515         }
1516     }
1517     m_effectNames = m_effectList.effectNames().join(" / ");
1518
1519     if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
1520         // Current effect was removed
1521         if (index.toInt() > m_effectList.count() - 1) {
1522             setSelectedEffect(m_effectList.count() - 1);
1523         } else setSelectedEffect(index.toInt());
1524     }
1525     if (needRepaint) update(boundingRect());
1526     else {
1527         QRectF r = boundingRect();
1528         r.setHeight(20);
1529         update(r);
1530     }
1531     //if (!m_effectList.isEmpty()) flashClip();
1532 }
1533
1534 double ClipItem::speed() const
1535 {
1536     return m_speed;
1537 }
1538
1539 int ClipItem::strobe() const
1540 {
1541     return m_strobe;
1542 }
1543
1544 void ClipItem::setSpeed(const double speed, const int strobe)
1545 {
1546     m_speed = speed;
1547     m_strobe = strobe;
1548     if (m_speed == 1.0) m_clipName = baseClip()->name();
1549     else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + '%';
1550     m_info.cropStart = GenTime((int)(m_speedIndependantInfo.cropStart.frames(m_fps) / m_speed + 0.5), m_fps);
1551     m_info.cropDuration = GenTime((int)(m_speedIndependantInfo.cropDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1552     //update();
1553 }
1554
1555 GenTime ClipItem::maxDuration() const
1556 {
1557     return GenTime((int)(m_maxDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1558 }
1559
1560 GenTime ClipItem::speedIndependantCropStart() const
1561 {
1562     return m_speedIndependantInfo.cropStart;
1563 }
1564
1565 GenTime ClipItem::speedIndependantCropDuration() const
1566 {
1567     return m_speedIndependantInfo.cropDuration;
1568 }
1569
1570
1571 const ItemInfo ClipItem::speedIndependantInfo() const
1572 {
1573     return m_speedIndependantInfo;
1574 }
1575
1576 //virtual
1577 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
1578 {
1579     const QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
1580     QDomDocument doc;
1581     doc.setContent(effects, true);
1582     const QDomElement e = doc.documentElement();
1583     if (scene() && !scene()->views().isEmpty()) {
1584         event->accept();
1585         CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1586         if (view) view->slotAddEffect(e, m_info.startPos, track());
1587     }
1588 }
1589
1590 //virtual
1591 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
1592 {
1593     if (isItemLocked()) event->setAccepted(false);
1594     else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1595 }
1596
1597 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
1598 {
1599     Q_UNUSED(event);
1600 }
1601
1602 void ClipItem::addTransition(Transition* t)
1603 {
1604     m_transitionsList.append(t);
1605     //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1606     QDomDocument doc;
1607     QDomElement e = doc.documentElement();
1608     //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1609 }
1610
1611 void ClipItem::setVideoOnly(bool force)
1612 {
1613     m_videoOnly = force;
1614 }
1615
1616 void ClipItem::setAudioOnly(bool force)
1617 {
1618     m_audioOnly = force;
1619     if (m_audioOnly) m_baseColor = QColor(141, 215, 166);
1620     else {
1621         if (m_clipType == COLOR) {
1622             QString colour = m_clip->getProperty("colour");
1623             colour = colour.replace(0, 2, "#");
1624             m_baseColor = QColor(colour.left(7));
1625         } else if (m_clipType == AUDIO) m_baseColor = QColor(141, 215, 166);
1626         else m_baseColor = QColor(141, 166, 215);
1627     }
1628     m_audioThumbCachePic.clear();
1629 }
1630
1631 bool ClipItem::isAudioOnly() const
1632 {
1633     return m_audioOnly;
1634 }
1635
1636 bool ClipItem::isVideoOnly() const
1637 {
1638     return m_videoOnly;
1639 }
1640
1641 void ClipItem::insertKeyframe(QDomElement effect, int pos, int val)
1642 {
1643     if (effect.attribute("disable") == "1") return;
1644     effect.setAttribute("active_keyframe", pos);
1645     m_editedKeyframe = pos;
1646     QDomNodeList params = effect.elementsByTagName("parameter");
1647     for (int i = 0; i < params.count(); i++) {
1648         QDomElement e = params.item(i).toElement();
1649         QString kfr = e.attribute("keyframes");
1650         const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1651         QStringList newkfr;
1652         bool added = false;
1653         foreach(const QString &str, keyframes) {
1654             int kpos = str.section(':', 0, 0).toInt();
1655             double newval = str.section(':', 1, 1).toDouble();
1656             if (kpos < pos) {
1657                 newkfr.append(str);
1658             } else if (!added) {
1659                 if (i == 0) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1660                 else newkfr.append(QString::number(pos) + ":" + QString::number(newval));
1661                 if (kpos > pos) newkfr.append(str);
1662                 added = true;
1663             } else newkfr.append(str);
1664         }
1665         if (!added) newkfr.append(QString::number(pos) + ":" + QString::number(val));
1666         e.setAttribute("keyframes", newkfr.join(";"));
1667     }
1668 }
1669
1670 void ClipItem::movedKeyframe(QDomElement effect, int oldpos, int newpos, double value)
1671 {
1672     if (effect.attribute("disable") == "1") return;
1673     effect.setAttribute("active_keyframe", newpos);
1674     QDomNodeList params = effect.elementsByTagName("parameter");
1675     int start = cropStart().frames(m_fps);
1676     int end = (cropStart() + cropDuration()).frames(m_fps) - 1;
1677     for (int i = 0; i < params.count(); i++) {
1678         QDomElement e = params.item(i).toElement();
1679         QString kfr = e.attribute("keyframes");
1680         const QStringList keyframes = kfr.split(';', QString::SkipEmptyParts);
1681         QStringList newkfr;
1682         foreach(const QString &str, keyframes) {
1683             if (str.section(':', 0, 0).toInt() != oldpos) {
1684                 newkfr.append(str);
1685             } else if (newpos != -1) {
1686                 newpos = qMax(newpos, start);
1687                 newpos = qMin(newpos, end);
1688                 if (i == 0) newkfr.append(QString::number(newpos) + ":" + QString::number(value));
1689                 else newkfr.append(QString::number(newpos) + ":" + str.section(':', 1, 1));
1690             }
1691         }
1692         e.setAttribute("keyframes", newkfr.join(";"));
1693     }
1694
1695     updateKeyframes(effect);
1696     update();
1697 }
1698
1699 void ClipItem::updateKeyframes(QDomElement effect)
1700 {
1701     m_keyframes.clear();
1702     // parse keyframes
1703     QDomNodeList params = effect.elementsByTagName("parameter");
1704     QDomElement e = params.item(0).toElement();
1705     const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1706     foreach(const QString &str, keyframes) {
1707         int pos = str.section(':', 0, 0).toInt();
1708         double val = str.section(':', 1, 1).toDouble();
1709         m_keyframes[pos] = val;
1710     }
1711     if (!m_keyframes.contains(m_selectedKeyframe)) m_selectedKeyframe = -1;
1712 }
1713 #include "clipitem.moc"