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>
29 #include <QGraphicsView>
32 Guide::Guide(CustomTrackView *view, GenTime pos, QString label, double height) :
39 setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIgnoresTransformations);
40 #if QT_VERSION >= 0x040600
41 setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
44 setLine(0, 0, 0, height);
45 if (m_position < GenTime()) m_position = GenTime();
46 setPos(m_position.frames(m_view->fps()), 0);
48 m_pen.setColor(QColor(0, 0, 200, 180));
49 //m_pen.setCosmetic(true);
52 setAcceptsHoverEvents(true);
53 const QFontMetrics metric = m_view->fontMetrics();
54 m_width = metric.width(' ' + m_label + ' ') + 2;
55 prepareGeometryChange();
58 QString Guide::label() const
63 GenTime Guide::position() const
68 CommentedTime Guide::info() const
70 return CommentedTime(m_position, m_label);
73 void Guide::updateGuide(const GenTime &newPos, const QString &comment)
76 setPos(m_position.frames(m_view->fps()), 0);
77 if (!comment.isEmpty()) {
80 const QFontMetrics metric = m_view->fontMetrics();
81 m_width = metric.width(' ' + m_label + ' ') + 2;
82 prepareGeometryChange();
86 void Guide::updatePos()
88 setPos(m_position.frames(m_view->fps()), 0);
92 int Guide::type() const
98 void Guide::hoverEnterEvent(QGraphicsSceneHoverEvent *)
100 m_pen.setColor(QColor(200, 0, 0, 180));
105 void Guide::hoverLeaveEvent(QGraphicsSceneHoverEvent *)
107 m_pen.setColor(QColor(0, 0, 200, 180));
112 QVariant Guide::itemChange(GraphicsItemChange change, const QVariant &value)
114 if (change == ItemPositionChange && scene()) {
115 // value is the new position.
116 QPointF newPos = value.toPointF();
118 newPos.setX(m_view->getSnapPointForPos(newPos.x()));
119 if (newPos.x() < 0.0) newPos.setX(0.0);
122 return QGraphicsItem::itemChange(change, value);
126 QRectF Guide::boundingRect() const
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);
139 QPainterPath Guide::shape() const
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);
155 void Guide::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget */*w*/)
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();
162 // makes sure the text stays visible when scrolling vertical
163 int offset = scene()->views()[0]->verticalScrollBar()->value();
165 QRectF txtBounding = painter->boundingRect(p1.x(), p1.y() + offset, m_width, metric.height(), Qt::AlignLeft | Qt::AlignTop, m_label);
166 // draw the text on a rect with a arc appended
168 path.moveTo(txtBounding.bottomRight());
169 path.arcTo(txtBounding.right() - txtBounding.height() - 2, txtBounding.top() - txtBounding.height(), txtBounding.height() * 2, txtBounding.height() * 2, 270, 90);
170 path.lineTo(txtBounding.topLeft());
171 path.lineTo(txtBounding.bottomLeft());
172 painter->fillPath(path, m_pen.color());
173 painter->setPen(Qt::white);
174 painter->drawText(txtBounding.adjusted(1, 0, 1, 0), Qt::AlignCenter, m_label);