]> git.sesse.net Git - kdenlive/blobdiff - src/customtrackview.cpp
Fix effect regressions introduced by recent cleanup
[kdenlive] / src / customtrackview.cpp
index 7faf8a2a8bdbfe4e5b4aee56cb1123daf2d01f1e..1538e679e54fafb30799bde7f7ddf76c89429320 100644 (file)
 #include "docclipbase.h"
 #include "clipitem.h"
 #include "definitions.h"
-#include "moveclipcommand.h"
-#include "movetransitioncommand.h"
-#include "resizeclipcommand.h"
-#include "editguidecommand.h"
-#include "addtimelineclipcommand.h"
-#include "addeffectcommand.h"
-#include "editeffectcommand.h"
-#include "moveeffectcommand.h"
-#include "addtransitioncommand.h"
-#include "edittransitioncommand.h"
-#include "editkeyframecommand.h"
-#include "changespeedcommand.h"
-#include "addmarkercommand.h"
-#include "razorclipcommand.h"
+#include "commands/moveclipcommand.h"
+#include "commands/movetransitioncommand.h"
+#include "commands/resizeclipcommand.h"
+#include "commands/editguidecommand.h"
+#include "commands/addtimelineclipcommand.h"
+#include "commands/addeffectcommand.h"
+#include "commands/editeffectcommand.h"
+#include "commands/moveeffectcommand.h"
+#include "commands/addtransitioncommand.h"
+#include "commands/edittransitioncommand.h"
+#include "commands/editkeyframecommand.h"
+#include "commands/changespeedcommand.h"
+#include "commands/addmarkercommand.h"
+#include "commands/razorclipcommand.h"
 #include "kdenlivesettings.h"
 #include "transition.h"
 #include "clipmanager.h"
 #include "ui_keyframedialog_ui.h"
 #include "clipdurationdialog.h"
 #include "abstractgroupitem.h"
-#include "insertspacecommand.h"
+#include "commands/insertspacecommand.h"
 #include "spacerdialog.h"
-#include "addtrackcommand.h"
-#include "movegroupcommand.h"
+#include "commands/addtrackcommand.h"
+#include "commands/changeeffectstatecommand.h"
+#include "commands/movegroupcommand.h"
 #include "ui_addtrack_ui.h"
 #include "initeffects.h"
-#include "locktrackcommand.h"
-#include "groupclipscommand.h"
-#include "splitaudiocommand.h"
-#include "changecliptypecommand.h"
+#include "commands/locktrackcommand.h"
+#include "commands/groupclipscommand.h"
+#include "commands/splitaudiocommand.h"
+#include "commands/changecliptypecommand.h"
 #include "trackdialog.h"
 #include "tracksconfigdialog.h"
-#include "configtrackscommand.h"
-#include "rebuildgroupcommand.h"
-#include "razorgroupcommand.h"
+#include "commands/configtrackscommand.h"
+#include "commands/rebuildgroupcommand.h"
+#include "commands/razorgroupcommand.h"
 #include "profilesdialog.h"
 
+#include "lib/audio/audioEnvelope.h"
+#include "lib/audio/audioCorrelation.h"
+
 #include <KDebug>
 #include <KLocale>
 #include <KUrl>
@@ -83,6 +87,8 @@
 #include <QGraphicsDropShadowEffect>
 #endif
 
+//#define DEBUG
+
 bool sortGuidesList(const Guide *g1 , const Guide *g2)
 {
     return (*g1).position() < (*g2).position();
@@ -98,38 +104,41 @@ bool sortGuidesList(const Guide *g1 , const Guide *g2)
 // const int duration = animate ? 1500 : 1;
 
 CustomTrackView::CustomTrackView(KdenliveDoc *doc, CustomTrackScene* projectscene, QWidget *parent) :
-        QGraphicsView(projectscene, parent),
-        m_tracksHeight(KdenliveSettings::trackheight()),
-        m_projectDuration(0),
-        m_cursorPos(0),
-        m_document(doc),
-        m_scene(projectscene),
-        m_cursorLine(NULL),
-        m_operationMode(NONE),
-        m_moveOpMode(NONE),
-        m_dragItem(NULL),
-        m_dragGuide(NULL),
-        m_visualTip(NULL),
-        m_animation(NULL),
-        m_clickPoint(),
-        m_autoScroll(KdenliveSettings::autoscroll()),
-        m_pasteEffectsAction(NULL),
-        m_ungroupAction(NULL),
-        m_scrollOffset(0),
-        m_clipDrag(false),
-        m_findIndex(0),
-        m_tool(SELECTTOOL),
-        m_copiedItems(),
-        m_menuPosition(),
-        m_blockRefresh(false),
-        m_selectionGroup(NULL),
-        m_selectedTrack(0),
-        m_controlModifier(false)
-{
-    if (doc)
+    QGraphicsView(projectscene, parent),
+    m_tracksHeight(KdenliveSettings::trackheight()),
+    m_projectDuration(0),
+    m_cursorPos(0),
+    m_document(doc),
+    m_scene(projectscene),
+    m_cursorLine(NULL),
+    m_operationMode(NONE),
+    m_moveOpMode(NONE),
+    m_dragItem(NULL),
+    m_dragGuide(NULL),
+    m_visualTip(NULL),
+    m_animation(NULL),
+    m_clickPoint(),
+    m_autoScroll(KdenliveSettings::autoscroll()),
+    m_pasteEffectsAction(NULL),
+    m_ungroupAction(NULL),
+    m_scrollOffset(0),
+    m_clipDrag(false),
+    m_findIndex(0),
+    m_tool(SELECTTOOL),
+    m_copiedItems(),
+    m_menuPosition(),
+    m_blockRefresh(false),
+    m_selectionGroup(NULL),
+    m_selectedTrack(0),
+    m_audioCorrelator(NULL),
+    m_audioAlignmentReference(NULL),
+    m_controlModifier(false)
+{
+    if (doc) {
         m_commandStack = doc->commandStack();
-    else
+    } else {
         m_commandStack = NULL;
+    }
     m_ct = 0;
     setMouseTracking(true);
     setAcceptDrops(true);
@@ -262,14 +271,35 @@ void CustomTrackView::checkAutoScroll()
     return m_scene->m_tracksList;
 }*/
 
-void CustomTrackView::checkTrackHeight()
+
+int CustomTrackView::getFrameWidth()
+{
+    return (int) (m_tracksHeight * m_document->mltProfile().display_aspect_num / m_document->mltProfile().display_aspect_den + 0.5);
+}
+
+void CustomTrackView::updateSceneFrameWidth()
+{
+    int frameWidth = getFrameWidth();
+    QList<QGraphicsItem *> itemList = items();
+    ClipItem *item;
+    for (int i = 0; i < itemList.count(); i++) {
+        if (itemList.at(i)->type() == AVWIDGET) {
+            item = (ClipItem*) itemList.at(i);
+            item->resetFrameWidth(frameWidth);
+            item->resetThumbs(true);
+        }
+    }
+}
+
+bool CustomTrackView::checkTrackHeight()
 {
-    if (m_tracksHeight == KdenliveSettings::trackheight()) return;
+    if (m_tracksHeight == KdenliveSettings::trackheight()) return false;
     m_tracksHeight = KdenliveSettings::trackheight();
     emit trackHeightChanged();
     QList<QGraphicsItem *> itemList = items();
     ClipItem *item;
     Transition *transitionitem;
+    int frameWidth = getFrameWidth();
     bool snap = KdenliveSettings::snaptopoints();
     KdenliveSettings::setSnaptopoints(false);
     for (int i = 0; i < itemList.count(); i++) {
@@ -277,6 +307,7 @@ void CustomTrackView::checkTrackHeight()
             item = (ClipItem*) itemList.at(i);
             item->setRect(0, 0, item->rect().width(), m_tracksHeight - 1);
             item->setPos((qreal) item->startPos().frames(m_document->fps()), (qreal) item->track() * m_tracksHeight + 1);
+            item->resetFrameWidth(frameWidth);
             item->resetThumbs(true);
         } else if (itemList.at(i)->type() == TRANSITIONWIDGET) {
             transitionitem = (Transition*) itemList.at(i);
@@ -297,6 +328,7 @@ void CustomTrackView::checkTrackHeight()
 //     verticalScrollBar()->setMaximum(m_tracksHeight * m_document->tracksCount());
     KdenliveSettings::setSnaptopoints(snap);
     viewport()->update();
+    return true;
 }
 
 /** Zoom or move viewport on mousewheel
@@ -579,12 +611,19 @@ void CustomTrackView::mouseMoveEvent(QMouseEvent * event)
             // razor tool over a clip, display current frame in monitor
             if (false && !m_blockRefresh && item->type() == AVWIDGET) {
                 //TODO: solve crash when showing frame when moving razor over clip
-                emit showClipFrame(((ClipItem *) item)->baseClip(), QPoint(), mappedXPos - (clip->startPos() - clip->cropStart()).frames(m_document->fps()));
+                emit showClipFrame(((ClipItem *) item)->baseClip(), QPoint(), false, mappedXPos - (clip->startPos() - clip->cropStart()).frames(m_document->fps()));
             }
             event->accept();
             return;
         }
-        opMode = clip->operationMode(mapToScene(event->pos()));
+
+        if (m_selectionGroup && clip->parentItem() == m_selectionGroup) {
+            // all other modes break the selection, so the user probably wants to move it
+            opMode = MOVE;
+        } else {
+            opMode = clip->operationMode(mapToScene(event->pos()));
+        }
+
         const double size = 5;
         if (opMode == m_moveOpMode) {
             QGraphicsView::mouseMoveEvent(event);
@@ -701,7 +740,6 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
 
     m_blockRefresh = true;
     m_dragGuide = NULL;
-    bool collision = false;
 
     if (m_tool != RAZORTOOL) activateMonitor();
     else if (m_document->renderer()->playSpeed() != 0.0) {
@@ -712,7 +750,6 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
 
     // check item under mouse
     QList<QGraphicsItem *> collisionList = items(m_clickEvent);
-
     if (event->modifiers() == Qt::ControlModifier && m_tool != SPACERTOOL && collisionList.count() == 0) {
         // Pressing Ctrl + left mouse button in an empty area scrolls the timeline
         setDragMode(QGraphicsView::ScrollHandDrag);
@@ -729,7 +766,6 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
             m_dragGuide = (Guide *) collisionList.at(i);
             if (event->button() == Qt::LeftButton) { // move it
                 m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, true);
-                collision = true;
                 m_operationMode = MOVEGUIDE;
                 // deselect all clips so that only the guide will move
                 m_scene->clearSelection();
@@ -900,7 +936,8 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
         } else {
             setCursorPos((int)(mapToScene(event->x(), 0).x()));
         }
-        QGraphicsView::mousePressEvent(event);
+        //QGraphicsView::mousePressEvent(event);
+        event->ignore();
         return;
     }
 
@@ -969,7 +1006,12 @@ void CustomTrackView::mousePressEvent(QMouseEvent * event)
     if (event->modifiers() != Qt::ControlModifier && m_operationMode == NONE) QGraphicsView::mousePressEvent(event);
 
     m_clickPoint = QPoint((int)(mapToScene(event->pos()).x() - m_dragItem->startPos().frames(m_document->fps())), (int)(event->pos().y() - m_dragItem->pos().y()));
-    m_operationMode = m_dragItem->operationMode(mapToScene(event->pos()));
+    if (m_selectionGroup && m_dragItem->parentItem() == m_selectionGroup) {
+        // all other modes break the selection, so the user probably wants to move it
+        m_operationMode = MOVE;
+    } else {
+        m_operationMode = m_dragItem->operationMode(mapToScene(event->pos()));
+    }
     m_controlModifier = (event->modifiers() == Qt::ControlModifier);
 
     // Update snap points
@@ -1250,14 +1292,14 @@ void CustomTrackView::mouseDoubleClickEvent(QMouseEvent *event)
             ClipItem * item = static_cast <ClipItem *>(m_dragItem);
             //QString previous = item->keyframes(item->selectedEffectIndex());
             QDomElement oldEffect = item->selectedEffect().cloneNode().toElement();
-            item->insertKeyframe(item->getEffectAt(item->selectedEffectIndex()), keyFramePos.frames(m_document->fps()), val);
+            item->insertKeyframe(item->getEffectAtIndex(item->selectedEffectIndex()), keyFramePos.frames(m_document->fps()), val);
             //item->updateKeyframeEffect();
             //QString next = item->keyframes(item->selectedEffectIndex());
             QDomElement newEffect = item->selectedEffect().cloneNode().toElement();
-            EditEffectCommand *command = new EditEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), oldEffect, newEffect, item->selectedEffectIndex(), false);
+            EditEffectCommand *command = new EditEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), oldEffect, newEffect, item->selectedEffectIndex(), false, false);
             //EditKeyFrameCommand *command = new EditKeyFrameCommand(this, m_dragItem->track(), m_dragItem->startPos(), item->selectedEffectIndex(), previous, next, false);
             m_commandStack->push(command);
-            updateEffect(m_document->tracksCount() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
+            updateEffect(m_document->tracksCount() - item->track(), item->startPos(), item->selectedEffect());
             emit clipItemSelected(item, item->selectedEffectIndex());
         }
     } else if (m_dragItem && !m_dragItem->isItemLocked()) {
@@ -1361,7 +1403,7 @@ void CustomTrackView::editItemDuration()
     }
 }
 
-void CustomTrackView::editKeyFrame(const GenTime /*pos*/, const int /*track*/, const int /*index*/, const QString /*keyframes*/)
+void CustomTrackView::editKeyFrame(const GenTime & /*pos*/, const int /*track*/, const int /*index*/, const QString & /*keyframes*/)
 {
     /*ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()), track);
     if (clip) {
@@ -1446,7 +1488,7 @@ void CustomTrackView::insertClipCut(DocClipBase *clip, int in, int out)
         splitAudio();
 }
 
-bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint pos)
+bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint &pos)
 {
     if (data->hasFormat("kdenlive/clip")) {
         m_clipDrag = true;
@@ -1458,7 +1500,7 @@ bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint pos)
             kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << list.at(0);
             return false;
         }
-        if (clip->producer() == NULL) {
+        if (clip->getProducer() == NULL) {
             emit displayMessage(i18n("Clip not ready"), ErrorMessage);
             return false;
         }
@@ -1481,7 +1523,7 @@ bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint pos)
             return true;
         }
         m_selectionGroup = new AbstractGroupItem(m_document->fps());
-        ClipItem *item = new ClipItem(clip, info, m_document->fps(), 1.0, 1);
+        ClipItem *item = new ClipItem(clip, info, m_document->fps(), 1.0, 1, getFrameWidth());
         m_selectionGroup->addToGroup(item);
         item->setFlag(QGraphicsItem::ItemIsMovable, false);
 
@@ -1513,7 +1555,7 @@ bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint pos)
                 kDebug() << " WARNING))))))))) CLIP NOT FOUND : " << ids.at(i);
                 return false;
             }
-            if (clip->producer() == NULL) {
+            if (clip->getProducer() == NULL) {
                 emit displayMessage(i18n("Clip not ready"), ErrorMessage);
                 return false;
             }
@@ -1539,7 +1581,7 @@ bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint pos)
             info.track = 0;
             start += info.cropDuration;
             offsetList.append(start);
-            ClipItem *item = new ClipItem(clip, info, m_document->fps(), 1.0, 1, false);
+            ClipItem *item = new ClipItem(clip, info, m_document->fps(), 1.0, 1, getFrameWidth(), false);
             item->setFlag(QGraphicsItem::ItemIsMovable, false);
             m_selectionGroup->addToGroup(item);
             if (!clip->isPlaceHolder()) m_waitingThumbs.append(item);
@@ -1564,7 +1606,13 @@ bool CustomTrackView::insertDropClips(const QMimeData *data, const QPoint pos)
 void CustomTrackView::dragEnterEvent(QDragEnterEvent * event)
 {
     if (insertDropClips(event->mimeData(), event->pos())) {
-        event->acceptProposedAction();
+      if (event->source() == this) {
+             event->setDropAction(Qt::MoveAction);
+             event->accept();
+         } else {
+            event->setDropAction(Qt::MoveAction);
+             event->acceptProposedAction();
+        }
     } else QGraphicsView::dragEnterEvent(event);
 }
 
@@ -1591,13 +1639,13 @@ void CustomTrackView::slotRefreshEffects(ClipItem *clip)
 {
     int track = m_document->tracksCount() - clip->track();
     GenTime pos = clip->startPos();
-    if (!m_document->renderer()->mltRemoveEffect(track, pos, "-1", false, false)) {
+    if (!m_document->renderer()->mltRemoveEffect(track, pos, -1, false, false)) {
         emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
         return;
     }
     bool success = true;
     for (int i = 0; i < clip->effectsCount(); i++) {
-        if (!m_document->renderer()->mltAddEffect(track, pos, getEffectArgs(clip->effectAt(i)), false)) success = false;
+        if (!m_document->renderer()->mltAddEffect(track, pos, getEffectArgs(clip->effect(i)), false)) success = false;
     }
     if (!success) emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
     m_document->renderer()->doRefresh();
@@ -1607,6 +1655,10 @@ void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect)
 {
     if (pos < GenTime()) {
         // Add track effect
+        if (effect.attribute("id") == "speed") {
+            emit displayMessage(i18n("Cannot add speed effect to track"), ErrorMessage);
+            return;
+        }
         clearSelection();
         m_document->addTrackEffect(track - 1, effect);
         m_document->renderer()->mltAddTrackEffect(track, getEffectArgs(effect));
@@ -1622,7 +1674,8 @@ void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect)
                 emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
                 return;
             }
-            double speed = EffectsList::parameter(effect, "speed").toDouble() / 100.0;
+            QLocale locale;
+            double speed = locale.toDouble(EffectsList::parameter(effect, "speed")) / 100.0;
             int strobe = EffectsList::parameter(effect, "strobe").toInt();
             if (strobe == 0) strobe = 1;
             doChangeClipSpeed(clip->info(), clip->speedIndependantInfo(), speed, 1.0, strobe, clip->baseClip()->getId());
@@ -1634,6 +1687,7 @@ void CustomTrackView::addEffect(int track, GenTime pos, QDomElement effect)
         EffectsParameterList params = clip->addEffect(effect);
         if (!m_document->renderer()->mltAddEffect(track, pos, params))
             emit displayMessage(i18n("Problem adding effect to clip"), ErrorMessage);
+       clip->setSelectedEffect(params.paramValue("kdenlive_ix").toInt());
         if (clip->isSelected()) emit clipItemSelected(clip);
     } else emit displayMessage(i18n("Cannot find clip to add effect"), ErrorMessage);
 }
@@ -1643,7 +1697,7 @@ void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect)
     QString index = effect.attribute("kdenlive_ix");
     if (pos < GenTime()) {
         // Delete track effect
-        if (m_document->renderer()->mltRemoveTrackEffect(track, index, true)) {
+        if (m_document->renderer()->mltRemoveTrackEffect(track, index.toInt(), true)) {
            m_document->removeTrackEffect(track - 1, effect);
        }
        else emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
@@ -1658,11 +1712,11 @@ void CustomTrackView::deleteEffect(int track, GenTime pos, QDomElement effect)
             doChangeClipSpeed(clip->info(), clip->speedIndependantInfo(), 1.0, clip->speed(), 1, clip->baseClip()->getId());
             clip->deleteEffect(index);
             emit clipItemSelected(clip);
-            m_document->renderer()->mltRemoveEffect(track, pos, index, true);
+            m_document->renderer()->mltRemoveEffect(track, pos, index.toInt(), true);
             return;
         }
     }
-    if (!m_document->renderer()->mltRemoveEffect(track, pos, index, true)) {
+    if (!m_document->renderer()->mltRemoveEffect(track, pos, index.toInt(), true)) {
         kDebug() << "// ERROR REMOV EFFECT: " << index << ", DISABLE: " << effect.attribute("disable");
         emit displayMessage(i18n("Problem deleting effect"), ErrorMessage);
         return;
@@ -1679,56 +1733,58 @@ void CustomTrackView::slotAddGroupEffect(QDomElement effect, AbstractGroupItem *
     QList<QGraphicsItem *> itemList = group->childItems();
     QUndoCommand *effectCommand = new QUndoCommand();
     QString effectName;
-    QDomNode namenode = effect.elementsByTagName("name").item(0);
-    if (!namenode.isNull()) effectName = i18n(namenode.toElement().text().toUtf8().data());
+    QDomElement namenode = effect.firstChildElement("name");
+    if (!namenode.isNull()) effectName = i18n(namenode.text().toUtf8().data());
     else effectName = i18n("effect");
     effectCommand->setText(i18n("Add %1", effectName));
-    int count = 0;
     for (int i = 0; i < itemList.count(); i++) {
         if (itemList.at(i)->type() == AVWIDGET) {
             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
-            if (effect.attribute("type") == "audio") {
-                // Don't add audio effects on video clips
-                if (item->isVideoOnly() || (item->clipType() != AUDIO && item->clipType() != AV && item->clipType() != PLAYLIST)) continue;
-            } else if (effect.hasAttribute("type") == false) {
-                // Don't add video effect on audio clips
-                if (item->isAudioOnly() || item->clipType() == AUDIO) continue;
-            }
-
-            if (effect.attribute("unique", "0") != "0" && item->hasEffect(effect.attribute("tag"), effect.attribute("id")) != -1) {
-                emit displayMessage(i18n("Effect already present in clip"), ErrorMessage);
-                continue;
-            }
-            if (item->isItemLocked()) {
-                continue;
-            }
-            item->initEffect(effect);
-
-            // Old LADSPA filter, deprecated
-            /*
-            if (effect.attribute("tag") == "ladspa") {
-                QString ladpsaFile = m_document->getLadspaFile();
-                initEffects::ladspaEffectFile(ladpsaFile, effect.attribute("ladspaid").toInt(), getLadspaParams(effect));
-                effect.setAttribute("src", ladpsaFile);
-            }*/
-            new AddEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), effect, true, effectCommand);
-            count++;
+            if (effect.tagName() == "effectgroup") {
+               QDomNodeList effectlist = effect.elementsByTagName("effect");
+               for (int j = 0; j < effectlist.count(); j++) {
+                   QDomElement subeffect = effectlist.at(j).toElement();
+                   if (subeffect.hasAttribute("kdenlive_info")) {
+                       // effect is in a group
+                       EffectInfo effectInfo;
+                       effectInfo.fromString(subeffect.attribute("kdenlive_info"));
+                       if (effectInfo.groupIndex < 0) {
+                           // group needs to be appended
+                           effectInfo.groupIndex = item->nextFreeEffectGroupIndex();
+                           subeffect.setAttribute("kdenlive_info", effectInfo.toString());
+                       }
+                   }
+                   processEffect(item, subeffect, effectCommand);
+               }
+           }
+            else {
+               processEffect(item, effect, effectCommand);
+           }
         }
     }
