Skip to content
Extraits de code Groupes Projets
Valider 895b46c7 rédigé par Elliu's avatar Elliu Validation de Elliu
Parcourir les fichiers

Moving ticks refreshing routine in its own function

In order to only call it when needed (will be connected to a signal of
when the width of the audio changes)
parent b6f5ab1e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
1 requête de fusion!29Improve timingView with display of the ASS lines
...@@ -14,7 +14,7 @@ using namespace Vivy; ...@@ -14,7 +14,7 @@ using namespace Vivy;
TimingAxis::TimingAxis(qreal soundLength_, int x0_, int x1_, int y_) noexcept TimingAxis::TimingAxis(qreal soundLength_, int x0_, int x1_, int y_) noexcept
: QGraphicsItem() : QGraphicsItem()
, soundLength(soundLength_) , audioLength(soundLength_)
, x0(x0_) , x0(x0_)
, x1(x1_) , x1(x1_)
, y(y_) , y(y_)
...@@ -27,39 +27,48 @@ TimingAxis::boundingRect() const ...@@ -27,39 +27,48 @@ TimingAxis::boundingRect() const
return QRectF(x0, y - penWidth, x1, y + penWidth); return QRectF(x0, y - penWidth, x1, y + penWidth);
} }
/*
* Refresh the minor/major ticks value
* For intended results, this function should only be called when scene() returns a valid QGraphicsScene (so not in the constructors)
*/
void void
TimingAxis::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) TimingAxis::refreshTicks()
{ {
painter->drawLine(x0, y, x1, y); int nbAvailableTicks = availableTicks.size();
int minorTicksIndex = 0;
if (soundLength == 0) QGraphicsScene *parentScene = scene();
return; if (parentScene != nullptr) {
for (minorTicksIndex = 0; minorTicksIndex < nbAvailableTicks; minorTicksIndex++) {
qDebug() << "Audio length is " << soundLength; if (parentScene->width() / (audioLength * availableTicks[minorTicksIndex]) <
int minorTicksIndex = 0;
// Find what is our ticks
QGraphicsScene *currScene = scene();
if (currScene != nullptr) {
for (minorTicksIndex = 0; minorTicksIndex < availableTicks.size(); minorTicksIndex++) {
qDebug() << "Calculated is "
<< currScene->width() / (qreal(soundLength) * availableTicks[minorTicksIndex]);
if (currScene->width() / (qreal(soundLength) * availableTicks[minorTicksIndex]) <
minBetweenMinor) { minBetweenMinor) {
minorTicksIndex = minorTicksIndex == 0 ? 0 : minorTicksIndex - 1; minorTicksIndex = minorTicksIndex == 0 ? 0 : minorTicksIndex - 1;
break; break;
} }
} }
} }
qreal minorTicks = availableTicks[minorTicksIndex];
qreal majorTicks = availableTicks[minorTicksIndex + 1];
qDebug() << "Minor ticks : " << minorTicks; if (minorTicksIndex < nbAvailableTicks - 1) {
qDebug() << "Major ticks : " << majorTicks; minorTicks = availableTicks[minorTicksIndex];
majorTicks = availableTicks[minorTicksIndex + 1];
} else {
minorTicks = -1;
majorTicks = availableTicks[minorTicksIndex];
}
}
void
TimingAxis::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
if (audioLength == 0)
return;
painter->drawLine(x0, y, x1, y);
for (qreal i = majorTicks; i < x1 - x0; i += majorTicks) for (qreal i = majorTicks; i < x1 - x0; i += majorTicks) {
painter->drawLine(int(x0 + i), y - 5, int(x0 + i), y + 5); painter->drawLine(int(x0 + i), y - 5, int(x0 + i), y + 5);
for (qreal i = minorTicks; i < x1 - x0; i += minorTicks) }
if (fmod(i, majorTicks)) if (minorTicks >= 0)
painter->drawLine(int(x0 + i), y - 2, int(x0 + i), y + 2); for (qreal i = minorTicks; i < x1 - x0; i += minorTicks)
if (fmod(i, majorTicks) != 0)
painter->drawLine(int(x0 + i), y - 2, int(x0 + i), y + 2);
} }
...@@ -7,13 +7,14 @@ ...@@ -7,13 +7,14 @@
#include <QGraphicsWidget> #include <QGraphicsWidget>
#include <QPainter> #include <QPainter>
#include <QGraphicsScene>
#include <cmath> #include <cmath>
namespace Vivy namespace Vivy
{ {
class TimingAxis final : public QGraphicsItem { class TimingAxis final : public QGraphicsItem {
public: public:
explicit TimingAxis(qreal soundLength, int x0, int x1, int y) noexcept; explicit TimingAxis(qreal audioLength, int x0, int x1, int y) noexcept;
~TimingAxis() noexcept override = default; ~TimingAxis() noexcept override = default;
QRectF boundingRect() const override; QRectF boundingRect() const override;
...@@ -24,14 +25,18 @@ private: ...@@ -24,14 +25,18 @@ private:
QVector<qreal> availableTicks = { 0.01, 0.1, 1, 10, 60, 3600, 86400 }; QVector<qreal> availableTicks = { 0.01, 0.1, 1, 10, 60, 3600, 86400 };
qreal minBetweenMinor{ 5 }; qreal minBetweenMinor{ 5 };
qreal minorTicks;
qreal majorTicks;
qreal penWidth{ 1 }; qreal penWidth{ 1 };
qreal soundLength{ 0 }; qreal audioLength{ 0 };
int x0{ 0 }; int x0{ 0 };
int x1{ 0 }; int x1{ 0 };
int y{ 0 }; int y{ 0 };
protected: protected:
public slots:
void refreshTicks();
}; };
} }
......
...@@ -29,6 +29,7 @@ TimingScene::TimingScene(QImage img_, qreal soundLength_, QWidget *parent) noexc ...@@ -29,6 +29,7 @@ TimingScene::TimingScene(QImage img_, qreal soundLength_, QWidget *parent) noexc
backgroundImg = addPixmap(pixmap); backgroundImg = addPixmap(pixmap);
TimingAxis *ax = new TimingAxis(soundLength, 0, pixmap.width(), 10); TimingAxis *ax = new TimingAxis(soundLength, 0, pixmap.width(), 10);
addItem(ax); addItem(ax);
ax->refreshTicks();
} }
void void
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter