144 lines
4.4 KiB
C++
Executable File
144 lines
4.4 KiB
C++
Executable File
#include "tln_timeline_delegate.h"
|
|
#include "tln_timeline_model.h"
|
|
|
|
#include <LtsTimeline/timeline_view.h>
|
|
#include <LtsTimeline/timeline_header_view.h>
|
|
|
|
#include <QGuiApplication>
|
|
#include <QPainter>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
namespace Timeline
|
|
{
|
|
|
|
const int TimelineDelegate::s_zeroDurationItemWidth = 12;
|
|
|
|
|
|
TimelineDelegate::TimelineDelegate(LtsTimeline::TimelineView *parent)
|
|
: LtsTimeline::ItemDelegate(parent)
|
|
{}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
// PUBLIC
|
|
|
|
QSize TimelineDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
Q_UNUSED(option);
|
|
Q_UNUSED(index);
|
|
return QSize(50, 20);
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
// PRIVATE
|
|
|
|
void TimelineDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
|
|
{
|
|
//qWarning() << "delegate: paint" << index;
|
|
|
|
if (index.column() == LtsTimeline::Section::eActuator)
|
|
{
|
|
QStyledItemDelegate::paint(painter, option, index);
|
|
painter->save();
|
|
painter->setPen(QPen(QGuiApplication::palette().color(QPalette::Text), 2));
|
|
painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
|
|
painter->restore();
|
|
return;
|
|
}
|
|
|
|
int trackId = index.data().toInt();
|
|
|
|
TimelineModel *model = qobject_cast<TimelineModel *>(m_view->timelineModel());
|
|
|
|
QMutexLocker _(&model->m_mutex);
|
|
|
|
// if (model->isCurrentlyUpdating())
|
|
// return;
|
|
|
|
//qWarning() << "delegate: paint #2" << index;
|
|
|
|
painter->save();
|
|
|
|
painter->setClipping(true);
|
|
painter->setClipRect(option.rect);
|
|
|
|
try
|
|
{
|
|
const LtsTimeline::Track &track = model->track(trackId);
|
|
|
|
// paint vertical lines
|
|
painter->setPen(QGuiApplication::palette().color(QPalette::Text));
|
|
for (const LtsTimeline::Tick &tick : qAsConst(m_view->header()->majorTicks()))
|
|
painter->drawLine(tick.pos, option.rect.top(), tick.pos, option.rect.bottom());
|
|
|
|
int top = option.rect.y();
|
|
int bottom = option.rect.bottom();
|
|
|
|
// paint area with pending data red (but only if not live)
|
|
if (!model->isLive() && model->trackType(track.id()) != TrackType::Marker)
|
|
{
|
|
qint64 knownStart;
|
|
qint64 knownEnd;
|
|
if (model->knownRange(knownStart, knownEnd))
|
|
{
|
|
if (knownStart > m_view->currentTime())
|
|
{
|
|
painter->setPen(QPen(Qt::transparent, 0));
|
|
painter->setBrush(QBrush(QColor(255, 0, 0, 153)));
|
|
painter->drawRect(QRect(QPoint(option.rect.left(), top), QPoint(m_view->toPixel(knownStart), bottom)));
|
|
}
|
|
if (knownEnd < m_view->currentTime() + m_view->zoomFactor())
|
|
{
|
|
painter->setPen(QPen(Qt::transparent, 0));
|
|
painter->setBrush(QBrush(QColor(255, 0, 0, 153)));
|
|
painter->drawRect(QRect(QPoint(m_view->toPixel(knownEnd), top), QPoint(option.rect.right(), bottom)));
|
|
}
|
|
}
|
|
}
|
|
|
|
// draw items
|
|
const LtsTimeline::Items &items = track.items();
|
|
for (const LtsTimeline::ItemPtr &item : items)
|
|
drawItem(option, index, painter, model, *item.get());
|
|
|
|
// draw marker lines
|
|
painter->setPen(Qt::black);
|
|
if (model->trackType(track) == TrackType::Marker)
|
|
top = bottom - (option.rect.height() - AbstractEventItem::s_defaultHeight) / 2;
|
|
for (qint64 time : model->markerLines())
|
|
{
|
|
int x = m_view->toPixel(time);
|
|
painter->drawLine(x, top, x, bottom);
|
|
}
|
|
}
|
|
catch (const QString &errMsg)
|
|
{
|
|
qCritical() << LOGFUNCTION << "paint failed with an exception:" << errMsg;
|
|
}
|
|
|
|
painter->restore();
|
|
}
|
|
|
|
|
|
void TimelineDelegate::drawItem(
|
|
const QStyleOptionViewItem &option, const QModelIndex &index, QPainter *painter, TimelineModel *model, const Item &item
|
|
) const
|
|
{
|
|
qint64 duration = model == nullptr ? 0 : model->currentEventWidthTime();
|
|
QStyleOptionViewItem opt = option;
|
|
if (item.duration() < 0) // NOTE: + 1 for the width to prevent drawing the right border line
|
|
{
|
|
int left = m_view->toPixel(item.startTime());
|
|
opt.rect = QRect(left, option.rect.top() + 1, option.rect.right() - left + 1, option.rect.height() - 2);
|
|
}
|
|
else if (item.duration() < duration)
|
|
opt.rect = QRect(m_view->toPixel(item.startTime()), option.rect.top() + 1, s_zeroDurationItemWidth, option.rect.height() - 2);
|
|
else
|
|
opt.rect = QRect(QPoint(m_view->toPixel(item.startTime()), option.rect.top() + 1),
|
|
QPoint(m_view->toPixel(item.startTime() + item.duration()) - 1, option.rect.bottom() - 1));
|
|
item.paint(painter, opt, index);
|
|
}
|
|
|
|
} // namespace Timeline
|