|
27 | 27 | TimeRuler::TimeRuler(QWidget *_parent)
|
28 | 28 | : QWidget(_parent)
|
29 | 29 | {
|
30 |
| - m_startTime = 0.0; |
31 |
| - m_endTime = 1.0; |
32 | 30 | }
|
33 | 31 |
|
34 | 32 | void TimeRuler::setTimeWindow(qreal _startTime, qreal _endTime)
|
@@ -87,4 +85,62 @@ void TimeRuler::paintEvent(QPaintEvent *_event)
|
87 | 85 | }
|
88 | 86 |
|
89 | 87 | painter.drawLines(lines);
|
| 88 | + |
| 89 | + QBrush brush(Qt::BrushStyle::SolidPattern); |
| 90 | + brush.setColor(QColor(255, 255, 0)); |
| 91 | + painter.setRenderHint(QPainter::Antialiasing); |
| 92 | + painter.setPen(Qt::NoPen); |
| 93 | + |
| 94 | + int cursorRadius = 2; |
| 95 | + int height = size().height() - 1; |
| 96 | + int x = qRound(Utils::MapValue(m_currentTime, m_startTime, m_endTime, 0.0, size().width())) - CURSOR_HALF_WIDTH; |
| 97 | + |
| 98 | + QPainterPath cursor; |
| 99 | + cursor.moveTo(x, cursorRadius); |
| 100 | + cursor.arcTo(x, 0, 2 * cursorRadius, 2 * cursorRadius, 180, -90); |
| 101 | + cursor.arcTo(x + CURSOR_HALF_WIDTH * 2 - 2 * cursorRadius, 0, 2 * cursorRadius, 2 * cursorRadius, 90, -90); |
| 102 | + cursor.lineTo(x + 2 * CURSOR_HALF_WIDTH, height - CURSOR_HALF_WIDTH); |
| 103 | + cursor.lineTo(x + CURSOR_HALF_WIDTH, height); |
| 104 | + cursor.lineTo(x, height - CURSOR_HALF_WIDTH); |
| 105 | + cursor.closeSubpath(); |
| 106 | + |
| 107 | + painter.fillPath(cursor, brush); |
| 108 | +} |
| 109 | + |
| 110 | +void TimeRuler::mousePressEvent(QMouseEvent *_event) |
| 111 | +{ |
| 112 | + if(_event->button() == Qt::MouseButton::LeftButton) |
| 113 | + { |
| 114 | + m_draggingCursor = true; |
| 115 | + int cursorPos = qRound(Utils::MapValue(m_currentTime, m_startTime, m_endTime, 0.0, size().width())); |
| 116 | + int mousePos = _event->pos().x(); |
| 117 | + m_cursorOffset = qAbs(cursorPos - mousePos) < CURSOR_HALF_WIDTH ? cursorPos - mousePos : 0; |
| 118 | + updateCursor(mousePos); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +void TimeRuler::mouseMoveEvent(QMouseEvent *_event) |
| 123 | +{ |
| 124 | + if(m_draggingCursor) |
| 125 | + { |
| 126 | + updateCursor(_event->pos().x()); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +void TimeRuler::mouseReleaseEvent(QMouseEvent *_event) |
| 131 | +{ |
| 132 | + Q_UNUSED(_event) |
| 133 | + |
| 134 | + if(_event->button() == Qt::MouseButton::LeftButton) |
| 135 | + { |
| 136 | + m_draggingCursor = false; |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +void TimeRuler::updateCursor(int _mousePosX) |
| 141 | +{ |
| 142 | + qreal value = static_cast<qreal>(_mousePosX + m_cursorOffset); |
| 143 | + m_currentTime = Utils::MapValue(value, 0, static_cast<qreal>(size().width()), m_startTime, m_endTime); |
| 144 | + update(); |
| 145 | + emit onTimeSelected(m_currentTime); |
90 | 146 | }
|
0 commit comments