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)
56 : QWidget(parent), m_timecode(tc), m_view(parent), m_duration(0), m_offset(0) {
57 setFont(KGlobalSettings::toolBarFont());
59 m_bgColor = QColor(245, 245, 245);
60 m_zoneColor = QColor(133, 255, 143);
61 littleMarkDistance = FRAME_SIZE;
62 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
63 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
66 m_contextMenu = new QMenu(this);
67 QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
68 connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
69 QAction *editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
70 connect(editGuide, SIGNAL(triggered()), m_view, SLOT(slotEditGuide()));
71 QAction *delGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
72 connect(delGuide, SIGNAL(triggered()), m_view, SLOT(slotDeleteGuide()));
73 QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
74 connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
75 setMouseTracking(true);
79 void CustomRuler::setZone(QPoint p) {
80 int min = qMin(m_zoneStart, p.x());
81 int max = qMax(m_zoneEnd, p.y());
84 update(min * m_factor - 2, 0, (max - min) * m_factor + 4, height());
88 void CustomRuler::mousePressEvent(QMouseEvent * event) {
89 if (event->button() == Qt::RightButton) {
90 m_contextMenu->exec(event->globalPos());
93 m_view->activateMonitor();
94 int pos = (int)((event->x() + offset()));
95 m_moveCursor = RULER_CURSOR;
96 if (event->y() > 10) {
97 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
98 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
99 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
101 if (m_moveCursor == RULER_CURSOR)
102 m_view->setCursorPos((int) pos / m_factor);
106 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
107 if (event->buttons() == Qt::LeftButton) {
108 int pos = (int)((event->x() + offset()) / m_factor);
109 int zoneStart = m_zoneStart;
110 int zoneEnd = m_zoneEnd;
111 if (pos < 0) pos = 0;
112 if (m_moveCursor == RULER_CURSOR) {
113 m_view->setCursorPos(pos);
114 m_view->slotCheckPositionScrolling();
116 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
117 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
118 else if (m_moveCursor == RULER_MIDDLE) {
119 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
120 if (move + m_zoneStart < 0) move = - m_zoneStart;
124 emit zoneMoved(m_zoneStart, m_zoneEnd);
125 m_view->setDocumentModified();
127 int min = qMin(m_zoneStart, zoneStart);
128 int max = qMax(m_zoneEnd, zoneEnd);
129 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
132 int pos = (int)((event->x() + offset()));
133 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
134 else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
135 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
136 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
137 else setCursor(Qt::ArrowCursor);
143 void CustomRuler::wheelEvent(QWheelEvent * e) {
145 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
146 if (e->delta() < 0) delta = 0 - delta;
147 m_view->moveCursorPos(delta);
150 int CustomRuler::inPoint() const {
154 int CustomRuler::outPoint() const {
158 void CustomRuler::slotMoveRuler(int newPos) {
163 int CustomRuler::offset() const {
167 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
168 if (qAbs(oldpos - newpos) * m_factor > 40) {
169 update(oldpos * m_factor - offset() - 6, 7, 17, 16);
170 update(newpos * m_factor - offset() - 6, 7, 17, 16);
171 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 7, qAbs(oldpos - newpos) * m_factor + 17, 16);
174 void CustomRuler::setPixelPerMark(double rate) {
175 int scale = comboScale[(int) rate];
176 m_factor = 1.0 / (double) scale * FRAME_SIZE;
177 m_scale = 1.0 / (double) scale;
178 double fend = m_scale * littleMarkDistance;
179 switch ((int) rate) {
181 m_textSpacing = fend;
184 m_textSpacing = fend * 5;
189 m_textSpacing = fend * m_timecode.fps();
192 m_textSpacing = fend * m_timecode.fps() * 5;
195 m_textSpacing = fend * m_timecode.fps() * 10;
198 m_textSpacing = fend * m_timecode.fps() * 30;
203 m_textSpacing = fend * m_timecode.fps() * 60;
207 m_textSpacing = fend * m_timecode.fps() * 300;
210 m_textSpacing = fend * m_timecode.fps() * 600;
216 void CustomRuler::setDuration(int d) {
217 int oldduration = m_duration;
219 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
223 void CustomRuler::paintEvent(QPaintEvent *e) {
224 QStylePainter p(this);
225 p.setClipRect(e->rect());
227 const int projectEnd = (int)(m_duration * m_factor);
228 p.fillRect(0, 0, projectEnd - m_offset, height(), m_bgColor);
230 const int zoneStart = (int)(m_zoneStart * m_factor);
231 const int zoneEnd = (int)(m_zoneEnd * m_factor);
232 const QRect zoneRect();
234 p.fillRect(zoneStart - offset(), height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
236 const int value = m_view->cursorPos() * m_factor - offset();
237 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
238 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
239 if (minval < 0) minval = 0;
242 const int offsetmax = maxval * FRAME_SIZE;
245 p.setPen(palette.dark().color());
246 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
247 offsetmin = offsetmin * m_textSpacing;
248 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
249 QString lab = m_timecode.getTimecodeFromFrames((int)((f) / m_factor + 0.5));
250 p.drawText((int)f - m_offset + 2, LABEL_SIZE, lab);
254 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
255 offsetmin = offsetmin * littleMarkDistance;
256 // draw the little marks
257 fend = m_scale * littleMarkDistance;
258 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
259 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
262 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
263 offsetmin = offsetmin * mediumMarkDistance;
265 fend = m_scale * mediumMarkDistance;
266 if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
267 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
270 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
271 offsetmin = offsetmin * bigMarkDistance;
273 fend = m_scale * bigMarkDistance;
274 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
275 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
282 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
287 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
288 p.fillRect(rec, QColor(255, 255, 255, 150));
292 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
298 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
299 p.setBrush(QBrush(Qt::yellow));
303 #include "customruler.moc"