customtrackview.cpp 369 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/***************************************************************************
 *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
 ***************************************************************************/


Vincent Pinon's avatar
Vincent Pinon committed
21
#include "customtrackview.h"
22 23
#include "timeline.h"
#include "track.h"
Vincent Pinon's avatar
Vincent Pinon committed
24 25 26 27 28 29 30 31 32
#include "clipitem.h"
#include "timelinecommands.h"
#include "transition.h"
#include "markerdialog.h"
#include "clipdurationdialog.h"
#include "abstractgroupitem.h"
#include "spacerdialog.h"
#include "trackdialog.h"
#include "tracksconfigdialog.h"
33
#include "mltcontroller/clipcontroller.h"
34
#include "mltcontroller/effectscontroller.h"
35
#include "definitions.h"
36
#include "kdenlivesettings.h"
37
#include "renderer.h"
38
#include "bin/projectclip.h"
39
#include "mainwindow.h"
40
#include "transitionhandler.h"
Vincent Pinon's avatar
Vincent Pinon committed
41
#include "project/clipmanager.h"
42
#include "utils/KoIconUtils.h"
Vincent Pinon's avatar
Vincent Pinon committed
43
#include "effectslist/initeffects.h"
44
#include "effectstack/widgets/keyframeimport.h"
Vincent Pinon's avatar
Vincent Pinon committed
45
#include "dialogs/profilesdialog.h"
46 47 48
#include "managers/guidemanager.h"
#include "managers/razormanager.h"
#include "managers/selectmanager.h"
49
#include "ui_keyframedialog_ui.h"
50
#include "ui_addtrack_ui.h"
51

Simon Eugster's avatar
Simon Eugster committed
52 53 54
#include "lib/audio/audioEnvelope.h"
#include "lib/audio/audioCorrelation.h"

Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
55
#include <QDebug>
56
#include <klocalizedstring.h>
57
#include <KCursor>
58
#include <KMessageBox>
59
#include <KRecentDirs>
60

61 62
#include <QUrl>
#include <QIcon>
63 64 65 66
#include <QMouseEvent>
#include <QGraphicsItem>
#include <QScrollBar>
#include <QApplication>
67
#include <QMimeData>
68

69 70
#include <QGraphicsDropShadowEffect>

71
#define SEEK_INACTIVE (-1)
72 73
//#define DEBUG

74 75 76 77 78
bool sortGuidesList(const Guide *g1 , const Guide *g2)
{
    return (*g1).position() < (*g2).position();
}

79
CustomTrackView::CustomTrackView(KdenliveDoc *doc, Timeline *timeline, CustomTrackScene* projectscene, QWidget *parent) :
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
80
    QGraphicsView(projectscene, parent)
81 82 83
  , m_tracksHeight(KdenliveSettings::trackheight())
  , m_projectDuration(0)
  , m_cursorPos(0)
84
  , m_cursorOffset(0)
85
  , m_document(doc)
Vincent Pinon's avatar
Vincent Pinon committed
86
  , m_timeline(timeline)
87 88
  , m_scene(projectscene)
  , m_cursorLine(NULL)
89
  , m_cutLine(NULL)
90 91
  , m_operationMode(None)
  , m_moveOpMode(None)
92 93 94
  , m_dragItem(NULL)
  , m_dragGuide(NULL)
  , m_visualTip(NULL)
95
  , m_keyProperties(NULL)
96 97 98 99
  , m_autoScroll(KdenliveSettings::autoscroll())
  , m_timelineContextMenu(NULL)
  , m_timelineContextClipMenu(NULL)
  , m_timelineContextTransitionMenu(NULL)
100
  , m_timelineContextKeyframeMenu(NULL)
Vincent Pinon's avatar
Vincent Pinon committed
101
  , m_selectKeyframeType(NULL)
102 103 104 105 106 107 108 109 110 111
  , m_markerMenu(NULL)
  , m_autoTransition(NULL)
  , m_pasteEffectsAction(NULL)
  , m_ungroupAction(NULL)
  , m_editGuide(NULL)
  , m_deleteGuide(NULL)
  , m_clipTypeGroup(NULL)
  , m_scrollOffset(0)
  , m_clipDrag(false)
  , m_findIndex(0)
112
  , m_tool(SelectTool)
113 114 115
  , m_copiedItems()
  , m_menuPosition()
  , m_selectionGroup(NULL)
116
  , m_selectedTrack(1)
117 118 119 120
  , m_spacerOffset(0)
  , m_audioCorrelator(NULL)
  , m_audioAlignmentReference(NULL)
  , m_controlModifier(false)
