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