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, 725, 1500, 3000, 6000, 12000};
55 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent) :
62 setFont(KGlobalSettings::toolBarFont());
64 m_bgColor = QColor(245, 245, 245);
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 QAction *editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
75 connect(editGuide, SIGNAL(triggered()), m_view, SLOT(slotEditGuide()));
76 QAction *delGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
77 connect(delGuide, SIGNAL(triggered()), m_view, 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::setZone(QPoint p)
86 int min = qMin(m_zoneStart, p.x());
87 int max = qMax(m_zoneEnd, p.y());
90 update(min * m_factor - 2, 0, (max - min) * m_factor + 4, height());
94 void CustomRuler::mousePressEvent(QMouseEvent * event)
96 if (event->button() == Qt::RightButton) {
97 m_contextMenu->exec(event->globalPos());
100 m_view->activateMonitor();
101 int pos = (int)((event->x() + offset()));
102 m_moveCursor = RULER_CURSOR;
103 if (event->y() > 10) {
104 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
105 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
106 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
108 if (m_moveCursor == RULER_CURSOR)
109 m_view->setCursorPos((int) pos / m_factor);
113 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
115 if (event->buttons() == Qt::LeftButton) {
116 int pos = (int)((event->x() + offset()) / m_factor);
117 int zoneStart = m_zoneStart;
118 int zoneEnd = m_zoneEnd;
119 if (pos < 0) pos = 0;
120 if (m_moveCursor == RULER_CURSOR) {
121 m_view->setCursorPos(pos);
122 m_view->slotCheckPositionScrolling();
124 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
125 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
126 else if (m_moveCursor == RULER_MIDDLE) {
127 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
128 if (move + m_zoneStart < 0) move = - m_zoneStart;
132 emit zoneMoved(m_zoneStart, m_zoneEnd);
133 m_view->setDocumentModified();
135 int min = qMin(m_zoneStart, zoneStart);
136 int max = qMax(m_zoneEnd, zoneEnd);
137 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
140 int pos = (int)((event->x() + offset()));
141 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
142 else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
143 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
144 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
145 else setCursor(Qt::ArrowCursor);
151 void CustomRuler::wheelEvent(QWheelEvent * e)
154 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
155 if (e->delta() < 0) delta = 0 - delta;
156 m_view->moveCursorPos(delta);
159 int CustomRuler::inPoint() const
164 int CustomRuler::outPoint() const
169 void CustomRuler::slotMoveRuler(int newPos)
175 int CustomRuler::offset() const
180 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
182 if (qAbs(oldpos - newpos) * m_factor > 40) {
183 update(oldpos * m_factor - offset() - 6, 7, 17, 16);
184 update(newpos * m_factor - offset() - 6, 7, 17, 16);
185 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 7, qAbs(oldpos - newpos) * m_factor + 17, 16);
188 void CustomRuler::setPixelPerMark(double rate)
190 int scale = comboScale[(int) rate];
191 m_factor = 1.0 / (double) scale * FRAME_SIZE;
192 m_scale = 1.0 / (double) scale;
193 double fend = m_scale * littleMarkDistance;
194 switch ((int) rate) {
196 m_textSpacing = fend;
199 m_textSpacing = fend * 5;
204 m_textSpacing = fend * m_timecode.fps();
207 m_textSpacing = fend * m_timecode.fps() * 5;
210 m_textSpacing = fend * m_timecode.fps() * 10;
213 m_textSpacing = fend * m_timecode.fps() * 30;
218 m_textSpacing = fend * m_timecode.fps() * 60;
222 m_textSpacing = fend * m_timecode.fps() * 300;
225 m_textSpacing = fend * m_timecode.fps() * 600;
231 void CustomRuler::setDuration(int d)
233 int oldduration = m_duration;
235 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
239 void CustomRuler::paintEvent(QPaintEvent *e)
241 QStylePainter p(this);
242 p.setClipRect(e->rect());
244 const int projectEnd = (int)(m_duration * m_factor);
245 p.fillRect(0, 0, projectEnd - m_offset, height(), m_bgColor);
247 const int zoneStart = (int)(m_zoneStart * m_factor);
248 const int zoneEnd = (int)(m_zoneEnd * m_factor);
249 const QRect zoneRect();
251 p.fillRect(zoneStart - offset(), height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
253 const int value = m_view->cursorPos() * m_factor - offset();
254 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
255 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
256 if (minval < 0) minval = 0;
259 const int offsetmax = maxval * FRAME_SIZE;
262 p.setPen(palette.dark().color());
263 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
264 offsetmin = offsetmin * m_textSpacing;
265 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
266 QString lab = m_timecode.getTimecodeFromFrames((int)((f) / m_factor + 0.5));
267 p.drawText((int)f - m_offset + 2, LABEL_SIZE, lab);
271 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
272 offsetmin = offsetmin * littleMarkDistance;
273 // draw the little marks
274 fend = m_scale * littleMarkDistance;
275 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
276 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
279 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
280 offsetmin = offsetmin * mediumMarkDistance;
282 fend = m_scale * mediumMarkDistance;
283 if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
284 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
287 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
288 offsetmin = offsetmin * bigMarkDistance;
290 fend = m_scale * bigMarkDistance;
291 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
292 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
299 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
304 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
305 p.fillRect(rec, QColor(255, 255, 255, 150));
309 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
315 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
316 p.setBrush(QBrush(Qt::yellow));
320 #include "customruler.moc"