]> git.sesse.net Git - kdenlive/blob - src/smallruler.cpp
Fully functionnal zone in monitors, enable drag & drop from clip monitor
[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 <QMouseEvent>
22 #include <QStylePainter>
23
24 #include <KDebug>
25
26 #include "smallruler.h"
27
28
29 SmallRuler::SmallRuler(QWidget *parent)
30         : QWidget(parent), m_scale(1), m_maxval(25) {
31     m_zoneStart = 10;
32     m_zoneEnd = 60;
33 }
34
35 void SmallRuler::adjustScale(int maximum) {
36     m_maxval = maximum;
37     m_scale = (double) width() / (double) maximum;
38     if (m_scale == 0) m_scale = 1;
39
40     if (m_scale > 0.5) {
41         m_small = 25;
42         m_medium = 5 * 25;
43     } else if (m_scale > 0.09) {
44         m_small = 5 * 25;
45         m_medium = 30 * 25;
46     } else {
47         m_small = 30 * 25;
48         m_medium = 60 * 25;
49     }
50     m_cursorPosition = m_cursorFramePosition * m_scale;
51     update();
52 }
53
54 void SmallRuler::setZone(int start, int end) {
55     if (start != -1) {
56         if (end != -1 && start >= end) {
57             m_zoneEnd = qMin(m_maxval, end + (start - m_zoneStart));
58             m_zoneStart = start;
59         } else if (end == -1 && start >= m_zoneEnd) {
60             m_zoneEnd = qMin(m_maxval, m_zoneEnd + (start - m_zoneStart));
61             m_zoneStart = start;
62         } else m_zoneStart = start;
63     }
64     if (end != -1) {
65         if (start != -1 && end <= start) {
66             m_zoneStart = qMax(0, start - (m_zoneEnd - end));
67             m_zoneEnd = end;
68         } else if (start == -1 && end <= m_zoneStart) {
69             m_zoneStart = qMax(0, m_zoneStart - (m_zoneEnd - end));
70             m_zoneEnd = end;
71         } else m_zoneEnd = end;
72     }
73     update();
74 }
75
76 QPoint SmallRuler::zone() {
77     return QPoint(m_zoneStart, m_zoneEnd);
78 }
79
80 // virtual
81 void SmallRuler::mousePressEvent(QMouseEvent * event) {
82     const int pos = event->x() / m_scale;
83     emit seekRenderer((int) pos);
84 }
85
86 // virtual
87 void SmallRuler::mouseMoveEvent(QMouseEvent * event) {
88     const int pos = event->x() / m_scale;
89     emit seekRenderer((int) pos);
90 }
91
92 void SmallRuler::slotNewValue(int value) {
93     m_cursorFramePosition = value;
94     int oldPos = m_cursorPosition;
95     m_cursorPosition = value * m_scale;
96     if (oldPos == m_cursorPosition) return;
97     const int offset = 6;
98     const int x = qMin(oldPos, m_cursorPosition);
99     const int w = qAbs(oldPos - m_cursorPosition);
100     update(x - offset, 9, w + 2 * offset, 6);
101 }
102
103 //virtual
104 void SmallRuler::resizeEvent(QResizeEvent *) {
105     adjustScale(m_maxval);
106 }
107
108 // virtual
109 void SmallRuler::paintEvent(QPaintEvent *e) {
110
111     QPainter p(this);
112     QRect r = e->rect();
113     p.setClipRect(r);
114
115     double f, fend;
116     p.setPen(palette().dark().color());
117
118     const int zoneStart = (int)(m_zoneStart * m_scale);
119     const int zoneEnd = (int)(m_zoneEnd * m_scale);
120
121     p.fillRect(QRect(zoneStart, height() / 2, zoneEnd - zoneStart, height() / 2), QBrush(QColor(133, 255, 143)));
122
123     if (r.top() < 9) {
124         // draw the little marks
125         fend = m_scale * m_small;
126         if (fend > 2) for (f = 0; f < width(); f += fend) {
127                 p.drawLine((int)f, 1, (int)f, 3);
128             }
129
130         // draw medium marks
131         fend = m_scale * m_medium;
132         if (fend > 2) for (f = 0; f < width(); f += fend) {
133                 p.drawLine((int)f, 1, (int)f, 5);
134             }
135     }
136
137     // draw pointer
138     QPolygon pa(3);
139     pa.setPoints(3, m_cursorPosition - 5, 14, m_cursorPosition + 5, 14, m_cursorPosition/*+0*/, 9);
140     p.setBrush(palette().dark().color());
141     p.drawPolygon(pa);
142 }
143
144 #include "smallruler.moc"