diff --git a/src/Ass/Line.h b/src/Ass/Line.h index ce6c4e0c55bad102edb81b1b0de674dd324751b0..6904659e8444c630ad2b4f3421a6a949c0cf089e 100644 --- a/src/Ass/Line.h +++ b/src/Ass/Line.h @@ -20,6 +20,17 @@ public: Line& operator=(const Line&) = delete; ~Line() noexcept = default; + + inline void setStart(quint64 s){ + start = s; + if (s > end) + end = s; + }; + inline void setEnd(quint64 s){ + end = s; + if (start > s) + start = s; + }; }; #endif // VIVY_ASS_LINE_H diff --git a/src/AudioVisualizer.cc b/src/AudioVisualizer.cc index 00c50febae5057b5a24b7251f593ba1f48d4735a..a4c664d427ba6342468d2b5bb27a4f0d9175449e 100644 --- a/src/AudioVisualizer.cc +++ b/src/AudioVisualizer.cc @@ -16,7 +16,7 @@ AudioVisualizer::AudioVisualizer(QWidget *parent) noexcept void AudioVisualizer::printSpectrum(QImage pixmap) noexcept { - TimingView *timer = new TimingView(pixmap); + TimingView *timer = new TimingView(pixmap, 0); QVBoxLayout *layout = new QVBoxLayout; layout->addWidget(timer); diff --git a/src/Document/CRTPDocument.h b/src/Document/CRTPDocument.h index 51d2010e14920168e5702c2eaaf67daedd9a47ca..2933f71b467a480523e2e4493d383af09212eb55 100644 --- a/src/Document/CRTPDocument.h +++ b/src/Document/CRTPDocument.h @@ -8,6 +8,7 @@ #include "../Utils.h" #include "../Ass/Ass.h" #include <QString> +#include <memory> /* Types for the different documents */ @@ -100,7 +101,7 @@ class AssDocument final : public CRTPDocument<AssDocumentType, AssDocument> { friend CRTPDocument<AssDocumentType, AssDocument>; private: - QVector<Line> lines; + QVector<std::shared_ptr<Line>> lines; }; #endif // VIVY_CRTP_DOCUMENT_H diff --git a/src/TimingScene.cc b/src/TimingScene.cc index a2e47d97f46556817d1341a29f813070b4041207..bffff44b6a355ce2f08029b7026c0f21c924c929 100644 --- a/src/TimingScene.cc +++ b/src/TimingScene.cc @@ -17,9 +17,10 @@ TimingScene::TimingScene(QWidget *parent) noexcept { } -TimingScene::TimingScene(QImage img, QWidget *parent) noexcept - : QGraphicsScene(parent) - , img(img) +TimingScene::TimingScene(QImage img, quint64 soundLength, QWidget *parent) noexcept + : QGraphicsScene(parent), + img(img), + soundLength(soundLength) { QPixmap pixmap(QPixmap::fromImage(img)); backgroundImg = addPixmap(pixmap); @@ -34,8 +35,21 @@ TimingScene::mousePressEvent(QGraphicsSceneMouseEvent *event) noexcept QGraphicsItem *got; if ((got = itemAt(pos, QTransform())) == nullptr || got == backgroundImg) { - addItem(new TimingBar(QLine(pos.x(), 0, pos.x(), height()), - event->button() == Qt::LeftButton ? startColour : endColour)); + if (auto p = currentLine.lock()){ + quint64 time = timeFromPos(pos.x()); + switch (event->button()){ + case Qt::LeftButton: + p->setStart(time); + break; + + case Qt::RightButton: + p->setEnd(time); + break; + + default: + break; + } + } } QGraphicsScene::mousePressEvent(event); diff --git a/src/TimingScene.hpp b/src/TimingScene.hpp index 945e54a19c79b24c2265d043886e02abfe174398..1858355b28293e02585f82206e5b8b72c4ab0d8d 100644 --- a/src/TimingScene.hpp +++ b/src/TimingScene.hpp @@ -2,12 +2,14 @@ #define VIVY_TIMING_SCENE_H #include "TimingBar.h" +#include "Ass/Line.h" #include <QWidget> #include <QColor> #include <QVector> #include <QGraphicsView> #include <QGraphicsScene> +#include <memory> class QGraphicsPixmapItem; @@ -19,19 +21,23 @@ public: static inline constexpr QColor endColour = QColor(0, 127, 0); explicit TimingScene(QWidget *parent = nullptr) noexcept; - TimingScene(QImage img, QWidget *parent = nullptr) noexcept; + TimingScene(QImage, quint64, QWidget* = nullptr) noexcept; ~TimingScene() noexcept = default; private: QGraphicsPixmapItem *backgroundImg = nullptr; QImage img; - QVector<QLine> lines; + quint64 soundLength; + std::weak_ptr<Line> currentLine; public: inline QGraphicsPixmapItem* bg() { return backgroundImg; }; void mousePressEvent(QGraphicsSceneMouseEvent *event) noexcept override; +private: + inline quint64 timeFromPos(qreal x) { return x * soundLength / width(); }; + public slots: }; diff --git a/src/TimingView.cc b/src/TimingView.cc index 9eedff0a00c7c5afde705e218481cdbe8ccc8d39..37827aa9c93daba1be30882140e81922b856555c 100644 --- a/src/TimingView.cc +++ b/src/TimingView.cc @@ -14,10 +14,10 @@ #define TO_ADD_TO_IMAGE_HEIGHT 2 /* Used for alignement */ -TimingView::TimingView(QImage img, QWidget *parent) noexcept +TimingView::TimingView(QImage img, quint64 soundLength, QWidget *parent) noexcept : QGraphicsView(parent) { - scene = new TimingScene(img); + scene = new TimingScene(img, soundLength); setFixedHeight(img.height()); setMaximumHeight(img.height() + horizontalScrollBar()->height() - TO_ADD_TO_IMAGE_HEIGHT); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOn); diff --git a/src/TimingView.hpp b/src/TimingView.hpp index 74b4be8b9da343380b630615809d99de2f593f50..eb69928299c6abdfab4520e431d16cd6027612fd 100644 --- a/src/TimingView.hpp +++ b/src/TimingView.hpp @@ -22,7 +22,7 @@ public: static inline constexpr QColor startColour = QColor(127, 0, 127); static inline constexpr QColor endColour = QColor(0, 127, 0); - explicit TimingView(QImage img, QWidget *parent = nullptr) noexcept; + explicit TimingView(QImage, quint64, QWidget* = nullptr) noexcept; ~TimingView() noexcept = default; private: