]> git.sesse.net Git - kdenlive/blob - src/guide.cpp
80ade91236291d912fcf14e9eb545a6e74043f94
[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(), m_view(view), m_position(pos), m_label(label), m_fps(fps)
32 {
33     setFlags(QGraphicsItem::ItemIsMovable);
34     setToolTip(label);
35     setLine(0, 0, 0, height);
36     setPos(m_position.frames(m_fps), 0);
37     setPen(QPen(QBrush(QColor(0, 0, 200, 180)), 1));
38     setZValue(999);
39     setAcceptsHoverEvents(true);
40     const QFontMetrics metric = m_view->fontMetrics();
41     m_width = metric.width(' ' + m_label + ' ') + 2;
42     prepareGeometryChange();
43 }
44
45 QString Guide::label() const
46 {
47     return m_label;
48 }
49
50 GenTime Guide::position() const
51 {
52     return m_position;
53 }
54
55 CommentedTime Guide::info() const
56 {
57     return CommentedTime(m_position, m_label);
58 }
59
60 void Guide::updateGuide(const GenTime newPos, const QString &comment)
61 {
62     m_position = newPos;
63     setPos(m_position.frames(m_fps), 0);
64     if (!comment.isEmpty()) {
65         m_label = comment;
66         setToolTip(m_label);
67         const QFontMetrics metric = m_view->fontMetrics();
68         m_width = metric.width(' ' + m_label + ' ') + 2;
69         prepareGeometryChange();
70     }
71 }
72
73 //virtual
74 int Guide::type() const
75 {
76     return GUIDEITEM;
77 }
78
79 //virtual
80 void Guide::hoverEnterEvent(QGraphicsSceneHoverEvent *)
81 {
82     setPen(QPen(QBrush(QColor(200, 0, 0, 180)), 2));
83 }
84
85 //virtual
86 void Guide::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
87 {
88     setPen(QPen(QBrush(QColor(0, 0, 200, 180)), 2));
89 }
90
91 //virtual
92 QVariant Guide::itemChange(GraphicsItemChange change, const QVariant &value)
93 {
94     if (change == ItemPositionChange && scene()) {
95         // value is the new position.
96         QPointF newPos = value.toPointF();
97         newPos.setY(0);
98         newPos.setX(m_view->getSnapPointForPos(newPos.x()));
99         return newPos;
100     }
101     return QGraphicsItem::itemChange(change, value);
102 }
103
104 // virtual
105 QRectF Guide::boundingRect() const
106 {
107     if (KdenliveSettings::showmarkers()) {
108         QRectF rect = QGraphicsLineItem::boundingRect();
109         rect.setLeft(line().x1());
110         rect.setWidth(m_width / static_cast <CustomTrackScene*>(scene())->scale());
111         return rect;
112     } else return QGraphicsLineItem::boundingRect();
113 }
114
115 // virtual
116 void Guide::paint(QPainter *painter, const QStyleOptionGraphicsItem */*option*/, QWidget */*w*/)
117 {
118     painter->setMatrixEnabled(false);
119     QLineF guideline = painter->matrix().map(line());
120     painter->setPen(pen());
121     painter->drawLine(guideline);
122     //painter->fillRect(painter->matrix().mapRect(boundingRect()), QColor(200, 100, 100, 100));
123     //QGraphicsLineItem::paint(painter, option, w);
124     if (KdenliveSettings::showmarkers()) {
125         QPointF p1 = guideline.p1() + QPointF(1, 0);
126         QRectF txtBounding = painter->boundingRect(p1.x(), p1.y() + 10, m_width, 50, Qt::AlignLeft | Qt::AlignTop, ' ' + m_label + ' ');
127         QPainterPath path;
128         path.addRoundedRect(txtBounding, 3, 3);
129         painter->fillPath(path, QBrush(pen().color()));
130         painter->setPen(Qt::white);
131         painter->drawText(txtBounding, Qt::AlignCenter, m_label);
132     }
133 }
134