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 #include "definitions.h"
50 const int CustomRuler::comboScale[] = { 1, 2, 5, 10, 25, 50, 125, 250, 500, 725, 1500, 3000, 6000, 12000};
52 CustomRuler::CustomRuler(Timecode tc, CustomTrackView *parent)
53 : KRuler(parent), m_timecode(tc), m_view(parent), m_duration(0) {
54 setFont(KGlobalSettings::toolBarFont());
56 setRulerMetricStyle(KRuler::Pixel);
60 setLittleMarkDistance(FRAME_SIZE);
61 setMediumMarkDistance(FRAME_SIZE * m_timecode.fps());
62 setBigMarkDistance(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::mousePressEvent(QMouseEvent * event) {
79 if (event->button() == Qt::RightButton) {
80 m_contextMenu->exec(event->globalPos());
83 m_view->activateMonitor();
84 int pos = (int)((event->x() + offset()));
85 m_moveCursor = RULER_CURSOR;
86 if (event->y() > 10) {
87 if (qAbs(pos - m_zoneStart * m_factor) < 4) m_moveCursor = RULER_START;
88 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) m_moveCursor = RULER_MIDDLE;
89 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) m_moveCursor = RULER_END;
91 if (m_moveCursor == RULER_CURSOR)
92 m_view->setCursorPos((int) pos / m_factor);
96 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
97 if (event->buttons() == Qt::LeftButton) {
98 int pos = (int)((event->x() + offset()) / m_factor);
100 if (m_moveCursor == RULER_CURSOR) {
101 m_view->setCursorPos(pos);
103 } else if (m_moveCursor == RULER_START) m_zoneStart = pos;
104 else if (m_moveCursor == RULER_END) m_zoneEnd = pos;
105 else if (m_moveCursor == RULER_MIDDLE) {
106 int move = pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2);
112 int pos = (int)((event->x() + offset()));
113 if (event->y() <= 10) setCursor(Qt::ArrowCursor);
114 else if (qAbs(pos - m_zoneStart * m_factor) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
115 else if (qAbs(pos - m_zoneEnd * m_factor) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
116 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * m_factor) < 4) setCursor(Qt::SizeHorCursor);
117 else setCursor(Qt::ArrowCursor);
123 void CustomRuler::wheelEvent(QWheelEvent * e) {
125 if (e->modifiers() == Qt::ControlModifier) delta = m_timecode.fps();
126 if (e->delta() < 0) delta = 0 - delta;
127 m_view->moveCursorPos(delta);
130 int CustomRuler::inPoint() const {
134 int CustomRuler::outPoint() const {
138 void CustomRuler::slotMoveRuler(int newPos) {
139 KRuler::slotNewOffset(newPos);
142 void CustomRuler::slotCursorMoved(int oldpos, int newpos) {
143 update(oldpos * m_factor - offset() - 6, 2, 17, 16);
144 update(newpos * m_factor - offset() - 6, 2, 17, 16);
147 void CustomRuler::setPixelPerMark(double rate) {
148 int scale = comboScale[(int) rate];
149 m_factor = 1.0 / (double) scale * FRAME_SIZE;
150 KRuler::setPixelPerMark(1.0 / scale);
151 double fend = pixelPerMark() * littleMarkDistance();
152 switch ((int) rate) {
154 m_textSpacing = fend;
157 m_textSpacing = fend * 5;
162 m_textSpacing = fend * m_timecode.fps();
165 m_textSpacing = fend * m_timecode.fps() * 5;
168 m_textSpacing = fend * m_timecode.fps() * 10;
171 m_textSpacing = fend * m_timecode.fps() * 30;
176 m_textSpacing = fend * m_timecode.fps() * 60;
180 m_textSpacing = fend * m_timecode.fps() * 300;
183 m_textSpacing = fend * m_timecode.fps() * 600;
188 void CustomRuler::setDuration(int d) {
194 void CustomRuler::paintEvent(QPaintEvent *e) {
195 QStylePainter p(this);
196 p.setClipRect(e->rect());
198 const int projectEnd = (int)(m_duration * m_factor);
199 p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
201 const int zoneStart = (int)(m_zoneStart * m_factor);
202 const int zoneEnd = (int)(m_zoneEnd * m_factor);
204 p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
206 const int value = m_view->cursorPos() * m_factor - offset();
207 const int minval = minimum();
208 const int maxval = maximum() + offset() - endOffset();
211 offsetmin = (double)(minval - offset()),
212 offsetmax = (double)(maxval - offset()),
213 fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
214 QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
217 //p.fillRect(bg, palette.light());
219 p.setPen(palette.dark().color());
220 // draw littlemarklabel
222 // draw mediummarklabel
228 if (d->dir == Qt::Horizontal) {
229 p.translate( fontOffset, 0 );
230 p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
233 // draw the tiny marks
234 //if (showTinyMarks())
236 fend = pixelPerMark()*tinyMarkDistance();
237 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
238 p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
242 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
243 QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / m_factor + 0.5));
244 p.drawText((int)f + 2, LABEL_SIZE, lab);
247 if (showLittleMarks()) {
248 // draw the little marks
249 fend = pixelPerMark() * littleMarkDistance();
250 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
251 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
253 if (showMediumMarks()) {
255 fend = pixelPerMark() * mediumMarkDistance();
256 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
257 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
259 if (showBigMarks()) {
261 fend = pixelPerMark() * bigMarkDistance();
262 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
263 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
267 if (d->dir == Qt::Horizontal) {
268 p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
269 p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
272 p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
273 p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
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"