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 * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_START;
88 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_MIDDLE;
89 else if (qAbs(pos - m_zoneEnd * pixelPerMark() * FRAME_SIZE) < 4) m_moveCursor = RULER_END;
91 if (m_moveCursor == RULER_CURSOR)
92 m_view->setCursorPos((int) pos / pixelPerMark() / FRAME_SIZE);
96 void CustomRuler::mouseMoveEvent(QMouseEvent * event) {
97 if (event->buttons() == Qt::LeftButton) {
98 int pos = (int)((event->x() + offset()) / pixelPerMark() / FRAME_SIZE);
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 * pixelPerMark() * FRAME_SIZE) < 4) setCursor(KCursor("left_side", Qt::SizeHorCursor));
115 else if (qAbs(pos - m_zoneEnd * pixelPerMark() * FRAME_SIZE) < 4) setCursor(KCursor("right_side", Qt::SizeHorCursor));
116 else if (qAbs(pos - (m_zoneStart + (m_zoneEnd - m_zoneStart) / 2) * pixelPerMark() * FRAME_SIZE) < 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 - offset() - 6, 2, 17, 16);
144 update(newpos - offset() - 6, 2, 17, 16);
147 void CustomRuler::setPixelPerMark(double rate) {
148 int scale = comboScale[(int) rate];
149 KRuler::setPixelPerMark(1.0 / scale);
150 double fend = pixelPerMark() * littleMarkDistance();
151 switch ((int) rate) {
153 m_textSpacing = fend;
156 m_textSpacing = fend * 5;
161 m_textSpacing = fend * m_timecode.fps();
164 m_textSpacing = fend * m_timecode.fps() * 5;
167 m_textSpacing = fend * m_timecode.fps() * 10;
170 m_textSpacing = fend * m_timecode.fps() * 30;
175 m_textSpacing = fend * m_timecode.fps() * 60;
179 m_textSpacing = fend * m_timecode.fps() * 300;
182 m_textSpacing = fend * m_timecode.fps() * 600;
187 void CustomRuler::setDuration(int d) {
193 void CustomRuler::paintEvent(QPaintEvent *e) {
194 // debug ("KRuler::drawContents, %s",(horizontal==dir)?"horizontal":"vertical");
196 QStylePainter p(this);
197 p.setClipRect(e->rect());
199 //p.fillRect(e->rect(), QBrush(QColor(200, 200, 200)));
200 //kDebug()<<"RULER ZONE: "<<m_zoneStart<<", OFF: "<<offset()<<", END: "<<m_zoneEnd<<", FACTOR: "<<pixelPerMark() * FRAME_SIZE;
201 int projectEnd = (int)(m_duration * pixelPerMark() * FRAME_SIZE);
202 p.fillRect(QRect(- offset(), e->rect().y(), projectEnd, e->rect().height()), QBrush(QColor(245, 245, 245)));
205 int zoneStart = (int)(m_zoneStart * pixelPerMark() * FRAME_SIZE);
206 int zoneEnd = (int)(m_zoneEnd * pixelPerMark() * FRAME_SIZE);
208 p.fillRect(QRect(zoneStart - offset(), e->rect().y() + e->rect().height() / 2, zoneEnd - zoneStart, e->rect().height() / 2), QBrush(QColor(133, 255, 143)));
210 int value = m_view->cursorPos() - offset();
211 int minval = minimum();
212 int maxval = maximum() + offset() - endOffset();
214 //ioffsetval = value-offset;
215 // pixelpm = (int)ppm;
216 // left = clip.left(),
217 // right = clip.right();
219 offsetmin = (double)(minval - offset()),
220 offsetmax = (double)(maxval - offset()),
221 fontOffset = (((double)minval) > offsetmin) ? (double)minval : offsetmin;
222 QRect bg = QRect((int)offsetmin, 0, (int)offsetmax, height());
225 //p.fillRect(bg, palette.light());
227 p.setPen(palette.dark().color());
228 // draw littlemarklabel
230 // draw mediummarklabel
236 if (d->dir == Qt::Horizontal) {
237 p.translate( fontOffset, 0 );
238 p.drawText( END_LABEL_X, END_LABEL_Y, d->endlabel );
241 // draw the tiny marks
242 //if (showTinyMarks())
244 fend = pixelPerMark()*tinyMarkDistance();
245 if (fend > 5) for ( f=offsetmin; f<offsetmax; f+=fend ) {
246 p.drawLine((int)f, BASE_MARK_X1, (int)f, BASE_MARK_X2);
250 for (f = offsetmin; f < offsetmax; f += m_textSpacing) {
251 QString lab = m_timecode.getTimecodeFromFrames((int)((f - offsetmin) / pixelPerMark() / FRAME_SIZE + 0.5));
252 p.drawText((int)f + 2, LABEL_SIZE, lab);
255 if (showLittleMarks()) {
256 // draw the little marks
257 fend = pixelPerMark() * littleMarkDistance();
258 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
259 p.drawLine((int)f, LITTLE_MARK_X1, (int)f, LITTLE_MARK_X2);
261 if (showMediumMarks()) {
263 fend = pixelPerMark() * mediumMarkDistance();
264 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
265 p.drawLine((int)f, MIDDLE_MARK_X1, (int)f, MIDDLE_MARK_X2);
267 if (showBigMarks()) {
269 fend = pixelPerMark() * bigMarkDistance();
270 if (fend > 5) for (f = offsetmin; f < offsetmax; f += fend)
271 p.drawLine((int)f, BIG_MARK_X1, (int)f, BIG_MARK_X2);
275 if (d->dir == Qt::Horizontal) {
276 p.drawLine(minval-d->offset, END_MARK_X1, minval-d->offset, END_MARK_X2);
277 p.drawLine(maxval-d->offset, END_MARK_X1, maxval-d->offset, END_MARK_X2);
280 p.drawLine(END_MARK_X1, minval-d->offset, END_MARK_X2, minval-d->offset);
281 p.drawLine(END_MARK_X1, maxval-d->offset, END_MARK_X2, maxval-d->offset);
290 pa.setPoints(4, zoneStart - off + 3, 9, zoneStart - off, 9, zoneStart - off, 18, zoneStart - off + 3, 18);
295 QRect rec(zoneStart - off + (zoneEnd - zoneStart) / 2 - 4, 9, 8, 9);
296 p.fillRect(rec, QColor(255, 255, 255, 150));
300 pa.setPoints(4, zoneEnd - off - 3, 9, zoneEnd - off, 9, zoneEnd - off, 18, zoneEnd - off - 3, 18);
305 if (showPointer() && value >= 0) {
307 pa.setPoints(3, value - 6, 7, value + 6, 7, value, 16);
308 p.setBrush(QBrush(Qt::yellow));
316 #include "customruler.moc"