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)
58 setFont(KGlobalSettings::toolBarFont());
60 m_bgColor = QColor(245, 245, 245);
61 m_zoneColor = QColor(133, 255, 143);
62 littleMarkDistance = FRAME_SIZE;
63 mediumMarkDistance = FRAME_SIZE * m_timecode.fps();
64 bigMarkDistance = FRAME_SIZE * m_timecode.fps() * 60;
67 m_contextMenu = new QMenu(this);
68 QAction *addGuide = m_contextMenu->addAction(KIcon("document-new"), i18n("Add Guide"));
69 connect(addGuide, SIGNAL(triggered()), m_view, SLOT(slotAddGuide()));
70 QAction *editGuide = m_contextMenu->addAction(KIcon("document-properties"), i18n("Edit Guide"));
71 connect(editGuide, SIGNAL(triggered()), m_view, SLOT(slotEditGuide()));
72 QAction *delGuide = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete Guide"));
73 connect(delGuide, SIGNAL(triggered()), m_view, SLOT(slotDeleteGuide()));
74 QAction *delAllGuides = m_contextMenu->addAction(KIcon("edit-delete"), i18n("Delete All Guides"));
75 connect(delAllGuides, SIGNAL(triggered()), m_view, SLOT(slotDeleteAllGuides()));
76 setMouseTracking(true);
80 void CustomRuler::setZone(QPoint p)
82 int min = qMin(m_zoneStart, p.x());
83 int max = qMax(m_zoneEnd, p.y());
86 update(min * m_factor - 2, 0, (max - min) * m_factor + 4, height());
90 void CustomRuler::mousePressEvent(QMouseEvent * event)
92 if (event->button() == Qt::RightButton) {
93 m_contextMenu->exec(event->globalPos());
96 m_view->activateMonitor();
97 int pos = (int)((event->x() + offset()));
98 m_moveCursor = RULER_CURSOR;
99 if (event->y() > 10) {
100 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
101 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
102 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
104 if (m_moveCursor == RULER_CURSOR)
105 m_view->setCursorPos((int) pos / m_factor);
109 void CustomRuler::mouseMoveEvent(QMouseEvent * event)
111 if (event->buttons() == Qt::LeftButton) {
112 int pos = (int)((event->x() + offset()) / m_factor);
113 int zoneStart = m_zoneStart;
114 int zoneEnd = m_zoneEnd;
115 if (pos < 0) pos = 0;
116 if (m_moveCursor == RULER_CURSOR) {
117 m_view->setCursorPos(pos);
118 m_view->slotCheckPositionScrolling();
120 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
121 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
122 else if (m_moveCursor == RULER_MIDDLE) {
123 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
124 if (move + m_zoneStart < 0) move = - m_zoneStart;
128 emit zoneMoved(m_zoneStart, m_zoneEnd);
129 m_view->setDocumentModified();
131 int min = qMin(m_zoneStart, zoneStart);
132 int max = qMax(m_zoneEnd, zoneEnd);
133 update(min * m_factor - m_offset - 2, 0, (max - min) * m_factor + 4, height());
136 int pos = (int)((event->x() + offset()));
137 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
138 else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
139 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
140 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
141 else setCursor(Qt::ArrowCursor);
147 void CustomRuler::wheelEvent(QWheelEvent * e)
150 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
151 if (e->delta() < 0) delta = 0 - delta;
152 m_view->moveCursorPos(delta);
155 int CustomRuler::inPoint() const
160 int CustomRuler::outPoint() const
165 void CustomRuler::slotMoveRuler(int newPos)
171 int CustomRuler::offset() const
176 void CustomRuler::slotCursorMoved(int oldpos, int newpos)
178 if (qAbs(oldpos - newpos) * m_factor > 40) {
179 update(oldpos * m_factor - offset() - 6, 7, 17, 16);
180 update(newpos * m_factor - offset() - 6, 7, 17, 16);
181 } else update(qMin(oldpos, newpos) * m_factor - offset() - 6, 7, qAbs(oldpos - newpos) * m_factor + 17, 16);
184 void CustomRuler::setPixelPerMark(double rate)
186 int scale = comboScale[(int) rate];
187 m_factor = 1.0 / (double) scale * FRAME_SIZE;
188 m_scale = 1.0 / (double) scale;
189 double fend = m_scale * littleMarkDistance;
190 switch ((int) rate) {
192 m_textSpacing = fend;
195 m_textSpacing = fend * 5;
200 m_textSpacing = fend * m_timecode.fps();
203 m_textSpacing = fend * m_timecode.fps() * 5;
206 m_textSpacing = fend * m_timecode.fps() * 10;
209 m_textSpacing = fend * m_timecode.fps() * 30;
214 m_textSpacing = fend * m_timecode.fps() * 60;
218 m_textSpacing = fend * m_timecode.fps() * 300;
221 m_textSpacing = fend * m_timecode.fps() * 600;
227 void CustomRuler::setDuration(int d)
229 int oldduration = m_duration;
231 update(qMin(oldduration, m_duration) * m_factor - 1 - offset(), 0, qAbs(oldduration - m_duration) * m_factor + 2, height());
235 void CustomRuler::paintEvent(QPaintEvent *e)
237 QStylePainter p(this);
238 p.setClipRect(e->rect());
240 const int projectEnd = (int)(m_duration * m_factor);
241 p.fillRect(0, 0, projectEnd - m_offset, height(), m_bgColor);
243 const int zoneStart = (int)(m_zoneStart * m_factor);
244 const int zoneEnd = (int)(m_zoneEnd * m_factor);
245 const QRect zoneRect();
247 p.fillRect(zoneStart - offset(), height() / 2, zoneEnd - zoneStart, height() / 2, m_zoneColor);
249 const int value = m_view->cursorPos() * m_factor - offset();
250 int minval = (e->rect().left() + m_offset) / FRAME_SIZE - 1;
251 const int maxval = (e->rect().right() + m_offset) / FRAME_SIZE + 1;
252 if (minval < 0) minval = 0;
255 const int offsetmax = maxval * FRAME_SIZE;
258 p.setPen(palette.dark().color());
259 int offsetmin = (e->rect().left() + m_offset) / m_textSpacing;
260 offsetmin = offsetmin * m_textSpacing;
261 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
262 QString lab = m_timecode.getTimecodeFromFrames((int)((f) / m_factor + 0.5));
263 p.drawText((int)f - m_offset + 2, LABEL_SIZE, lab);
267 offsetmin = (e->rect().left() + m_offset) / littleMarkDistance;
268 offsetmin = offsetmin * littleMarkDistance;
269 // draw the little marks
270 fend = m_scale * littleMarkDistance;
271 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
272 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
275 offsetmin = (e->rect().left() + m_offset) / mediumMarkDistance;
276 offsetmin = offsetmin * mediumMarkDistance;
278 fend = m_scale * mediumMarkDistance;
279 if (fend > 5) for (f = offsetmin - m_offset - fend; f < offsetmax - m_offset + fend; f += fend)
280 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
283 offsetmin = (e->rect().left() + m_offset) / bigMarkDistance;
284 offsetmin = offsetmin * bigMarkDistance;
286 fend = m_scale * bigMarkDistance;
287 if (fend > 5) for (f = offsetmin - m_offset; f < offsetmax - m_offset; f += fend)
288 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
295 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
300 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
301 p.fillRect(rec, QColor(255, 255, 255, 150));
305 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
311 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
312 p.setBrush(QBrush(Qt::yellow));
316 #include "customruler.moc"