]> git.sesse.net Git - kdenlive/blob - src/guide.cpp
Don't allow negative guides
[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 "kdenlivesettings.h"
23
24 #include <KDebug>
25
26 #include <QPen>
27 #include <QBrush>
28 #include <QStyleOptionGraphicsItem>
29
30 Guide::Guide(CustomTrackView *view, GenTime pos, QString label, double height) :
31         QGraphicsLineItem(),
32         m_position(pos),
33         m_label(label),
34         m_view(view),
35         m_pen(QPen())
36 {
37     setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIgnoresTransformations);
38 #if QT_VERSION >= 0x040600
39     setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
40 #endif
41     setToolTip(label);
42     setLine(0, 0, 0, height);
43     if (m_position < GenTime()) m_position = GenTime();
44     setPos(m_position.frames(m_view->fps()), 0);
45     m_pen.setWidthF(0);
46     m_pen.setColor(QColor(0, 0, 200, 180));
47     //m_pen.setCosmetic(true);
48     setPen(m_pen);
49     setZValue(999);
50     setAcceptsHoverEvents(true);
51     const QFontMetrics metric = m_view->fontMetrics();
52     m_width = metric.width(' ' + m_label + ' ') + 2;
53     prepareGeometryChange();
54 }
55
56 QString Guide::label() const
57 {
58     return m_label;
59 }
60
61 GenTime Guide::position() const
62 {
63     return m_position;
64 }
65
66 CommentedTime Guide::info() const
67 {
68     return CommentedTime(m_position, m_label);
69 }
70
71 void Guide::updateGuide(const GenTime newPos, const QString &comment)
72 {
73     m_position = newPos;
74     setPos(m_position.frames(m_view->fps()), 0);
75     if (!comment.isEmpty()) {
76         m_label = comment;
77         setToolTip(m_label);
78         const QFontMetrics metric = m_view->fontMetrics();
79         m_width = metric.width(' ' + m_label + ' ') + 2;
80         prepareGeometryChange();
81     }
82 }
83
84 void Guide::updatePos()
85 {
86     setPos(m_position.frames(m_view->fps()), 0);
87 }
88
89 //virtual
90 int Guide::type() const
91 {
92     return GUIDEITEM;
93 }
94
95 //virtual
96 void Guide::hoverEnterEvent(QGraphicsSceneHoverEvent *)
97 {
98     m_pen.setColor(QColor(200, 0, 0, 180));
99     setPen(m_pen);
100 }
101
102 //virtual
103 void Guide::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
104 {
105     m_pen.setColor(QColor(0, 0, 200, 180));
106     setPen(m_pen);
107 }
108
109 //virtual
110 QVariant Guide::itemChange(GraphicsItemChange change, const QVariant &value)
111 {
112     if (change == ItemPositionChange && scene()) {
113         // value is the new position.
114         QPointF newPos = value.toPointF();
115         newPos.setY(0);
116         newPos.setX(m_view->getSnapPointForPos(newPos.x()));
117         if (newPos.x() < 0.0) newPos.setX(0.0);
118         return newPos;
119     }
120     return QGraphicsItem::itemChange(change, value);
121 }
122
123 // virtual
124 QRectF Guide::boundingRect() const
125 {
126     double scale = m_view->matrix().m11();
127     double width = m_pen.widthF() / scale * 2;
128     QRectF rect(line().x1() - width / 2 , line().y1(), width, line().y2() - line().y1());
129     if (KdenliveSettings::showmarkers()) {
130         rect.setWidth(width + m_width);
131     }
132     return rect;
133 }
134
135 // virtual
136 QPainterPath Guide::shape() const
137 {
138     QPainterPath path;
139     if (!scene()) return path;
140     double width = m_pen.widthF() * 2;
141     path.addRect(line().x1() - width / 2 , line().y1(), width, line().y2() - line().y1());
142     if (KdenliveSettings::showmarkers()) {
143         const QFontMetrics metric = m_view->fontMetrics();
144         QRectF txtBounding(line().x1(), line().y1() + 10, m_width, metric.height());
145         path.addRect(txtBounding);
146     }
147     return path;
148 }
149
150 // virtual
151 void Guide::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*w*/)
152 {
153     QGraphicsLineItem::paint(painter, option);
154     if (KdenliveSettings::showmarkers()) {
155         QPointF p1 = line().p1() + QPointF(1, 0);
156         const QFontMetrics metric = m_view->fontMetrics();
157         QRectF txtBounding = painter->boundingRect(p1.x(), p1.y() + 10, m_width, metric.height(), Qt::AlignLeft | Qt::AlignTop, ' ' + m_label + ' ');
158         QPainterPath path;
159         path.addRoundedRect(txtBounding, 3, 3);
160         painter->fillPath(path, m_pen.color());
161         painter->setPen(Qt::white);
162         painter->drawText(txtBounding, Qt::AlignCenter, m_label);
163     }
164 }
165