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