-    if (count > 0) {
+    if (effectCommand->childCount() > 0) {
         m_commandStack->push(effectCommand);
         setDocumentModified();
     } else delete effectCommand;
 }
 
+void CustomTrackView::slotAddEffect(ClipItem *clip, QDomElement effect)
+{
+    if (clip) slotAddEffect(effect, clip->startPos(), clip->track());
+}
+
 void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track)
 {
     QList<QGraphicsItem *> itemList;
     QUndoCommand *effectCommand = new QUndoCommand();
     QString effectName;
-    QDomNode namenode = effect.elementsByTagName("name").item(0);
-    if (!namenode.isNull()) effectName = i18n(namenode.toElement().text().toUtf8().data());
-    else effectName = i18n("effect");
+    if (effect.tagName() == "effectgroup") {
+       effectName = effect.attribute("name");
+    } else {
+       QDomElement namenode = effect.firstChildElement("name");
+       if (!namenode.isNull()) effectName = i18n(namenode.text().toUtf8().data());
+       else effectName = i18n("effect");
+    }
     effectCommand->setText(i18n("Add %1", effectName));
 
     if (track == -1) itemList = scene()->selectedItems();
@@ -1751,46 +1807,24 @@ void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track)
     for (int i = 0; i < itemList.count(); i++) {
         if (itemList.at(i)->type() == AVWIDGET) {
             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
-            if (effect.attribute("type") == "audio") {
-                // Don't add audio effects on video clips
-                if (item->isVideoOnly() || (item->clipType() != AUDIO && item->clipType() != AV && item->clipType() != PLAYLIST)) {
-                    /* do not show error message when item is part of a group as the user probably knows what he does then
-                     * and the message is annoying when working with the split audio feature */
-                    if (!item->parentItem() || item->parentItem() == m_selectionGroup)
-                        emit displayMessage(i18n("Cannot add an audio effect to this clip"), ErrorMessage);
-                    continue;
-                }
-            } else if (effect.attribute("type") == "video" || !effect.hasAttribute("type")) {
-                // Don't add video effect on audio clips
-                if (item->isAudioOnly() || item->clipType() == AUDIO) {
-                    /* do not show error message when item is part of a group as the user probably knows what he does then
-                     * and the message is annoying when working with the split audio feature */
-                    if (!item->parentItem() || item->parentItem() == m_selectionGroup)
-                        emit displayMessage(i18n("Cannot add a video effect to this clip"), ErrorMessage);
-                    continue;
-                }
-            }
-            if (item->hasEffect(effect.attribute("tag"), effect.attribute("id")) != -1 && effect.attribute("unique", "0") != "0") {
-                emit displayMessage(i18n("Effect already present in clip"), ErrorMessage);
-                continue;
-            }
-            if (item->isItemLocked()) {
-                continue;
-            }
-
-            if (effect.attribute("id") == "freeze" && m_cursorPos > item->startPos().frames(m_document->fps()) && m_cursorPos < item->endPos().frames(m_document->fps())) {
-                item->initEffect(effect, m_cursorPos - item->startPos().frames(m_document->fps()));
-            } else {
-                item->initEffect(effect);
-            }
-            // Old LADSPA filter, deprecated
-            /* if (effect.attribute("tag") == "ladspa") {
-                QString ladpsaFile = m_document->getLadspaFile();
-                initEffects::ladspaEffectFile(ladpsaFile, effect.attribute("ladspaid").toInt(), getLadspaParams(effect));
-                effect.setAttribute("src", ladpsaFile);
-            }
-            */
-            new AddEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), effect, true, effectCommand);
+           if (effect.tagName() == "effectgroup") {
+               QDomNodeList effectlist = effect.elementsByTagName("effect");
+               for (int j = 0; j < effectlist.count(); j++) {
+                   QDomElement subeffect = effectlist.at(j).toElement();
+                   if (subeffect.hasAttribute("kdenlive_info")) {
+                       // effect is in a group
+                       EffectInfo effectInfo;
+                       effectInfo.fromString(subeffect.attribute("kdenlive_info"));
+                       if (effectInfo.groupIndex < 0) {
+                           // group needs to be appended
+                           effectInfo.groupIndex = item->nextFreeEffectGroupIndex();
+                           subeffect.setAttribute("kdenlive_info", effectInfo.toString());
+                       }
+                   }
+                   processEffect(item, subeffect, effectCommand);
+               }
+           }
+            else processEffect(item, effect, effectCommand);
         }
     }
     if (effectCommand->childCount() > 0) {
@@ -1801,12 +1835,12 @@ void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track)
            for (int i = 0; i < itemList.count(); i++) {
                if (itemList.at(i)->type() == AVWIDGET) {
                    ClipItem *clip = static_cast<ClipItem *>(itemList.at(i));
+                   clip->setSelectedEffect(clip->effectsCount());
                    if (!clip->isSelected()) {
-                       clearSelection();
+                       clearSelection(false);
                        clip->setSelected(true);
+                       emit clipItemSelected(clip);
                    }
-                   clip->setSelectedEffect(clip->effectsCount() - 1);
-                   emit clipItemSelected(clip, clip->effectsCount() - 1);
                    break;
                }
            }
@@ -1814,6 +1848,43 @@ void CustomTrackView::slotAddEffect(QDomElement effect, GenTime pos, int track)
     } else delete effectCommand;
 }
 
