1 /***************************************************************************
2 * Copyright (C) 2007 by Jean-Baptiste Mardelle (jb@kdenlive.org) *
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. *
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. *
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 ***************************************************************************/
20 #include "customruler.h"
25 #include <KGlobalSettings>
27 #include <QMouseEvent>
28 #include <QStylePainter>
30 static const int FIX_WIDTH = 24; /* widget width in pixel */
31 static const int LINE_END = (FIX_WIDTH - 3);
32 static const int END_MARK_LENGTH = (FIX_WIDTH - 8);
33 static const int BIG_MARK_LENGTH = (END_MARK_LENGTH * 3 / 4);
34 static const int BIG_MARK_X2 = LINE_END;
35 static const int BIG_MARK_X1 = (BIG_MARK_X2 - BIG_MARK_LENGTH);
36 static const int MIDDLE_MARK_LENGTH = (END_MARK_LENGTH / 2);
37 static const int MIDDLE_MARK_X2 = LINE_END;
38 static const int MIDDLE_MARK_X1 = (MIDDLE_MARK_X2 - MIDDLE_MARK_LENGTH);
39 static const int LITTLE_MARK_LENGTH = (MIDDLE_MARK_LENGTH / 2);
40 static const int LITTLE_MARK_X2 = LINE_END;
41 static const int LITTLE_MARK_X1 = (LITTLE_MARK_X2 - LITTLE_MARK_LENGTH);
43 static const int LABEL_SIZE = 9;
44 static const int END_LABEL_X = 4;
45 static const int END_LABEL_Y = (END_LABEL_X + LABEL_SIZE - 2);
47 static int littleMarkDistance;
48 static int mediumMarkDistance;
49 static int bigMarkDistance;
51 #include "definitions.h"
53 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 750, 1500, 3000, 6000, 12000};
55 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
63 setFont(KGlobalSettings::toolBarFont());
65 m_zoneColor = QColor(133, 255, 143);
66 littleMarkDistance = FRAME_SIZE;
67 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
68 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
71 m_contextMenu = new QMenu(this);
72 QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
73 connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
74 m_editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
75 connect(m_editGuide, SIGNAL(triggered()), this, SLOT(slotEditGuide()));
76 m_deleteGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
77 connect(m_deleteGuide , SIGNAL(triggered()), this, SLOT(slotDeleteGuide()));
78 QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
79 connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
80 setMouseTracking(true);
84 void CustomRuler::updateProjectFps(Timecode t)
87 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
88 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
92 void CustomRuler::slotEditGuide()
94 m_view->slotEditGuide(m_clickedGuide);
97 void CustomRuler::slotDeleteGuide()
99 m_view->slotDeleteGuide(m_clickedGuide);
102 void CustomRuler::setZone(QPoint p)
104 int min = qMin(m_zoneStart, p.x());
105 int max = qMax(m_zoneEnd, p.y());
108 update(min * m_factor - 2, 0, (max - min) * m_factor + 4, height());
112 void CustomRuler::mousePressEvent(QMouseEvent * event)
114 int pos = (int)((event->x() + offset()));
115 if (event->button() == Qt::RightButton) {
116 m_clickedGuide = m_view->hasGuide((int)(pos / m_factor), (int)(5 / m_factor + 1));
117 m_editGuide->setEnabled(m_clickedGuide > 0);
118 m_deleteGuide->setEnabled(m_clickedGuide > 0);
119 m_contextMenu->exec(event->globalPos());
122 setFocus(Qt::MouseFocusReason);
123 m_view->activateMonitor();
124 m_moveCursor = RULER_CURSOR;
125 if (event->y() > 10) {
126 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
127 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
128 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
130 if (m_moveCursor == RULER_CURSOR)
131 m_view->setCursorPos((int) pos / m_factor);
135 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
137 if (event->buttons() == Qt::LeftButton) {
138 int pos = (int)((event->x() + offset()) / m_factor);
139 int zoneStart = m_zoneStart;
140 int zoneEnd = m_zoneEnd;
141 if (pos < 0) pos = 0;
142 if (m_moveCursor == RULER_CURSOR) {
143 m_view->setCursorPos(pos);
144 m_view->slotCheckPositionScrolling();
146 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
147 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
148 else if (m_moveCursor == RULER_MIDDLE) {
149 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
150 if (move + m_zoneStart < 0) move = - m_zoneStart;
154 emit zoneMoved(m_zoneStart, m_zoneEnd);
155 m_view->setDocumentModified();
157 int min = qMin(m_zoneStart, zoneStart);
158 int max = qMax(m_zoneEnd, zoneEnd);
159 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
162 int pos = (int)((event->x() + offset()));
163 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
164 else if (qAbs(pos - m_zoneStart * m_factor) < 4) {
165 setCursor(KCursor("left_side", Qt::SizeHorCursor));
166 setToolTip(i18n("Zone start: %1", m_timecode.getTimecodeFromFrames(m_zoneStart)));
167 } else if (qAbs(pos - m_zoneEnd * m_factor) < 4) {
168 setCursor(KCursor("right_side", Qt::SizeHorCursor));
169 setToolTip(i18n("Zone end: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd)));
170 } else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) {
171 setCursor(Qt::SizeHorCursor);
172 setToolTip(i18n("Zone duration: %1", m_timecode.getTimecodeFromFrames(m_zoneEnd - m_zoneStart)));
174 setCursor(Qt::ArrowCursor);
175 setToolTip(QString());
182 void CustomRuler::wheelEvent(QWheelEvent * e)
185 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
186 if (e->delta() < 0) delta = 0 - delta;
187 m_view->moveCursorPos(delta);
190 int CustomRuler::inPoint() const
195 int CustomRuler::outPoint() const
200 void CustomRuler::slotMoveRuler(int newPos)
206 int CustomRuler::offset() const
211 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
213 if (qAbs(oldpos - newpos) * m_factor > m_textSpacing) {
214 update(oldpos * m_factor - offset() - 6, 0, 17, height());
215 update(newpos * m_factor - offset() - 6, 0, 17, height());
216 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 0, qAbs(oldpos - newpos) * m_factor + 17, height());
219 void CustomRuler::setPixelPerMark(double rate)
221 int scale = comboScale[(int) rate];
222 m_factor = 1.0 / (double) scale * FRAME_SIZE;
223 m_scale = 1.0 / (double) scale;
224 double fend = m_scale * littleMarkDistance;
226 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
227 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 300;
228 } else if (rate > 6) {
229 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 10;
230 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 30;
231 } else if (rate > 3) {
232 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
233 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 5;
235 mediumMarkDistance = (double) FRAME_SIZE * m_timecode.fps();
236 bigMarkDistance = (double) FRAME_SIZE * m_timecode.fps() * 60;
238 switch ((int) rate) {
240 m_textSpacing = fend;
243 m_textSpacing = fend * 5;
248 m_textSpacing = fend * m_timecode.fps();
251 m_textSpacing = fend * m_timecode.fps() * 5;
254 m_textSpacing = fend * m_timecode.fps() * 10;
257 m_textSpacing = fend * m_timecode.fps() * 30;
261 m_textSpacing = fend * m_timecode.fps() * 40;
264 m_textSpacing = fend * m_timecode.fps() * 80;
268 m_textSpacing = fend * m_timecode.fps() * 400;
271 m_textSpacing = fend * m_timecode.fps() * 800;
277 void CustomRuler::setDuration(int d)
279 int oldduration = m_duration;
281 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
285 void CustomRuler::paintEvent(QPaintEvent *e)
287 QStylePainter p(this);
288 p.setClipRect(e->rect());
289 const int projectEnd = (int)(m_duration * m_factor);
290 p.fillRect(0, 0, projectEnd - m_offset, height(), palette().alternateBase().color());
292 const int zoneStart = (int)(m_zoneStart * m_factor);
293 const int zoneEnd = (int)(m_zoneEnd * m_factor);
294 const QRect zoneRect();
296 p.fillRect(zoneStart - m_offset, height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
298 const int value = m_view->cursorPos() * m_factor - m_offset;
299 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
300 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
301 if (minval < 0) minval = 0;
304 const int offsetmax = maxval * FRAME_SIZE;
306 p.setPen(palette().dark().color());
309 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
310 offsetmin = offsetmin * m_textSpacing;
311 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
312 QString lab = m_timecode.getTimecodeFromFrames((int)(f / m_factor + 0.5));
313 p.drawText(f - m_offset + 2, LABEL_SIZE, lab);
316 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
317 offsetmin = offsetmin * littleMarkDistance;
318 // draw the little marks
319 fend = m_scale * littleMarkDistance;
320 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
321 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
323 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
324 offsetmin = offsetmin * mediumMarkDistance;
326 fend = m_scale * mediumMarkDistance;
327 if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
328 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
330 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
331 offsetmin = offsetmin * bigMarkDistance;
333 fend = m_scale * bigMarkDistance;
334 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
335 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
341 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
346 QColor center(Qt::white);
347 center.setAlpha(150);
348 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
349 p.fillRect(rec, center);
353 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
359 pa.setPoints(3, value - 6, 8, value + 6, 8, value, 16);
360 p.setBrush(palette().highlight());
364 #include "customruler.moc"