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