+void CustomTrackView::processEffect(ClipItem *item, QDomElement effect, QUndoCommand *effectCommand)
+{
+    if (effect.attribute("type") == "audio") {
+       // Don't add audio effects on video clips
+        if (item->isVideoOnly() || (item->clipType() != AUDIO && item->clipType() != AV && item->clipType() != PLAYLIST)) {
+           /* do not show error message when item is part of a group as the user probably knows what he does then
+            * and the message is annoying when working with the split audio feature */
+            if (!item->parentItem() || item->parentItem() == m_selectionGroup)
+               emit displayMessage(i18n("Cannot add an audio effect to this clip"), ErrorMessage);
+            return;
+        }
+    } else if (effect.attribute("type") == "video" || !effect.hasAttribute("type")) {
+       // Don't add video effect on audio clips
+        if (item->isAudioOnly() || item->clipType() == AUDIO) {
+           /* do not show error message when item is part of a group as the user probably knows what he does then
+            * and the message is annoying when working with the split audio feature */
+            if (!item->parentItem() || item->parentItem() == m_selectionGroup)
+               emit displayMessage(i18n("Cannot add a video effect to this clip"), ErrorMessage);
+            return;
+        }
+    }
+    if (effect.attribute("unique", "0") != "0" && item->hasEffect(effect.attribute("tag"), effect.attribute("id")) != -1) {
+       emit displayMessage(i18n("Effect already present in clip"), ErrorMessage);
+        return;
+    }
+    if (item->isItemLocked()) {
+       return;
+    }
+
+    if (effect.attribute("id") == "freeze" && m_cursorPos > item->startPos().frames(m_document->fps()) && m_cursorPos < item->endPos().frames(m_document->fps())) {
+       item->initEffect(effect, m_cursorPos - item->startPos().frames(m_document->fps()));
+    } else {
+       item->initEffect(effect);
+    }
+    new AddEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), effect, true, effectCommand);
+}
+
 void CustomTrackView::slotDeleteEffect(ClipItem *clip, int track, QDomElement effect, bool affectGroup)
 {
     if (clip == NULL) {
@@ -1828,8 +1899,8 @@ void CustomTrackView::slotDeleteEffect(ClipItem *clip, int track, QDomElement ef
         QList<QGraphicsItem *> items = m_selectionGroup->childItems();
         QUndoCommand *delCommand = new QUndoCommand();
         QString effectName;
-        QDomNode namenode = effect.elementsByTagName("name").item(0);
-        if (!namenode.isNull()) effectName = i18n(namenode.toElement().text().toUtf8().data());
+        QDomElement namenode = effect.firstChildElement("name");
+        if (!namenode.isNull()) effectName = i18n(namenode.text().toUtf8().data());
         else effectName = i18n("effect");
         delCommand->setText(i18n("Delete %1", effectName));
 
@@ -1848,7 +1919,7 @@ void CustomTrackView::slotDeleteEffect(ClipItem *clip, int track, QDomElement ef
                 ClipItem *item = static_cast <ClipItem *>(items.at(i));
                 int ix = item->hasEffect(effect.attribute("tag"), effect.attribute("id"));
                 if (ix != -1) {
-                    QDomElement eff = item->effectAt(ix);
+                    QDomElement eff = item->effectAtIndex(ix);
                     new AddEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), eff, false, delCommand);
                 }
             }
@@ -1865,29 +1936,27 @@ void CustomTrackView::slotDeleteEffect(ClipItem *clip, int track, QDomElement ef
     setDocumentModified();
 }
 
-void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement insertedEffect, int ix, bool triggeredByUser)
+void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement insertedEffect, bool updateEffectStack)
 {
     if (insertedEffect.isNull()) {
+       kDebug()<<"// Trying to add null effect";
         emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
         return;
     }
+    int ix = insertedEffect.attribute("kdenlive_ix").toInt();
     QDomElement effect = insertedEffect.cloneNode().toElement();
-    //kDebug() << "// update effect ix: " << effect.attribute("kdenlive_ix")<<", TRACK: "<<track;
+    //kDebug() << "// update effect ix: " << effect.attribute("kdenlive_ix")<<", GAIN: "<<EffectsList::parameter(effect, "gain");
     if (pos < GenTime()) {
         // editing a track effect
         EffectsParameterList effectParams = getEffectArgs(effect);
-        // Old LADSPA filter, deprecated
-        /*if (effect.attribute("tag") == "ladspa") {
-            // Update the ladspa affect file
-            initEffects::ladspaEffectFile(effect.attribute("src"), effect.attribute("ladspaid").toInt(), getLadspaParams(effect));
-        }*/
         // check if we are trying to reset a keyframe effect
         /*if (effectParams.hasParam("keyframes") && effectParams.paramValue("keyframes").isEmpty()) {
             clip->initEffect(effect);
             effectParams = getEffectArgs(effect);
         }*/
-        if (!m_document->renderer()->mltEditEffect(m_document->tracksCount() - track, pos, effectParams))
+        if (!m_document->renderer()->mltEditEffect(m_document->tracksCount() - track, pos, effectParams)) {
             emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
+       }
         m_document->setTrackEffect(m_document->tracksCount() - track - 1, ix, effect);
         emit updateTrackEffectState(track);
         setDocumentModified();
@@ -1901,26 +1970,23 @@ void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement insertedE
             if (effect.attribute("disable") == "1") {
                 doChangeClipSpeed(clip->info(), clip->speedIndependantInfo(), 1.0, clip->speed(), 1, clip->baseClip()->getId());
             } else {
-                double speed = EffectsList::parameter(effect, "speed").toDouble() / 100.0;
+                QLocale locale;
+                double speed = locale.toDouble(EffectsList::parameter(effect, "speed")) / 100.0;
                 int strobe = EffectsList::parameter(effect, "strobe").toInt();
                 if (strobe == 0) strobe = 1;
                 doChangeClipSpeed(clip->info(), clip->speedIndependantInfo(), speed, clip->speed(), strobe, clip->baseClip()->getId());
             }
-            clip->setEffectAt(ix, effect);
-            if (ix == clip->selectedEffectIndex()) {
-                clip->setSelectedEffect(ix);
-                if (!triggeredByUser)
-                    emit clipItemSelected(clip, ix);
-            }
+            clip->updateEffect(effect);
+           if (updateEffectStack && clip->isSelected())
+               emit clipItemSelected(clip);
+           if (ix == clip->selectedEffectIndex()) {
+               // make sure to update display of clip keyframes
+               clip->setSelectedEffect(ix);
+           }
             return;
         }
 
         EffectsParameterList effectParams = getEffectArgs(effect);
-        // Old LADSPA filter, deprecated
-        /*if (effect.attribute("tag") == "ladspa") {
-            // Update the ladspa affect file
-            initEffects::ladspaEffectFile(effect.attribute("src"), effect.attribute("ladspaid").toInt(), getLadspaParams(effect));
-        }*/
         // check if we are trying to reset a keyframe effect
         if (effectParams.hasParam("keyframes") && effectParams.paramValue("keyframes").isEmpty()) {
             clip->initEffect(effect);
@@ -1938,73 +2004,146 @@ void CustomTrackView::updateEffect(int track, GenTime pos, QDomElement insertedE
                 clip->setFadeOut(pos);
             }
         }
+       bool success = m_document->renderer()->mltEditEffect(m_document->tracksCount() - clip->track(), clip->startPos(), effectParams);
 
-        if (!m_document->renderer()->mltEditEffect(m_document->tracksCount() - clip->track(), clip->startPos(), effectParams))
-            emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
-
-        clip->setEffectAt(ix, effect);
-        if (ix == clip->selectedEffectIndex()) {
-            clip->setSelectedEffect(ix);
-            if (!triggeredByUser)
-                emit clipItemSelected(clip, ix);
-        }
+        if (success) {
+           clip->updateEffect(effect);
+           if (updateEffectStack && clip->isSelected()) {
+               emit clipItemSelected(clip);
+           }
+           if (ix == clip->selectedEffectIndex()) {
+               // make sure to update display of clip keyframes
+               clip->setSelectedEffect(ix);
+           }
+       }
+       else emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
     }
+    else emit displayMessage(i18n("Cannot find clip to update effect"), ErrorMessage);
     setDocumentModified();
 }
 
