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