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