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);
114 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
115 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
116 else if (m_moveCursor == RULER_MIDDLE) {
117 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
121 emit zoneMoved(m_zoneStart, m_zoneEnd);
122 m_view->setDocumentModified();
124 int min = qMin(m_zoneStart, zoneStart);
125 int max = qMax(m_zoneEnd, zoneEnd);
126 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
129 int pos = (int)((event->x() + offset()));
130 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
131 else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
132 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
133 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
134 else setCursor(Qt::ArrowCursor);
140 void CustomRuler::wheelEvent(QWheelEvent * e) {
142 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
143 if (e->delta() < 0) delta = 0 - delta;
144 m_view->moveCursorPos(delta);
147 int CustomRuler::inPoint() const {
151 int CustomRuler::outPoint() const {
155 void CustomRuler::slotMoveRuler(int newPos) {
160 int CustomRuler::offset() const {
164 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
165 if (qAbs(oldpos - newpos) * m_factor > 40) {
166 update(oldpos * m_factor - offset() - 6, 7, 17, 16);
167 update(newpos * m_factor - offset() - 6, 7, 17, 16);
168 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 7, qAbs(oldpos - newpos) * m_factor + 17, 16);
171 void CustomRuler::setPixelPerMark(double rate) {
172 int scale = comboScale[(int) rate];
173 m_factor = 1.0 / (double) scale * FRAME_SIZE;
174 m_scale = 1.0 / (double) scale;
175 double fend = m_scale * littleMarkDistance;
176 switch ((int) rate) {
178 m_textSpacing = fend;
181 m_textSpacing = fend * 5;
186 m_textSpacing = fend * m_timecode.fps();
189 m_textSpacing = fend * m_timecode.fps() * 5;
192 m_textSpacing = fend * m_timecode.fps() * 10;
195 m_textSpacing = fend * m_timecode.fps() * 30;
200 m_textSpacing = fend * m_timecode.fps() * 60;
204 m_textSpacing = fend * m_timecode.fps() * 300;
207 m_textSpacing = fend * m_timecode.fps() * 600;
213 void CustomRuler::setDuration(int d) {
214 int oldduration = m_duration;
216 update(qMin(oldduration, m_duration) * m_factor - 1, 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
220 void CustomRuler::paintEvent(QPaintEvent *e) {
221 QStylePainter p(this);
222 p.setClipRect(e->rect());
224 const int projectEnd = (int)(m_duration * m_factor);
225 p.fillRect(QRect(0, 0, projectEnd - m_offset, height()), QBrush(QColor(245, 245, 245)));
227 const int zoneStart = (int)(m_zoneStart * m_factor);
228 const int zoneEnd = (int)(m_zoneEnd * m_factor);
230 p.fillRect(QRect(zoneStart - offset(), height() / 2, zoneEnd - zoneStart, height() / 2), QBrush(QColor(133, 255, 143)));
232 const int value = m_view->cursorPos() * m_factor - offset();
233 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
234 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
235 if (minval < 0) minval = 0;
238 const int offsetmax = maxval * FRAME_SIZE;
241 p.setPen(palette.dark().color());
242 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
243 offsetmin = offsetmin * m_textSpacing;
244 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
245 QString lab = m_timecode.getTimecodeFromFrames((int)((f) / m_factor + 0.5));
246 p.drawText((int)f - m_offset + 2, LABEL_SIZE, lab);
250 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
251 offsetmin = offsetmin * littleMarkDistance;
252 // draw the little marks
253 fend = m_scale * littleMarkDistance;
254 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
255 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
258 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
259 offsetmin = offsetmin * mediumMarkDistance;
261 fend = m_scale * mediumMarkDistance;
262 if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
263 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
266 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
267 offsetmin = offsetmin * bigMarkDistance;
269 fend = m_scale * bigMarkDistance;
270 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
271 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
278 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
283 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
284 p.fillRect(rec, QColor(255, 255, 255, 150));
288 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
294 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
295 p.setBrush(QBrush(Qt::yellow));
299 #include "customruler.moc"