]> git.sesse.net Git - kdenlive/blob - src/smallruler.cpp
Render zone: fix color issues & snapping
[kdenlive] / src / smallruler.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 "smallruler.h"
22 #include "kdenlivesettings.h"
23
24 #include <KDebug>
25 #include <KColorScheme>
26
27 #include <QMouseEvent>
28 #include <QStylePainter>
29
30
31 SmallRuler::SmallRuler(MonitorManager *manager, QWidget *parent) :
32         QWidget(parent),
33         m_scale(1),
34         m_maxval(25),
35         m_manager(manager)
36 {
37     m_zoneStart = 10;
38     m_zoneEnd = 60;
39     m_zoneColor = KStatefulBrush(KColorScheme::View, KColorScheme::PositiveBackground, KSharedConfig::openConfig(KdenliveSettings::colortheme())).brush(this).color();
40     setMouseTracking(true);
41 }
42
43 void SmallRuler::adjustScale(int maximum)
44 {
45     m_maxval = maximum;
46     m_scale = (double) width() / (double) maximum;
47     if (m_scale == 0) m_scale = 1;
48
49     if (m_scale > 0.5) {
50         m_small = 25;
51         m_medium = 5 * 25;
52     } else if (m_scale > 0.09) {
53         m_small = 5 * 25;
54         m_medium = 30 * 25;
55     } else {
56         m_small = 30 * 25;
57         m_medium = 60 * 25;
58     }
59     m_cursorPosition = m_cursorFramePosition * m_scale;
60     updatePixmap();
61 }
62
63 void SmallRuler::setZone(int start, int end)
64 {
65     if (start != -1) {
66         if (end != -1 && start >= end) {
67             m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
68             m_zoneStart = start;
69         } else if (end == -1 && start >= m_zoneEnd) {
70             m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
71             m_zoneStart = start;
72         } else m_zoneStart = start;
73     }
74     if (end != -1) {
75         if (start != -1 && end <= start) {
76             m_zoneStart = qMax(0, start - (m_zoneEnd - end));
77             m_zoneEnd = end;
78         } else if (start == -1 && end <= m_zoneStart) {
79             m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
80             m_zoneEnd = end;
81         } else m_zoneEnd = end;
82     }
83     updatePixmap();
84 }
85
86 void SmallRuler::setMarkers(QList < int > list)
87 {
88     m_markers = list;
89     updatePixmap();
90 }
91
92 QPoint SmallRuler::zone()
93 {
94     return QPoint(m_zoneStart, m_zoneEnd);
95 }
96
97 // virtual
98 void SmallRuler::mousePressEvent(QMouseEvent * event)
99 {
100     const int pos = event->x() / m_scale;
101     if (event->button() == Qt::RightButton) {
102         // Right button clicked, move selection zone
103         if (qAbs(pos - m_zoneStart) < qAbs(pos - m_zoneEnd)) m_zoneStart = pos;
104         else m_zoneEnd = pos;
105         emit zoneChanged(QPoint(m_zoneStart, m_zoneEnd));
106         updatePixmap();
107
108     } else emit seekRenderer((int) pos);
109 }
110
111 // virtual
112 void SmallRuler::mouseMoveEvent(QMouseEvent * event)
113 {
114     const int pos = event->x() / m_scale;
115     if (event->buttons() & Qt::LeftButton) emit seekRenderer((int) pos);
116     else {
117         if (qAbs((pos - m_zoneStart) * m_scale) < 4) {
118             setToolTip(i18n("Zone start: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneStart)));
119         } else if (qAbs((pos - m_zoneEnd) * m_scale) < 4) {
120             setToolTip(i18n("Zone end: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd)));
121         } else if (pos > m_zoneStart && pos < m_zoneEnd) {
122             setToolTip(i18n("Zone duration: %1", m_manager->timecode().getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
123         } else setToolTip(i18n("Position: %1", m_manager->timecode().getTimecodeFromFrames(pos)));
124     }
125 }
126
127 bool SmallRuler::slotNewValue(int value)
128 {
129     if (value == m_cursorFramePosition) return false;
130     m_cursorFramePosition = value;
131     int oldPos = m_cursorPosition;
132     m_cursorPosition = value * m_scale;
133     const int offset = 6;
134     const int x = qMin(oldPos, m_cursorPosition);
135     const int w = qAbs(oldPos - m_cursorPosition);
136     update(x - offset, 4, w + 2 * offset, 6);
137     return true;
138 }
139
140 //virtual
141 void SmallRuler::resizeEvent(QResizeEvent *)
142 {
143     adjustScale(m_maxval);
144 }
145
146 void SmallRuler::updatePixmap()
147 {
148     m_pixmap = QPixmap(width(), height());
149     m_pixmap.fill(palette().window().color());
150     QPainter p(&m_pixmap);
151     double f, fend;
152
153     const int zoneStart = (int)(m_zoneStart * m_scale);
154     const int zoneEnd = (int)(m_zoneEnd * m_scale);
155     p.fillRect(zoneStart, height() / 2 - 1, zoneEnd - zoneStart, height() / 2, m_zoneColor);
156
157     // draw markers
158     if (!m_markers.isEmpty()) {
159         p.setPen(Qt::red);
160         for (int i = 0; i < m_markers.count(); i++) {
161             p.drawLine(m_markers.at(i) * m_scale, 0, m_markers.at(i) * m_scale, 9);
162         }
163     }
164     p.setPen(palette().text().color());
165     // draw the little marks
166     fend = m_scale * m_small;
167     if (fend > 2) for (f = 0; f < width(); f += fend) {
168             p.drawLine((int)f, 0, (int)f, 3);
169         }
170
171     // draw medium marks
172     fend = m_scale * m_medium;
173     if (fend > 2) for (f = 0; f < width(); f += fend) {
174             p.drawLine((int)f, 0, (int)f, 6);
175         }
176     p.end();
177     update();
178 }
179
180 // virtual
181 void SmallRuler::paintEvent(QPaintEvent *e)
182 {
183
184     QPainter p(this);
185     QRect r = e->rect();
186     p.setClipRect(r);
187     p.drawPixmap(QPointF(), m_pixmap);
188
189     // draw pointer
190     QPolygon pa(3);
191     pa.setPoints(3, m_cursorPosition - 5, 10, m_cursorPosition + 5, 10, m_cursorPosition/*+0*/, 5);
192     p.setBrush(palette().text().color());
193     p.setPen(Qt::NoPen);
194     p.drawPolygon(pa);
195 }
196
197 #include "smallruler.moc"