Simon Eugster's avatar
Simon Eugster committed
121 122
{
    if (doc) {
123
        m_commandStack = doc->commandStack();
Simon Eugster's avatar
Simon Eugster committed
124
    } else {
125
        m_commandStack = NULL;
Simon Eugster's avatar
Simon Eugster committed
126
    }
127
    m_ct = 0;
128 129
    setMouseTracking(true);
    setAcceptDrops(true);
130 131
    setFrameShape(QFrame::NoFrame);
    setLineWidth(0);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
132
    //setCacheMode(QGraphicsView::CacheBackground);
133
    setAutoFillBackground(false);
134
    setViewportUpdateMode(QGraphicsView::MinimalViewportUpdate);
135
    setContentsMargins(0, 0, 0, 0);
136 137 138
    KColorScheme scheme(palette().currentColorGroup(), KColorScheme::Window, KSharedConfig::openConfig(KdenliveSettings::colortheme()));
    m_selectedTrackColor = scheme.background(KColorScheme::ActiveBackground ).color();
    m_selectedTrackColor.setAlpha(150);
139

140 141 142 143
    m_keyPropertiesTimer = new QTimeLine(800);
    m_keyPropertiesTimer->setFrameRange(0, 5);
    m_keyPropertiesTimer->setUpdateInterval(100);
    m_keyPropertiesTimer->setLoopCount(0);
144

145
    m_tipColor = QColor(0, 192, 0, 200);
146
    m_tipPen.setColor(QColor(255, 255, 255, 100));
147
    m_tipPen.setWidth(3);
148

149 150
    setSceneRect(0, 0, sceneRect().width(), m_tracksHeight);
    verticalScrollBar()->setMaximum(m_tracksHeight);
151 152 153
    verticalScrollBar()->setTracking(true);
    // repaint guides when using vertical scroll
    connect(verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(slotRefreshGuides()));
154
    connect(horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(slotRefreshCutLine()));
155

156
    m_cursorLine = projectscene->addLine(0, 0, 0, m_tracksHeight);
157
    m_cursorLine->setZValue(1000);
158 159
    QPen pen1 = QPen();
    pen1.setWidth(1);
160 161 162
    QColor line(palette().text().color());
    line.setAlpha(100);
    pen1.setColor(line);
163
    m_cursorLine->setPen(pen1);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
164

165
    connect(&m_scrollTimer, SIGNAL(timeout()), this, SLOT(slotCheckMouseScrolling()));
166
    m_scrollTimer.setInterval(100);
167
    m_scrollTimer.setSingleShot(true);
168 169 170 171

    connect(&m_thumbsTimer, SIGNAL(timeout()), this, SLOT(slotFetchNextThumbs()));
    m_thumbsTimer.setInterval(500);
    m_thumbsTimer.setSingleShot(true);
172

173
    QIcon razorIcon = KoIconUtils::themedIcon(QStringLiteral("edit-cut"));
174
    m_razorCursor = QCursor(razorIcon.pixmap(32, 32));
175
    m_spacerCursor = QCursor(Qt::SplitHCursor);
176
    connect(m_document->renderer(), SIGNAL(prepareTimelineReplacement(QString)), this, SLOT(slotPrepareTimelineReplacement(QString)), Qt::DirectConnection);
177
    connect(m_document->renderer(), SIGNAL(replaceTimelineProducer(QString)), this, SLOT(slotReplaceTimelineProducer(QString)), Qt::DirectConnection);
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
178
    connect(m_document->renderer(), SIGNAL(updateTimelineProducer(QString)), this, SLOT(slotUpdateTimelineProducer(QString)));
179
    connect(m_document->renderer(), SIGNAL(rendererPosition(int)), this, SLOT(setCursorPos(int)));
180 181
    scale(1, 1);
    setAlignment(Qt::AlignLeft | Qt::AlignTop);
182 183
}

184 185
CustomTrackView::~CustomTrackView()
{
186
    qDeleteAll(m_guides);
187
    m_guides.clear();
188
    m_waitingThumbs.clear();
189
    delete m_keyPropertiesTimer;
190 191
}

192 193 194 195 196 197 198 199 200 201 202 203
//virtual
void CustomTrackView::keyPressEvent(QKeyEvent * event)
{
    if (event->key() == Qt::Key_Up) {
        slotTrackUp();
        event->accept();
    } else if (event->key() == Qt::Key_Down) {
        slotTrackDown();
        event->accept();
    } else QWidget::keyPressEvent(event);
}

204 205
void CustomTrackView::setDocumentModified()
{
206 207
    m_document->setModified(true);
}
208

209
void CustomTrackView::setContextMenu(QMenu *timeline, QMenu *clip, QMenu *transition, QActionGroup *clipTypeGroup, QMenu *markermenu)
210
{
211
    m_clipTypeGroup = clipTypeGroup;
212 213
    m_timelineContextMenu = timeline;
    m_timelineContextClipMenu = clip;
214 215
    m_timelineContextTransitionMenu = transition;
    connect(m_timelineContextTransitionMenu, SIGNAL(aboutToHide()), this, SLOT(slotResetMenuPosition()));
216
    connect(m_timelineContextMenu, SIGNAL(aboutToHide()), this, SLOT(slotResetMenuPosition()));
217 218 219 220
    connect(m_timelineContextClipMenu, SIGNAL(aboutToHide()), this, SLOT(slotResetMenuPosition()));
    connect(m_timelineContextTransitionMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotContextMenuActivated()));
    connect(m_timelineContextMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotContextMenuActivated()));
    connect(m_timelineContextClipMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotContextMenuActivated()));
221 222 223 224

    m_markerMenu = new QMenu(i18n("Go to marker..."), this);
    m_markerMenu->setEnabled(false);
    markermenu->addMenu(m_markerMenu);
Laurent Montel's avatar
Laurent Montel committed
225
    connect(m_markerMenu, SIGNAL(triggered(QAction*)), this, SLOT(slotGoToMarker(QAction*)));
226
    QList <QAction *> list = m_timelineContextClipMenu->actions();
227
    for (int i = 0; i < list.count(); ++i) {
228 229 230 231
        if (list.at(i)->data().toString() == QLatin1String("paste_effects")) m_pasteEffectsAction = list.at(i);
        else if (list.at(i)->data().toString() == QLatin1String("ungroup_clip")) m_ungroupAction = list.at(i);
        else if (list.at(i)->data().toString() == QLatin1String("A")) m_audioActions.append(list.at(i));
        else if (list.at(i)->data().toString() == QLatin1String("A+V")) m_avActions.append(list.at(i));
232
    }
233
    
234
    list = m_timelineContextTransitionMenu->actions();
235
    for (int i = 0; i < list.count(); ++i) {
236
        if (list.at(i)->data().toString() == QLatin1String("auto")) {
237 238 239 240
            m_autoTransition = list.at(i);
            break;
        }
    }
241 242

    m_timelineContextMenu->addSeparator();
243
    m_deleteGuide = new QAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Delete Guide"), this);
244 245 246
    connect(m_deleteGuide, SIGNAL(triggered()), this, SLOT(slotDeleteTimeLineGuide()));
    m_timelineContextMenu->addAction(m_deleteGuide);

247
    m_editGuide = new QAction(QIcon::fromTheme(QStringLiteral("document-properties")), i18n("Edit Guide"), this);
248 249
    connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditTimeLineGuide()));
    m_timelineContextMenu->addAction(m_editGuide);
250 251
}

252 253 254 255 256 257 258 259 260 261 262
void CustomTrackView::slotDoResetMenuPosition()
{
    m_menuPosition = QPoint();
}

void CustomTrackView::slotResetMenuPosition()
{
    // after a short time (so that the action is triggered / or menu is closed, we reset the menu pos
    QTimer::singleShot(300, this, SLOT(slotDoResetMenuPosition()));
}

