1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
21 #include "customtrackview.h"
22 #include "kdenlivesettings.h"
28 #include <QStyleOptionGraphicsItem>
30 Guide::Guide(CustomTrackView *view, GenTime pos, QString label, double height) :
37 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIgnoresTransformations);
38 #if QT_VERSION >= 0x040600
39 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
42 setLine(0, 0, 0, height);
43 if (m_position < GenTime()) m_position = GenTime();
44 setPos(m_position.frames(m_view->fps()), 0);
46 m_pen.setColor(QColor(0, 0, 200, 180));
47 //m_pen.setCosmetic(true);
50 setAcceptsHoverEvents(true);
51 const QFontMetrics metric = m_view->fontMetrics();
52 m_width = metric.width(' ' + m_label + ' ') + 2;
53 prepareGeometryChange();
56 QString Guide::label() const
61 GenTime Guide::position() const
66 CommentedTime Guide::info() const
68 return CommentedTime(m_position, m_label);
71 void Guide::updateGuide(const GenTime newPos, const QString &comment)
74 setPos(m_position.frames(m_view->fps()), 0);
75 if (!comment.isEmpty()) {
78 const QFontMetrics metric = m_view->fontMetrics();
79 m_width = metric.width(' ' + m_label + ' ') + 2;
80 prepareGeometryChange();
84 void Guide::updatePos()
86 setPos(m_position.frames(m_view->fps()), 0);
90 int Guide::type() const
96 void Guide::hoverEnterEvent(QGraphicsSceneHoverEvent *)
98 m_pen.setColor(QColor(200, 0, 0, 180));
103 void Guide::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
105 m_pen.setColor(QColor(0, 0, 200, 180));
110 QVariant Guide::itemChange(GraphicsItemChange change, const QVariant &value)
112 if (change == ItemPositionChange && scene()) {
113 // value is the new position.
114 QPointF newPos = value.toPointF();
116 newPos.setX(m_view->getSnapPointForPos(newPos.x()));
117 if (newPos.x() < 0.0) newPos.setX(0.0);
120 return QGraphicsItem::itemChange(change, value);
124 QRectF Guide::boundingRect() const
126 double scale = m_view->matrix().m11();
127 double width = m_pen.widthF() / scale * 2;
128 QRectF rect(line().x1() - width / 2 , line().y1(), width, line().y2() - line().y1());
129 if (KdenliveSettings::showmarkers()) {
130 rect.setWidth(width + m_width);
136 QPainterPath Guide::shape() const
139 if (!scene()) return path;
140 double width = m_pen.widthF() * 2;
141 path.addRect(line().x1() - width / 2 , line().y1(), width, line().y2() - line().y1());
142 if (KdenliveSettings::showmarkers()) {
143 const QFontMetrics metric = m_view->fontMetrics();
144 QRectF txtBounding(line().x1(), line().y1() + 10, m_width, metric.height());
145 path.addRect(txtBounding);
151 void Guide::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*w*/)
153 QGraphicsLineItem::paint(painter, option);
154 if (KdenliveSettings::showmarkers()) {
155 QPointF p1 = line().p1() + QPointF(1, 0);
156 const QFontMetrics metric = m_view->fontMetrics();
157 QRectF txtBounding = painter->boundingRect(p1.x(), p1.y() + 10, m_width, metric.height(), Qt::AlignLeft | Qt::AlignTop, ' ' + m_label + ' ');
159 path.addRoundedRect(txtBounding, 3, 3);
160 painter->fillPath(path, m_pen.color());
161 painter->setPen(Qt::white);
162 painter->drawText(txtBounding, Qt::AlignCenter, m_label);