-void CustomTrackView::moveEffect(int track, GenTime pos, int oldPos, int newPos)
+void CustomTrackView::updateEffectState(int track, GenTime pos, QList <int> effectIndexes, bool disable, bool updateEffectStack)
+{
+    if (pos < GenTime()) {
+        // editing a track effect
+        if (!m_document->renderer()->mltEnableEffects(m_document->tracksCount() - track, pos, effectIndexes, disable)) {
+            emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
+           return;
+       }
+        m_document->enableTrackEffects(m_document->tracksCount() - track - 1, effectIndexes, disable);
+        emit updateTrackEffectState(track);
+        setDocumentModified();
+        return;
+    }
+    // editing a clip effect
+    ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()), m_document->tracksCount() - track);
+    if (clip) {
+       bool success = m_document->renderer()->mltEnableEffects(m_document->tracksCount() - clip->track(), clip->startPos(), effectIndexes, disable);
+       if (success) {
+           clip->enableEffects(effectIndexes, disable);
+           if (updateEffectStack && clip->isSelected()) {
+               emit clipItemSelected(clip);
+           }
+           if (effectIndexes.contains(clip->selectedEffectIndex())) {
+               // make sure to update display of clip keyframes
+               clip->setSelectedEffect(clip->selectedEffectIndex());
+           }
+       }
+       else emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
+    }
+    else emit displayMessage(i18n("Cannot find clip to update effect"), ErrorMessage);
+}
+
+void CustomTrackView::moveEffect(int track, GenTime pos, QList <int> oldPos, QList <int> newPos)
 {
     if (pos < GenTime()) {
         // Moving track effect
-        QDomElement act = m_document->getTrackEffect(m_document->tracksCount() - track - 1, newPos - 1);
-        QDomElement before = m_document->getTrackEffect(m_document->tracksCount() - track - 1, oldPos - 1);
-
-        if (!act.isNull() && !before.isNull()) {
-            m_document->setTrackEffect(m_document->tracksCount() - track - 1, oldPos - 1, act);
-            m_document->setTrackEffect(m_document->tracksCount() - track - 1, newPos - 1, before);
-            m_document->renderer()->mltMoveEffect(m_document->tracksCount() - track, pos, oldPos, newPos);
-            emit showTrackEffects(m_document->tracksCount() - track, m_document->trackInfoAt(m_document->tracksCount() - track - 1));
-        } else emit displayMessage(i18n("Cannot move effect"), ErrorMessage);
+        int documentTrack = m_document->tracksCount() - track - 1;
+        int max = m_document->getTrackEffects(documentTrack).count();
+       int new_position = newPos.at(0);
+       if (new_position > max) {
+           new_position = max;
+       }
+       int old_position = oldPos.at(0);
+       for (int i = 0; i < newPos.count(); i++) {
+           QDomElement act = m_document->getTrackEffect(documentTrack, new_position);
+           if (old_position > new_position) {
+               // Moving up, we need to adjust index
+               old_position = oldPos.at(i);
+               new_position = newPos.at(i);
+           }
+           QDomElement before = m_document->getTrackEffect(documentTrack, old_position);
+           if (!act.isNull() && !before.isNull()) {
+               m_document->setTrackEffect(documentTrack, new_position, before);
+               m_document->renderer()->mltMoveEffect(m_document->tracksCount() - track, pos, old_position, new_position);
+           } else emit displayMessage(i18n("Cannot move effect"), ErrorMessage);
+       }
+       emit showTrackEffects(m_document->tracksCount() - track, m_document->trackInfoAt(documentTrack));
         return;
     }
     ClipItem *clip = getClipItemAt((int)pos.frames(m_document->fps()), m_document->tracksCount() - track);
-    if (clip && !clip->effectAt(newPos - 1).isNull() && !clip->effectAt(oldPos - 1).isNull()) {
-        QDomElement act = clip->effectAt(newPos - 1);
-        QDomElement before = clip->effectAt(oldPos - 1);
-        clip->setEffectAt(oldPos - 1, act);
-        clip->setEffectAt(newPos - 1, before);
-        // special case: speed effect, which is a pseudo-effect, not appearing in MLT's effects
-        if (act.attribute("id") == "speed") {
-            m_document->renderer()->mltUpdateEffectPosition(track, pos, oldPos, newPos);
-        } else if (before.attribute("id") == "speed") {
-            m_document->renderer()->mltUpdateEffectPosition(track, pos, newPos, oldPos);
-        } else m_document->renderer()->mltMoveEffect(track, pos, oldPos, newPos);
-        emit clipItemSelected(clip, newPos - 1);
+    if (clip) {
+       int new_position = newPos.at(0);
+       if (new_position > clip->effectsCount()) {
+           new_position = clip->effectsCount();
+       }
+       int old_position = oldPos.at(0);
+       for (int i = 0; i < newPos.count(); i++) {
+           QDomElement act = clip->effectAtIndex(new_position);
+           if (old_position > new_position) {
+               // Moving up, we need to adjust index
+               old_position = oldPos.at(i);
+               new_position = newPos.at(i);
+           }
+           QDomElement before = clip->effectAtIndex(old_position);
+           if (act.isNull() || before.isNull()) {
+               emit displayMessage(i18n("Cannot move effect"), ErrorMessage);
+               return;
+           }
+           clip->moveEffect(before, new_position);
+           // special case: speed effect, which is a pseudo-effect, not appearing in MLT's effects
+           if (act.attribute("id") == "speed") {
+               m_document->renderer()->mltUpdateEffectPosition(track, pos, old_position, new_position);
+           } else if (before.attribute("id") == "speed") {
+               m_document->renderer()->mltUpdateEffectPosition(track, pos, new_position, old_position);
+           } else m_document->renderer()->mltMoveEffect(track, pos, old_position, new_position);
+       }
+       clip->setSelectedEffect(newPos.at(0));
+       emit clipItemSelected(clip);
         setDocumentModified();
     } else emit displayMessage(i18n("Cannot move effect"), ErrorMessage);
 }
 
-void CustomTrackView::slotChangeEffectState(ClipItem *clip, int track, int effectPos, bool disable)
+void CustomTrackView::slotChangeEffectState(ClipItem *clip, int track, QList <int> effectIndexes, bool disable)
 {
-    EditEffectCommand *command;
-    QDomElement effect;
-    if (clip == NULL) effect = m_document->getTrackEffect(track - 1, effectPos);
-    else effect = clip->effectAt(effectPos);
-    QDomElement oldEffect = effect.cloneNode().toElement();
-    effect.setAttribute("disable", (int) disable);
-
-
+    ChangeEffectStateCommand *command;
     if (clip == NULL) {
         // editing track effect
-        command = new EditEffectCommand(this, m_document->tracksCount() - track, GenTime(-1), oldEffect, effect, effectPos, true);
+        command = new ChangeEffectStateCommand(this, m_document->tracksCount() - track, GenTime(-1), effectIndexes, disable, false, true);
     } else {
-        command = new EditEffectCommand(this, m_document->tracksCount() - clip->track(), clip->startPos(), oldEffect, effect, effectPos, true);
+       // Check if we have a speed effect, disabling / enabling it needs a special procedure since it is a pseudoo effect
+       QList <int> speedEffectIndexes;
+       for (int i = 0; i < effectIndexes.count(); i++) {
+           QDomElement effect = clip->effectAtIndex(effectIndexes.at(i));
+           if (effect.attribute("id") == "speed") {
+               // speed effect
+               speedEffectIndexes << effectIndexes.at(i);
+               QDomElement newEffect = effect.cloneNode().toElement();
+               newEffect.setAttribute("disable", (int) disable);
+               EditEffectCommand *editcommand = new EditEffectCommand(this, m_document->tracksCount() - clip->track(), clip->startPos(), effect, newEffect, effectIndexes.at(i), false, true);
+               m_commandStack->push(editcommand);
+           }
+       }
+       for (int j = 0; j < speedEffectIndexes.count(); j++) {
+           effectIndexes.removeAll(speedEffectIndexes.at(j));
+       }
+        command = new ChangeEffectStateCommand(this, m_document->tracksCount() - clip->track(), clip->startPos(), effectIndexes, disable, false, true);
     }
     m_commandStack->push(command);
-    setDocumentModified();;
+    setDocumentModified();
 }
 
-void CustomTrackView::slotChangeEffectPosition(ClipItem *clip, int track, int currentPos, int newPos)
+void CustomTrackView::slotChangeEffectPosition(ClipItem *clip, int track, QList <int> currentPos, int newPos)
 {
     MoveEffectCommand *command;
     if (clip == NULL) {
@@ -2015,20 +2154,20 @@ void CustomTrackView::slotChangeEffectPosition(ClipItem *clip, int track, int cu
     setDocumentModified();
 }
 
-void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, int track, QDomElement oldeffect, QDomElement effect, int ix)
+void CustomTrackView::slotUpdateClipEffect(ClipItem *clip, int track, QDomElement oldeffect, QDomElement effect, int ix, bool refreshEffectStack)
 {
     EditEffectCommand *command;
-    if (clip) command = new EditEffectCommand(this, m_document->tracksCount() - clip->track(), clip->startPos(), oldeffect, effect, ix, true);
-    else command = new EditEffectCommand(this, m_document->tracksCount() - track, GenTime(-1), oldeffect, effect, ix, true);
+    if (clip) command = new EditEffectCommand(this, m_document->tracksCount() - clip->track(), clip->startPos(), oldeffect, effect, ix, refreshEffectStack, true);
+    else command = new EditEffectCommand(this, m_document->tracksCount() - track, GenTime(-1), oldeffect, effect, ix, refreshEffectStack, true);
     m_commandStack->push(command);
 }
 
 void CustomTrackView::slotUpdateClipRegion(ClipItem *clip, int ix, QString region)
 {
-    QDomElement effect = clip->getEffectAt(ix);
+    QDomElement effect = clip->getEffectAtIndex(ix);
     QDomElement oldeffect = effect.cloneNode().toElement();
     effect.setAttribute("region", region);
-    EditEffectCommand *command = new EditEffectCommand(this, m_document->tracksCount() - clip->track(), clip->startPos(), oldeffect, effect, ix, true);
+    EditEffectCommand *command = new EditEffectCommand(this, m_document->tracksCount() - clip->track(), clip->startPos(), oldeffect, effect, ix, true, true);
     m_commandStack->push(command);
 }
 
@@ -2064,23 +2203,23 @@ ClipItem *CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut, boo
         // fade in from 2nd part of the clip
         int ix = dup->hasEffect(QString(), "fadein");
         if (ix != -1) {
-            QDomElement oldeffect = dup->effectAt(ix);
+            QDomElement oldeffect = dup->effectAtIndex(ix);
             dup->deleteEffect(oldeffect.attribute("kdenlive_ix"));
         }
         ix = dup->hasEffect(QString(), "fade_from_black");
         if (ix != -1) {
-            QDomElement oldeffect = dup->effectAt(ix);
+            QDomElement oldeffect = dup->effectAtIndex(ix);
             dup->deleteEffect(oldeffect.attribute("kdenlive_ix"));
         }
         // fade out from 1st part of the clip
         ix = item->hasEffect(QString(), "fadeout");
         if (ix != -1) {
-            QDomElement oldeffect = item->effectAt(ix);
+            QDomElement oldeffect = item->effectAtIndex(ix);
             item->deleteEffect(oldeffect.attribute("kdenlive_ix"));
         }
         ix = item->hasEffect(QString(), "fade_to_black");
         if (ix != -1) {
-            QDomElement oldeffect = item->effectAt(ix);
+            QDomElement oldeffect = item->effectAtIndex(ix);
             item->deleteEffect(oldeffect.attribute("kdenlive_ix"));
         }
 
@@ -2121,12 +2260,12 @@ ClipItem *CustomTrackView::cutClip(ItemInfo info, GenTime cutTime, bool cut, boo
         // join fade effects again
         int ix = dup->hasEffect(QString(), "fadeout");
         if (ix != -1) {
-            QDomElement effect = dup->effectAt(ix);
+            QDomElement effect = dup->effectAtIndex(ix);
             item->addEffect(effect);
         }
         ix = dup->hasEffect(QString(), "fade_to_black");
         if (ix != -1) {
-            QDomElement effect = dup->effectAt(ix);
+            QDomElement effect = dup->effectAtIndex(ix);
             item->addEffect(effect);
         }
 
@@ -2404,7 +2543,7 @@ void CustomTrackView::dropEvent(QDropEvent * event)
             ItemInfo clipInfo = info;
             clipInfo.track = m_document->tracksCount() - item->track();
 
-            int worked = m_document->renderer()->mltInsertClip(clipInfo, item->xml(), item->baseClip()->producer(item->track()), m_scene->editMode() == OVERWRITEEDIT, m_scene->editMode() == INSERTEDIT);
+            int worked = m_document->renderer()->mltInsertClip(clipInfo, item->xml(), item->baseClip()->getProducer(item->track()), m_scene->editMode() == OVERWRITEEDIT, m_scene->editMode() == INSERTEDIT);
             if (worked == -1) {
                 emit displayMessage(i18n("Cannot insert clip in timeline"), ErrorMessage);
                 brokenClips.append(item);
@@ -2455,10 +2594,14 @@ void CustomTrackView::dropEvent(QDropEvent * event)
             groupSelectedItems(true);
         } else if (items.count() == 1) {
             m_dragItem = static_cast <AbstractClipItem *>(items.at(0));
-            emit clipItemSelected((ClipItem*)m_dragItem, -1, false);
+            emit clipItemSelected((ClipItem*)m_dragItem, false);
         }
         event->setDropAction(Qt::MoveAction);
         event->accept();
+
+        /// \todo enable when really working
+//        alignAudio();
+
     } else QGraphicsView::dropEvent(event);
     setFocus();
 }
@@ -2622,13 +2765,14 @@ int CustomTrackView::duration() const
 
 void CustomTrackView::addTrack(TrackInfo type, int ix)
 {
+    QList <TransitionInfo> transitionInfos;
     if (ix == -1 || ix == m_document->tracksCount()) {
         m_document->insertTrack(0, type);
-        m_document->renderer()->mltInsertTrack(1, type.type == VIDEOTRACK);
+        transitionInfos = m_document->renderer()->mltInsertTrack(1, type.type == VIDEOTRACK);
     } else {
         m_document->insertTrack(m_document->tracksCount() - ix, type);
         // insert track in MLT playlist
-        m_document->renderer()->mltInsertTrack(m_document->tracksCount() - ix, type.type == VIDEOTRACK);
+        transitionInfos = m_document->renderer()->mltInsertTrack(m_document->tracksCount() - ix, type.type == VIDEOTRACK);
 
         double startY = ix * m_tracksHeight + 1 + m_tracksHeight / 2;
         QRectF r(0, startY, sceneRect().width(), sceneRect().height() - startY);
@@ -2647,6 +2791,7 @@ void CustomTrackView::addTrack(TrackInfo type, int ix)
         m_selectionGroup->translate(0, m_tracksHeight);
 
         // adjust track number
+        Mlt::Tractor *tractor = m_document->renderer()->lockService();
         QList<QGraphicsItem *> children = m_selectionGroup->childItems();
         for (int i = 0; i < children.count(); i++) {
             if (children.at(i)->type() == GROUPWIDGET) {
@@ -2664,20 +2809,31 @@ void CustomTrackView::addTrack(TrackInfo type, int ix)
                 // We add a move clip command so that we get the correct producer for new track number
                 if (clip->clipType() == AV || clip->clipType() == AUDIO) {
                     Mlt::Producer *prod = clip->getProducer(clipinfo.track);
-                    if (m_document->renderer()->mltUpdateClipProducer((int)(m_document->tracksCount() - clipinfo.track), clipinfo.startPos.frames(m_document->fps()), prod) == false) {
+                    if (m_document->renderer()->mltUpdateClipProducer(tractor, (int)(m_document->tracksCount() - clipinfo.track), clipinfo.startPos.frames(m_document->fps()), prod) == false) {
                         // problem updating clip
                         emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)", clipinfo.startPos.frames(m_document->fps()), clipinfo.track), ErrorMessage);
                     }
                 }
-            } else if (item->type() == TRANSITIONWIDGET) {
+            } /*else if (item->type() == TRANSITIONWIDGET) {
                 Transition *tr = static_cast <Transition *>(item);
                 int track = tr->transitionEndTrack();
                 if (track >= ix) {
                     tr->updateTransitionEndTrack(getPreviousVideoTrack(clipinfo.track));
                 }
-            }
+            }*/
         }
+        // Sync transition tracks with MLT playlist
+        Transition *tr;        
+       TransitionInfo info;
+       for (int i = 0; i < transitionInfos.count(); i++) {
+           info = transitionInfos.at(i);
+           tr = getTransitionItem(info);
+           if (tr) tr->setForcedTrack(info.forceTrack, info.a_track);
+           else kDebug()<<"// Cannot update TRANSITION AT: "<<info.b_track<<" / "<<info.startPos.frames(m_document->fps()); 
+       }
+       
         resetSelectionGroup(false);
+        m_document->renderer()->unlockService(tractor);
     }
 
     int maxHeight = m_tracksHeight * m_document->tracksCount() * matrix().m22();
@@ -2719,6 +2875,7 @@ void CustomTrackView::removeTrack(int ix)
     // Move graphic items
     qreal ydiff = 0 - (int) m_tracksHeight;
     m_selectionGroup->translate(0, ydiff);
+    Mlt::Tractor *tractor = m_document->renderer()->lockService();
 
     // adjust track number
     QList<QGraphicsItem *> children = m_selectionGroup->childItems();
@@ -2736,7 +2893,7 @@ void CustomTrackView::removeTrack(int ix)
             // We add a move clip command so that we get the correct producer for new track number
             if (clip->clipType() == AV || clip->clipType() == AUDIO || clip->clipType() == PLAYLIST) {
                 Mlt::Producer *prod = clip->getProducer(clipinfo.track);
-                if (prod == NULL || !m_document->renderer()->mltUpdateClipProducer((int)(m_document->tracksCount() - clipinfo.track), clipinfo.startPos.frames(m_document->fps()), prod)) {
+                if (prod == NULL || !m_document->renderer()->mltUpdateClipProducer(tractor, (int)(m_document->tracksCount() - clipinfo.track), clipinfo.startPos.frames(m_document->fps()), prod)) {
                     emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)", clipinfo.startPos.frames(m_document->fps()), clipinfo.track), ErrorMessage);
                 }
             }
@@ -2751,6 +2908,7 @@ void CustomTrackView::removeTrack(int ix)
         }
     }
     resetSelectionGroup(false);
+    m_document->renderer()->unlockService(tractor);
 
     int maxHeight = m_tracksHeight * m_document->tracksCount() * matrix().m22();
     for (int i = 0; i < m_guides.count(); i++) {
@@ -2903,9 +3061,16 @@ void CustomTrackView::slotRemoveSpace()
     int track = 0;
     if (m_menuPosition.isNull()) {
         pos = GenTime(cursorPos(), m_document->fps());
-        bool ok;
-        track = QInputDialog::getInteger(this, i18n("Remove Space"), i18n("Track"), 0, 0, m_document->tracksCount() - 1, 1, &ok);
-        if (!ok) return;
+
+        TrackDialog d(m_document, parentWidget());
+        d.comboTracks->setCurrentIndex(m_selectedTrack);
+        d.label->setText(i18n("Track"));
+        d.before_select->setHidden(true);
+        d.setWindowTitle(i18n("Remove Space"));
+        d.video_track->setHidden(true);
+        d.audio_track->setHidden(true);
+        if (d.exec() != QDialog::Accepted) return;
+        track = d.comboTracks->currentIndex();
     } else {
         pos = GenTime((int)(mapToScene(m_menuPosition).x()), m_document->fps());
         track = (int)(mapToScene(m_menuPosition).y() / m_tracksHeight);
@@ -2922,7 +3087,6 @@ void CustomTrackView::slotRemoveSpace()
         return;
     }
     int length = m_document->renderer()->mltGetSpaceLength(pos, m_document->tracksCount() - track, true);
-    //kDebug() << "// GOT LENGT; " << length;
     if (length <= 0) {
         emit displayMessage(i18n("You must be in an empty space to remove space (time: %1, track: %2)", m_document->timecode().getTimecodeFromFrames(mapToScene(m_menuPosition).x()), track), ErrorMessage);
         return;
@@ -2954,6 +3118,38 @@ void CustomTrackView::slotRemoveSpace()
         }
     }
 