263 264 265 266 267 268
void CustomTrackView::slotContextMenuActivated()
{
    // Menu disappeared, restore default operation mode
    m_operationMode = None;
}

269 270
void CustomTrackView::checkAutoScroll()
{
271
    m_autoScroll = KdenliveSettings::autoscroll();
272 273
}

274
int CustomTrackView::getFrameWidth() const
275
{
276
    return (int) (m_tracksHeight * m_document->dar() + 0.5);
277 278
}

279
void CustomTrackView::updateSceneFrameWidth(bool fpsChanged)
280 281
{
    int frameWidth = getFrameWidth();
282 283 284 285 286 287 288 289 290 291
    if (fpsChanged && m_projectDuration > 0) {
        reloadTimeline();
    } else {
        QList<QGraphicsItem *> itemList = items();
        ClipItem *item;
        for (int i = 0; i < itemList.count(); ++i) {
            if (itemList.at(i)->type() == AVWidget) {
                item = static_cast<ClipItem*>(itemList.at(i));
                item->resetFrameWidth(frameWidth);
            }
292 293 294 295
        }
    }
}

296
bool CustomTrackView::checkTrackHeight(bool force)
297
{
298
    if (!force && m_tracksHeight == KdenliveSettings::trackheight() && sceneRect().height() == m_tracksHeight * m_timeline->visibleTracksCount()) return false;
299
    int frameWidth = getFrameWidth();
300 301 302 303 304 305 306 307 308 309
    if (m_tracksHeight != KdenliveSettings::trackheight()) {
        QList<QGraphicsItem *> itemList = items();
        ClipItem *item;
        Transition *transitionitem;
        m_tracksHeight = KdenliveSettings::trackheight();
        // Remove all items, and re-add them one by one to avoid collisions
        for (int i = 0; i < itemList.count(); ++i) {
            if (itemList.at(i)->type() == AVWidget || itemList.at(i)->type() == TransitionWidget) {
                m_scene->removeItem(itemList.at(i));
            }
310
        }
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
        bool snap = KdenliveSettings::snaptopoints();
        KdenliveSettings::setSnaptopoints(false);
        for (int i = 0; i < itemList.count(); ++i) {
            if (itemList.at(i)->type() == AVWidget) {
                item = static_cast<ClipItem*>(itemList.at(i));
                item->setRect(0, 0, item->rect().width(), m_tracksHeight - 1);
                item->setPos((qreal) item->startPos().frames(m_document->fps()), getPositionFromTrack(item->track()) + 1);
                m_scene->addItem(item);
                item->resetFrameWidth(frameWidth);
            } else if (itemList.at(i)->type() == TransitionWidget) {
                transitionitem = static_cast<Transition*>(itemList.at(i));
                transitionitem->setRect(0, 0, transitionitem->rect().width(), m_tracksHeight / 3 * 2 - 1);
                transitionitem->setPos((qreal) transitionitem->startPos().frames(m_document->fps()), getPositionFromTrack(transitionitem->track()) + transitionitem->itemOffset());
                m_scene->addItem(transitionitem);
            }
326
        }
327
        KdenliveSettings::setSnaptopoints(snap);
328
    }
329
    double newHeight = m_tracksHeight * m_timeline->visibleTracksCount() * matrix().m22();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
330
    m_cursorLine->setLine(0, 0, 0, newHeight - 1);
331
    if (m_cutLine) {
332
        m_cutLine->setLine(0, 0, 0, m_tracksHeight * m_scene->scale().y());
333
    }
334
    for (int i = 0; i < m_guides.count(); ++i) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
335
        m_guides.at(i)->setLine(0, 0, 0, newHeight - 1);
336 337
    }

338
    setSceneRect(0, 0, sceneRect().width(), m_tracksHeight * m_timeline->visibleTracksCount());
339
    viewport()->update();
340
    return true;
341 342
}

343 344 345 346 347 348 349 350
/** Zoom or move viewport on mousewheel
 *
 * If mousewheel+Ctrl, zooms in/out on the timeline.
 *
 * With Ctrl, moves viewport towards end of timeline if down/back,
 * opposite on up/forward.
 *
 * See also http://www.kdenlive.org/mantis/view.php?id=265 */
351 352
void CustomTrackView::wheelEvent(QWheelEvent * e)
{
353
    if (e->modifiers() == Qt::ControlModifier) {
354
        if (m_moveOpMode == None || m_moveOpMode == WaitingForConfirm || m_moveOpMode == ZoomTimeline) {
355 356 357
            if (e->delta() > 0) emit zoomIn();
            else emit zoomOut();
        }
358 359 360 361 362
    } else if (e->modifiers() == Qt::AltModifier) {
        if (m_moveOpMode == None || m_moveOpMode == WaitingForConfirm || m_moveOpMode == ZoomTimeline) {
            if (e->delta() > 0) slotSeekToNextSnap();
            else slotSeekToPreviousSnap();
        }
363
    } else {
364
        if (m_moveOpMode == ResizeStart || m_moveOpMode == ResizeEnd) {
365 366 367
            // Don't allow scrolling + resizing
            return;
        }
368 369 370 371
        if (m_operationMode == None || m_operationMode == ZoomTimeline) {
            // Prevent unwanted object move
            m_scene->isZooming = true;
        }
372
        if (e->delta() <= 0) horizontalScrollBar()->setValue(horizontalScrollBar()->value() + horizontalScrollBar()->singleStep());
373
        else  horizontalScrollBar()->setValue(horizontalScrollBar()->value() - horizontalScrollBar()->singleStep());
374 375 376
        if (m_operationMode == None || m_operationMode == ZoomTimeline) {
            m_scene->isZooming = false;
        }
377
    }
378 379
}

380 381
int CustomTrackView::getPreviousVideoTrack(int track)
{
382 383
    int i = track - 1;
    for (; i > 0; i--) {
384
        if (m_timeline->getTrackInfo(i).type == VideoTrack) break;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
385
    }
386
    return i;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
387
}
388

