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