+    if (!transitionsToMove.isEmpty()) {
+        // Make sure that by moving the items, we don't get a transition collision
+        // Find first transition
+        ItemInfo info = transitionsToMove.at(0);
+        for (int i = 1; i < transitionsToMove.count(); i++)
+            if (transitionsToMove.at(i).startPos < info.startPos) info = transitionsToMove.at(i);
+
+        // make sure there are no transitions on the way
+        QRectF rect(info.startPos.frames(m_document->fps()) - length, track * m_tracksHeight + m_tracksHeight / 2, length - 1, m_tracksHeight / 2 - 2);
+        items = scene()->items(rect);
+        int transitionCorrection = -1;
+        for (int i = 0; i < items.count(); i++) {
+            if (items.at(i)->type() == TRANSITIONWIDGET) {
+                // There is a transition on the way
+                AbstractClipItem *item = static_cast <AbstractClipItem *>(items.at(i));
+                int transitionEnd = item->endPos().frames(m_document->fps());
+                if (transitionEnd > transitionCorrection) transitionCorrection = transitionEnd;
+            }
+        }
+
+        if (transitionCorrection > 0) {
+            // We need to fix the move length
+            length = info.startPos.frames(m_document->fps()) - transitionCorrection;
+        }
+            
+        // Make sure we don't send transition before 0
+        if (info.startPos.frames(m_document->fps()) < length) {
+            // reduce length to maximum possible
+            length = info.startPos.frames(m_document->fps());
+        }           
+    }
+
     InsertSpaceCommand *command = new InsertSpaceCommand(this, clipsToMove, transitionsToMove, track, GenTime(-length, m_document->fps()), true);
     updateTrackDuration(track, command);
     m_commandStack->push(command);
@@ -3019,7 +3215,7 @@ void CustomTrackView::slotInsertSpace()
     }
 }
 
-void CustomTrackView::insertSpace(QList<ItemInfo> clipsToMove, QList<ItemInfo> transToMove, int track, const GenTime duration, const GenTime offset)
+void CustomTrackView::insertSpace(QList<ItemInfo> clipsToMove, QList<ItemInfo> transToMove, int track, const GenTime &duration, const GenTime &offset)
 {
     int diff = duration.frames(m_document->fps());
     resetSelectionGroup();
@@ -3385,7 +3581,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
                     KdenliveSettings::setSnaptopoints(false);
                     item->setPos((int) m_dragItemInfo.startPos.frames(m_document->fps()), (int)(m_dragItemInfo.track * m_tracksHeight + 1));
                     KdenliveSettings::setSnaptopoints(snap);
-                    emit displayMessage(i18n("Cannot move clip to position %1", m_document->timecode().getTimecodeFromFrames(m_dragItemInfo.startPos.frames(m_document->fps()))), ErrorMessage);
+                    emit displayMessage(i18n("Cannot move clip to position %1", m_document->timecode().getTimecodeFromFrames(info.startPos.frames(m_document->fps()))), ErrorMessage);
                 }
                 setDocumentModified();
             } else if (m_dragItem->type() == TRANSITIONWIDGET && (m_dragItemInfo.startPos != info.startPos || m_dragItemInfo.track != info.track)) {
@@ -3474,7 +3670,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
                         adjustTimelineClips(m_scene->editMode(), clip, ItemInfo(), moveGroup);
                         m_document->renderer()->mltInsertClip(info, clip->xml(), clip->getProducer(trackProducer), m_scene->editMode() == OVERWRITEEDIT, m_scene->editMode() == INSERTEDIT);
                         for (int i = 0; i < clip->effectsCount(); i++) {
-                            m_document->renderer()->mltAddEffect(info.track, info.startPos, getEffectArgs(clip->effectAt(i)), false);
+                            m_document->renderer()->mltAddEffect(info.track, info.startPos, getEffectArgs(clip->effect(i)), false);
                         }
                     } else {
                         Transition *tr = static_cast <Transition*>(item);
@@ -3578,7 +3774,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
         int ix = item->hasEffect("volume", "fadein");
         int ix2 = item->hasEffect("", "fade_from_black");
         if (ix != -1) {
-            QDomElement oldeffect = item->effectAt(ix);
+            QDomElement oldeffect = item->effectAtIndex(ix);
             int start = item->cropStart().frames(m_document->fps());
             int end = item->fadeIn();
             if (end == 0) {
@@ -3589,7 +3785,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
                 EffectsList::setParameter(oldeffect, "in", QString::number(start));
                 EffectsList::setParameter(oldeffect, "out", QString::number(end));
                 slotUpdateClipEffect(item, -1, effect, oldeffect, ix);
-                emit clipItemSelected(item, ix);
+                emit clipItemSelected(item);
             }
         } else if (item->fadeIn() != 0 && ix2 == -1) {
             QDomElement effect;
@@ -3601,7 +3797,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
         }
         if (ix2 != -1) {
-            QDomElement oldeffect = item->effectAt(ix2);
+            QDomElement oldeffect = item->effectAtIndex(ix2);
             int start = item->cropStart().frames(m_document->fps());
             int end = item->fadeIn();
             if (end == 0) {
@@ -3612,7 +3808,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
                 EffectsList::setParameter(oldeffect, "in", QString::number(start));
                 EffectsList::setParameter(oldeffect, "out", QString::number(end));
                 slotUpdateClipEffect(item, -1, effect, oldeffect, ix2);
-                emit clipItemSelected(item, ix2);
+                emit clipItemSelected(item);
             }
         }
     } else if (m_operationMode == FADEOUT) {
@@ -3621,7 +3817,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
         int ix = item->hasEffect("volume", "fadeout");
         int ix2 = item->hasEffect("", "fade_to_black");
         if (ix != -1) {
-            QDomElement oldeffect = item->effectAt(ix);
+            QDomElement oldeffect = item->effectAtIndex(ix);
             int end = (item->cropDuration() + item->cropStart()).frames(m_document->fps());
             int start = item->fadeOut();
             if (start == 0) {
@@ -3633,7 +3829,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
                 EffectsList::setParameter(oldeffect, "out", QString::number(end));
                 // kDebug()<<"EDIT FADE OUT : "<<start<<"x"<<end;
                 slotUpdateClipEffect(item, -1, effect, oldeffect, ix);
-                emit clipItemSelected(item, ix);
+                emit clipItemSelected(item);
             }
         } else if (item->fadeOut() != 0 && ix2 == -1) {
             QDomElement effect;
@@ -3646,7 +3842,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
             slotAddEffect(effect, m_dragItem->startPos(), m_dragItem->track());
         }
         if (ix2 != -1) {
-            QDomElement oldeffect = item->effectAt(ix2);
+            QDomElement oldeffect = item->effectAtIndex(ix2);
             int end = (item->cropDuration() + item->cropStart()).frames(m_document->fps());
             int start = item->fadeOut();
             if (start == 0) {
@@ -3658,7 +3854,7 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
                 EffectsList::setParameter(oldeffect, "out", QString::number(end));
                 // kDebug()<<"EDIT FADE OUT : "<<start<<"x"<<end;
                 slotUpdateClipEffect(item, -1, effect, oldeffect, ix2);
-                emit clipItemSelected(item, ix2);
+                emit clipItemSelected(item);
             }
         }
     } else if (m_operationMode == KEYFRAME) {
@@ -3676,20 +3872,20 @@ void CustomTrackView::mouseReleaseEvent(QMouseEvent * event)
 
         if ((val < -50 || val > 150) && item->editedKeyFramePos() != start && item->editedKeyFramePos() != end && item->keyFrameNumber() > 1) {
             //delete keyframe
-            item->movedKeyframe(item->getEffectAt(item->selectedEffectIndex()), item->selectedKeyFramePos(), -1, 0);
+            item->movedKeyframe(item->getEffectAtIndex(item->selectedEffectIndex()), item->selectedKeyFramePos(), -1, 0);
         } else {
-            item->movedKeyframe(item->getEffectAt(item->selectedEffectIndex()), item->selectedKeyFramePos(), item->editedKeyFramePos(), item->editedKeyFrameValue());
+            item->movedKeyframe(item->getEffectAtIndex(item->selectedEffectIndex()), item->selectedKeyFramePos(), item->editedKeyFramePos(), item->editedKeyFrameValue());
         }
 
         QDomElement newEffect = item->selectedEffect().cloneNode().toElement();
         //item->updateKeyframeEffect();
         //QString next = item->keyframes(item->selectedEffectIndex());
         //EditKeyFrameCommand *command = new EditKeyFrameCommand(this, item->track(), item->startPos(), item->selectedEffectIndex(), previous, next, false);
-        EditEffectCommand *command = new EditEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), oldEffect, newEffect, item->selectedEffectIndex(), false);
+        EditEffectCommand *command = new EditEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), oldEffect, newEffect, item->selectedEffectIndex(), false, false);
 
         m_commandStack->push(command);
-        updateEffect(m_document->tracksCount() - item->track(), item->startPos(), item->selectedEffect(), item->selectedEffectIndex());
-        emit clipItemSelected(item, item->selectedEffectIndex());
+        updateEffect(m_document->tracksCount() - item->track(), item->startPos(), item->selectedEffect());
+        emit clipItemSelected(item);
     }
     if (m_dragItem && m_dragItem->type() == TRANSITIONWIDGET && m_dragItem->isSelected()) {
         // A transition is selected
@@ -3719,10 +3915,10 @@ void CustomTrackView::deleteClip(ItemInfo info, bool refresh)
         emit displayMessage(i18n("Error removing clip at %1 on track %2", m_document->timecode().getTimecodeFromFrames(info.startPos.frames(m_document->fps())), info.track), ErrorMessage);
         kDebug()<<"CANNOT REMOVE: "<<info.startPos.frames(m_document->fps())<<", TK: "<<info.track;
         //m_document->renderer()->saveSceneList(QString("/tmp/error%1.mlt").arg(m_ct), QDomElement());
-        exit(1);
         return;
     }
     m_waitingThumbs.removeAll(item);
+    item->stopThumbs();
     if (item->isSelected()) emit clipItemSelected(NULL);
     item->baseClip()->removeReference();
     m_document->updateClip(item->baseClip()->getId());
@@ -3769,7 +3965,6 @@ void CustomTrackView::deleteSelectedClips()
     scene()->clearSelection();
     QUndoCommand *deleteSelected = new QUndoCommand();
 
-    bool resetGroup = false;
     int groupCount = 0;
     int clipCount = 0;
     int transitionCount = 0;
@@ -3780,7 +3975,6 @@ void CustomTrackView::deleteSelectedClips()
             QList<QGraphicsItem *> children = itemList.at(i)->childItems();
             QList <ItemInfo> clipInfos;
             QList <ItemInfo> transitionInfos;
