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