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