summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Qt3DStudio/Palettes/TimelineGraphicsView/ui/PlayHead.cpp
blob: 3ddfdcad66543dc30a3c81a6a0ca8eb86bc81e38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "PlayHead.h"
#include "Ruler.h"
#include "TimelineConstants.h"
#include "StudioPreferences.h"
#include "StudioUtils.h"

#include <QtGui/qpainter.h>
#include <QtGui/qcursor.h>
#include <QtWidgets/qwidget.h>

PlayHead::PlayHead(Ruler *ruler)
    : QGraphicsRectItem()
    , m_ruler(ruler)
{
    setZValue(99);
    setRect(-TimelineConstants::PLAYHEAD_W * .5, 0, TimelineConstants::PLAYHEAD_W, 0);
}

void PlayHead::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option)
    Q_UNUSED(widget)

    bool hiResIcons = StudioUtils::devicePixelRatio(widget->window()->windowHandle()) > 1.0;
    static const QPixmap pixHead = QPixmap(":/images/PlaybackHead.png");
    static const QPixmap pixHead2x = QPixmap(":/images/PlaybackHead@2x.png");

    static const int PLAY_HEAD_H = 999999; // theoretically big enough height
    painter->drawPixmap(-TimelineConstants::PLAYHEAD_W * .5, 0, hiResIcons ? pixHead2x : pixHead);
    painter->setPen(CStudioPreferences::timelinePlayheadLineColor());
    painter->drawLine(0, 0, 0, PLAY_HEAD_H);
}

void PlayHead::setHeight(int height)
{
    setRect(rect().x(), rect().y(), rect().width(), height);
}

void PlayHead::setTime(long time)
{
    if (time < 0)
        time = 0;
    else if (time > m_ruler->duration())
        time = m_ruler->duration();

    m_time = time;
    updatePosition();
}

void PlayHead::setPosition(double posX)
{
    posX = qBound(TimelineConstants::RULER_EDGE_OFFSET, posX, m_ruler->duration()
                  * TimelineConstants::RULER_MILLI_W * m_ruler->timelineScale()
                  + TimelineConstants::RULER_EDGE_OFFSET);

    setX(m_ruler->x() + posX);
    m_time = (posX - TimelineConstants::RULER_EDGE_OFFSET)
             / (TimelineConstants::RULER_MILLI_W * m_ruler->timelineScale());
}

void PlayHead::updatePosition()
{
    setX(m_ruler->x() + TimelineConstants::RULER_EDGE_OFFSET
         + m_time * TimelineConstants::RULER_MILLI_W * m_ruler->timelineScale());
}

long PlayHead::time() const
{
    return m_time;
}

int PlayHead::type() const
{
    // Enable the use of qgraphicsitem_cast with this item.
    return TimelineItem::TypePlayHead;
}