]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
removed warnings to see real warning messages (except customtrackview)
[kdenlive] / src / customruler.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 #include <QMouseEvent>
21 #include <QStylePainter>
22
23 #include <KDebug>
24
25
26 #include "customruler.h"
27
28
29 #define INIT_VALUE 0
30 #define INIT_MIN_VALUE 0
31 #define INIT_MAX_VALUE 100
32 #define INIT_TINY_MARK_DISTANCE 1
33 #define INIT_LITTLE_MARK_DISTANCE 5
34 #define INIT_MIDDLE_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 2)
35 #define INIT_BIG_MARK_DISTANCE (INIT_LITTLE_MARK_DISTANCE * 10)
36 #define INIT_SHOW_TINY_MARK false
37 #define INIT_SHOW_LITTLE_MARK true
38 #define INIT_SHOW_MEDIUM_MARK true
39 #define INIT_SHOW_BIG_MARK true
40 #define INIT_SHOW_END_MARK true
41 #define INIT_SHOW_POINTER true
42 #define INIT_SHOW_END_LABEL true
43
44 #define INIT_PIXEL_PER_MARK (double)10.0 /* distance between 2 base marks in pixel */
45 #define INIT_OFFSET (-20)
46 #define INIT_LENGTH_FIX true
47 #define INIT_END_OFFSET 0
48
49 #define FIX_WIDTH 20 /* widget width in pixel */
50 #define LINE_END (FIX_WIDTH - 3)
51 #define END_MARK_LENGTH (FIX_WIDTH - 6)
52 #define END_MARK_X2 LINE_END
53 #define END_MARK_X1 (END_MARK_X2 - END_MARK_LENGTH)
54 #define BIG_MARK_LENGTH (END_MARK_LENGTH*3/4)
55 #define BIG_MARK_X2 LINE_END
56 #define BIG_MARK_X1 (BIG_MARK_X2 - BIG_MARK_LENGTH)
57 #define MIDDLE_MARK_LENGTH (END_MARK_LENGTH/2)
58 #define MIDDLE_MARK_X2 LINE_END
59 #define MIDDLE_MARK_X1 (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH)
60 #define LITTLE_MARK_LENGTH (MIDDLE_MARK_LENGTH/2)
61 #define LITTLE_MARK_X2 LINE_END
62 #define LITTLE_MARK_X1 (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH)
63 #define BASE_MARK_LENGTH (LITTLE_MARK_LENGTH/2)
64 #define BASE_MARK_X2 LINE_END
65 #define BASE_MARK_X1 (BASE_MARK_X2 - 3) //BASE_MARK_LENGTH
66
67 #define LABEL_SIZE 8
68 #define END_LABEL_X 4
69 #define END_LABEL_Y (END_LABEL_X + LABEL_SIZE - 2)
70
71 #include "definitions.h"
72
73 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000,
74                                         12000
75                                       };
76
77 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
78         : KRuler(parent), m_timecode(tc), m_view(parent), m_duration(0) {
79     slotNewOffset(0);
80     setRulerMetricStyle(KRuler::Pixel);
81     setLength(1024);
82     setMaximum(1024);
83     setPixelPerMark(3);
84     setLittleMarkDistance(FRAME_SIZE);
85     setMediumMarkDistance(FRAME_SIZE * 25);
86     setBigMarkDistance(FRAME_SIZE * 25 * 60);
87     m_zoneStart = 50;
88     m_zoneEnd = 250;
89 }
90
91 // virtual
92 void CustomRuler::mousePressEvent(QMouseEvent * event) {
93     m_view->activateMonitor();
94     int pos = (int)((event->x() + offset()) / pixelPerMark() / FRAME_SIZE);
95     m_moveCursor = RULER_CURSOR;
96     if (event->y() > 10) {
97         if (abs(pos - m_zoneStart) < 4) m_moveCursor = RULER_START;
98         else if (abs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2)) < 4) m_moveCursor = RULER_MIDDLE;
99         else if (abs(pos - m_zoneEnd) < 4) m_moveCursor = RULER_END;
100     }
101     if (m_moveCursor == RULER_CURSOR)
102         m_view->setCursorPos(pos);
103 }
104
105 // virtual
106 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
107     int pos = (int)((event->x() + offset()) / pixelPerMark() / FRAME_SIZE);
108     if (m_moveCursor == RULER_CURSOR) {
109         m_view->setCursorPos(pos);
110         return;
111     } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
112     else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
113     else if (m_moveCursor == RULER_MIDDLE) {
114         int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
115         m_zoneStart += move;
116         m_zoneEnd += move;
117     }
118     update();
119 }
120
121 int CustomRuler::inPoint() const {
122     return m_zoneStart;
123 }
124
125 int CustomRuler::outPoint() const {
126     return m_zoneEnd;
127 }
128
129 void CustomRuler::slotMoveRuler(int newPos) {
130     KRuler::slotNewOffset(newPos);
131 }
132
133 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
134     //TODO: optimize (redraw only around cursor positions
135     update();
136 }
137
138 void CustomRuler::setPixelPerMark(double rate) {
139     int scale = comboScale[(int) rate];
140     KRuler::setPixelPerMark(1.0 / scale);
141 }
142
143 void CustomRuler::setDuration(int d) {
144     m_duration = d;
145     update();
146 }
147
148 // virtual
149 void CustomRuler::paintEvent(QPaintEvent *e) {
150     //  debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
151
152     QStylePainter p(this);
153     p.setClipRect(e->rect());
154
155     //p.fillRect(e->rect(), QBrush(QColor(200, 200, 200)));
156     //kDebug()<<"RULER ZONE: "<<m_zoneStart<<", OFF: "<<offset()<<", END: "<<m_zoneEnd<<", FACTOR: "<<pixelPerMark() * FRAME_SIZE;
157     int projectEnd = (int)(m_duration * pixelPerMark() * FRAME_SIZE);
158     p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
159
160
161     int zoneStart = (int)(m_zoneStart * pixelPerMark() * FRAME_SIZE);
162     int zoneEnd = (int)(m_zoneEnd * pixelPerMark() * FRAME_SIZE);
163
164     p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
165
166     int value  = m_view->cursorPos() - offset() + 4;
167     int minval = minimum();
168     int maxval = maximum() + offset() - endOffset();
169
170     //ioffsetval = value-offset;
171     //    pixelpm = (int)ppm;
172     //    left  = clip.left(),
173     //    right = clip.right();
174     double f, fend,
175     offsetmin = (double)(minval - offset()),
176                 offsetmax = (double)(maxval - offset()),
177                             fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
178     QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
179
180     QPalette palette;
181     //p.fillRect(bg, palette.light());
182     // draw labels
183     QFont font = p.font();
184     font.setPointSize(LABEL_SIZE);
185     p.setFont(font);
186     p.setPen(palette.dark().color());
187     // draw littlemarklabel
188
189     // draw mediummarklabel
190
191     // draw bigmarklabel
192
193     // draw endlabel
194     /*if (d->showEndL) {
195       if (d->dir == Qt::Horizontal) {
196         p.translate( fontOffset, 0 );
197         p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
198       }*/
199
200     // draw the tiny marks
201     //if (showTinyMarks())
202     /*{
203       fend =   pixelPerMark()*tinyMarkDistance();
204       if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
205           p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
206       }
207     }*/
208     if (showLittleMarks()) {
209         // draw the little marks
210         fend = pixelPerMark() * littleMarkDistance();
211         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend) {
212                 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
213                 if (fend > 60) {
214                     QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
215                     p.drawText((int)f + 2, LABEL_SIZE, lab);
216                 }
217             }
218     }
219     if (showMediumMarks()) {
220         // draw medium marks
221         fend = pixelPerMark() * mediumMarkDistance();
222         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend) {
223                 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
224                 if (fend > 60) {
225                     QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
226                     p.drawText((int)f + 2, LABEL_SIZE, lab);
227                 }
228             }
229     }
230     if (showBigMarks()) {
231         // draw big marks
232         fend = pixelPerMark() * bigMarkDistance();
233         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend) {
234                 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
235                 if (fend > 60) {
236                     QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
237                     p.drawText((int)f + 2, LABEL_SIZE, lab);
238                 } else if (((int)(f - offsetmin)) % ((int)(fend * 5)) == 0) {
239                     QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
240                     p.drawText((int)f + 2, LABEL_SIZE, lab);
241                 }
242             }
243     }
244     /*   if (d->showem) {
245          // draw end marks
246          if (d->dir == Qt::Horizontal) {
247            p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
248            p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
249          }
250          else {
251            p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
252            p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
253          }
254        }*/
255
256
257     // draw zone cursors
258     int off = offset();
259     if (zoneStart > 0) {
260         QPolygon pa(4);
261         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
262         p.drawPolyline(pa);
263     }
264
265     if (zoneEnd > 0) {
266         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
267         p.fillRect(rec, QColor(255, 255, 255, 150));
268         p.drawRect(rec);
269
270         QPolygon pa(4);
271         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
272         p.drawPolyline(pa);
273     }
274
275     // draw pointer
276     if (showPointer() && value > 0) {
277         QPolygon pa(3);
278         pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
279         p.setBrush(QBrush(Qt::yellow));
280         p.drawPolygon(pa);
281     }
282
283
284
285 }
286
287 #include "customruler.moc"