-            GenTime currentPos = GenTime(m_cursorPos, m_document->fps());
             for (int j = 0; j < children.count(); j++) {
                 if (children.at(j)->type() == AVWIDGET) {
                     AbstractClipItem *clip = static_cast <AbstractClipItem *>(children.at(j));
@@ -3806,7 +4000,6 @@ void CustomTrackView::deleteSelectedClips()
         if (itemList.at(i)->type() == AVWIDGET) {
             clipCount++;
             ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
-            if (item->parentItem()) resetGroup = true;
             //kDebug()<<"// DELETE CLP AT: "<<item->info().startPos.frames(25);
             new AddTimelineClipCommand(this, item->xml(), item->clipProducer(), item->info(), item->effectList(), false, false, true, true, deleteSelected);
             emit clipItemSelected(NULL);
@@ -3814,7 +4007,6 @@ void CustomTrackView::deleteSelectedClips()
             transitionCount++;
             Transition *item = static_cast <Transition *>(itemList.at(i));
             //kDebug()<<"// DELETE TRANS AT: "<<item->info().startPos.frames(25);
-            if (item->parentItem()) resetGroup = true;
             new AddTransitionCommand(this, item->info(), item->transitionEndTrack(), item->toXML(), true, true, deleteSelected);
             emit transitionItemSelected(NULL);
         }
@@ -3830,34 +4022,6 @@ void CustomTrackView::deleteSelectedClips()
     m_commandStack->push(deleteSelected);
 }
 
-void CustomTrackView::changeClipSpeed()
-{
-    QList<QGraphicsItem *> itemList = scene()->selectedItems();
-    if (itemList.count() == 0) {
-        emit displayMessage(i18n("Select clip to change speed"), ErrorMessage);
-        return;
-    }
-    QUndoCommand *changeSelected = new QUndoCommand();
-    changeSelected->setText("Edit clip speed");
-    int count = 0;
-    int percent = -1;
-    bool ok;
-    for (int i = 0; i < itemList.count(); i++) {
-        if (itemList.at(i)->type() == AVWIDGET) {
-            ClipItem *item = static_cast <ClipItem *>(itemList.at(i));
-            ItemInfo info = item->info();
-            if (percent == -1) percent = QInputDialog::getInteger(this, i18n("Edit Clip Speed"), i18n("New speed (percents)"), item->speed() * 100, 1, 10000, 1, &ok);
-            if (!ok) break;
-            double speed = (double) percent / 100.0;
-            if (item->speed() != speed && (item->clipType() == VIDEO || item->clipType() == AV)) {
-                count++;
-                //new ChangeSpeedCommand(this, info, item->speed(), speed, item->clipProducer(), changeSelected);
-            }
-        }
-    }
-    if (count > 0) m_commandStack->push(changeSelected);
-    else delete changeSelected;
-}
 
 void CustomTrackView::doChangeClipSpeed(ItemInfo info, ItemInfo speedIndependantInfo, const double speed, const double oldspeed, int strobe, const QString &id)
 {
@@ -3995,7 +4159,6 @@ void CustomTrackView::groupClips(bool group)
     QList<QGraphicsItem *> itemList = scene()->selectedItems();
     QList <ItemInfo> clipInfos;
     QList <ItemInfo> transitionInfos;
-    GenTime currentPos = GenTime(m_cursorPos, m_document->fps());
 
     // Expand groups
     int max = itemList.count();
@@ -4074,18 +4237,19 @@ void CustomTrackView::addClip(QDomElement xml, const QString &clipId, ItemInfo i
         return;
     }
 
-    if (baseclip->producer() == NULL) {
+    if (baseclip->getProducer() == NULL) {
         // If the clip has no producer, we must wait until it is created...
         m_mutex.lock();
         emit displayMessage(i18n("Waiting for clip..."), InformationMessage);
         emit forceClipProcessing(clipId);
         qApp->processEvents();
-        for (int i = 0; i < 3; i++) {
-            if (baseclip->producer() == NULL) {
-                m_producerNotReady.wait(&m_mutex, 500 + 500 * i);
+        for (int i = 0; i < 10; i++) {
+            if (baseclip->getProducer() == NULL) {
+                qApp->processEvents();
+                m_producerNotReady.wait(&m_mutex, 200);
             } else break;
         }
-        if (baseclip->producer() == NULL) {
+        if (baseclip->getProducer() == NULL) {
             emit displayMessage(i18n("Cannot insert clip..."), ErrorMessage);
             m_mutex.unlock();
             return;
@@ -4094,7 +4258,7 @@ void CustomTrackView::addClip(QDomElement xml, const QString &clipId, ItemInfo i
         m_mutex.unlock();
     }
 
-    ClipItem *item = new ClipItem(baseclip, info, m_document->fps(), xml.attribute("speed", "1").toDouble(), xml.attribute("strobe", "1").toInt());
+    ClipItem *item = new ClipItem(baseclip, info, m_document->fps(), xml.attribute("speed", "1").toDouble(), xml.attribute("strobe", "1").toInt(), getFrameWidth());
     item->setEffectList(effects);
     if (xml.hasAttribute("audio_only")) item->setAudioOnly(true);
     else if (xml.hasAttribute("video_only")) item->setVideoOnly(true);
@@ -4110,7 +4274,7 @@ void CustomTrackView::addClip(QDomElement xml, const QString &clipId, ItemInfo i
     info.track = m_document->tracksCount() - info.track;
     m_document->renderer()->mltInsertClip(info, xml, item->getProducer(producerTrack), overwrite, push);
     for (int i = 0; i < item->effectsCount(); i++) {
-        m_document->renderer()->mltAddEffect(info.track, info.startPos, getEffectArgs(item->effectAt(i)), false);
+        m_document->renderer()->mltAddEffect(info.track, info.startPos, getEffectArgs(item->effect(i)), false);
     }
     setDocumentModified();
     if (refresh)
@@ -4122,19 +4286,25 @@ void CustomTrackView::addClip(QDomElement xml, const QString &clipId, ItemInfo i
 
 void CustomTrackView::slotUpdateClip(const QString &clipId, bool reload)
 {
+    QMutexLocker locker(&m_mutex);
     QList<QGraphicsItem *> list = scene()->items();
     QList <ClipItem *>clipList;
     ClipItem *clip = NULL;
+    DocClipBase *baseClip = NULL;
+    Mlt::Tractor *tractor = m_document->renderer()->lockService();
     for (int i = 0; i < list.size(); ++i) {
         if (list.at(i)->type() == AVWIDGET) {
             clip = static_cast <ClipItem *>(list.at(i));
             if (clip->clipProducer() == clipId) {
+                if (baseClip == NULL) {
+                    baseClip = clip->baseClip();
+                }
                 ItemInfo info = clip->info();
                 Mlt::Producer *prod = NULL;
-                if (clip->isAudioOnly()) prod = clip->baseClip()->audioProducer(info.track);
-                else if (clip->isVideoOnly()) prod = clip->baseClip()->videoProducer();
-                else prod = clip->baseClip()->producer(info.track);
-                if (reload && !m_document->renderer()->mltUpdateClip(info, clip->xml(), prod)) {
+                if (clip->isAudioOnly()) prod = baseClip->audioProducer(info.track);
+                else if (clip->isVideoOnly()) prod = baseClip->videoProducer();
+                else prod = baseClip->getProducer(info.track);
+                if (reload && !m_document->renderer()->mltUpdateClip(tractor, info, clip->xml(), prod)) {
                     emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)", info.startPos.frames(m_document->fps()), info.track), ErrorMessage);
                 }
                 else clipList.append(clip);
@@ -4143,6 +4313,7 @@ void CustomTrackView::slotUpdateClip(const QString &clipId, bool reload)
     }
     for (int i = 0; i < clipList.count(); i++)
         clipList.at(i)->refreshClip(true, true);
+    m_document->renderer()->unlockService(tractor);
 }
 
 ClipItem *CustomTrackView::getClipItemAtEnd(GenTime pos, int track)
@@ -4196,9 +4367,17 @@ ClipItem *CustomTrackView::getClipItemAt(GenTime pos, int track)
     return getClipItemAt((int) pos.frames(m_document->fps()), track);
 }
 
+
+Transition *CustomTrackView::getTransitionItem(TransitionInfo info)
+{
+    int pos = info.startPos.frames(m_document->fps());
+    int track = m_document->tracksCount() - info.b_track;
+    return getTransitionItemAt(pos, track);
+}
+
 Transition *CustomTrackView::getTransitionItemAt(int pos, int track)
 {
-    const QPointF p(pos, (track + 1) * m_tracksHeight);
+    const QPointF p(pos, track * m_tracksHeight + Transition::itemOffset() + 1);
     QList<QGraphicsItem *> list = scene()->items(p);
     Transition *clip = NULL;
     for (int i = 0; i < list.size(); i++) {
@@ -4219,7 +4398,7 @@ Transition *CustomTrackView::getTransitionItemAt(GenTime pos, int track)
 Transition *CustomTrackView::getTransitionItemAtEnd(GenTime pos, int track)
 {
     int framepos = (int)(pos.frames(m_document->fps()));
-    const QPointF p(framepos - 1, (track + 1) * m_tracksHeight);
+    const QPointF p(framepos - 1, track * m_tracksHeight + Transition::itemOffset() + 1);
     QList<QGraphicsItem *> list = scene()->items(p);
     Transition *clip = NULL;
     for (int i = 0; i < list.size(); i++) {
@@ -4235,7 +4414,7 @@ Transition *CustomTrackView::getTransitionItemAtEnd(GenTime pos, int track)
 
 Transition *CustomTrackView::getTransitionItemAtStart(GenTime pos, int track)
 {
-    const QPointF p(pos.frames(m_document->fps()), (track + 1) * m_tracksHeight);
+    const QPointF p(pos.frames(m_document->fps()), track * m_tracksHeight + Transition::itemOffset() + 1);
     QList<QGraphicsItem *> list = scene()->items(p);
     Transition *clip = NULL;
     for (int i = 0; i < list.size(); ++i) {
@@ -4249,18 +4428,26 @@ Transition *CustomTrackView::getTransitionItemAtStart(GenTime pos, int track)
     return clip;
 }
 
-void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end, bool refresh)
+bool CustomTrackView::moveClip(const ItemInfo &start, const ItemInfo &end, bool refresh, ItemInfo *out_actualEnd)
 {
     if (m_selectionGroup) resetSelectionGroup(false);
     ClipItem *item = getClipItemAt((int) start.startPos.frames(m_document->fps()), start.track);
     if (!item) {
         emit displayMessage(i18n("Cannot move clip at time: %1 on track %2", m_document->timecode().getTimecodeFromFrames(start.startPos.frames(m_document->fps())), start.track), ErrorMessage);
         kDebug() << "----------------  ERROR, CANNOT find clip to move at.. ";
-        return;
+        return false;
     }
     Mlt::Producer *prod = item->getProducer(end.track);
 
-    bool success = m_document->renderer()->mltMoveClip((int)(m_document->tracksCount() - start.track), (int)(m_document->tracksCount() - end.track), (int) start.startPos.frames(m_document->fps()), (int)end.startPos.frames(m_document->fps()), prod);
+#ifdef DEBUG
+    qDebug() << "Moving item " << (long)item << " from .. to:";
+    qDebug() << item->info();
+    qDebug() << start;
+    qDebug() << end;
+#endif
+    bool success = m_document->renderer()->mltMoveClip((int)(m_document->tracksCount() - start.track), (int)(m_document->tracksCount() - end.track),
+                                                       (int) start.startPos.frames(m_document->fps()), (int)end.startPos.frames(m_document->fps()),
+                                                       prod);
     if (success) {
         bool snap = KdenliveSettings::snaptopoints();
         KdenliveSettings::setSnaptopoints(false);
@@ -4290,9 +4477,19 @@ void CustomTrackView::moveClip(const ItemInfo start, const ItemInfo end, bool re
         emit displayMessage(i18n("Cannot move clip to position %1", m_document->timecode().getTimecodeFromFrames(end.startPos.frames(m_document->fps()))), ErrorMessage);
     }
     if (refresh) m_document->renderer()->doRefresh();
+    if (out_actualEnd != NULL) {
+        *out_actualEnd = item->info();
+#ifdef DEBUG
+        qDebug() << "Actual end position updated:" << *out_actualEnd;
+#endif
+    }
+#ifdef DEBUG
+    qDebug() << item->info();
+#endif
+    return success;
 }
 
-void CustomTrackView::moveGroup(QList <ItemInfo> startClip, QList <ItemInfo> startTransition, const GenTime offset, const int trackOffset, bool reverseMove)
+void CustomTrackView::moveGroup(QList <ItemInfo> startClip, QList <ItemInfo> startTransition, const GenTime &offset, const int trackOffset, bool reverseMove)
 {
     // Group Items
     resetSelectionGroup();
@@ -4381,7 +4578,7 @@ void CustomTrackView::moveGroup(QList <ItemInfo> startClip, QList <ItemInfo> sta
                 info.track = m_document->tracksCount() - info.track;
                 m_document->renderer()->mltInsertClip(info, clip->xml(), clip->getProducer(trackProducer));
                 for (int i = 0; i < clip->effectsCount(); i++) {
-                    m_document->renderer()->mltAddEffect(info.track, info.startPos, getEffectArgs(clip->effectAt(i)), false);
+                    m_document->renderer()->mltAddEffect(info.track, info.startPos, getEffectArgs(clip->effect(i)), false);
                 }
             } else if (item->type() == TRANSITIONWIDGET) {
                 Transition *tr = static_cast <Transition*>(item);
@@ -4410,7 +4607,7 @@ void CustomTrackView::moveGroup(QList <ItemInfo> startClip, QList <ItemInfo> sta
     } else kDebug() << "///////// WARNING; NO GROUP TO MOVE";
 }
 
-void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end, bool m_refresh)
+void CustomTrackView::moveTransition(const ItemInfo &start, const ItemInfo &end, bool refresh)
 {
     Transition *item = getTransitionItemAt(start.startPos, start.track);
     if (!item) {
@@ -4454,11 +4651,11 @@ void CustomTrackView::moveTransition(const ItemInfo start, const ItemInfo end, b
         }
         emit transitionItemSelected(item, getPreviousVideoTrack(item->track()), p);
     }
-    if (m_refresh) m_document->renderer()->doRefresh();
+    if (refresh) m_document->renderer()->doRefresh();
     setDocumentModified();
 }
 
-void CustomTrackView::resizeClip(const ItemInfo start, const ItemInfo end, bool dontWorry)
+void CustomTrackView::resizeClip(const ItemInfo &start, const ItemInfo &end, bool dontWorry)
 {
     bool resizeClipStart = (start.startPos != end.startPos);
     ClipItem *item = getClipItemAtStart(start.startPos, start.track);
@@ -4710,7 +4907,7 @@ void CustomTrackView::updatePositionEffects(ClipItem* item, ItemInfo info, bool
         // there is a fade in effect
         int effectPos = item->hasEffect("volume", "fadein");
         if (effectPos != -1) {
-            QDomElement effect = item->getEffectAt(effectPos);
+            QDomElement effect = item->getEffectAtIndex(effectPos);
             int start = item->cropStart().frames(m_document->fps());
             int max = item->cropDuration().frames(m_document->fps());
             if (end > max) {
@@ -4726,12 +4923,12 @@ void CustomTrackView::updatePositionEffects(ClipItem* item, ItemInfo info, bool
                     emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
                 // if fade effect is displayed, update the effect edit widget with new clip duration
                 if (item->isSelected() && effectPos == item->selectedEffectIndex())
-                    emit clipItemSelected(item, effectPos);
+                    emit clipItemSelected(item);
             }
         }
         effectPos = item->hasEffect("brightness", "fade_from_black");
         if (effectPos != -1) {
-            QDomElement effect = item->getEffectAt(effectPos);
+            QDomElement effect = item->getEffectAtIndex(effectPos);
             int start = item->cropStart().frames(m_document->fps());
             int max = item->cropDuration().frames(m_document->fps());
             if (end > max) {
@@ -4747,7 +4944,7 @@ void CustomTrackView::updatePositionEffects(ClipItem* item, ItemInfo info, bool
                     emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
                 // if fade effect is displayed, update the effect edit widget with new clip duration
                 if (item->isSelected() && effectPos == item->selectedEffectIndex())
-                    emit clipItemSelected(item, effectPos);
+                    emit clipItemSelected(item);
             }
         }
     }
@@ -4757,7 +4954,7 @@ void CustomTrackView::updatePositionEffects(ClipItem* item, ItemInfo info, bool
         // there is a fade out effect
         int effectPos = item->hasEffect("volume", "fadeout");
         if (effectPos != -1) {
-            QDomElement effect = item->getEffectAt(effectPos);
+            QDomElement effect = item->getEffectAtIndex(effectPos);
             int max = item->cropDuration().frames(m_document->fps());
             int end = max + item->cropStart().frames(m_document->fps());
             if (start > max) {
@@ -4773,12 +4970,12 @@ void CustomTrackView::updatePositionEffects(ClipItem* item, ItemInfo info, bool
                     emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
                 // if fade effect is displayed, update the effect edit widget with new clip duration
                 if (item->isSelected() && effectPos == item->selectedEffectIndex())
-                    emit clipItemSelected(item, effectPos);
+                    emit clipItemSelected(item);
             }
         }
         effectPos = item->hasEffect("brightness", "fade_to_black");
         if (effectPos != -1) {
-            QDomElement effect = item->getEffectAt(effectPos);
+            QDomElement effect = item->getEffectAtIndex(effectPos);
             int max = item->cropDuration().frames(m_document->fps());
             int end = max + item->cropStart().frames(m_document->fps());
             if (start > max) {
@@ -4794,7 +4991,7 @@ void CustomTrackView::updatePositionEffects(ClipItem* item, ItemInfo info, bool
                     emit displayMessage(i18n("Problem editing effect"), ErrorMessage);
                 // if fade effect is displayed, update the effect edit widget with new clip duration
                 if (item->isSelected() && effectPos == item->selectedEffectIndex())
-                    emit clipItemSelected(item, effectPos);
+                    emit clipItemSelected(item);
             }
         }
     }
@@ -4803,13 +5000,13 @@ void CustomTrackView::updatePositionEffects(ClipItem* item, ItemInfo info, bool
     if (effectPos != -1) {
         // Freeze effect needs to be adjusted with clip resize
         int diff = (info.startPos - item->startPos()).frames(m_document->fps());
-        QDomElement eff = item->getEffectAt(effectPos);
+        QDomElement eff = item->getEffectAtIndex(effectPos);
         if (!eff.isNull() && diff != 0) {
             int freeze_pos = EffectsList::parameter(eff, "frame").toInt() + diff;
             EffectsList::setParameter(eff, "frame", QString::number(freeze_pos));
             if (standalone) {
                 if (item->isSelected() && item->selectedEffect().attribute("id") == "freeze") {
-                    emit clipItemSelected(item, item->selectedEffectIndex());
+                    emit clipItemSelected(item);
                 }
             }
         }
@@ -4962,7 +5159,7 @@ void CustomTrackView::slotDeleteAllClipMarkers(const QString &id)
     m_commandStack->push(deleteMarkers);
 }
 
-void CustomTrackView::addMarker(const QString &id, const GenTime &pos, const QString comment)
+void CustomTrackView::addMarker(const QString &id, const GenTime &pos, const QString &comment)
 {
     DocClipBase *base = m_document->clipManager()->getClipById(id);
     if (!comment.isEmpty()) base->addSnapMarker(pos, comment);
@@ -4994,7 +5191,7 @@ void CustomTrackView::buildGuidesMenu(QMenu *goMenu) const
     goMenu->setEnabled(!m_guides.isEmpty());
 }
 
-void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const QString &comment)
+void CustomTrackView::editGuide(const GenTime &oldPos, const GenTime &pos, const QString &comment)
 {
     if (oldPos > GenTime() && pos > GenTime()) {
         // move guide
@@ -5022,7 +5219,7 @@ void CustomTrackView::editGuide(const GenTime oldPos, const GenTime pos, const Q
     m_document->syncGuides(m_guides);
 }
 
-bool CustomTrackView::addGuide(const GenTime pos, const QString &comment)
+bool CustomTrackView::addGuide(const GenTime &pos, const QString &comment)
 {
     for (int i = 0; i < m_guides.count(); i++) {
         if (m_guides.at(i)->position() == pos) {
@@ -5494,7 +5691,7 @@ void CustomTrackView::pasteClipEffects()
         if (clips.at(i)->type() == AVWIDGET) {
             ClipItem *item = static_cast < ClipItem *>(clips.at(i));
             for (int j = 0; j < clip->effectsCount(); j++) {
-                QDomElement eff = clip->effectAt(j);
+                QDomElement eff = clip->effect(j);
                 if (eff.attribute("unique", "0") == "0" || item->hasEffect(eff.attribute("tag"), eff.attribute("id")) == -1) {
                     adjustKeyfames(clip->cropStart(), item->cropStart(), item->cropDuration(), eff);
                     new AddEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), eff, true, paste);
@@ -5719,6 +5916,7 @@ void CustomTrackView::slotInsertTrack(int ix)
         info.duration = 0;
         info.isMute = false;
         info.isLocked = false;
+       info.effectsList = EffectsList(true);
         if (d.video_track->isChecked()) {
             info.type = VIDEOTRACK;
             info.isBlind = false;
@@ -5812,27 +6010,7 @@ void CustomTrackView::autoTransition()
     setDocumentModified();
 }
 
-
-QStringList CustomTrackView::getLadspaParams(QDomElement effect) const
-{
-    QStringList result;
-    QLocale locale;
-    QDomNodeList params = effect.elementsByTagName("parameter");
-    for (int i = 0; i < params.count(); i++) {
-        QDomElement e = params.item(i).toElement();
-        if (!e.isNull() && e.attribute("type") == "constant") {
-            if (e.hasAttribute("factor")) {
-                double factor = e.attribute("factor").toDouble();
-                double value = e.attribute("value").toDouble();
-                value = value / factor;
-                result.append(locale.toString(value));
-            } else result.append(e.attribute("value"));
-        }
-    }
-    return result;
-}
-
-void CustomTrackView::clipNameChanged(const QString id, const QString name)
+void CustomTrackView::clipNameChanged(const QString &id, const QString &name)
 {
     QList<QGraphicsItem *> list = scene()->items();
     ClipItem *clip = NULL;
@@ -5879,7 +6057,7 @@ void CustomTrackView::getTransitionAvailableSpace(AbstractClipItem *item, GenTim
     }
 }
 
-void CustomTrackView::loadGroups(const QDomNodeList groups)
+void CustomTrackView::loadGroups(const QDomNodeList &groups)
 {
     for (int i = 0; i < groups.count(); i++) {
         QDomNodeList children = groups.at(i).childNodes();
@@ -5917,7 +6095,9 @@ void CustomTrackView::splitAudio()
                 if (clip->parentItem()) {
                     emit displayMessage(i18n("Cannot split audio of grouped clips"), ErrorMessage);
                 } else {
-                    new SplitAudioCommand(this, clip->track(), clip->startPos(), splitCommand);
+                    EffectsList effects;
+                    effects.clone(clip->effectList());
+                    new SplitAudioCommand(this, clip->track(), clip->startPos(), effects, splitCommand);
                 }
             }
         }
@@ -5928,7 +6108,147 @@ void CustomTrackView::splitAudio()
     }
 }
 
-void CustomTrackView::doSplitAudio(const GenTime &pos, int track, bool split)
+void CustomTrackView::setAudioAlignReference()
+{
+    QList<QGraphicsItem *> selection = scene()->selectedItems();
+    if (selection.isEmpty() || selection.size() > 1) {
+        emit displayMessage(i18n("You must select exactly one clip for the audio reference."), ErrorMessage);
+        return;
+    }
+    if (m_audioCorrelator != NULL) {
+        delete m_audioCorrelator;
+    }
+    if (selection.at(0)->type() == AVWIDGET) {
+        ClipItem *clip = static_cast<ClipItem*>(selection.at(0));
+        if (clip->clipType() == AV || clip->clipType() == AUDIO) {
+            m_audioAlignmentReference = clip;
+
+            AudioEnvelope *envelope = new AudioEnvelope(clip->getProducer(clip->track()));
+            m_audioCorrelator = new AudioCorrelation(envelope);
+
+
+#ifdef DEBUG
+            envelope->drawEnvelope().save("kdenlive-audio-reference-envelope.png");
+            envelope->dumpInfo();
+#endif
+
+
+            emit displayMessage(i18n("Audio align reference set."), InformationMessage);
+        }
+        return;
+    }
+    emit displayMessage(i18n("Reference for audio alignment must contain audio data."), ErrorMessage);
+}
+
+void CustomTrackView::alignAudio()
+{
+    bool referenceOK = true;
+    if (m_audioCorrelator == NULL) {
+        referenceOK = false;
+    }
+    if (referenceOK) {
+        if (!scene()->items().contains(m_audioAlignmentReference)) {
+            // The reference item has been deleted from the timeline (or so)
+            referenceOK = false;
+        }
+    }
+    if (!referenceOK) {
+        emit displayMessage(i18n("Audio alignment reference not yet set."), InformationMessage);
+        return;
+    }
+
+    int counter = 0;
+    QList<QGraphicsItem *> selection = scene()->selectedItems();
+    foreach (QGraphicsItem *item, selection) {
+        if (item->type() == AVWIDGET) {
+
+            ClipItem *clip = static_cast<ClipItem*>(item);
+            if (clip == m_audioAlignmentReference) {
+                continue;
+            }
+
+            if (clip->clipType() == AV || clip->clipType() == AUDIO) {
+                AudioEnvelope *envelope = new AudioEnvelope(clip->getProducer(clip->track()),
+                                                            clip->info().cropStart.frames(m_document->fps()),
+                                                            clip->info().cropDuration.frames(m_document->fps()));
+
+                // FFT only for larger vectors. We could use it all time, but for small vectors
+                // the (anyway not noticeable) overhead is smaller with a nested for loop correlation.
+                int index = m_audioCorrelator->addChild(envelope, envelope->envelopeSize() > 200);
+                int shift = m_audioCorrelator->getShift(index);
+                counter++;
+
+
+#ifdef DEBUG
+                m_audioCorrelator->info(index)->toImage().save("kdenlive-audio-align-cross-correlation.png");
+                envelope->drawEnvelope().save("kdenlive-audio-align-envelope.png");
+                envelope->dumpInfo();
+
+                int targetPos = m_audioAlignmentReference->startPos().frames(m_document->fps()) + shift;
+                qDebug() << "Reference starts at " << m_audioAlignmentReference->startPos().frames(m_document->fps());
+                qDebug() << "We will start at " << targetPos;
+                qDebug() << "to shift by " << shift;
+                qDebug() << "(eventually)";
+                qDebug() << "(maybe)";
+#endif
+
+
+                QUndoCommand *moveCommand = new QUndoCommand();
+
+                GenTime add(shift, m_document->fps());
+                ItemInfo start = clip->info();
+
+                ItemInfo end = start;
+                end.startPos = m_audioAlignmentReference->startPos() + add - m_audioAlignmentReference->cropStart();
+                end.endPos = end.startPos + start.cropDuration;
+
+                if ( end.startPos.seconds() < 0 ) {
+                    // Clip would start before 0, so crop it first
+                    GenTime cropBy = -end.startPos;
+
+#ifdef DEBUG
+                    qDebug() << "Need to crop clip. " << start;
+                    qDebug() << "end.startPos: " << end.startPos.toString() << ", cropBy: " << cropBy.toString();
+#endif
+
+                    ItemInfo resized = start;
+                    resized.startPos += cropBy;
+
+                    resizeClip(start, resized);
+                    new ResizeClipCommand(this, start, resized, false, false, moveCommand);
+
+                    start = clip->info();
+                    end.startPos += cropBy;
+
+#ifdef DEBUG
+                    qDebug() << "Clip cropped. " << start;
+                    qDebug() << "Moving to: " << end;
+#endif
+                }
+
+                if (itemCollision(clip, end)) {
+                    delete moveCommand;
+                    emit displayMessage(i18n("Unable to move clip due to collision."), ErrorMessage);
+                    return;
+                }
+
+                moveCommand->setText(i18n("Auto-align clip"));
+                new MoveClipCommand(this, start, end, true, moveCommand);
+                updateTrackDuration(clip->track(), moveCommand);
+                m_commandStack->push(moveCommand);
+
+            }
+        }
+    }
+
+    if (counter == 0) {
+        emit displayMessage(i18n("No audio clips selected."), ErrorMessage);
+    } else {
+        emit displayMessage(i18n("Auto-aligned %1 clips.").arg(counter), InformationMessage);
+    }
+}
+
+void CustomTrackView::doSplitAudio(const GenTime &pos, int track, EffectsList effects, bool split)
 {
     ClipItem *clip = getClipItemAt(pos, track);
     if (clip == NULL) {
@@ -5964,15 +6284,31 @@ void CustomTrackView::doSplitAudio(const GenTime &pos, int track, bool split)
             clip->setSelected(true);
             ClipItem *audioClip = getClipItemAt(start, info.track);
             if (audioClip) {
+                Mlt::Tractor *tractor = m_document->renderer()->lockService();
                 clip->setVideoOnly(true);
-                if (m_document->renderer()->mltUpdateClipProducer(m_document->tracksCount() - track, start, clip->baseClip()->videoProducer()) == false) {
+                if (m_document->renderer()->mltUpdateClipProducer(tractor, m_document->tracksCount() - track, start, clip->baseClip()->videoProducer()) == false) {
                     emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)", start, track), ErrorMessage);
                 }
-                if (m_document->renderer()->mltUpdateClipProducer(m_document->tracksCount() - info.track, start, clip->baseClip()->audioProducer(info.track)) == false) {
+                if (m_document->renderer()->mltUpdateClipProducer(tractor, m_document->tracksCount() - info.track, start, clip->baseClip()->audioProducer(info.track)) == false) {
                     emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)", start, info.track), ErrorMessage);
                 }
+                m_document->renderer()->unlockService(tractor);
                 audioClip->setSelected(true);
                 audioClip->setAudioOnly(true);
+
+                // keep video effects, move audio effects to audio clip
+                int videoIx = 0;
+                int audioIx = 0;
+                for (int i = 0; i < effects.count(); ++i) {
+                    if (effects.at(i).attribute("type") == "audio") {
+                        deleteEffect(m_document->tracksCount() - track, pos, clip->effect(videoIx));
+                        audioIx++;
+                    } else {
+                        deleteEffect(freetrack, pos, audioClip->effect(audioIx));
+                        videoIx++;
+                    }
+                }
+
                 groupSelectedItems(false, true);
             }
         }
@@ -5994,9 +6330,26 @@ void CustomTrackView::doSplitAudio(const GenTime &pos, int track, bool split)
                 ItemInfo info = clip->info();
                 deleteClip(clp->info());
                 clip->setVideoOnly(false);
-                if (!m_document->renderer()->mltUpdateClipProducer(m_document->tracksCount() - info.track, info.startPos.frames(m_document->fps()), clip->baseClip()->producer(info.track))) {
-                    emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)", info.startPos.frames(m_document->fps()), info.track), ErrorMessage);
+                Mlt::Tractor *tractor = m_document->renderer()->lockService();
+                if (!m_document->renderer()->mltUpdateClipProducer(
+                            tractor,
+                            m_document->tracksCount() - info.track,
+                            info.startPos.frames(m_document->fps()),
+                            clip->baseClip()->getProducer(info.track))) {
+
+                    emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)",
+                                             info.startPos.frames(m_document->fps()), info.track),
+                                        ErrorMessage);
+                }
+                m_document->renderer()->unlockService(tractor);
+
+                // re-add audio effects
+                for (int i = 0; i < effects.count(); ++i) {
+                    if (effects.at(i).attribute("type") == "audio") {
+                        addEffect(m_document->tracksCount() - track, pos, effects.at(i));
+                    }
                 }
+
                 break;
             }
         }
@@ -6088,28 +6441,30 @@ void CustomTrackView::doChangeClipType(const GenTime &pos, int track, bool video
         kDebug() << "// Cannot find clip to split!!!";
         return;
     }
+    Mlt::Tractor *tractor = m_document->renderer()->lockService();
     if (videoOnly) {
         int start = pos.frames(m_document->fps());
         clip->setVideoOnly(true);
         clip->setAudioOnly(false);
-        if (m_document->renderer()->mltUpdateClipProducer(m_document->tracksCount() - track, start, clip->baseClip()->videoProducer()) == false) {
+        if (m_document->renderer()->mltUpdateClipProducer(tractor, m_document->tracksCount() - track, start, clip->baseClip()->videoProducer()) == false) {
             emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)", start, track), ErrorMessage);
         }
     } else if (audioOnly) {
         int start = pos.frames(m_document->fps());
         clip->setAudioOnly(true);
         clip->setVideoOnly(false);
-        if (m_document->renderer()->mltUpdateClipProducer(m_document->tracksCount() - track, start, clip->baseClip()->audioProducer(track)) == false) {
+        if (m_document->renderer()->mltUpdateClipProducer(tractor, m_document->tracksCount() - track, start, clip->baseClip()->audioProducer(track)) == false) {
             emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)", start, track), ErrorMessage);
         }
     } else {
         int start = pos.frames(m_document->fps());
         clip->setAudioOnly(false);
         clip->setVideoOnly(false);
-        if (m_document->renderer()->mltUpdateClipProducer(m_document->tracksCount() - track, start, clip->baseClip()->producer(track)) == false) {
+        if (m_document->renderer()->mltUpdateClipProducer(tractor, m_document->tracksCount() - track, start, clip->baseClip()->getProducer(track)) == false) {
             emit displayMessage(i18n("Cannot update clip (time: %1, track: %2)", start, track), ErrorMessage);
         }
     }
+    m_document->renderer()->unlockService(tractor);
     clip->update();
     setDocumentModified();
 }
@@ -6300,6 +6655,33 @@ void CustomTrackView::slotSelectTrack(int ix)
     viewport()->update();
 }
 
+void CustomTrackView::slotSelectClipsInTrack()
+{
+    QRectF rect(0, m_selectedTrack * m_tracksHeight + m_tracksHeight / 2, sceneRect().width(), m_tracksHeight / 2 - 1);
+    QList<QGraphicsItem *> selection = m_scene->items(rect);
+    m_scene->clearSelection();
+    for (int i = 0; i < selection.count(); i++) {
+        if (selection.at(i)->type() == AVWIDGET || selection.at(i)->type() == TRANSITIONWIDGET || selection.at(i)->type() == GROUPWIDGET) {
+            selection.at(i)->setSelected(true);
+        }
+    }    
+    resetSelectionGroup();
+    groupSelectedItems();
+}
+
+void CustomTrackView::slotSelectAllClips()
+{
+    QList<QGraphicsItem *> selection = m_scene->items();
+    m_scene->clearSelection();
+    for (int i = 0; i < selection.count(); i++) {
+        if (selection.at(i)->type() == AVWIDGET || selection.at(i)->type() == TRANSITIONWIDGET  || selection.at(i)->type() == GROUPWIDGET) {
+            selection.at(i)->setSelected(true);
+        }
+    }
+    resetSelectionGroup();
+    groupSelectedItems();
+}
+
 void CustomTrackView::selectClip(bool add, bool group, int track, int pos)
 {
     QRectF rect;
@@ -6350,9 +6732,7 @@ QStringList CustomTrackView::extractTransitionsLumas()
             if (!luma.isEmpty()) urls << KUrl(luma).path();
         }
     }
-#if QT_VERSION >= 0x040500
     urls.removeDuplicates();
-#endif
     return urls;
 }
 
@@ -6406,12 +6786,12 @@ void CustomTrackView::insertZoneOverwrite(QStringList data, int in)
         splitAudio();
 }
 
-void CustomTrackView::clearSelection()
+void CustomTrackView::clearSelection(bool emitInfo)
 {
     resetSelectionGroup();
     scene()->clearSelection();
     m_dragItem = NULL;
-    emit clipItemSelected(NULL);
+    if (emitInfo) emit clipItemSelected(NULL);
 }
 
 void CustomTrackView::updatePalette()
@@ -6423,7 +6803,6 @@ void CustomTrackView::updatePalette()
         pen1.setColor(palette().text().color());
         m_cursorLine->setPen(pen1);
     }
-    emit tracksChanged();
 }
 
 void CustomTrackView::removeTipAnimation()
@@ -6528,45 +6907,101 @@ bool CustomTrackView::hasAudio(int track) const
     return false;
 }
 
-void CustomTrackView::slotAddTrackEffect(const QDomElement effect, int ix)
+void CustomTrackView::slotAddTrackEffect(const QDomElement &effect, int ix)
 {
-    AddEffectCommand *command = new AddEffectCommand(this, m_document->tracksCount() - ix, GenTime(-1), effect, true);
-    m_commandStack->push(command);
-    setDocumentModified();
+    
+    QUndoCommand *effectCommand = new QUndoCommand();
+    QString effectName;
+    if (effect.tagName() == "effectgroup") {
+        effectName = effect.attribute("name");
+    } else {
+        QDomElement namenode = effect.firstChildElement("name");
+        if (!namenode.isNull()) effectName = i18n(namenode.text().toUtf8().data());
+        else effectName = i18n("effect");
+    }
+    effectCommand->setText(i18n("Add %1", effectName));
+    if (effect.tagName() == "effectgroup") {
+        QDomNodeList effectlist = effect.elementsByTagName("effect");
+        for (int j = 0; j < effectlist.count(); j++) {
+            QDomElement trackeffect = effectlist.at(j).toElement();
+            if (trackeffect.attribute("unique", "0") != "0" && m_document->hasTrackEffect(m_document->tracksCount() - ix - 1, trackeffect.attribute("tag"), trackeffect.attribute("id")) != -1) {
+                emit displayMessage(i18n("Effect already present in track"), ErrorMessage);
+                continue;
+            }
+            new AddEffectCommand(this, m_document->tracksCount() - ix, GenTime(-1), trackeffect, true, effectCommand);
+        }
+    }
+    else {
+        if (effect.attribute("unique", "0") != "0" && m_document->hasTrackEffect(m_document->tracksCount() - ix - 1, effect.attribute("tag"), effect.attribute("id")) != -1) {
+            emit displayMessage(i18n("Effect already present in track"), ErrorMessage);
+            delete effectCommand;
+            return;
+        }
+        new AddEffectCommand(this, m_document->tracksCount() - ix, GenTime(-1), effect, true, effectCommand);
+    }
+
+    if (effectCommand->childCount() > 0) {
+        m_commandStack->push(effectCommand);
+        setDocumentModified();
+    }
+    else delete effectCommand;
 }
 
 
-EffectsParameterList CustomTrackView::getEffectArgs(const QDomElement effect)
+EffectsParameterList CustomTrackView::getEffectArgs(const QDomElement &effect)
 {
     EffectsParameterList parameters;
     QLocale locale;
     parameters.addParam("tag", effect.attribute("tag"));
-    if (effect.hasAttribute("region")) parameters.addParam("region", effect.attribute("region"));
+    //if (effect.hasAttribute("region")) parameters.addParam("region", effect.attribute("region"));
     parameters.addParam("kdenlive_ix", effect.attribute("kdenlive_ix"));
+    parameters.addParam("kdenlive_info", effect.attribute("kdenlive_info"));
     parameters.addParam("id", effect.attribute("id"));
     if (effect.hasAttribute("src")) parameters.addParam("src", effect.attribute("src"));
     if (effect.hasAttribute("disable")) parameters.addParam("disable", effect.attribute("disable"));
     if (effect.hasAttribute("in")) parameters.addParam("in", effect.attribute("in"));
     if (effect.hasAttribute("out")) parameters.addParam("out", effect.attribute("out"));
+    if (effect.attribute("id") == "region") {
+       QDomNodeList subeffects = effect.elementsByTagName("effect");
+       for (int i = 0; i < subeffects.count(); i++) {
+           QDomElement subeffect = subeffects.at(i).toElement();
+           int subeffectix = subeffect.attribute("region_ix").toInt();
+           parameters.addParam(QString("filter%1").arg(subeffectix), subeffect.attribute("id"));
+           parameters.addParam(QString("filter%1.tag").arg(subeffectix), subeffect.attribute("tag"));
+           parameters.addParam(QString("filter%1.kdenlive_info").arg(subeffectix), subeffect.attribute("kdenlive_info"));
+           QDomNodeList subparams = subeffect.elementsByTagName("parameter");
+           adjustEffectParameters(parameters, subparams, QString("filter%1.").arg(subeffectix));
+       }
+    }
 
     QDomNodeList params = effect.elementsByTagName("parameter");
-    for (int i = 0; i < params.count(); i++) {
+    adjustEffectParameters(parameters, params);
+    
+    return parameters;
+}
+
+
+void CustomTrackView::adjustEffectParameters(EffectsParameterList &parameters, QDomNodeList params, const QString &prefix)
+{
+  QLocale locale;
+  for (int i = 0; i < params.count(); i++) {
         QDomElement e = params.item(i).toElement();
+       QString paramname = prefix + e.attribute("name");
         if (e.attribute("type") == "geometry" && !e.hasAttribute("fixed")) {
             // effects with geometry param need in / out synced with the clip, request it...
             parameters.addParam("_sync_in_out", "1");
         }
         if (e.attribute("type") == "simplekeyframe") {
-
             QStringList values = e.attribute("keyframes").split(";", QString::SkipEmptyParts);
             double factor = e.attribute("factor", "1").toDouble();
+            double offset = e.attribute("offset", "0").toDouble();
             for (int j = 0; j < values.count(); j++) {
                 QString pos = values.at(j).section(':', 0, 0);
-                double val = values.at(j).section(':', 1, 1).toDouble() / factor;
+                double val = (values.at(j).section(':', 1, 1).toDouble() - offset) / factor;
                 values[j] = pos + "=" + locale.toString(val);
             }
             // kDebug() << "/ / / /SENDING KEYFR:" << values;
-            parameters.addParam(e.attribute("name"), values.join(";"));
+            parameters.addParam(paramname, values.join(";"));
             /*parameters.addParam(e.attribute("name"), e.attribute("keyframes").replace(":", "="));
             parameters.addParam("max", e.attribute("max"));
             parameters.addParam("min", e.attribute("min"));
@@ -6577,6 +7012,7 @@ EffectsParameterList CustomTrackView::getEffectArgs(const QDomElement effect)
             parameters.addParam("max", e.attribute("max"));
             parameters.addParam("min", e.attribute("min"));
             parameters.addParam("factor", e.attribute("factor", "1"));
+            parameters.addParam("offset", e.attribute("offset", "0"));
             parameters.addParam("starttag", e.attribute("starttag", "start"));
             parameters.addParam("endtag", e.attribute("endtag", "end"));
         } else if (e.attribute("namedesc").contains(';')) {
@@ -6593,20 +7029,23 @@ EffectsParameterList CustomTrackView::getEffectArgs(const QDomElement effect)
             }
             parameters.addParam("start", neu);
         } else {
-            if (e.attribute("factor", "1") != "1") {
+            if (e.attribute("factor", "1") != "1" || e.attribute("offset", "0") != "0") {
                 double fact;
                 if (e.attribute("factor").contains('%')) {
                     fact = ProfilesDialog::getStringEval(m_document->mltProfile(), e.attribute("factor"));
-                } else fact = e.attribute("factor", "1").toDouble();
-                parameters.addParam(e.attribute("name"), locale.toString(e.attribute("value").toDouble() / fact));
+                } else {
+                    fact = e.attribute("factor", "1").toDouble();
+                }
+                double offset = e.attribute("offset", "0").toDouble();
+                parameters.addParam(paramname, locale.toString((e.attribute("value").toDouble() - offset) / fact));
             } else {
-                parameters.addParam(e.attribute("name"), e.attribute("value"));
+                parameters.addParam(paramname, e.attribute("value"));
             }
         }
     }
-    return parameters;
 }
 
+
 void CustomTrackView::updateTrackNames(int track, bool added)
 {
     QList <TrackInfo> tracks = m_document->tracksList();
@@ -6696,10 +7135,35 @@ void CustomTrackView::adjustEffects(ClipItem* item, ItemInfo oldInfo, QUndoComma
     if (effects.count()) {
         QMap<int, QDomElement>::const_iterator i = effects.constBegin();
         while (i != effects.constEnd()) {
-            new EditEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), i.value(), item->effectAt(i.key()), i.key(), false, command);
+            new EditEffectCommand(this, m_document->tracksCount() - item->track(), item->startPos(), i.value(), item->effect(i.key()), i.key(), false, false, command);
+            ++i;
+        }
+    }
+}
+
+
+void CustomTrackView::slotGotFilterJobResults(const QString &/*id*/, int startPos, int track, const QString &filter, stringMap filterParams)
+{
+    ClipItem *clip = getClipItemAt(GenTime(startPos, m_document->fps()), track);
+    if (clip == NULL) {
+        emit displayMessage(i18n("Cannot find clip for effect update %1.", filter), ErrorMessage);
+        return;
+    }
+    QDomElement newEffect;
+    QDomElement effect = clip->getEffectAtIndex(clip->selectedEffectIndex());
+    if (effect.attribute("id") == filter) {
+        newEffect = effect.cloneNode().toElement();
+        QMap<QString, QString>::const_iterator i = filterParams.constBegin();
+        while (i != filterParams.constEnd()) {
+            EffectsList::setParameter(newEffect, i.key(), i.value());
+            kDebug()<<"// RESULT FILTER: "<<i.key()<<"="<< i.value();
             ++i;
         }
+        EditEffectCommand *command = new EditEffectCommand(this, m_document->tracksCount() - clip->track(), clip->startPos(), effect, newEffect, clip->selectedEffectIndex(), true, true);
+        m_commandStack->push(command);
+        emit clipItemSelected(clip);
     }
+    
 }