Files
qt5-legatus-legacy/Lts/Common/LtsScriptTimeline/track_drag_handler.cpp
2026-08-13 18:33:10 +02:00

184 lines
4.5 KiB
C++
Executable File

#include "track_drag_handler.h"
#include "track_content_widget.h"
#include "track_entry.h"
#include "tools.h"
#include "track_widget.h"
#include "script_timeline.h"
#include <LtsWidgets/lts_message_dialog.h>
#include <QDragEnterEvent>
#include <QDropEvent>
#include <QDrag>
#include <QMimeData>
#include <QGuiApplication>
#include <QApplication>
#include <QDebug>
namespace LtsScriptTimeline
{
void DragHandler::dragEnterEvent(QDragEnterEvent *event)
{
if (m_tcw->isMultiLineMultiSelectionMode()) // in multiline selection mode, ignore enter events from other tracks
{
if (event->source() != m_tcw)
{
event->ignore();
return;
}
else
{
for (const auto &tcw : qAsConst(m_tcwCorresponding))
tcw->dragEnter(tcw->selectedItemsToData(tcw->m_tools->getTime(m_dragStartPos.x())));
}
}
m_tcw->dragEnd();
if (!event->mimeData()->hasFormat(TrackContentWidget::mime))
return;
if (m_tcw->dragEnter(event->mimeData()->data(TrackContentWidget::mime)))
event->acceptProposedAction();
}
void DragHandler::dragLeaveEvent(QDragLeaveEvent *event)
{
if (m_tcw->isMultiLineMultiSelectionMode())
{
event->ignore();
for (const auto &tcw : qAsConst(m_tcwCorresponding))
tcw->dragEnd();
}
m_tcw->dragEnd();
}
void DragHandler::dropEvent(QDropEvent *event)
{
int dropPos = event->pos().x();
int dropTime = m_tcw->m_tools->getTime(dropPos, m_tcw->m_isFilter) - m_tcw->m_dragOffsetTime;
/*
* If set, check other track content widget for collisions to avoid parallel topics of same type,
* since not all topics can be played on both screens at the same time.
*/
for (const auto &tcw : qAsConst(m_tcwCorresponding))
{
bool hasParallelTopicsOfSameType = TrackContentWidget::checkForParallelTopics(tcw, m_tcw->m_preview, dropTime);
if (hasParallelTopicsOfSameType)
{
new LtsWidgets::LtsMessageDialogNew(m_tcw, TrackContentWidget::parallelError(), QObject::tr("Error"));
break;
}
}
if (m_tcw->dragDrop(dropPos))
event->acceptProposedAction();
else
emit m_tcw->signalMultiLineMultiSelectionDone(0);
}
void DragHandler::dragMoveEvent(QDragMoveEvent *event)
{
int pos = event->pos().x();
if (m_tcw->dragMove(pos))
event->acceptProposedAction();
// only called from the master
if (m_tcw->isMultiLineMultiSelectionMode())
emit m_tcw->signalMultiLineMultiSelectionMove(pos);
}
void DragHandler::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() != Qt::LeftButton)
return;
m_dragStartPos = event->pos();
}
void DragHandler::mouseMoveEvent(QMouseEvent *event) // before a drag is started - drag is started here
{
if (event->buttons() != Qt::LeftButton)
return;
QPoint cursorPos = m_tcw->mapFromGlobal(event->globalPos());
// check if it's a drag *pun unintended*
if (!m_tcw->m_mainSelected /* only allow drag if there was ANY te under the cursor */
|| ((cursorPos - m_dragStartPos).manhattanLength() < QApplication::startDragDistance()
&& (m_dragStartPos - cursorPos).manhattanLength() < QApplication::startDragDistance()))
return;
QVector<TrackEntry *> selected = m_tcw->getSelectedEntries();
if (selected.isEmpty())
return;
int cursorPosTime = m_tcw->m_tools->getTime(m_dragStartPos.x());
QMimeData *mimeData = new QMimeData();
mimeData->setData(TrackContentWidget::mime, m_tcw->selectedItemsToData(cursorPosTime));
QDrag *drag = new QDrag(m_tcw);
drag->setMimeData(mimeData);
if (m_tcw->isMultiLineMultiSelectionMode())
emit m_tcw->signalMultiLineMultiSelectionStart(cursorPosTime);
QVector<TrackEntry *> selectedOld = m_tcw->m_selected; // copy list, only delete OLD selection [NOTE: m_selected has to be fetched BEFORE exec(), otherwise m_select has changed (due to new / moved TEs!)
if (drag->exec(Qt::MoveAction) == Qt::MoveAction)
{
if (!m_tcw->isMultiLineMultiSelectionMode())
{
// move action - TEs were moved
for (auto i = selectedOld.begin(); i != selectedOld.end(); ++i)
emit m_tcw->signalTrackEntryDeleted((*i)->m_topicId); // triggers the deletion of old filters
qDeleteAll(selectedOld);
}
}
else // user pressed escape while dragging
{
emit m_tcw->signalMultiLineMultiSelectionDone(-1);
}
}
void DragHandler::keyPressEvent(QKeyEvent *event)
{
switch (event->key())
{
case Qt::Key_Left:
{
emit m_tcw->signalMoveAll(-1);
break;
}
case Qt::Key_Right:
{
emit m_tcw->signalMoveAll(1);
break;
}
default:
{
break;
}
}
}
} // namespace LtsScriptTimeline