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