389 390 391 392 393 394 395 396
int CustomTrackView::getNextVideoTrack(int track)
{
    for (; track < m_timeline->visibleTracksCount(); track++) {
        if (m_timeline->getTrackInfo(track).type == VideoTrack) break;
    }
    return track;
}

397

398 399 400 401
void CustomTrackView::slotFetchNextThumbs()
{
    if (!m_waitingThumbs.isEmpty()) {
        ClipItem *item = m_waitingThumbs.takeFirst();
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
402
        while (item == NULL && !m_waitingThumbs.isEmpty()) {
403 404 405 406 407 408 409
            item = m_waitingThumbs.takeFirst();
        }
        if (item) item->slotFetchThumbs();
        if (!m_waitingThumbs.isEmpty()) m_thumbsTimer.start();
    }
}

410 411
void CustomTrackView::slotCheckMouseScrolling()
{
412 413 414 415 416 417 418 419
    if (m_scrollOffset == 0) {
        m_scrollTimer.stop();
        return;
    }
    horizontalScrollBar()->setValue(horizontalScrollBar()->value() + m_scrollOffset);
    m_scrollTimer.start();
}

420 421
void CustomTrackView::slotCheckPositionScrolling()
{
422
    // If mouse is at a border of the view, scroll
423
    if (m_moveOpMode != Seek) return;
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
424
    if (mapFromScene(m_cursorPos, 0).x() < 3) {
425
        if (horizontalScrollBar()->value() == 0) return;
426 427
        horizontalScrollBar()->setValue(horizontalScrollBar()->value() - 2);
        QTimer::singleShot(200, this, SLOT(slotCheckPositionScrolling()));
428
        seekCursorPos(mapToScene(QPoint(-2, 0)).x());
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
429
    } else if (viewport()->width() - 3 < mapFromScene(m_cursorPos + 1, 0).x()) {
430
        horizontalScrollBar()->setValue(horizontalScrollBar()->value() + 2);
431
        seekCursorPos(mapToScene(QPoint(viewport()->width(), 0)).x() + 1);
432 433 434 435
        QTimer::singleShot(200, this, SLOT(slotCheckPositionScrolling()));
    }
}

436
void CustomTrackView::slotAlignPlayheadToMousePos()
437
{
438 439 440 441 442 443
    /* get curser point ref in screen coord */
    QPoint ps = QCursor::pos();
    /* get xPos in scene coord */
    int mappedXPos = qMax((int)(mapToScene(mapFromGlobal(ps)).x() + 0.5), 0);
    /* move playhead to new xPos*/
    seekCursorPos(mappedXPos);
444
}
445

446 447 448 449 450
int CustomTrackView::getMousePos() const
{
    return qMax((int)(mapToScene(mapFromGlobal(QCursor::pos())).x() + 0.5), 0);
}

451 452
void CustomTrackView::spaceToolMoveToSnapPos(double snappedPos)
{
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
    // Make sure there is no collision
    QList<QGraphicsItem *> children = m_selectionGroup->childItems();
    QPainterPath shape = m_selectionGroup->clipGroupSpacerShape(QPointF(snappedPos - m_selectionGroup->sceneBoundingRect().left(), 0));
    QList<QGraphicsItem*> collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
    collidingItems.removeAll(m_selectionGroup);
    for (int i = 0; i < children.count(); ++i) {
        if (children.at(i)->type() == GroupWidget) {
            QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
            for (int j = 0; j < subchildren.count(); ++j)
                collidingItems.removeAll(subchildren.at(j));
        }
        collidingItems.removeAll(children.at(i));
    }
    bool collision = false;
    int offset = 0;
    for (int i = 0; i < collidingItems.count(); ++i) {
        if (!collidingItems.at(i)->isEnabled()) continue;
        if (collidingItems.at(i)->type() == AVWidget && snappedPos < m_selectionGroup->sceneBoundingRect().left()) {
            AbstractClipItem *item = static_cast <AbstractClipItem *>(collidingItems.at(i));
            // Moving backward, determine best pos
            QPainterPath clipPath;
            clipPath.addRect(item->sceneBoundingRect());
            QPainterPath res = shape.intersected(clipPath);
            offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
        }
    }
    snappedPos += offset;
    // make sure we have no collision
    shape = m_selectionGroup->clipGroupSpacerShape(QPointF(snappedPos - m_selectionGroup->sceneBoundingRect().left(), 0));
    collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
    collidingItems.removeAll(m_selectionGroup);
    for (int i = 0; i < children.count(); ++i) {
        if (children.at(i)->type() == GroupWidget) {
            QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
            for (int j = 0; j < subchildren.count(); ++j)
                collidingItems.removeAll(subchildren.at(j));
        }
        collidingItems.removeAll(children.at(i));
    }
492

493 494 495 496 497 498 499
    for (int i = 0; i < collidingItems.count(); ++i) {
        if (!collidingItems.at(i)->isEnabled()) continue;
        if (collidingItems.at(i)->type() == AVWidget) {
            collision = true;
            break;
        }
    }
500 501


502 503 504 505 506 507 508 509 510 511 512 513 514 515
    if (!collision) {
        // Check transitions
        shape = m_selectionGroup->transitionGroupShape(QPointF(snappedPos - m_selectionGroup->sceneBoundingRect().left(), 0));
        collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
        collidingItems.removeAll(m_selectionGroup);
        for (int i = 0; i < children.count(); ++i) {
            if (children.at(i)->type() == GroupWidget) {
                QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
                for (int j = 0; j < subchildren.count(); ++j)
                    collidingItems.removeAll(subchildren.at(j));
            }
            collidingItems.removeAll(children.at(i));
        }
        offset = 0;
516

517 518 519 520 521 522 523 524 525 526
        for (int i = 0; i < collidingItems.count(); ++i) {
            if (collidingItems.at(i)->type() == TransitionWidget && snappedPos < m_selectionGroup->sceneBoundingRect().left()) {
                AbstractClipItem *item = static_cast <AbstractClipItem *>(collidingItems.at(i));
                // Moving backward, determine best pos
                QPainterPath clipPath;
                clipPath.addRect(item->sceneBoundingRect());
                QPainterPath res = shape.intersected(clipPath);
                offset = qMax(offset, (int)(res.boundingRect().width() + 0.5));
            }
        }
527

528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
        snappedPos += offset;
        // make sure we have no collision
        shape = m_selectionGroup->transitionGroupShape(QPointF(snappedPos - m_selectionGroup->sceneBoundingRect().left(), 0));
        collidingItems = scene()->items(shape, Qt::IntersectsItemShape);
        collidingItems.removeAll(m_selectionGroup);
        for (int i = 0; i < children.count(); ++i) {
            if (children.at(i)->type() == GroupWidget) {
                QList<QGraphicsItem *> subchildren = children.at(i)->childItems();
                for (int j = 0; j < subchildren.count(); ++j)
                    collidingItems.removeAll(subchildren.at(j));
            }
            collidingItems.removeAll(children.at(i));
        }
        for (int i = 0; i < collidingItems.count(); ++i) {
            if (collidingItems.at(i)->type() == TransitionWidget) {
                collision = true;
                break;
            }
        }
    }
548

549 550
    if (!collision)
        m_selectionGroup->setTransform(QTransform::fromTranslate(snappedPos - m_selectionGroup->sceneBoundingRect().left(), 0), true);
551 552
}

553
// virtual
554 555
void CustomTrackView::mouseMoveEvent(QMouseEvent * event)
{
556
    int pos = event->x();
557
    int mappedXPos = qMax((int)(mapToScene(event->pos()).x()), 0);
558
    double snappedPos = getSnapPointForPos(mappedXPos);
559
    emit mousePosition(mappedXPos);
560

561 562 563
    if (m_cutLine) {
        m_cutLine->setPos(mappedXPos, getPositionFromTrack(getTrackFromPos(mapToScene(event->pos()).y())));
    }
564 565 566 567 568 569 570 571 572 573
    if (m_moveOpMode == Seek && event->buttons() != Qt::NoButton) {
        QGraphicsView::mouseMoveEvent(event);
        if (mappedXPos != m_document->renderer()->getCurrentSeekPosition() && mappedXPos != cursorPos()) {
                seekCursorPos(mappedXPos);
                slotCheckPositionScrolling();
        }
        return;
    }

    if (m_moveOpMode == ScrollTimeline) {
574 575
        QGraphicsView::mouseMoveEvent(event);
        return;
576
    }
577
    if (event->buttons() & Qt::MidButton) return;
578 579
    if (m_moveOpMode == RubberSelection) {
	QGraphicsView::mouseMoveEvent(event);
580 581 582
        return;
    }

583 584 585 586 587 588 589 590
    if (m_moveOpMode == WaitingForConfirm && event->buttons() != Qt::NoButton) {
	bool move = (event->pos() - m_clickEvent).manhattanLength() >= QApplication::startDragDistance();
	if (move) {
	    m_moveOpMode = m_operationMode;
	}
    }
    if (m_moveOpMode != None && m_moveOpMode != WaitingForConfirm && event->buttons() != Qt::NoButton) {
        if (m_dragItem && m_operationMode != ZoomTimeline) m_clipDrag = true;
591
        if (m_dragItem && m_tool == SelectTool) {
592
            if (m_moveOpMode == MoveOperation && m_clipDrag) {
593
                QGraphicsView::mouseMoveEvent(event);
594 595
                // If mouse is at a border of the view, scroll
                if (pos < 5) {
596
                    m_scrollOffset = -30;
597 598
                    m_scrollTimer.start();
                } else if (viewport()->width() - pos < 10) {
599
                    m_scrollOffset = 30;
600
                    m_scrollTimer.start();
601 602 603
                } else if (m_scrollTimer.isActive()) {
                    m_scrollTimer.stop();
                }
604
            } else if (m_moveOpMode == ResizeStart) {
605
                m_document->renderer()->pause();
606
                if (!m_controlModifier && m_dragItem->type() == AVWidget && m_dragItem->parentItem() && m_dragItem->parentItem() != m_selectionGroup) {
607 608
                    AbstractGroupItem *parent = static_cast <AbstractGroupItem *>(m_dragItem->parentItem());
                    if (parent)
609
                        parent->resizeStart((int)(snappedPos - m_dragItemInfo.startPos.frames(m_document->fps())));
610
                } else {
611
                    m_dragItem->resizeStart((int)(snappedPos), true, false);
612
                }
613 614 615
                QString crop = m_document->timecode().getDisplayTimecode(m_dragItem->cropStart(), KdenliveSettings::frametimecode());
                QString duration = m_document->timecode().getDisplayTimecode(m_dragItem->cropDuration(), KdenliveSettings::frametimecode());
                QString offset = m_document->timecode().getDisplayTimecode(m_dragItem->cropStart() - m_dragItemInfo.cropStart, KdenliveSettings::frametimecode());
616
                emit displayMessage(i18n("Crop from start:") + ' ' + crop + ' ' + i18n("Duration:") + ' ' + duration + ' ' + i18n("Offset:") + ' ' + offset, InformationMessage);
617
            } else if (m_moveOpMode == ResizeEnd) {
618
                m_document->renderer()->pause();
619
                if (!m_controlModifier && m_dragItem->type() == AVWidget && m_dragItem->parentItem() && m_dragItem->parentItem() != m_selectionGroup) {
620
                    AbstractGroupItem *parent = static_cast <AbstractGroupItem *>(m_dragItem->parentItem());
621
                    if (parent) {
622
                        parent->resizeEnd((int)(snappedPos - m_dragItemInfo.endPos.frames(m_document->fps())));
623
                    }
624
                } else {
625
                    m_dragItem->resizeEnd((int)(snappedPos), false);
626
                }
627 628
                QString duration = m_document->timecode().getDisplayTimecode(m_dragItem->cropDuration(), KdenliveSettings::frametimecode());
                QString offset = m_document->timecode().getDisplayTimecode(m_dragItem->cropDuration() - m_dragItemInfo.cropDuration, KdenliveSettings::frametimecode());
629
                emit displayMessage(i18n("Duration:") + ' ' + duration + ' ' + i18n("Offset:") + ' ' + offset, InformationMessage);
630
            } else if (m_moveOpMode == FadeIn) {
Vincent Pinon's avatar
Vincent Pinon committed
631
                static_cast<ClipItem*>(m_dragItem)->setFadeIn(static_cast<int>(mappedXPos - m_dragItem->startPos().frames(m_document->fps())));
632
            } else if (m_moveOpMode == FadeOut) {
Vincent Pinon's avatar
Vincent Pinon committed
633
                static_cast<ClipItem*>(m_dragItem)->setFadeOut(static_cast<int>(m_dragItem->endPos().frames(m_document->fps()) - mappedXPos));
634
            } else if (m_moveOpMode == KeyFrame) {
635 636 637
                GenTime keyFramePos = GenTime(mappedXPos, m_document->fps()) - m_dragItem->startPos();
                double value = m_dragItem->mapFromScene(mapToScene(event->pos()).toPoint()).y();
                m_dragItem->updateKeyFramePos(keyFramePos.frames(fps()), value);
638
                QString position = m_document->timecode().getDisplayTimecodeFromFrames(m_dragItem->selectedKeyFramePos(), KdenliveSettings::frametimecode());
639
                emit displayMessage(position + " : " + QString::number(m_dragItem->editedKeyFrameValue()), InformationMessage);
640
            }
641
            removeTipAnimation();
642
            event->accept();
643
            return;
644
        } else if (m_moveOpMode == MoveGuide) {
645
            removeTipAnimation();
646 647
            QGraphicsView::mouseMoveEvent(event);
            return;
648
        } else if (m_moveOpMode == Spacer && m_selectionGroup) {
649
            // spacer tool
650
            snappedPos = getSnapPointForPos(mappedXPos + m_spacerOffset);
651
            if (snappedPos < 0) snappedPos = 0;
652
            spaceToolMoveToSnapPos(snappedPos);
653
        }
654
    }
655

656
    if (m_tool == SpacerTool) {
657
        setCursor(m_spacerCursor);
658 659
        event->accept();
        return;
660
    }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
661

662 663
    QList<QGraphicsItem *> itemList = items(event->pos());
    QGraphicsRectItem *item = NULL;
664

665 666 667
    bool abort = false;
    GuideManager::checkOperation(itemList, this, event, m_operationMode, abort);
    if (abort) {
668 669
        return;
    }
670

671 672 673 674 675
    if (!abort) {
        for (int i = 0; i < itemList.count(); ++i) {
            if (itemList.at(i)->type() == AVWidget || itemList.at(i)->type() == TransitionWidget) {
                item = (QGraphicsRectItem*) itemList.at(i);
                break;
676 677
            }
        }
678 679 680 681 682 683 684 685
    }
    switch (m_tool) {
        case RazorTool:
            setCursor(m_razorCursor);
            RazorManager::checkOperation(item, this, event, mappedXPos, m_operationMode, abort);
            break;
        case SelectTool:
        default:
686
            SelectManager::checkOperation(item, this, event, m_selectionGroup, m_operationMode, m_moveOpMode);
687 688 689
            break;
    }
}
690

691 692 693 694
QString CustomTrackView::getDisplayTimecode(const GenTime &time) const
{
    return m_document->timecode().getDisplayTimecode(time, KdenliveSettings::frametimecode());
}
695

696 697 698 699
QString CustomTrackView::getDisplayTimecodeFromFrames(int frames) const
{
    return m_document->timecode().getDisplayTimecodeFromFrames(frames, KdenliveSettings::frametimecode());
}
700

701 702 703
void CustomTrackView::graphicsViewMouseEvent(QMouseEvent * event)
{
    QGraphicsView::mouseMoveEvent(event);
704 705
}

706 707
void CustomTrackView::createRectangleSelection(QMouseEvent * event)
{
708
    setDragMode(QGraphicsView::RubberBandDrag);
709
    setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
710 711 712 713
    if (!(event->modifiers() & Qt::ControlModifier)) {
        resetSelectionGroup();
        if (m_dragItem) {
            emit clipItemSelected(NULL);
714
            m_dragItem->setMainSelectedClip(false);
715 716 717
            m_dragItem = NULL;
        }
        scene()->clearSelection();
718
    }
719
    m_moveOpMode = RubberSelection;
720
    QGraphicsView::mousePressEvent(event);
721 722 723 724 725 726 727 728 729
}

QList<QGraphicsItem *> CustomTrackView::selectAllItemsToTheRight(int x)
{
  return items(x, 1, mapFromScene(sceneRect().width(), 0).x() - x, sceneRect().height());
}

int CustomTrackView::spaceToolSelectTrackOnly(int track, QList<QGraphicsItem *> &selection)
{
730
    if (m_timeline->getTrackInfo(track).isLocked) {
731 732 733 734
        // Cannot use spacer on locked track
        emit displayMessage(i18n("Cannot use spacer in a locked track"), ErrorMessage);
        return -1;
    }
735
    QRectF rect(mapToScene(m_clickEvent).x(), getPositionFromTrack(track) + m_tracksHeight / 2, sceneRect().width() - mapToScene(m_clickEvent).x(), m_tracksHeight / 2 - 2);
736 737 738 739 740 741 742 743 744
    bool isOk;
    selection = checkForGroups(rect, &isOk);
    if (!isOk) {
        // groups found on track, do not allow the move
        emit displayMessage(i18n("Cannot use spacer in a track with a group"), ErrorMessage);
        return -1;
    }
    //qDebug() << "SPACER TOOL + CTRL, SELECTING ALL CLIPS ON TRACK " << track << " WITH SELECTION RECT " << m_clickEvent.x() << '/' <<  track * m_tracksHeight + 1 << "; " << mapFromScene(sceneRect().width(), 0).x() - m_clickEvent.x() << '/' << m_tracksHeight - 2;
    return 0;
745 746 747 748
}

