|
| 1 | +/* |
| 2 | + * Copyright 2020 Benoit Pelletier |
| 3 | + * |
| 4 | + * This file is part of Sound Generator. |
| 5 | + * |
| 6 | + * Sound Generator is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * Sound Generator is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with Sound Generator. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +#include "TimeRuler.h" |
| 22 | +#include <QPainter> |
| 23 | +#include <qmath.h> |
| 24 | +#include "Utils.h" |
| 25 | +#include <math.h> |
| 26 | + |
| 27 | +TimeRuler::TimeRuler(QWidget *_parent) |
| 28 | + : QWidget(_parent) |
| 29 | +{ |
| 30 | + m_startTime = 0.0; |
| 31 | + m_endTime = 1.0; |
| 32 | +} |
| 33 | + |
| 34 | +void TimeRuler::setTimeWindow(qreal _startTime, qreal _endTime) |
| 35 | +{ |
| 36 | + m_startTime = _startTime; |
| 37 | + m_endTime = _endTime; |
| 38 | + |
| 39 | + update(); |
| 40 | +} |
| 41 | + |
| 42 | +void TimeRuler::paintEvent(QPaintEvent *_event) |
| 43 | +{ |
| 44 | + Q_UNUSED(_event); |
| 45 | + |
| 46 | + QPen pen; |
| 47 | + pen.setColor(QColor(200, 200, 200)); |
| 48 | + pen.setStyle(Qt::PenStyle::SolidLine); |
| 49 | + |
| 50 | + QPainter painter(this); |
| 51 | + painter.setPen(pen); |
| 52 | + |
| 53 | + qreal pixelPerSecond = size().width() / (m_endTime - m_startTime); |
| 54 | + qreal minDistBetweenTimecodes = 200.0; |
| 55 | + qreal timeInterval = 1.0 / qPow(2, qFloor(log2(pixelPerSecond / minDistBetweenTimecodes))); |
| 56 | + qreal pixelInterval = timeInterval * pixelPerSecond; |
| 57 | + qreal firstTimeCode = qFloor(m_startTime / timeInterval) * timeInterval; |
| 58 | + int pixelOffset = qRound((firstTimeCode - m_startTime) * pixelPerSecond); |
| 59 | + int nbLine = qCeil(size().width() / pixelInterval) + 1; |
| 60 | + int nbSubdiv = 10; |
| 61 | + |
| 62 | + QVector<QLineF> lines(nbLine * nbSubdiv); |
| 63 | + QRectF textRect; |
| 64 | + QPointF pos; |
| 65 | + qreal minY = 15.0; |
| 66 | + qreal maxY = static_cast<qreal>(size().height()); |
| 67 | + |
| 68 | + // main divisions with timecodes |
| 69 | + for(int i = 0; i < nbLine; ++i) |
| 70 | + { |
| 71 | + pos.setX(static_cast<qreal>(qRound(i * pixelInterval + pixelOffset))); |
| 72 | + textRect.moveCenter(pos); |
| 73 | + painter.drawText(textRect, Qt::AlignHCenter | Qt::AlignTop | Qt::TextDontClip |
| 74 | + , Utils::FormatTimeCode(firstTimeCode + i * timeInterval)); |
| 75 | + |
| 76 | + lines[i * nbSubdiv].setLine(pos.x(), minY, pos.x(), maxY); |
| 77 | + } |
| 78 | + |
| 79 | + // sub division (N subdivisions, so N-1 lines in each division) |
| 80 | + for(int i = 0; i < nbLine * nbSubdiv; ++i) |
| 81 | + { |
| 82 | + if(i % nbSubdiv != 0) |
| 83 | + { |
| 84 | + qreal x = static_cast<qreal>(qRound(i * pixelInterval / static_cast<qreal>(nbSubdiv) + pixelOffset)); |
| 85 | + lines[i].setLine(x, minY + 5, x, maxY); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + painter.drawLines(lines); |
| 90 | +} |
0 commit comments