]> git.sesse.net Git - kdenlive/blob - src/clipitem.cpp
Fix crash on clip deletion, fix issues with placeholder clips
[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("disabled") == "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                 m_editedKeyframe = -1;
331                 // parse keyframes
332                 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
333                 foreach(const QString &str, keyframes) {
334                     int pos = str.section(':', 0, 0).toInt();
335                     double val = str.section(':', 1, 1).toDouble();
336                     m_keyframes[pos] = val;
337                 }
338                 update();
339                 return;
340             }
341             break;
342         }
343     }
344 }
345
346
347 void ClipItem::setSelectedEffect(const int ix)
348 {
349     m_selectedEffect = ix;
350     QDomElement effect = effectAt(m_selectedEffect);
351     if (effect.isNull() == false) {
352         QDomNodeList params = effect.elementsByTagName("parameter");
353         if (effect.attribute("disabled") != "1")
354             for (int i = 0; i < params.count(); i++) {
355                 QDomElement e = params.item(i).toElement();
356                 if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
357                     m_keyframes.clear();
358                     double max = e.attribute("max").toDouble();
359                     double min = e.attribute("min").toDouble();
360                     m_keyframeFactor = 100.0 / (max - min);
361                     m_keyframeDefault = e.attribute("default").toDouble();
362                     m_selectedKeyframe = 0;
363                     m_editedKeyframe = -1;
364                     // parse keyframes
365                     const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
366                     foreach(const QString &str, keyframes) {
367                         int pos = str.section(':', 0, 0).toInt();
368                         double val = str.section(':', 1, 1).toDouble();
369                         m_keyframes[pos] = val;
370                     }
371                     update();
372                     return;
373                 }
374             }
375     }
376     if (!m_keyframes.isEmpty()) {
377         m_keyframes.clear();
378         update();
379     }
380 }
381
382 QString ClipItem::keyframes(const int index)
383 {
384     QString result;
385     QDomElement effect = effectAt(index);
386     QDomNodeList params = effect.elementsByTagName("parameter");
387
388     for (int i = 0; i < params.count(); i++) {
389         QDomElement e = params.item(i).toElement();
390         if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
391             result = e.attribute("keyframes");
392             break;
393         }
394     }
395     return result;
396 }
397
398 void ClipItem::updateKeyframeEffect()
399 {
400     // regenerate xml parameter from the clip keyframes
401     QDomElement effect = getEffectAt(m_selectedEffect);
402     if (effect.attribute("disabled") == "1") return;
403     QDomNodeList params = effect.elementsByTagName("parameter");
404
405     for (int i = 0; i < params.count(); i++) {
406         QDomElement e = params.item(i).toElement();
407         if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
408             QString keyframes;
409             if (m_keyframes.count() > 0) {
410                 QMap<int, int>::const_iterator i = m_keyframes.constBegin();
411                 while (i != m_keyframes.constEnd()) {
412                     keyframes.append(QString::number(i.key()) + ':' + QString::number(i.value()) + ';');
413                     ++i;
414                 }
415             }
416             // Effect has a keyframe type parameter, we need to set the values
417             //kDebug() << ":::::::::::::::   SETTING EFFECT KEYFRAMES: " << keyframes;
418             e.setAttribute("keyframes", keyframes);
419             break;
420         }
421     }
422 }
423
424 QDomElement ClipItem::selectedEffect()
425 {
426     if (m_selectedEffect == -1 || m_effectList.isEmpty()) return QDomElement();
427     return effectAt(m_selectedEffect);
428 }
429
430 void ClipItem::resetThumbs(bool clearExistingThumbs)
431 {
432     if (clearExistingThumbs) {
433         m_startPix = QPixmap();
434         m_endPix = QPixmap();
435         m_audioThumbCachePic.clear();
436     }
437     slotFetchThumbs();
438 }
439
440
441 void ClipItem::refreshClip(bool checkDuration)
442 {
443     if (checkDuration && (m_maxDuration != m_clip->maxDuration())) {
444         m_maxDuration = m_clip->maxDuration();
445         if (m_clipType != IMAGE && m_clipType != TEXT && m_clipType != COLOR) {
446             if (m_maxDuration != GenTime() && m_info.cropStart + m_info.cropDuration > m_maxDuration) {
447                 // Clip duration changed, make sure to stay in correct range
448                 if (m_info.cropStart > m_maxDuration) {
449                     m_info.cropStart = GenTime();
450                     m_info.cropDuration = qMin(m_info.cropDuration, m_maxDuration);
451                     updateRectGeometry();
452                 } else {
453                     m_info.cropDuration = m_maxDuration;
454                     updateRectGeometry();
455                 }
456             }
457         }
458     }
459     if (m_clipType == COLOR) {
460         QString colour = m_clip->getProperty("colour");
461         colour = colour.replace(0, 2, "#");
462         m_baseColor = QColor(colour.left(7));
463     } else resetThumbs(checkDuration);
464 }
465
466 void ClipItem::slotFetchThumbs()
467 {
468     if (scene() == NULL || m_clipType == AUDIO || m_clipType == COLOR) return;
469     if (m_clipType == IMAGE) {
470         if (m_startPix.isNull()) {
471             m_startPix = KThumb::getImage(KUrl(m_clip->getProperty("resource")), (int)(KdenliveSettings::trackheight() * KdenliveSettings::project_display_ratio()), KdenliveSettings::trackheight());
472             update();
473         }
474         return;
475     }
476
477     if (m_clipType == TEXT) {
478         if (m_startPix.isNull()) slotGetStartThumb();
479         return;
480     }
481
482     if (m_endPix.isNull() && m_startPix.isNull()) {
483         m_startThumbRequested = true;
484         m_endThumbRequested = true;
485         emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
486     } else {
487         if (m_endPix.isNull()) {
488             slotGetEndThumb();
489         }
490         if (m_startPix.isNull()) {
491             slotGetStartThumb();
492         }
493     }
494 }
495
496 void ClipItem::slotGetStartThumb()
497 {
498     m_startThumbRequested = true;
499     emit getThumb((int)m_speedIndependantInfo.cropStart.frames(m_fps), -1);
500 }
501
502 void ClipItem::slotGetEndThumb()
503 {
504     m_endThumbRequested = true;
505     emit getThumb(-1, (int)(m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1);
506 }
507
508
509 void ClipItem::slotSetStartThumb(QImage img)
510 {
511     if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
512         QPixmap pix = QPixmap::fromImage(img);
513         m_startPix = pix;
514         QRectF r = sceneBoundingRect();
515         r.setRight(pix.width() + 2);
516         update(r);
517     }
518 }
519
520 void ClipItem::slotSetEndThumb(QImage img)
521 {
522     if (!img.isNull() && img.format() == QImage::Format_ARGB32) {
523         QPixmap pix = QPixmap::fromImage(img);
524         m_endPix = pix;
525         QRectF r = sceneBoundingRect();
526         r.setLeft(r.right() - pix.width() - 2);
527         update(r);
528     }
529 }
530
531 void ClipItem::slotThumbReady(int frame, QImage img)
532 {
533     if (scene() == NULL) return;
534     QRectF r = boundingRect();
535     QPixmap pix = QPixmap::fromImage(img);
536     double width = pix.width() / projectScene()->scale().x();
537     if (m_startThumbRequested && frame == m_speedIndependantInfo.cropStart.frames(m_fps)) {
538         m_startPix = pix;
539         m_startThumbRequested = false;
540         update(r.left(), r.top(), width, pix.height());
541         if (m_clipType == IMAGE || m_clipType == TEXT) {
542             update(r.right() - width, r.top(), width, pix.height());
543         }
544     } else if (m_endThumbRequested && frame == (m_speedIndependantInfo.cropStart + m_speedIndependantInfo.cropDuration).frames(m_fps) - 1) {
545         m_endPix = pix;
546         m_endThumbRequested = false;
547         update(r.right() - width, r.top(), width, pix.height());
548     }
549 }
550
551 void ClipItem::slotSetStartThumb(const QPixmap pix)
552 {
553     m_startPix = pix;
554 }
555
556 void ClipItem::slotSetEndThumb(const QPixmap pix)
557 {
558     m_endPix = pix;
559 }
560
561 QPixmap ClipItem::startThumb() const
562 {
563     return m_startPix;
564 }
565
566 QPixmap ClipItem::endThumb() const
567 {
568     return m_endPix;
569 }
570
571 void ClipItem::slotGotAudioData()
572 {
573     m_audioThumbReady = true;
574     if (m_clipType == AV && !isAudioOnly()) {
575         QRectF r = boundingRect();
576         r.setTop(r.top() + r.height() / 2 - 1);
577         update(r);
578     } else update();
579 }
580
581 int ClipItem::type() const
582 {
583     return AVWIDGET;
584 }
585
586 DocClipBase *ClipItem::baseClip() const
587 {
588     return m_clip;
589 }
590
591 QDomElement ClipItem::xml() const
592 {
593     QDomElement xml = m_clip->toXML();
594     if (m_speed != 1.0) xml.setAttribute("speed", m_speed);
595     if (m_strobe > 1) xml.setAttribute("strobe", m_strobe);
596     if (m_audioOnly) xml.setAttribute("audio_only", 1);
597     else if (m_videoOnly) xml.setAttribute("video_only", 1);
598     return xml;
599 }
600
601 int ClipItem::clipType() const
602 {
603     return m_clipType;
604 }
605
606 QString ClipItem::clipName() const
607 {
608     return m_clipName;
609 }
610
611 void ClipItem::setClipName(const QString &name)
612 {
613     m_clipName = name;
614 }
615
616 const QString ClipItem::clipProducer() const
617 {
618     return m_producer;
619 }
620
621 void ClipItem::flashClip()
622 {
623     if (m_timeLine == 0) {
624         m_timeLine = new QTimeLine(750, this);
625         m_timeLine->setUpdateInterval(80);
626         m_timeLine->setCurveShape(QTimeLine::EaseInOutCurve);
627         m_timeLine->setFrameRange(0, 100);
628         connect(m_timeLine, SIGNAL(valueChanged(qreal)), this, SLOT(animate(qreal)));
629     }
630     //m_timeLine->start();
631 }
632
633 void ClipItem::animate(qreal /*value*/)
634 {
635     QRectF r = boundingRect();
636     r.setHeight(20);
637     update(r);
638 }
639
640 // virtual
641 void ClipItem::paint(QPainter *painter,
642                      const QStyleOptionGraphicsItem *option,
643                      QWidget *)
644 {
645     QColor paintColor;
646     if (parentItem()) paintColor = QColor(255, 248, 149);
647     else paintColor = m_baseColor;
648     if (isSelected() || (parentItem() && parentItem()->isSelected())) paintColor = paintColor.darker();
649
650     painter->setMatrixEnabled(false);
651     const QRectF mapped = painter->matrix().mapRect(rect()).adjusted(0.5, 0, 0.5, 0);
652     const QRectF exposed = option->exposedRect;
653     painter->setClipRect(mapped);
654     painter->fillRect(mapped, paintColor);
655
656     // draw thumbnails
657     if (KdenliveSettings::videothumbnails() && !isAudioOnly()) {
658         QPen pen = painter->pen();
659         pen.setColor(QColor(255, 255, 255, 150));
660         const QRectF source(0.0, 0.0, (double) m_startPix.width(), (double) m_startPix.height());
661         painter->setPen(pen);
662         if ((m_clipType == IMAGE || m_clipType == TEXT) && !m_startPix.isNull()) {
663             const QPointF top = mapped.topRight() - QPointF(m_startPix.width() - 1, 0);
664             painter->drawPixmap(top, m_startPix);
665             QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
666             painter->drawLine(l2);
667         } else if (!m_endPix.isNull()) {
668             const QPointF top = mapped.topRight() - QPointF(m_endPix.width() - 1, 0);
669             painter->drawPixmap(top, m_endPix);
670             QLineF l2(top.x(), mapped.top(), top.x(), mapped.bottom());
671             painter->drawLine(l2);
672         }
673         if (!m_startPix.isNull()) {
674             painter->drawPixmap(mapped.topLeft(), m_startPix);
675             QLineF l2(mapped.left() + m_startPix.width(), mapped.top(), mapped.left() + m_startPix.width(), mapped.bottom());
676             painter->drawLine(l2);
677         }
678         painter->setPen(Qt::black);
679     }
680
681     // draw audio thumbnails
682     if (KdenliveSettings::audiothumbnails() && m_speed == 1.0 && !isVideoOnly() && ((m_clipType == AV && (exposed.bottom() > (rect().height() / 2) || isAudioOnly())) || m_clipType == AUDIO) && m_audioThumbReady) {
683
684         double startpixel = exposed.left();
685         if (startpixel < 0)
686             startpixel = 0;
687         double endpixel = exposed.right();
688         if (endpixel < 0)
689             endpixel = 0;
690         //kDebug()<<"///  REPAINTING AUDIO THMBS ZONE: "<<startpixel<<"x"<<endpixel;
691
692         /*QPainterPath path = m_clipType == AV ? roundRectPathLower : resultClipPath;*/
693         QRectF mappedRect;
694         if (m_clipType == AV && !isAudioOnly()) {
695             mappedRect = mapped;
696             mappedRect.setTop(mappedRect.bottom() - mapped.height() / 2);
697         } else mappedRect = mapped;
698
699         double scale = painter->matrix().m11();
700         int channels = baseClip()->getProperty("channels").toInt();
701         if (scale != m_framePixelWidth)
702             m_audioThumbCachePic.clear();
703         double cropLeft = m_info.cropStart.frames(m_fps);
704         const int clipStart = mappedRect.x();
705         const int mappedStartPixel =  painter->matrix().map(QPointF(startpixel + cropLeft, 0)).x() - clipStart;
706         const int mappedEndPixel =  painter->matrix().map(QPointF(endpixel + cropLeft, 0)).x() - clipStart;
707         cropLeft = cropLeft * scale;
708
709         if (channels >= 1) {
710             emit prepareAudioThumb(scale, mappedStartPixel, mappedEndPixel, channels);
711         }
712
713         for (int startCache = mappedStartPixel - (mappedStartPixel) % 100; startCache < mappedEndPixel; startCache += 100) {
714             if (m_audioThumbCachePic.contains(startCache) && !m_audioThumbCachePic[startCache].isNull())
715                 painter->drawPixmap(clipStart + startCache - cropLeft, mappedRect.y(),  m_audioThumbCachePic[startCache]);
716         }
717     }
718
719     // Draw effects names
720     if (!m_effectNames.isEmpty() && mapped.width() > 40) {
721         QRectF txtBounding = painter->boundingRect(mapped, Qt::AlignLeft | Qt::AlignTop, m_effectNames);
722         QColor bgColor;
723         if (m_timeLine && m_timeLine->state() == QTimeLine::Running) {
724             qreal value = m_timeLine->currentValue();
725             txtBounding.setWidth(txtBounding.width() * value);
726             bgColor.setRgb(50 + 200 *(1.0 - value), 50, 50, 100 + 50 * value);
727         } else bgColor.setRgb(50, 50, 90, 180);
728
729         QPainterPath rounded;
730         rounded.moveTo(txtBounding.bottomRight());
731         rounded.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
732         rounded.lineTo(txtBounding.topLeft());
733         rounded.lineTo(txtBounding.bottomLeft());
734         painter->fillPath(rounded, bgColor);
735         painter->setPen(Qt::lightGray);
736         painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_effectNames);
737     }
738
739     // Draw clip name
740     QColor frameColor(paintColor.darker());
741     if (isSelected() || (parentItem() && parentItem()->isSelected())) {
742         frameColor = QColor(Qt::red);
743     }
744     frameColor.setAlpha(160);
745
746     const QRectF txtBounding2 = painter->boundingRect(mapped, Qt::AlignHCenter | Qt::AlignVCenter, ' ' + m_clipName + ' ');
747     //painter->fillRect(txtBounding2, frameColor);
748     painter->setBrush(frameColor);
749     painter->setPen(Qt::NoPen);
750     painter->drawRoundedRect(txtBounding2, 3, 3);
751     painter->setBrush(QBrush(Qt::NoBrush));
752
753     //painter->setPen(QColor(0, 0, 0, 180));
754     //painter->drawText(txtBounding, Qt::AlignCenter, m_clipName);
755     if (m_videoOnly) {
756         painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_videoPix);
757     } else if (m_audioOnly) {
758         painter->drawPixmap(txtBounding2.topLeft() - QPointF(17, -1), m_audioPix);
759     }
760     painter->setPen(Qt::white);
761     painter->drawText(txtBounding2, Qt::AlignCenter, m_clipName);
762
763
764     // draw markers
765     if (isEnabled() && m_clip) {
766         QList < CommentedTime > markers = m_clip->commentedSnapMarkers();
767         QList < CommentedTime >::Iterator it = markers.begin();
768         GenTime pos;
769         double framepos;
770         QBrush markerBrush(QColor(120, 120, 0, 140));
771         QPen pen = painter->pen();
772         pen.setColor(QColor(255, 255, 255, 200));
773         pen.setStyle(Qt::DotLine);
774
775         for (; it != markers.end(); ++it) {
776             pos = GenTime((int)((*it).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
777             if (pos > GenTime()) {
778                 if (pos > cropDuration()) break;
779                 QLineF l(rect().x() + pos.frames(m_fps), rect().y(), rect().x() + pos.frames(m_fps), rect().bottom());
780                 QLineF l2 = painter->matrix().map(l);
781                 painter->setPen(pen);
782                 painter->drawLine(l2);
783                 if (KdenliveSettings::showmarkers()) {
784                     framepos = rect().x() + pos.frames(m_fps);
785                     const QRectF r1(framepos + 0.04, 10, rect().width() - framepos - 2, rect().height() - 10);
786                     const QRectF r2 = painter->matrix().mapRect(r1);
787                     const QRectF txtBounding3 = painter->boundingRect(r2, Qt::AlignLeft | Qt::AlignTop, ' ' + (*it).comment() + ' ');
788                     painter->setBrush(markerBrush);
789                     painter->setPen(Qt::NoPen);
790                     painter->drawRoundedRect(txtBounding3, 3, 3);
791                     painter->setBrush(QBrush(Qt::NoBrush));
792                     painter->setPen(Qt::white);
793                     painter->drawText(txtBounding3, Qt::AlignCenter, (*it).comment());
794                 }
795                 //painter->fillRect(QRect(br.x() + framepos, br.y(), 10, br.height()), QBrush(QColor(0, 0, 0, 150)));
796             }
797         }
798     }
799
800     // draw start / end fades
801     QBrush fades;
802     if (isSelected()) {
803         fades = QBrush(QColor(200, 50, 50, 150));
804     } else fades = QBrush(QColor(200, 200, 200, 200));
805
806     if (m_startFade != 0) {
807         QPainterPath fadeInPath;
808         fadeInPath.moveTo(0, 0);
809         fadeInPath.lineTo(0, rect().height());
810         fadeInPath.lineTo(m_startFade, 0);
811         fadeInPath.closeSubpath();
812         QPainterPath f1 = painter->matrix().map(fadeInPath);
813         painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
814         /*if (isSelected()) {
815             QLineF l(m_startFade * scale, 0, 0, itemHeight);
816             painter->drawLine(l);
817         }*/
818     }
819     if (m_endFade != 0) {
820         QPainterPath fadeOutPath;
821         fadeOutPath.moveTo(rect().width(), 0);
822         fadeOutPath.lineTo(rect().width(), rect().height());
823         fadeOutPath.lineTo(rect().width() - m_endFade, 0);
824         fadeOutPath.closeSubpath();
825         QPainterPath f1 = painter->matrix().map(fadeOutPath);
826         painter->fillPath(f1/*.intersected(resultClipPath)*/, fades);
827         /*if (isSelected()) {
828             QLineF l(itemWidth - m_endFade * scale, 0, itemWidth, itemHeight);
829             painter->drawLine(l);
830         }*/
831     }
832
833
834     painter->setPen(QPen(Qt::lightGray));
835     // draw effect or transition keyframes
836     if (mapped.width() > 20) drawKeyFrames(painter, exposed);
837
838     //painter->setMatrixEnabled(true);
839
840     // draw clip border
841     // expand clip rect to allow correct painting of clip border
842     QPen pen1(frameColor);
843     painter->setPen(pen1);
844     painter->setClipping(false);
845     painter->drawRect(painter->matrix().mapRect(rect()));
846 }
847
848
849 OPERATIONTYPE ClipItem::operationMode(QPointF pos)
850 {
851     if (isItemLocked()) return NONE;
852     const double scale = projectScene()->scale().x();
853     double maximumOffset = 6 / scale;
854     if (isSelected() || (parentItem() && parentItem()->isSelected())) {
855         m_editedKeyframe = mouseOverKeyFrames(pos, maximumOffset);
856         if (m_editedKeyframe != -1) return KEYFRAME;
857     }
858     QRectF rect = sceneBoundingRect();
859     int addtransitionOffset = 10;
860     // Don't allow add transition if track height is very small
861     if (rect.height() < 30) addtransitionOffset = 0;
862
863     if (qAbs((int)(pos.x() - (rect.x() + m_startFade))) < maximumOffset  && qAbs((int)(pos.y() - rect.y())) < 6) {
864         if (m_startFade == 0) setToolTip(i18n("Add audio fade"));
865         // xgettext:no-c-format
866         else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_startFade, m_fps).seconds()));
867         return FADEIN;
868     } else if (pos.x() - rect.x() < maximumOffset && (rect.bottom() - pos.y() > addtransitionOffset)) {
869         // xgettext:no-c-format
870         setToolTip(i18n("Crop from start: %1s", cropStart().seconds()));
871         return RESIZESTART;
872     } else if (qAbs((int)(pos.x() - (rect.x() + rect.width() - m_endFade))) < maximumOffset && qAbs((int)(pos.y() - rect.y())) < 6) {
873         if (m_endFade == 0) setToolTip(i18n("Add audio fade"));
874         // xgettext:no-c-format
875         else setToolTip(i18n("Audio fade duration: %1s", GenTime(m_endFade, m_fps).seconds()));
876         return FADEOUT;
877     } else if ((rect.right() - pos.x() < maximumOffset) && (rect.bottom() - pos.y() > addtransitionOffset)) {
878         // xgettext:no-c-format
879         setToolTip(i18n("Clip duration: %1s", cropDuration().seconds()));
880         return RESIZEEND;
881     } else if ((pos.x() - rect.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
882         setToolTip(i18n("Add transition"));
883         return TRANSITIONSTART;
884     } else if ((rect.right() - pos.x() < 16 / scale) && (rect.bottom() - pos.y() <= addtransitionOffset)) {
885         setToolTip(i18n("Add transition"));
886         return TRANSITIONEND;
887     }
888     setToolTip(QString());
889     return MOVE;
890 }
891
892 QList <GenTime> ClipItem::snapMarkers() const
893 {
894     QList < GenTime > snaps;
895     QList < GenTime > markers = baseClip()->snapMarkers();
896     GenTime pos;
897
898     for (int i = 0; i < markers.size(); i++) {
899
900         pos = GenTime((int)(markers.at(i).frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
901         if (pos > GenTime()) {
902             if (pos > cropDuration()) break;
903             else snaps.append(pos + startPos());
904         }
905     }
906     return snaps;
907 }
908
909 QList <CommentedTime> ClipItem::commentedSnapMarkers() const
910 {
911     QList < CommentedTime > snaps;
912     QList < CommentedTime > markers = baseClip()->commentedSnapMarkers();
913     GenTime pos;
914
915     for (int i = 0; i < markers.size(); i++) {
916         pos = GenTime((int)(markers.at(i).time().frames(m_fps) / m_speed + 0.5), m_fps) - cropStart();
917         if (pos > GenTime()) {
918             if (pos > cropDuration()) break;
919             else snaps.append(CommentedTime(pos + startPos(), markers.at(i).comment()));
920         }
921     }
922     return snaps;
923 }
924
925 void ClipItem::slotPrepareAudioThumb(double pixelForOneFrame, int startpixel, int endpixel, int channels)
926 {
927     QRectF re =  sceneBoundingRect();
928     if (m_clipType == AV && !isAudioOnly()) re.setTop(re.y() + re.height() / 2);
929
930     //kDebug() << "// PREP AUDIO THMB FRMO : scale:" << pixelForOneFrame<< ", from: " << startpixel << ", to: " << endpixel;
931     //if ( (!audioThumbWasDrawn || framePixelWidth!=pixelForOneFrame ) && !baseClip()->audioFrameChache.isEmpty()){
932
933     for (int startCache = startpixel - startpixel % 100; startCache < endpixel; startCache += 100) {
934         //kDebug() << "creating " << startCache;
935         //if (framePixelWidth!=pixelForOneFrame  ||
936         if (m_framePixelWidth == pixelForOneFrame && m_audioThumbCachePic.contains(startCache))
937             continue;
938         if (m_audioThumbCachePic[startCache].isNull() || m_framePixelWidth != pixelForOneFrame) {
939             m_audioThumbCachePic[startCache] = QPixmap(100, (int)(re.height()));
940             m_audioThumbCachePic[startCache].fill(QColor(180, 180, 200, 140));
941         }
942         bool fullAreaDraw = pixelForOneFrame < 10;
943         QMap<int, QPainterPath > positiveChannelPaths;
944         QMap<int, QPainterPath > negativeChannelPaths;
945         QPainter pixpainter(&m_audioThumbCachePic[startCache]);
946         QPen audiopen;
947         audiopen.setWidth(0);
948         pixpainter.setPen(audiopen);
949         //pixpainter.setRenderHint(QPainter::Antialiasing,true);
950         //pixpainter.drawLine(0,0,100,re.height());
951         // Bail out, if caller provided invalid data
952         if (channels <= 0) {
953             kWarning() << "Unable to draw image with " << channels << "number of channels";
954             return;
955         }
956
957         int channelHeight = m_audioThumbCachePic[startCache].height() / channels;
958
959         for (int i = 0; i < channels; i++) {
960
961             positiveChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
962             negativeChannelPaths[i].moveTo(0, channelHeight*i + channelHeight / 2);
963         }
964
965         for (int samples = 0; samples <= 100; samples++) {
966             double frame = (double)(samples + startCache - 0) / pixelForOneFrame;
967             int sample = (int)((frame - (int)(frame)) * 20);   // AUDIO_FRAME_SIZE
968             if (frame < 0 || sample < 0 || sample > 19)
969                 continue;
970             QMap<int, QByteArray> frame_channel_data = baseClip()->m_audioFrameCache[(int)frame];
971
972             for (int channel = 0; channel < channels && frame_channel_data[channel].size() > 0; channel++) {
973
974                 int y = channelHeight * channel + channelHeight / 2;
975                 int delta = (int)(frame_channel_data[channel][sample] - 127 / 2)  * channelHeight / 64;
976                 if (fullAreaDraw) {
977                     positiveChannelPaths[channel].lineTo(samples, 0.1 + y + qAbs(delta));
978                     negativeChannelPaths[channel].lineTo(samples, 0.1 + y - qAbs(delta));
979                 } else {
980                     positiveChannelPaths[channel].lineTo(samples, 0.1 + y + delta);
981                     negativeChannelPaths[channel].lineTo(samples, 0.1 + y - delta);
982                 }
983             }
984             for (int channel = 0; channel < channels ; channel++)
985                 if (fullAreaDraw && samples == 100) {
986                     positiveChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
987                     negativeChannelPaths[channel].lineTo(samples, channelHeight*channel + channelHeight / 2);
988                     positiveChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
989                     negativeChannelPaths[channel].lineTo(0, channelHeight*channel + channelHeight / 2);
990                 }
991
992         }
993         pixpainter.setPen(QPen(QColor(0, 0, 0)));
994         pixpainter.setBrush(QBrush(QColor(60, 60, 60)));
995
996         for (int i = 0; i < channels; i++) {
997             if (fullAreaDraw) {
998                 //pixpainter.fillPath(positiveChannelPaths[i].united(negativeChannelPaths[i]),QBrush(Qt::SolidPattern));//or singleif looks better
999                 pixpainter.drawPath(positiveChannelPaths[i].united(negativeChannelPaths[i]));//or singleif looks better
1000             } else
1001                 pixpainter.drawPath(positiveChannelPaths[i]);
1002         }
1003     }
1004     //audioThumbWasDrawn=true;
1005     m_framePixelWidth = pixelForOneFrame;
1006
1007     //}
1008 }
1009
1010 int ClipItem::fadeIn() const
1011 {
1012     return m_startFade;
1013 }
1014
1015 int ClipItem::fadeOut() const
1016 {
1017     return m_endFade;
1018 }
1019
1020
1021 void ClipItem::setFadeIn(int pos)
1022 {
1023     if (pos == m_startFade) return;
1024     int oldIn = m_startFade;
1025     if (pos < 0) pos = 0;
1026     if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1027     m_startFade = pos;
1028     QRectF rect = boundingRect();
1029     update(rect.x(), rect.y(), qMax(oldIn, pos), rect.height());
1030 }
1031
1032 void ClipItem::setFadeOut(int pos)
1033 {
1034     if (pos == m_endFade) return;
1035     int oldOut = m_endFade;
1036     if (pos < 0) pos = 0;
1037     if (pos > cropDuration().frames(m_fps)) pos = (int)(cropDuration().frames(m_fps));
1038     m_endFade = pos;
1039     QRectF rect = boundingRect();
1040     update(rect.x() + rect.width() - qMax(oldOut, pos), rect.y(), qMax(oldOut, pos), rect.height());
1041
1042 }
1043
1044 /*
1045 //virtual
1046 void ClipItem::hoverEnterEvent(QGraphicsSceneHoverEvent *e)
1047 {
1048     //if (e->pos().x() < 20) m_hover = true;
1049     return;
1050     if (isItemLocked()) return;
1051     m_hover = true;
1052     QRectF r = boundingRect();
1053     double width = 35 / projectScene()->scale().x();
1054     double height = r.height() / 2;
1055     //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1056     update(r.x(), r.y() + height, width, height);
1057     update(r.right() - width, r.y() + height, width, height);
1058 }
1059
1060 //virtual
1061 void ClipItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
1062 {
1063     if (isItemLocked()) return;
1064     m_hover = false;
1065     QRectF r = boundingRect();
1066     double width = 35 / projectScene()->scale().x();
1067     double height = r.height() / 2;
1068     //WARNING: seems like it generates a full repaint of the clip, maybe not so good...
1069     update(r.x(), r.y() + height, width, height);
1070     update(r.right() - width, r.y() + height, width, height);
1071 }
1072 */
1073
1074 void ClipItem::resizeStart(int posx)
1075 {
1076     const int min = (startPos() - cropStart()).frames(m_fps);
1077     if (posx < min) posx = min;
1078     if (posx == startPos().frames(m_fps)) return;
1079     const int previous = cropStart().frames(m_fps);
1080     AbstractClipItem::resizeStart(posx);
1081
1082     // set speed independant info
1083     m_speedIndependantInfo = m_info;
1084     m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1085     m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1086
1087     if ((int) cropStart().frames(m_fps) != previous) {
1088         if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1089             m_startThumbTimer.start(150);
1090         }
1091     }
1092 }
1093
1094 void ClipItem::resizeEnd(int posx)
1095 {
1096     const int max = (startPos() - cropStart() + maxDuration()).frames(m_fps);
1097     if (posx > max && maxDuration() != GenTime()) posx = max;
1098     if (posx == endPos().frames(m_fps)) return;
1099     //kDebug() << "// NEW POS: " << posx << ", OLD END: " << endPos().frames(m_fps);
1100     const int previous = cropDuration().frames(m_fps);
1101     AbstractClipItem::resizeEnd(posx);
1102
1103     // set speed independant info
1104     m_speedIndependantInfo = m_info;
1105     m_speedIndependantInfo.cropStart = GenTime((int)(m_info.cropStart.frames(m_fps) * m_speed), m_fps);
1106     m_speedIndependantInfo.cropDuration = GenTime((int)(m_info.cropDuration.frames(m_fps) * m_speed), m_fps);
1107
1108     if ((int) cropDuration().frames(m_fps) != previous) {
1109         if (m_hasThumbs && KdenliveSettings::videothumbnails()) {
1110             m_endThumbTimer.start(150);
1111         }
1112     }
1113 }
1114
1115
1116 bool ClipItem::checkEffectsKeyframesPos(const int previous, const int current, bool fromStart)
1117 {
1118     bool modified = false;
1119     for (int i = 0; i < m_effectList.count(); i++) {
1120         QDomElement effect = m_effectList.at(i);
1121         QDomNodeList params = effect.elementsByTagName("parameter");
1122         for (int j = 0; j < params.count(); j++) {
1123             QDomElement e = params.item(i).toElement();
1124             if (!e.isNull() && (e.attribute("type") == "keyframe" || e.attribute("type") == "simplekeyframe")) {
1125                 // parse keyframes and adjust values
1126                 const QStringList keyframes = e.attribute("keyframes").split(';', QString::SkipEmptyParts);
1127                 QMap <int, double> kfr;
1128                 int pos;
1129                 double val;
1130                 foreach(const QString &str, keyframes) {
1131                     pos = str.section(':', 0, 0).toInt();
1132                     val = str.section(':', 1, 1).toDouble();
1133                     if (pos == previous) {
1134                         kfr[current] = val;
1135                         modified = true;
1136                     } else {
1137                         if ((fromStart && pos >= current) || (!fromStart && pos <= current)) {
1138                             kfr[pos] = val;
1139                             modified = true;
1140                         }
1141                     }
1142                 }
1143                 if (modified) {
1144                     QString newkfr;
1145                     QMap<int, double>::const_iterator k = kfr.constBegin();
1146                     while (k != kfr.constEnd()) {
1147                         newkfr.append(QString::number(k.key()) + ':' + QString::number(k.value()) + ';');
1148                         ++k;
1149                     }
1150                     e.setAttribute("keyframes", newkfr);
1151                     break;
1152                 }
1153             }
1154         }
1155     }
1156     if (modified && m_selectedEffect >= 0) setSelectedEffect(m_selectedEffect);
1157     return modified;
1158 }
1159
1160 //virtual
1161 QVariant ClipItem::itemChange(GraphicsItemChange change, const QVariant &value)
1162 {
1163     if (change == QGraphicsItem::ItemSelectedChange) {
1164         if (value.toBool()) setZValue(10);
1165         else setZValue(2);
1166     }
1167     if (change == ItemPositionChange && scene()) {
1168         // calculate new position.
1169         //if (parentItem()) return pos();
1170         QPointF newPos = value.toPointF();
1171         //kDebug() << "/// MOVING CLIP ITEM.------------\n++++++++++";
1172         int xpos = projectScene()->getSnapPointForPos((int) newPos.x(), KdenliveSettings::snaptopoints());
1173         xpos = qMax(xpos, 0);
1174         newPos.setX(xpos);
1175         int newTrack = newPos.y() / KdenliveSettings::trackheight();
1176         newTrack = qMin(newTrack, projectScene()->tracksCount() - 1);
1177         newTrack = qMax(newTrack, 0);
1178         newPos.setY((int)(newTrack  * KdenliveSettings::trackheight() + 1));
1179         // Only one clip is moving
1180         QRectF sceneShape = rect();
1181         sceneShape.translate(newPos);
1182         QList<QGraphicsItem*> items;
1183         if (projectScene()->editMode() == NORMALEDIT)
1184             items = scene()->items(sceneShape, Qt::IntersectsItemShape);
1185         items.removeAll(this);
1186         bool forwardMove = newPos.x() > pos().x();
1187         int offset = 0;
1188         if (!items.isEmpty()) {
1189             for (int i = 0; i < items.count(); i++) {
1190                 if (!items.at(i)->isEnabled()) continue;
1191                 if (items.at(i)->type() == type()) {
1192                     // Collision!
1193                     QPointF otherPos = items.at(i)->pos();
1194                     if ((int) otherPos.y() != (int) pos().y()) {
1195                         return pos();
1196                     }
1197                     if (forwardMove) {
1198                         offset = qMax(offset, (int)(newPos.x() - (static_cast < AbstractClipItem* >(items.at(i))->startPos() - cropDuration()).frames(m_fps)));
1199                     } else {
1200                         offset = qMax(offset, (int)((static_cast < AbstractClipItem* >(items.at(i))->endPos().frames(m_fps)) - newPos.x()));
1201                     }
1202
1203                     if (offset > 0) {
1204                         if (forwardMove) {
1205                             sceneShape.translate(QPointF(-offset, 0));
1206                             newPos.setX(newPos.x() - offset);
1207                         } else {
1208                             sceneShape.translate(QPointF(offset, 0));
1209                             newPos.setX(newPos.x() + offset);
1210                         }
1211                         QList<QGraphicsItem*> subitems = scene()->items(sceneShape, Qt::IntersectsItemShape);
1212                         subitems.removeAll(this);
1213                         for (int j = 0; j < subitems.count(); j++) {
1214                             if (!subitems.at(j)->isEnabled()) continue;
1215                             if (subitems.at(j)->type() == type()) {
1216                                 // move was not successful, revert to previous pos
1217                                 m_info.startPos = GenTime((int) pos().x(), m_fps);
1218                                 return pos();
1219                             }
1220                         }
1221                     }
1222
1223                     m_info.track = newTrack;
1224                     m_info.startPos = GenTime((int) newPos.x(), m_fps);
1225
1226                     return newPos;
1227                 }
1228             }
1229         }
1230         m_info.track = newTrack;
1231         m_info.startPos = GenTime((int) newPos.x(), m_fps);
1232         //kDebug()<<"// ITEM NEW POS: "<<newPos.x()<<", mapped: "<<mapToScene(newPos.x(), 0).x();
1233         return newPos;
1234     }
1235     return QGraphicsItem::itemChange(change, value);
1236 }
1237
1238 // virtual
1239 /*void ClipItem::mouseMoveEvent(QGraphicsSceneMouseEvent * event) {
1240 }*/
1241
1242 int ClipItem::effectsCounter()
1243 {
1244     return effectsCount() + 1;
1245 }
1246
1247 int ClipItem::effectsCount()
1248 {
1249     return m_effectList.count();
1250 }
1251
1252 int ClipItem::hasEffect(const QString &tag, const QString &id) const
1253 {
1254     return m_effectList.hasEffect(tag, id);
1255 }
1256
1257 QStringList ClipItem::effectNames()
1258 {
1259     return m_effectList.effectNames();
1260 }
1261
1262 QDomElement ClipItem::effectAt(int ix) const
1263 {
1264     if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1265     return m_effectList.at(ix).cloneNode().toElement();
1266 }
1267
1268 QDomElement ClipItem::getEffectAt(int ix) const
1269 {
1270     if (ix > m_effectList.count() - 1 || ix < 0 || m_effectList.at(ix).isNull()) return QDomElement();
1271     return m_effectList.at(ix);
1272 }
1273
1274 void ClipItem::setEffectAt(int ix, QDomElement effect)
1275 {
1276     if (ix < 0 || ix > (m_effectList.count() - 1) || effect.isNull()) {
1277         kDebug() << "Invalid effect index: " << ix;
1278         return;
1279     }
1280     //kDebug() << "CHange EFFECT AT: " << ix << ", CURR: " << m_effectList.at(ix).attribute("tag") << ", NEW: " << effect.attribute("tag");
1281     effect.setAttribute("kdenlive_ix", ix + 1);
1282     m_effectList.replace(ix, effect);
1283     m_effectNames = m_effectList.effectNames().join(" / ");
1284     QString id = effect.attribute("id");
1285     if (id == "fadein" || id == "fadeout" || id == "fade_from_black" || id == "fade_to_black")
1286         update();
1287     else {
1288         QRectF r = boundingRect();
1289         r.setHeight(20);
1290         update(r);
1291     }
1292 }
1293
1294 EffectsParameterList ClipItem::addEffect(const QDomElement effect, bool /*animate*/)
1295 {
1296     bool needRepaint = false;
1297     int ix;
1298     if (!effect.hasAttribute("kdenlive_ix")) {
1299         ix = effectsCounter();
1300     } else ix = effect.attribute("kdenlive_ix").toInt();
1301     if (!m_effectList.isEmpty() && ix <= m_effectList.count()) {
1302         needRepaint = true;
1303         m_effectList.insert(ix - 1, effect);
1304         for (int i = ix; i < m_effectList.count(); i++) {
1305             int index = m_effectList.item(i).attribute("kdenlive_ix").toInt();
1306             if (index >= ix) m_effectList.item(i).setAttribute("kdenlive_ix", index + 1);
1307         }
1308     } else m_effectList.append(effect);
1309     EffectsParameterList parameters;
1310     parameters.addParam("tag", effect.attribute("tag"));
1311     parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1312     if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1313     if (effect.hasAttribute("disabled")) parameters.addParam("disabled", effect.attribute("disabled"));
1314
1315
1316     QString effectId = effect.attribute("id");
1317     if (effectId.isEmpty()) effectId = effect.attribute("tag");
1318     parameters.addParam("id", effectId);
1319
1320     QDomNodeList params = effect.elementsByTagName("parameter");
1321     int fade = 0;
1322     for (int i = 0; i < params.count(); i++) {
1323         QDomElement e = params.item(i).toElement();
1324         if (!e.isNull()) {
1325             if (e.attribute("type") == "simplekeyframe") {
1326                 QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1327                 double factor = e.attribute("factor", "1").toDouble();
1328                 if (factor != 1) {
1329                     for (int j = 0; j < values.count(); j++) {
1330                         QString pos = values.at(j).section(":", 0, 0);
1331                         double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1332                         values[j] = pos + "=" + QString::number(val);
1333                     }
1334                 }
1335                 parameters.addParam(e.attribute("name"), values.join(";"));
1336                 /*parameters.addParam("max", e.attribute("max"));
1337                 parameters.addParam("min", e.attribute("min"));
1338                 parameters.addParam("factor", );*/
1339             } else if (e.attribute("type") == "keyframe") {
1340                 parameters.addParam("keyframes", e.attribute("keyframes"));
1341                 parameters.addParam("max", e.attribute("max"));
1342                 parameters.addParam("min", e.attribute("min"));
1343                 parameters.addParam("factor", e.attribute("factor", "1"));
1344                 parameters.addParam("starttag", e.attribute("starttag", "start"));
1345                 parameters.addParam("endtag", e.attribute("endtag", "end"));
1346             } else if (e.attribute("factor", "1") == "1") {
1347                 parameters.addParam(e.attribute("name"), e.attribute("value"));
1348
1349                 // check if it is a fade effect
1350                 if (effectId == "fadein") {
1351                     needRepaint = true;
1352                     if (m_effectList.hasEffect(QString(), "fade_from_black") == -1) {
1353                         if (e.attribute("name") == "out") fade += e.attribute("value").toInt();
1354                         else if (e.attribute("name") == "in") fade -= e.attribute("value").toInt();
1355                     } else {
1356                         QDomElement fadein = m_effectList.getEffectByTag(QString(), "fade_from_black");
1357                         if (fadein.attribute("name") == "out") fade += fadein.attribute("value").toInt();
1358                         else if (fadein.attribute("name") == "in") fade -= fadein.attribute("value").toInt();
1359                     }
1360                 } else if (effectId == "fade_from_black") {
1361                     needRepaint = true;
1362                     if (m_effectList.hasEffect(QString(), "fadein") == -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(), "fadein");
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 == "fadeout") {
1371                     needRepaint = true;
1372                     if (m_effectList.hasEffect(QString(), "fade_to_black") == -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 fadeout = m_effectList.getEffectByTag(QString(), "fade_to_black");
1377                         if (fadeout.attribute("name") == "out") fade -= fadeout.attribute("value").toInt();
1378                         else if (fadeout.attribute("name") == "in") fade += fadeout.attribute("value").toInt();
1379                     }
1380                 } else if (effectId == "fade_to_black") {
1381                     needRepaint = true;
1382                     if (m_effectList.hasEffect(QString(), "fadeout") == -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(), "fadeout");
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                 }
1391             } else {
1392                 double fact;
1393                 if (e.attribute("factor").startsWith('%')) {
1394                     fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1395                 } else fact = e.attribute("factor", "1").toDouble();
1396                 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1397             }
1398         }
1399     }
1400     m_effectNames = m_effectList.effectNames().join(" / ");
1401     if (fade > 0) m_startFade = fade;
1402     else if (fade < 0) m_endFade = -fade;
1403
1404     if (m_selectedEffect == -1) {
1405         setSelectedEffect(0);
1406     } else if (m_selectedEffect == ix - 1) setSelectedEffect(m_selectedEffect);
1407     if (needRepaint) update(boundingRect());
1408     /*if (animate) {
1409         flashClip();
1410     } */
1411     else { /*if (!needRepaint) */
1412         QRectF r = boundingRect();
1413         r.setHeight(20);
1414         update(r);
1415     }
1416     return parameters;
1417 }
1418
1419 EffectsParameterList ClipItem::getEffectArgs(const QDomElement effect)
1420 {
1421     EffectsParameterList parameters;
1422     parameters.addParam("tag", effect.attribute("tag"));
1423     parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
1424     parameters.addParam("id", effect.attribute("id"));
1425     if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
1426     if (effect.hasAttribute("disabled")) parameters.addParam("disabled", effect.attribute("disabled"));
1427
1428     QDomNodeList params = effect.elementsByTagName("parameter");
1429     for (int i = 0; i < params.count(); i++) {
1430         QDomElement e = params.item(i).toElement();
1431         //kDebug() << "/ / / /SENDING EFFECT PARAM: " << e.attribute("type") << ", NAME_ " << e.attribute("tag");
1432         if (e.attribute("type") == "simplekeyframe") {
1433
1434             QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
1435             double factor = e.attribute("factor", "1").toDouble();
1436             for (int j = 0; j < values.count(); j++) {
1437                 QString pos = values.at(j).section(":", 0, 0);
1438                 double val = values.at(j).section(":", 1, 1).toDouble() / factor;
1439                 values[j] = pos + "=" + QString::number(val);
1440             }
1441             // kDebug() << "/ / / /SENDING KEYFR:" << values;
1442             parameters.addParam(e.attribute("name"), values.join(";"));
1443             /*parameters.addParam(e.attribute("name"), e.attribute("keyframes").replace(":", "="));
1444             parameters.addParam("max", e.attribute("max"));
1445             parameters.addParam("min", e.attribute("min"));
1446             parameters.addParam("factor", e.attribute("factor", "1"));*/
1447         } else if (e.attribute("type") == "keyframe") {
1448             kDebug() << "/ / / /SENDING KEYFR EFFECT TYPE";
1449             parameters.addParam("keyframes", e.attribute("keyframes"));
1450             parameters.addParam("max", e.attribute("max"));
1451             parameters.addParam("min", e.attribute("min"));
1452             parameters.addParam("factor", e.attribute("factor", "1"));
1453             parameters.addParam("starttag", e.attribute("starttag", "start"));
1454             parameters.addParam("endtag", e.attribute("endtag", "end"));
1455         } else if (e.attribute("namedesc").contains(';')) {
1456             QString format = e.attribute("format");
1457             QStringList separators = format.split("%d", QString::SkipEmptyParts);
1458             QStringList values = e.attribute("value").split(QRegExp("[,:;x]"));
1459             QString neu;
1460             QTextStream txtNeu(&neu);
1461             if (values.size() > 0)
1462                 txtNeu << (int)values[0].toDouble();
1463             for (int i = 0; i < separators.size() && i + 1 < values.size(); i++) {
1464                 txtNeu << separators[i];
1465                 txtNeu << (int)(values[i+1].toDouble());
1466             }
1467             parameters.addParam("start", neu);
1468         } else {
1469             if (e.attribute("factor", "1") != "1") {
1470                 double fact;
1471                 if (e.attribute("factor").startsWith('%')) {
1472                     fact = ProfilesDialog::getStringEval(projectScene()->profile(), e.attribute("factor"));
1473                 } else fact = e.attribute("factor", "1").toDouble();
1474                 parameters.addParam(e.attribute("name"), QString::number(e.attribute("value").toDouble() / fact));
1475             } else {
1476                 parameters.addParam(e.attribute("name"), e.attribute("value"));
1477             }
1478         }
1479     }
1480     return parameters;
1481 }
1482
1483 void ClipItem::deleteEffect(QString index)
1484 {
1485     bool needRepaint = false;
1486     QString ix;
1487
1488     for (int i = 0; i < m_effectList.count(); ++i) {
1489         ix = m_effectList.at(i).attribute("kdenlive_ix");
1490         if (ix == index) {
1491             QString effectId = m_effectList.at(i).attribute("id");
1492             if ((effectId == "fadein" && hasEffect(QString(), "fade_from_black") == -1) ||
1493                     (effectId == "fade_from_black" && hasEffect(QString(), "fadein") == -1)) {
1494                 m_startFade = 0;
1495                 needRepaint = true;
1496             } else if ((effectId == "fadeout" && hasEffect(QString(), "fade_to_black") == -1) ||
1497                        (effectId == "fade_to_black" && hasEffect(QString(), "fadeout") == -1)) {
1498                 m_endFade = 0;
1499                 needRepaint = true;
1500             } else if (EffectsList::hasKeyFrames(m_effectList.at(i))) needRepaint = true;
1501             m_effectList.removeAt(i);
1502             i--;
1503         } else if (ix.toInt() > index.toInt()) {
1504             m_effectList.item(i).setAttribute("kdenlive_ix", ix.toInt() - 1);
1505         }
1506     }
1507     m_effectNames = m_effectList.effectNames().join(" / ");
1508
1509     if (m_effectList.isEmpty() || m_selectedEffect + 1 == index.toInt()) {
1510         // Current effect was removed
1511         if (index.toInt() > m_effectList.count() - 1) {
1512             setSelectedEffect(m_effectList.count() - 1);
1513         } else setSelectedEffect(index.toInt());
1514     }
1515     if (needRepaint) update(boundingRect());
1516     else {
1517         QRectF r = boundingRect();
1518         r.setHeight(20);
1519         update(r);
1520     }
1521     //if (!m_effectList.isEmpty()) flashClip();
1522 }
1523
1524 double ClipItem::speed() const
1525 {
1526     return m_speed;
1527 }
1528
1529 int ClipItem::strobe() const
1530 {
1531     return m_strobe;
1532 }
1533
1534 void ClipItem::setSpeed(const double speed, const int strobe)
1535 {
1536     m_speed = speed;
1537     m_strobe = strobe;
1538     if (m_speed == 1.0) m_clipName = baseClip()->name();
1539     else m_clipName = baseClip()->name() + " - " + QString::number(speed * 100, 'f', 0) + '%';
1540     m_info.cropStart = GenTime((int)(m_speedIndependantInfo.cropStart.frames(m_fps) / m_speed + 0.5), m_fps);
1541     m_info.cropDuration = GenTime((int)(m_speedIndependantInfo.cropDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1542     //update();
1543 }
1544
1545 GenTime ClipItem::maxDuration() const
1546 {
1547     return GenTime((int)(m_maxDuration.frames(m_fps) / m_speed + 0.5), m_fps);
1548 }
1549
1550 GenTime ClipItem::speedIndependantCropStart() const
1551 {
1552     return m_speedIndependantInfo.cropStart;
1553 }
1554
1555 GenTime ClipItem::speedIndependantCropDuration() const
1556 {
1557     return m_speedIndependantInfo.cropDuration;
1558 }
1559
1560
1561 const ItemInfo ClipItem::speedIndependantInfo() const
1562 {
1563     return m_speedIndependantInfo;
1564 }
1565
1566 //virtual
1567 void ClipItem::dropEvent(QGraphicsSceneDragDropEvent * event)
1568 {
1569     const QString effects = QString(event->mimeData()->data("kdenlive/effectslist"));
1570     QDomDocument doc;
1571     doc.setContent(effects, true);
1572     const QDomElement e = doc.documentElement();
1573     if (scene() && !scene()->views().isEmpty()) {
1574         event->accept();
1575         CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1576         if (view) view->slotAddEffect(e, m_info.startPos, track());
1577     }
1578 }
1579
1580 //virtual
1581 void ClipItem::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
1582 {
1583     if (isItemLocked()) event->setAccepted(false);
1584     else event->setAccepted(event->mimeData()->hasFormat("kdenlive/effectslist"));
1585 }
1586
1587 void ClipItem::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
1588 {
1589     Q_UNUSED(event);
1590 }
1591
1592 void ClipItem::addTransition(Transition* t)
1593 {
1594     m_transitionsList.append(t);
1595     //CustomTrackView *view = (CustomTrackView *) scene()->views()[0];
1596     QDomDocument doc;
1597     QDomElement e = doc.documentElement();
1598     //if (view) view->slotAddTransition(this, t->toXML() , t->startPos(), track());
1599 }
1600
1601 void ClipItem::setVideoOnly(bool force)
1602 {
1603     m_videoOnly = force;
1604 }
1605
1606 void ClipItem::setAudioOnly(bool force)
1607 {
1608     m_audioOnly = force;
1609     if (m_audioOnly) m_baseColor = QColor(141, 215, 166);
1610     else {
1611         if (m_clipType == COLOR) {
1612             QString colour = m_clip->getProperty("colour");
1613             colour = colour.replace(0, 2, "#");
1614             m_baseColor = QColor(colour.left(7));
1615         } else if (m_clipType == AUDIO) m_baseColor = QColor(141, 215, 166);
1616         else m_baseColor = QColor(141, 166, 215);
1617     }
1618     m_audioThumbCachePic.clear();
1619 }
1620
1621 bool ClipItem::isAudioOnly() const
1622 {
1623     return m_audioOnly;
1624 }
1625
1626 bool ClipItem::isVideoOnly() const
1627 {
1628     return m_videoOnly;
1629 }
1630
1631
1632 #include "clipitem.moc"