void CustomTrackView::createGroupForSelectedItems(QList<QGraphicsItem *> &selection)
{
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
    QList <GenTime> offsetList;
    // create group to hold selected items
    m_selectionMutex.lock();
    m_selectionGroup = new AbstractGroupItem(m_document->fps());
    scene()->addItem(m_selectionGroup);

    for (int i = 0; i < selection.count(); ++i) {
        if (selection.at(i)->parentItem() == 0 && (selection.at(i)->type() == AVWidget || selection.at(i)->type() == TransitionWidget)) {
            AbstractClipItem *item = static_cast<AbstractClipItem *>(selection.at(i));
            if (item->isItemLocked()) continue;
            offsetList.append(item->startPos());
            offsetList.append(item->endPos());
            m_selectionGroup->addItem(selection.at(i));
        } else if (selection.at(i)->type() == GroupWidget) {
            if (static_cast<AbstractGroupItem *>(selection.at(i))->isItemLocked()) continue;
            QList<QGraphicsItem *> children = selection.at(i)->childItems();
            for (int j = 0; j < children.count(); ++j) {
                AbstractClipItem *item = static_cast<AbstractClipItem *>(children.at(j));
                offsetList.append(item->startPos());
                offsetList.append(item->endPos());
            }
            m_selectionGroup->addItem(selection.at(i));
        } else if (selection.at(i)->parentItem() && !selection.contains(selection.at(i)->parentItem())) {
            if (static_cast<AbstractGroupItem *>(selection.at(i)->parentItem())->isItemLocked()) continue;
            m_selectionGroup->addItem(selection.at(i)->parentItem());
        }
    }
    m_spacerOffset = m_selectionGroup->sceneBoundingRect().left() - (int)(mapToScene(m_clickEvent).x());
    m_selectionMutex.unlock();
    if (!offsetList.isEmpty()) {
        qSort(offsetList);
        QList <GenTime> cleandOffsetList;
        GenTime startOffset = offsetList.takeFirst();
        for (int k = 0; k < offsetList.size(); ++k) {
            GenTime newoffset = offsetList.at(k) - startOffset;
            if (newoffset != GenTime() && !cleandOffsetList.contains(newoffset)) {
                cleandOffsetList.append(newoffset);
            }
        }
        updateSnapPoints(NULL, cleandOffsetList, true);
    }
790 791 792 793
}

void CustomTrackView::spaceToolSelect(QMouseEvent * event)
{
794 795 796
    QList<QGraphicsItem *> selection;
    if (event->modifiers() == Qt::ControlModifier) {
        // Ctrl + click, select all items on track after click position
797
        int track = getTrackFromPos(mapToScene(m_clickEvent).y());
798 799 800 801 802 803 804 805 806
        if (spaceToolSelectTrackOnly(track, selection))
            return;
    } else {
        // Select all items on all tracks after click position
        selection = selectAllItemsToTheRight(event->pos().x());
        //qDebug() << "SELELCTING ELEMENTS WITHIN =" << event->pos().x() << '/' <<  1 << ", " << mapFromScene(sceneRect().width(), 0).x() - event->pos().x() << '/' << sceneRect().height();
    }
    createGroupForSelectedItems(selection);
    m_operationMode = Spacer;
807 808 809 810 811 812 813 814
}

void CustomTrackView::selectItemsRightOfFrame(int frame)
{
  QList<QGraphicsItem *> selection = selectAllItemsToTheRight(mapFromScene(frame, 1).x());
  createGroupForSelectedItems(selection);
}

815 816 817 818 819 820 821 822 823 824

void CustomTrackView::updateTimelineSelection()
{
    if (m_dragItem) {
	m_dragItem->setZValue(99);
	if (m_dragItem->parentItem()) m_dragItem->parentItem()->setZValue(99);
	
	// clip selected, update effect stack
	if (m_dragItem->type() == AVWidget && !m_dragItem->isItemLocked()) {
	    ClipItem *selected = static_cast <ClipItem*>(m_dragItem);
825
	    emit clipItemSelected(selected, false);
826 827 828 829 830 831 832 833 834 835 836
	} else {
	    emit clipItemSelected(NULL);
	}
	if (m_dragItem->type() == TransitionWidget && m_dragItem->isEnabled()) {
	    // update transition menu action
	    m_autoTransition->setChecked(static_cast<Transition *>(m_dragItem)->isAutomatic());
	    m_autoTransition->setEnabled(true);
	    // A transition is selected
	    QPoint p;
	    ClipItem *transitionClip = getClipItemAtStart(m_dragItemInfo.startPos, m_dragItemInfo.track);
	    if (transitionClip && transitionClip->binClip()) {
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
837 838 839
		int frameWidth = transitionClip->binClip()->getProducerIntProperty(QStringLiteral("meta.media.width"));
		int frameHeight = transitionClip->binClip()->getProducerIntProperty(QStringLiteral("meta.media.height"));
		double factor = transitionClip->binClip()->getProducerProperty(QStringLiteral("aspect_ratio")).toDouble();
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
		if (factor == 0) factor = 1.0;
		p.setX((int)(frameWidth * factor + 0.5));
		p.setY(frameHeight);
	    }
	    emit transitionItemSelected(static_cast <Transition *>(m_dragItem), getPreviousVideoTrack(m_dragItem->track()), p);
	} else {
	    emit transitionItemSelected(NULL);
	    m_autoTransition->setEnabled(false);
	}
    } else {
	    emit clipItemSelected(NULL);
	    emit transitionItemSelected(NULL);
	    m_autoTransition->setEnabled(false);
    }
}

