#include "timeline_header_view_mouse_handler.h" #include "timeline_header_view.h" #include "timeline_view.h" #include #include #include #include namespace LtsTimeline { TimelineHeaderViewMouseHandler::TimelineHeaderViewMouseHandler(TimelineHeaderView *header) : m_header(header) {} void TimelineHeaderViewMouseHandler::mouseMoveEvent(QMouseEvent *e) { if (m_isLeftButtonPressed) m_hasMouseMovedLeft = true; if (m_isRightButtonPressed) m_hasMouseMovedRight = true; if (!m_isResizingSection) { if (m_header->sectionPosition(Section::eItems) - 10 < e->pos().x() && e->pos().x() < m_header->sectionPosition(Section::eItems)) m_header->m_timelineView->setCursor(QCursor(Qt::SizeHorCursor)); else if (e->pos().y() <= m_header->geometry().bottom()) m_header->m_timelineView->setCursor(QCursor(Qt::ArrowCursor)); } else m_header->resizeSection(Section::eActuator, std::min(e->pos().x(), m_header->geometry().width() / 2)); const bool isWithinRangeX = isMouseWithinRangeX(e->pos().x()); if (!isWithinRangeX && !m_isLeftButtonPressed) { m_header->setHoverPosition(-1); return; } if (isWithinRangeX) m_header->setHoverPosition(e->pos().x()); if (m_isLeftButtonPressed) { if (m_isIndicatorSelected && !m_header->m_isIndicatorLocked) { m_header->m_timelineView->setCursor(QCursor(Qt::ClosedHandCursor)); if (isWithinRangeX) m_header->setIndicatorPosition(e->pos().x()); } } else { if (isMouseOverIndicator(e->pos()) && m_header->m_isIndicatorVisible && !m_header->m_isIndicatorLocked) m_header->m_timelineView->setCursor(QCursor(Qt::OpenHandCursor)); else if (e->pos().y() <= m_header->geometry().bottom()) m_header->m_timelineView->setCursor(QCursor(Qt::ArrowCursor)); } } void TimelineHeaderViewMouseHandler::mousePressEvent(QMouseEvent *e) { if (e->button() == Qt::LeftButton) { m_lastPos = e->pos(); m_isLeftButtonPressed = true; if (isMouseOverIndicator(e->pos()) && m_header->m_isIndicatorVisible && !m_header->m_isIndicatorLocked) { m_header->m_timelineView->setCursor(QCursor(Qt::ClosedHandCursor)); m_isIndicatorSelected = true; } else if (m_header->m_timelineView->cursor().shape() == Qt::SizeHorCursor) { m_isResizingSection = true; } } if (e->button() == Qt::RightButton) m_isRightButtonPressed = true; } void TimelineHeaderViewMouseHandler::mouseReleaseEvent(QMouseEvent *e) { if (e->button() == Qt::LeftButton) { if (!m_hasMouseMovedLeft && isMouseWithinRangeX(e->pos().x()) && !m_header->m_isIndicatorLocked) m_header->setIndicatorPosition(e->pos().x()); if (isMouseOverIndicator(e->pos())) m_header->m_timelineView->setCursor(QCursor(Qt::OpenHandCursor)); else m_header->m_timelineView->setCursor(QCursor(Qt::ArrowCursor)); m_lastPos = {}; m_isIndicatorSelected = false; m_isResizingSection = false; m_isLeftButtonPressed = false; m_hasMouseMovedLeft = false; } if (e->button() == Qt::RightButton) { m_header->setDateTimeFormat(static_cast((m_header->dateTimeFormat() + 1) % DateTime::numberOfDateTimeFormats())); m_isRightButtonPressed = false; m_hasMouseMovedRight = false; } } void TimelineHeaderViewMouseHandler::wheelEvent(QWheelEvent *e) { if (!m_header->m_timelineView) return; if (QGuiApplication::queryKeyboardModifiers() == Qt::ControlModifier) { if (e->angleDelta().y() < 0) m_header->m_timelineView->setZoomFactor(Zoom::increment(m_header->m_timelineView->zoomFactor())); else m_header->m_timelineView->setZoomFactor(Zoom::decrement(m_header->m_timelineView->zoomFactor())); } else { if (e->angleDelta().y() > 0) m_header->m_timelineView->setCurrentTime( m_header->m_timelineView->currentTime() - Zoom::majorInterval(m_header->m_timelineView->zoomFactor()) ); else m_header->m_timelineView->setCurrentTime( m_header->m_timelineView->currentTime() + Zoom::majorInterval(m_header->m_timelineView->zoomFactor()) ); } } bool TimelineHeaderViewMouseHandler::isMouseWithinRangeX(int x) const { return m_header->sectionPosition(Section::eItems) < x && x < m_header->geometry().right(); } bool TimelineHeaderViewMouseHandler::isMouseWithinRangeY(int y) const { return m_header->geometry().top() < y && y < m_header->geometry().bottom(); } bool TimelineHeaderViewMouseHandler::isMouseOverIndicator(const QPoint &pos) const { const int height = m_header->height(); return m_header->indicatorPosition() - height / 4 <= pos.x() && pos.x() <= m_header->indicatorPosition() + height / 4 && m_header->geometry().center().y() <= pos.y() && pos.y() < m_header->geometry().bottom(); } }