]> git.sesse.net Git - kdenlive/blob - src/guide.cpp
Rewrote correct implementation of vertical timeline zoom
[kdenlive] / src / guide.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org)        *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20 #include "guide.h"
21 #include "customtrackview.h"
22 #include "customtrackscene.h"
23 #include "kdenlivesettings.h"
24
25 #include <KDebug>
26
27 #include <QPen>
28 #include <QBrush>
29
30 Guide::Guide(CustomTrackView *view, GenTime pos, QString label, double fps, double height) :
31         QGraphicsLineItem(),
32         m_position(pos),
33         m_label(label),
34         m_fps(fps),
35         m_view(view)
36 {
37     setFlags(QGraphicsItem::ItemIsMovable);
38     setToolTip(label);
39     setLine(0, 0, 0, height);
40     setPos(m_position.frames(m_fps), 0);
41     setPen(QPen(QBrush(QColor(0, 0, 200, 180)), 2));
42     setZValue(999);
43     setAcceptsHoverEvents(true);
44     const QFontMetrics metric = m_view->fontMetrics();
45     m_width = metric.width(' ' + m_label + ' ') + 2;
46     prepareGeometryChange();
47 }
48
49 QString Guide::label() const
50 {
51     return m_label;
52 }
53
54 GenTime Guide::position() const
55 {
56     return m_position;
57 }
58
59 CommentedTime Guide::info() const
60 {
61     return CommentedTime(m_position, m_label);
62 }
63
64 void Guide::updateGuide(const GenTime newPos, const QString &comment)
65 {
66     m_position = newPos;
67     setPos(m_position.frames(m_fps), 0);
68     if (!comment.isEmpty()) {
69         m_label = comment;
70         setToolTip(m_label);
71         const QFontMetrics metric = m_view->fontMetrics();
72         m_width = metric.width(' ' + m_label + ' ') + 2;
73         prepareGeometryChange();
74     }
75 }
76
77 //virtual
78 int Guide::type() const
79 {
80     return GUIDEITEM;
81 }
82
83 //virtual
84 void Guide::hoverEnterEvent(QGraphicsSceneHoverEvent *)
85 {
86     setPen(QPen(QBrush(QColor(200, 0, 0, 180)), 2));
87 }
88
89 //virtual
90 void Guide::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
91 {
92     setPen(QPen(QBrush(QColor(0, 0, 200, 180)), 2));
93 }
94
95 //virtual
96 QVariant Guide::itemChange(GraphicsItemChange change, const QVariant &value)
97 {
98     if (change == ItemPositionChange && scene()) {
99         // value is the new position.
100         QPointF newPos = value.toPointF();
101         newPos.setY(0);
102         newPos.setX(m_view->getSnapPointForPos(newPos.x()));
103         return newPos;
104     }
105     return QGraphicsItem::itemChange(change, value);
106 }
107
108 // virtual
109 QRectF Guide::boundingRect() const
110 {
111     if (KdenliveSettings::showmarkers()) {
112         QRectF rect = QGraphicsLineItem::boundingRect();
113         rect.setLeft(line().x1());
114         rect.setWidth(m_width / static_cast <CustomTrackScene*>(scene())->scale().x());
115         return rect;
116     } else return QGraphicsLineItem::boundingRect();
117 }
118
119 // virtual
120 QPainterPath Guide::shape() const
121 {
122     QPainterPath path;
123     path.addRect(line().x1() - pen().widthF() / 2, line().y1(), pen().widthF(), line().y2() - line().y1());
124     if (KdenliveSettings::showmarkers()) {
125         const QFontMetrics metric = m_view->fontMetrics();
126         int height = metric.height();
127         path.addRoundedRect(line().x1(), line().y1() + 10, m_width / static_cast <CustomTrackScene*>(scene())->scale().x(), height, 3, 3);
128     }
129     return path;
130 }
131
132 // virtual
133 void Guide::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*w*/)
134 {
135     painter->setMatrixEnabled(false);
136     QLineF guideline = painter->matrix().map(line());
137     painter->setPen(pen());
138     painter->drawLine(guideline);
139     //painter->fillRect(painter->matrix().mapRect(boundingRect()), QColor(200, 100, 100, 100));
140     //QGraphicsLineItem::paint(painter, option, w);
141     if (KdenliveSettings::showmarkers()) {
142         QPointF p1 = guideline.p1() + QPointF(1, 0);
143         QRectF txtBounding = painter->boundingRect(p1.x(), p1.y() + 10, m_width, 50, Qt::AlignLeft | Qt::AlignTop, ' ' + m_label + ' ');
144         QPainterPath path;
145         path.addRoundedRect(txtBounding, 3, 3);
146         painter->fillPath(path, QBrush(pen().color()));
147         painter->setPen(Qt::white);
148         painter->drawText(txtBounding, Qt::AlignCenter, m_label);
149     }
150 }
151