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