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