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 <QMouseEvent>
21 #include <QStylePainter>
26 #include <KGlobalSettings>
28 #include "customruler.h"
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);
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);
48 static int littleMarkDistance;
49 static int mediumMarkDistance;
50 static int bigMarkDistance;
52 #include "definitions.h"
54 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
56 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
57 : QWidget(parent), m_timecode(tc), m_view(parent), m_duration(0), m_offset(0) {
58 setFont(KGlobalSettings::toolBarFont());
60 littleMarkDistance = FRAME_SIZE;
61 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
62 bigMarkDistance = 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);
78 void CustomRuler::setZone(QPoint p) {
79 int min = qMin(m_zoneStart, p.x());
80 int max = qMax(m_zoneEnd, p.y());
83 update(min * m_factor - 2, 0, (max - min) * m_factor + 4, height());
87 void CustomRuler::mousePressEvent(QMouseEvent * event) {
88 if (event->button() == Qt::RightButton) {
89 m_contextMenu->exec(event->globalPos());
92 m_view->activateMonitor();
93 int pos = (int)((event->x() + offset()));
94 m_moveCursor = RULER_CURSOR;
95 if (event->y() > 10) {
96 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
97 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
98 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
100 if (m_moveCursor == RULER_CURSOR)
101 m_view->setCursorPos((int) pos / m_factor);
105 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
106 if (event->buttons() == Qt::LeftButton) {
107 int pos = (int)((event->x() + offset()) / m_factor);
108 int zoneStart = m_zoneStart;
109 int zoneEnd = m_zoneEnd;
110 if (pos < 0) pos = 0;
111 if (m_moveCursor == RULER_CURSOR) {
112 m_view->setCursorPos(pos);
113 m_view->slotCheckPositionScrolling();
115 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
116 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
117 else if (m_moveCursor == RULER_MIDDLE) {
118 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
119 if (move + m_zoneStart < 0) move = - m_zoneStart;
123 emit zoneMoved(m_zoneStart, m_zoneEnd);
124 m_view->setDocumentModified();
126 int min = qMin(m_zoneStart, zoneStart);
127 int max = qMax(m_zoneEnd, zoneEnd);
128 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
131 int pos = (int)((event->x() + offset()));
132 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
133 else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
134 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
135 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
136 else setCursor(Qt::ArrowCursor);
142 void CustomRuler::wheelEvent(QWheelEvent * e) {
144 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
145 if (e->delta() < 0) delta = 0 - delta;
146 m_view->moveCursorPos(delta);
149 int CustomRuler::inPoint() const {
153 int CustomRuler::outPoint() const {
157 void CustomRuler::slotMoveRuler(int newPos) {
162 int CustomRuler::offset() const {
166 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
167 if (qAbs(oldpos - newpos) * m_factor > 40) {
168 update(oldpos * m_factor - offset() - 6, 7, 17, 16);
169 update(newpos * m_factor - offset() - 6, 7, 17, 16);
170 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 7, qAbs(oldpos - newpos) * m_factor + 17, 16);
173 void CustomRuler::setPixelPerMark(double rate) {
174 int scale = comboScale[(int) rate];
175 m_factor = 1.0 / (double) scale * FRAME_SIZE;
176 m_scale = 1.0 / (double) scale;
177 double fend = m_scale * littleMarkDistance;
178 switch ((int) rate) {
180 m_textSpacing = fend;
183 m_textSpacing = fend * 5;
188 m_textSpacing = fend * m_timecode.fps();
191 m_textSpacing = fend * m_timecode.fps() * 5;
194 m_textSpacing = fend * m_timecode.fps() * 10;
197 m_textSpacing = fend * m_timecode.fps() * 30;
202 m_textSpacing = fend * m_timecode.fps() * 60;
206 m_textSpacing = fend * m_timecode.fps() * 300;
209 m_textSpacing = fend * m_timecode.fps() * 600;
215 void CustomRuler::setDuration(int d) {
216 int oldduration = m_duration;
218 update(qMin(oldduration, m_duration) * m_factor - 1, 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
222 void CustomRuler::paintEvent(QPaintEvent *e) {
223 QStylePainter p(this);
224 p.setClipRect(e->rect());
226 const int projectEnd = (int)(m_duration * m_factor);
227 p.fillRect(QRect(0, 0, projectEnd - m_offset, height()), QBrush(QColor(245, 245, 245)));
229 const int zoneStart = (int)(m_zoneStart * m_factor);
230 const int zoneEnd = (int)(m_zoneEnd * m_factor);
232 p.fillRect(QRect(zoneStart - offset(), height() / 2, zoneEnd - zoneStart, height() / 2), QBrush(QColor(133, 255, 143)));
234 const int value = m_view->cursorPos() * m_factor - offset();
235 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
236 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
237 if (minval < 0) minval = 0;
240 const int offsetmax = maxval * FRAME_SIZE;
243 p.setPen(palette.dark().color());
244 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
245 offsetmin = offsetmin * m_textSpacing;
246 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
247 QString lab = m_timecode.getTimecodeFromFrames((int)((f) / m_factor + 0.5));
248 p.drawText((int)f - m_offset + 2, LABEL_SIZE, lab);
252 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
253 offsetmin = offsetmin * littleMarkDistance;
254 // draw the little marks
255 fend = m_scale * littleMarkDistance;
256 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
257 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
260 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
261 offsetmin = offsetmin * mediumMarkDistance;
263 fend = m_scale * mediumMarkDistance;
264 if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
265 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
268 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
269 offsetmin = offsetmin * bigMarkDistance;
271 fend = m_scale * bigMarkDistance;
272 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
273 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
280 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
285 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
286 p.fillRect(rec, QColor(255, 255, 255, 150));
290 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
296 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
297 p.setBrush(QBrush(Qt::yellow));
301 #include "customruler.moc"