#include "TimingView.hh"

using namespace Vivy;

TimingView::TimingView(QGraphicsScene *scene, QImage img, AudioContext::StreamPtr stream,
                       QWidget *parent) noexcept
    : QGraphicsView(scene, parent)
    , audioStream(stream)
{
    setMouseTracking(true);
    int interestingViewHeight = int(scene->height() + horizontalScrollBar()->height());
    setFixedHeight(interestingViewHeight);
    setMaximumHeight(interestingViewHeight);

    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Minimum);
    QObject::connect(verticalScrollBar(), &QScrollBar::rangeChanged, this,
                     &TimingView::moveScrollBarToBottom);

    // We want to see the bottom part of the spectrum, the top is less important if a few pixels get trimmed
    centerOn(0, interestingViewHeight);
}

void
TimingView::wheelEvent(QWheelEvent *event) noexcept
{
    horizontalScrollBar()->setValue(horizontalScrollBar()->value() -
                                    event->angleDelta().y() * wheelAngleToScrollRatio);
}

void
TimingView::moveScrollBarToBottom(int, int max) noexcept
{
    verticalScrollBar()->setValue(max);
}