Files
qt6-migration-pipeline/Lts/Common/LtsScriptTimeline/track_header.cpp
T

192 lines
3.4 KiB
C++
Executable File

#include "track_header.h"
#include "tools.h"
#include <QPainter>
#include <QBrush>
#include <QPen>
#include <QColor>
#include <QCursor>
#include <QMouseEvent>
#include <QFontMetrics>
#include <QFont>
#include <QTime>
#include <QApplication>
#include <QDebug>
namespace LtsScriptTimeline
{
TrackHeader::TrackHeader(QWidget *parent)
: QWidget(parent)
, m_indicatorTime(-1)
, m_textMargin(12)
, m_textSpacing(10)
, m_indicatorLock(false)
, m_tools(nullptr)
{
setCursor(Qt::IBeamCursor);
m_timeFormat = QStringLiteral("HH:mm:ss");
}
void TrackHeader::setTools(Tools *tools)
{
m_tools = tools;
}
void TrackHeader::mousePressEvent(QMouseEvent *event)
{
if (event->buttons() != Qt::LeftButton)
return;
if (!m_indicatorLock)
{
setIndicatorTime(m_tools->getTime(event->localPos().x()));
emit signalHeaderIndicatorTimeUserChanged(m_indicatorTime);
}
}
void TrackHeader::setIndicatorTime(int time)
{
m_indicatorTime = time;
emit signalHeaderIndicatorTimeChanged(m_indicatorTime);
update();
}
void TrackHeader::recreatePositionLabels()
{
QTime t;
t.setHMS(23, 59, 59);
QFontMetrics fm(QApplication::font());
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
int currentTextWidth = fm.width(t.toString());
#else
int currentTextWidth = fm.horizontalAdvance(t.toString());
#endif
if (currentTextWidth < 1 )
{
qCritical() << "TrackHeader::recreatePositionLabels: invalid text size";
return;
}
int currentTextWidthTime = m_tools->getDuration(currentTextWidth);
int time = m_tools->getDuration(m_textMargin);
int end = (m_tools->m_timelineLength * 2) + currentTextWidthTime;
int paintStartTime = m_tools->getTime(0) - currentTextWidthTime;
int paintEndTime = m_tools->getTime(width() * 2);
int count = 0;
m_positionMarkers.clear();
int textSpacingTime = m_tools->getDuration(m_textSpacing);
while (time < end && count < 5000)
{
if (time >= paintStartTime && time <= paintEndTime)
{
m_positionMarkers.append(m_tools->getPos(time));
}
time += currentTextWidthTime + textSpacingTime;
count++;
}
update();
}
void TrackHeader::resizeEvent(QResizeEvent *event)
{
recreatePositionLabels();
QWidget::resizeEvent(event);
}
void TrackHeader::updateMe()
{
recreatePositionLabels();
update();
}
void TrackHeader::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QWidget::paintEvent(event);
painter.save();
painter.setPen(QGuiApplication::palette().windowText().color());
painter.setBrush(QGuiApplication::palette().window().color());
painter.drawRect(rect().adjusted(0, 0, -1, -1));
int h = height();
if (m_indicatorTime > -1 && m_indicatorTime <= m_tools->getTime(width()))
{
painter.setPen(QColor("cyan"));
int indicatorPos = m_tools->getPos(m_indicatorTime);
painter.drawLine(indicatorPos, h - (h / 4), indicatorPos, h);
}
painter.setPen(QGuiApplication::palette().windowText().color());
for (auto i = m_positionMarkers.constBegin(); i != m_positionMarkers.constEnd(); ++i)
{
int pos = *i;
int time = m_tools->getTime(pos);
QTime t = QTime::fromMSecsSinceStartOfDay(time * 1000);
QString x = t.toString(m_timeFormat);
painter.drawText(pos, h / 2 , x);
painter.save();
painter.setPen(QGuiApplication::palette().link().color());
painter.drawLine(pos, (h / 2) + 2, pos, h);
painter.restore();
}
painter.restore();
}
}