856
// virtual
857 858
void CustomTrackView::mousePressEvent(QMouseEvent * event)
{
Jean-Baptiste Mardelle's avatar
Fix :  
Jean-Baptiste Mardelle committed
859
    setFocus(Qt::MouseFocusReason);
860
    m_menuPosition = QPoint();
861
    if (m_moveOpMode == MoveOperation) {
862 863 864 865
        // click while dragging, ignore
        event->ignore();
        return;
    }
866
    m_moveOpMode = WaitingForConfirm;
867
    m_clipDrag = false;
868

869
    // special cases (middle click button or ctrl / shift click)
870
    if (event->button() == Qt::MidButton) {
871 872 873
        if (m_operationMode == KeyFrame) {
            if (m_dragItem->type() == AVWidget) {
                ClipItem *item = static_cast<ClipItem *>(m_dragItem);
874
                m_dragItem->insertKeyframe(item->getEffectAtIndex(item->selectedEffectIndex()), m_dragItem->selectedKeyFramePos(), -1, true);
875 876 877 878 879 880
                m_dragItem->update();
            }
        } else {
            emit playMonitor();
            m_operationMode = None;
        }
881 882 883
        return;
    }

884
    if (event->button() == Qt::LeftButton) {
885
        if (event->modifiers() & Qt::ShiftModifier && m_tool == SelectTool) {
886 887 888 889 890 891 892 893
            createRectangleSelection(event);
            return;
        }
        if (m_tool != RazorTool) activateMonitor();
        else if (m_document->renderer()->isPlaying()) {
            m_document->renderer()->pause();
            return;
        }
894 895
    }

896
    m_dragGuide = NULL;
897
    m_clickEvent = event->pos();
898 899

    // check item under mouse
900
    QList<QGraphicsItem *> collisionList = items(m_clickEvent);
901
    if (event->button() == Qt::LeftButton && event->modifiers() == Qt::ControlModifier && m_tool != SpacerTool && collisionList.count() == 0) {
902
        // Pressing Ctrl + left mouse button in an empty area scrolls the timeline
903
        setDragMode(QGraphicsView::ScrollHandDrag);
904
        m_moveOpMode = ScrollTimeline;
905
        QGraphicsView::mousePressEvent(event);
906
        return;
907
    }
908 909
    // if a guide and a clip were pressed, just select the guide
    for (int i = 0; i < collisionList.count(); ++i) {
Alberto Villa's avatar
Alberto Villa committed
910 911
        if (collisionList.at(i)->type() == GUIDEITEM) {
            // a guide item was pressed
Vincent Pinon's avatar
Vincent Pinon committed
912
            m_dragGuide = static_cast<Guide*>(collisionList.at(i));
913 914
            if (event->button() == Qt::LeftButton) { // move it
                m_dragGuide->setFlag(QGraphicsItem::ItemIsMovable, true);
915
                m_operationMode = MoveGuide;
916 917 918 919 920 921 922 923
                // deselect all clips so that only the guide will move
                m_scene->clearSelection();
                resetSelectionGroup(false);
                updateSnapPoints(NULL);
                QGraphicsView::mousePressEvent(event);
                return;
            } else // show context menu
                break;
Alberto Villa's avatar
Alberto Villa committed
924
        }
925 926
    }

927
    // Find first clip, transition or group under mouse (when no guides selected)
928 929
    int ct = 0;
    AbstractGroupItem *dragGroup = NULL;
930 931
    AbstractClipItem *collisionClip = NULL;
    bool found = false;
932 933
    QStringList lockedTracks;
    double yOffset = 0;
934
    m_selectionMutex.lock();
935
    while (!m_dragGuide && ct < collisionList.count()) {
936
        if (collisionList.at(ct)->type() == AVWidget || collisionList.at(ct)->type() == TransitionWidget) {
937
            collisionClip = static_cast <AbstractClipItem *>(collisionList.at(ct));
938
            if (collisionClip->isItemLocked() || !collisionClip->isEnabled()) {
939
                ct++;
940
                continue;
941
            }
942
            if (collisionClip == m_dragItem) {
943
                collisionClip = NULL;
944
            }
945
            else {
946 947 948
                if (m_dragItem) {
                    m_dragItem->setMainSelectedClip(false);
                }
949
                m_dragItem = collisionClip;
950
                m_dragItem->setMainSelectedClip(true);
951
            }
952
            found = true;
953 954
            for (int i = 1; i < m_timeline->tracksCount(); ++i) {
                if (m_timeline->getTrackInfo(i).isLocked) lockedTracks << QString::number(i);
955 956 957 958
            }
            yOffset = mapToScene(m_clickEvent).y() - m_dragItem->scenePos().y();
            m_dragItem->setProperty("y_absolute", yOffset);
            m_dragItem->setProperty("locked_tracks", lockedTracks);
959
            m_dragItemInfo = m_dragItem->info();
960 961 962 963
            if (m_selectionGroup) {
                m_selectionGroup->setProperty("y_absolute", yOffset);
                m_selectionGroup->setProperty("locked_tracks", lockedTracks);
            }
964
            if (m_dragItem->parentItem() && m_dragItem->parentItem()->type() == GroupWidget && m_dragItem->parentItem() != m_selectionGroup) {
965
                QGraphicsItem *topGroup = m_dragItem->parentItem();
966
                while (topGroup->parentItem() && topGroup->parentItem()->type() == GroupWidget && topGroup->parentItem() != m_selectionGroup) {
967 968
                    topGroup = topGroup->parentItem();
                }
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
969
                dragGroup = static_cast <AbstractGroupItem *>(topGroup);
970 971
                dragGroup->setProperty("y_absolute", yOffset);
                dragGroup->setProperty("locked_tracks", lockedTracks);
972
            }
973 974
            break;
        }
975
        ct++;
976
    }
977
    m_selectionMutex.unlock();
978
    if (!found) {
979 980
        if (m_dragItem)
            m_dragItem->setMainSelectedClip(false);
981 982
        m_dragItem = NULL;
    }
983

984
    // Add shadow to dragged item, currently disabled because of painting artifacts
985
    /*if (m_dragItem) {
986 987 988 989
    QGraphicsDropShadowEffect *eff = new QGraphicsDropShadowEffect();
    eff->setBlurRadius(5);
    eff->setOffset(3, 3);
    m_dragItem->setGraphicsEffect(eff);
990
    }*/
991 992

    // No item under click
Jean-Baptiste Mardelle's avatar
Jean-Baptiste Mardelle committed
993
    if (m_dragItem == NULL && m_tool != SpacerTool) {
994 995 996
        resetSelectionGroup(false);
        m_scene->clearSelection();
        updateClipTypeActions(NULL);
997
	updateTimelineSelection();
998
        if (event->button() == Qt::LeftButton) {
999
	    m_moveOpMode = Seek;
1000 1001 1002
            setCursor(Qt::ArrowCursor);
            seekCursorPos((int)(mapToScene(event->x(), 0).x()));
            event->setAccepted(true);
1003
	    QGraphicsView::mousePressEvent(event);
1004 1005 1006 1007
            return;
        }
    }

1008 1009
    // context menu requested
    if (event->button() == Qt::RightButton) {
1010
        // Check if we want keyframes context menu
1011
        if (!m_dragItem && !m_dragGuide) {