Files
2026-08-13 18:33:10 +02:00

192 lines
4.2 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"); // TODO: load time format or whatever
}
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()
{
// get font metrics by probing
QTime t;
t.setHMS(23, 59, 59);
QFontMetrics fm(QApplication::font());
//m_currentTextHeight = fm.height(); TODO: impl usage
#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 /*|| m_currentTextHeight < 1*/)
{
qCritical() << "TrackHeader::recreatePositionLabels: invalid text size";
return;
}
// find where to put texts
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; // allow drawing from Timeline-Start + Text-End
int paintEndTime = m_tools->getTime(width() * 2);
int count = 0;
m_positionMarkers.clear();
//qCritical() << "time" << time << "end" << end << "paintStartTime" << paintStartTime << "paintEndTime" << paintEndTime;
int textSpacingTime = m_tools->getDuration(m_textSpacing);
while (time < end && count < 5000)
{
//qCritical() << "time =" << time << "count =" << count;
if (time >= paintStartTime && time <= paintEndTime)
{
//qCritical() << "added time" << time;
m_positionMarkers.append(m_tools->getPos(time));
}
time += currentTextWidthTime + textSpacingTime;
count++;
}
// TODO: tell all tcws' !!
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();
// draw base
painter.setPen(QGuiApplication::palette().windowText().color());
painter.setBrush(QGuiApplication::palette().window().color());
painter.drawRect(rect().adjusted(0, 0, -1, -1));
int h = height();
// draw position pointer (if available)
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);
}
// draw position texts
painter.setPen(QGuiApplication::palette().windowText().color());
//qCritical() << "draw position texts";
for (auto i = m_positionMarkers.constBegin(); i != m_positionMarkers.constEnd(); ++i)
{
int pos = *i;
//qCritical() << "pos" << pos;
int time = m_tools->getTime(pos);
//qCritical() << "time" << time;
QTime t = QTime::fromMSecsSinceStartOfDay(time * 1000);
//qCritical() << "t" << t;
QString x = t.toString(m_timeFormat);
//qCritical() << "x" << x;
painter.drawText(pos, h / 2 /* TODO: calc exactly*/, x);
painter.save();
painter.setPen(QGuiApplication::palette().link().color());
painter.drawLine(pos, (h / 2) + 2, pos, h); // time indicator
painter.restore();
//qCritical() << "drew at" << pos << "/" << x;
}
painter.restore();
}
} // namespace LtsScriptTimeline