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