]> git.sesse.net Git - kdenlive/blob - src/customruler.cpp
Don't allow clip moving over another one
[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 #include <KIcon>
25 #include <KCursor>
26 #include <KGlobalSettings>
27
28 #include "customruler.h"
29
30
31 static const int FIX_WIDTH = 24; /* widget width in pixel */
32 static const int LINE_END = (FIX_WIDTH - 3);
33 static const int END_MARK_LENGTH = (FIX_WIDTH - 8);
34 static const int BIG_MARK_LENGTH = (END_MARK_LENGTH * 3 / 4);
35 static const int BIG_MARK_X2 = LINE_END;
36 static const int BIG_MARK_X1 = (BIG_MARK_X2 - BIG_MARK_LENGTH);
37 static const int MIDDLE_MARK_LENGTH = (END_MARK_LENGTH / 2);
38 static const int MIDDLE_MARK_X2 = LINE_END;
39 static const int MIDDLE_MARK_X1 = (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH);
40 static const int LITTLE_MARK_LENGTH = (MIDDLE_MARK_LENGTH / 2);
41 static const int LITTLE_MARK_X2 = LINE_END;
42 static const int LITTLE_MARK_X1 = (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH);
43
44 static const int LABEL_SIZE = 9;
45 static const int END_LABEL_X = 4;
46 static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2);
47
48 #include "definitions.h"
49
50 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
51
52 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
53         : KRuler(parent), m_timecode(tc), m_view(parent), m_duration(0) {
54     setFont(KGlobalSettings::toolBarFont());
55     slotNewOffset(0);
56     setRulerMetricStyle(KRuler::Pixel);
57     setLength(1024);
58     setMaximum(1024);
59     setPixelPerMark(3);
60     setLittleMarkDistance(FRAME_SIZE);
61     setMediumMarkDistance(FRAME_SIZE * m_timecode.fps());
62     setBigMarkDistance(FRAME_SIZE * m_timecode.fps() * 60);
63     m_zoneStart = 2 * m_timecode.fps();
64     m_zoneEnd = 10 * m_timecode.fps();
65     m_contextMenu = new QMenu(this);
66     QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
67     connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
68     QAction *editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
69     connect(editGuide, SIGNAL(triggered()), m_view, SLOT(slotEditGuide()));
70     QAction *delGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
71     connect(delGuide, SIGNAL(triggered()), m_view, SLOT(slotDeleteGuide()));
72     QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
73     connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
74     setMouseTracking(true);
75 }
76
77 // virtual
78 void CustomRuler::mousePressEvent(QMouseEvent * event) {
79     if (event->button() == Qt::RightButton) {
80         m_contextMenu->exec(event->globalPos());
81         return;
82     }
83     m_view->activateMonitor();
84     int pos = (int)((event->x() + offset()));
85     m_moveCursor = RULER_CURSOR;
86     if (event->y() > 10) {
87         if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
88         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
89         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
90     }
91     if (m_moveCursor == RULER_CURSOR)
92         m_view->setCursorPos((int) pos / m_factor);
93 }
94
95 // virtual
96 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
97     if (event->buttons() == Qt::LeftButton) {
98         int pos = (int)((event->x() + offset()) / m_factor);
99         if (pos < 0) pos = 0;
100         if (m_moveCursor == RULER_CURSOR) {
101             m_view->setCursorPos(pos);
102             return;
103         } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
104         else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
105         else if (m_moveCursor == RULER_MIDDLE) {
106             int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
107             m_zoneStart += move;
108             m_zoneEnd += move;
109         }
110         m_view->setDocumentModified();
111         update();
112     } else {
113         int pos = (int)((event->x() + offset()));
114         if (event->y() <= 10) setCursor(Qt::ArrowCursor);
115         else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
116         else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
117         else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
118         else setCursor(Qt::ArrowCursor);
119     }
120 }
121
122
123 // virtual
124 void CustomRuler::wheelEvent(QWheelEvent * e) {
125     int delta = 1;
126     if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
127     if (e->delta() < 0) delta = 0 - delta;
128     m_view->moveCursorPos(delta);
129 }
130
131 int CustomRuler::inPoint() const {
132     return m_zoneStart;
133 }
134
135 int CustomRuler::outPoint() const {
136     return m_zoneEnd;
137 }
138
139 void CustomRuler::slotMoveRuler(int newPos) {
140     KRuler::slotNewOffset(newPos);
141 }
142
143 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
144     update(oldpos * m_factor - offset() - 6, 2, 17, 16);
145     update(newpos * m_factor - offset() - 6, 2, 17, 16);
146 }
147
148 void CustomRuler::setPixelPerMark(double rate) {
149     int scale = comboScale[(int) rate];
150     m_factor = 1.0 / (double) scale * FRAME_SIZE;
151     KRuler::setPixelPerMark(1.0 / scale);
152     double fend = pixelPerMark() * littleMarkDistance();
153     switch ((int) rate) {
154     case 0:
155         m_textSpacing = fend;
156         break;
157     case 1:
158         m_textSpacing = fend * 5;
159         break;
160     case 2:
161     case 3:
162     case 4:
163         m_textSpacing = fend * m_timecode.fps();
164         break;
165     case 5:
166         m_textSpacing = fend * m_timecode.fps() * 5;
167         break;
168     case 6:
169         m_textSpacing = fend * m_timecode.fps() * 10;
170         break;
171     case 7:
172         m_textSpacing = fend * m_timecode.fps() * 30;
173         break;
174     case 8:
175     case 9:
176     case 10:
177         m_textSpacing = fend * m_timecode.fps() * 60;
178         break;
179     case 11:
180     case 12:
181         m_textSpacing = fend * m_timecode.fps() * 300;
182         break;
183     case 13:
184         m_textSpacing = fend * m_timecode.fps() * 600;
185         break;
186     }
187 }
188
189 void CustomRuler::setDuration(int d) {
190     m_duration = d;
191     update();
192 }
193
194 // virtual
195 void CustomRuler::paintEvent(QPaintEvent *e) {
196     QStylePainter p(this);
197     p.setClipRect(e->rect());
198
199     const int projectEnd = (int)(m_duration * m_factor);
200     p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
201
202     const int zoneStart = (int)(m_zoneStart * m_factor);
203     const int zoneEnd = (int)(m_zoneEnd * m_factor);
204
205     p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
206
207     const int value  = m_view->cursorPos() * m_factor - offset();
208     const int minval = minimum();
209     const int maxval = maximum() + offset() - endOffset();
210
211     double f, fend,
212     offsetmin = (double)(minval - offset()),
213                 offsetmax = (double)(maxval - offset()),
214                             fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
215     QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
216
217     QPalette palette;
218     //p.fillRect(bg, palette.light());
219     // draw labels
220     p.setPen(palette.dark().color());
221     // draw littlemarklabel
222
223     // draw mediummarklabel
224
225     // draw bigmarklabel
226
227     // draw endlabel
228     /*if (d->showEndL) {
229       if (d->dir == Qt::Horizontal) {
230         p.translate( fontOffset, 0 );
231         p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
232       }*/
233
234     // draw the tiny marks
235     //if (showTinyMarks())
236     /*{
237       fend =   pixelPerMark()*tinyMarkDistance();
238       if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
239           p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
240       }
241     }*/
242
243     for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
244         QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / m_factor + 0.5));
245         p.drawText((int)f + 2, LABEL_SIZE, lab);
246     }
247
248     if (showLittleMarks()) {
249         // draw the little marks
250         fend = pixelPerMark() * littleMarkDistance();
251         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
252                 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
253     }
254     if (showMediumMarks()) {
255         // draw medium marks
256         fend = pixelPerMark() * mediumMarkDistance();
257         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
258                 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
259     }
260     if (showBigMarks()) {
261         // draw big marks
262         fend = pixelPerMark() * bigMarkDistance();
263         if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
264                 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
265     }
266     /*   if (d->showem) {
267          // draw end marks
268          if (d->dir == Qt::Horizontal) {
269            p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
270            p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
271          }
272          else {
273            p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
274            p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
275          }
276        }*/
277
278
279     // draw zone cursors
280     int off = offset();
281     if (zoneStart > 0) {
282         QPolygon pa(4);
283         pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
284         p.drawPolyline(pa);
285     }
286
287     if (zoneEnd > 0) {
288         QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
289         p.fillRect(rec, QColor(255, 255, 255, 150));
290         p.drawRect(rec);
291
292         QPolygon pa(4);
293         pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
294         p.drawPolyline(pa);
295     }
296
297     // draw pointer
298     QPolygon pa(3);
299     pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
300     p.setBrush(QBrush(Qt::yellow));
301     p.drawPolygon(pa);
302 }
303
304 #include "customruler.moc"