const int SigView::LabelMarginWidth = 70;
const int SigView::RulerHeight = 30;
+const int SigView::MinorTickSubdivision = 4;
const int SigView::ScaleUnits[3] = {1, 2, 5};
const QString SigView::SIPrefixes[9] =
_scale(1e-6),
_offset(0)
{
- connect(&_session, SIGNAL(dataUpdated()),
- this, SLOT(dataUpdated()));
+ connect(&_session, SIGNAL(data_updated()),
+ this, SLOT(data_updated()));
setMouseTracking(true);
setAutoFillBackground(false);
void SigView::resizeGL(int width, int height)
{
- setupViewport(width, height);
+ setup_viewport(width, height);
}
void SigView::paintEvent(QPaintEvent *event)
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
- setupViewport(width(), height());
+ setup_viewport(width(), height());
qglClearColor(Qt::white);
glClear(GL_COLOR_BUFFER_BIT);
}
// Paint the ruler
- paintRuler(painter);
+ paint_ruler(painter);
painter.end();
}
-void SigView::dataUpdated()
+void SigView::data_updated()
{
update();
}
-void SigView::mouseMoveEvent(QMouseEvent *event)
+void SigView::mousePressEvent(QMouseEvent *event)
{
assert(event);
+
+ _mouse_down_point = event->pos();
+ _mouse_down_offset = _offset;
}
-void SigView::mousePressEvent(QMouseEvent *event)
+void SigView::mouseMoveEvent(QMouseEvent *event)
{
assert(event);
+
+ if(event->buttons() & Qt::LeftButton)
+ {
+ _offset = _mouse_down_offset + (_mouse_down_point - event->pos()).x() * _scale;
+ update();
+ }
}
void SigView::mouseReleaseEvent(QMouseEvent *event)
{
assert(event);
+}
- const double cursor_offset = _offset + _scale * (double)event->x();
-
- switch(event->button())
- {
- case Qt::LeftButton:
- _scale *= 2.0 / 3.0;
- break;
-
- case Qt::RightButton:
- _scale *= 3.0 / 2.0;
- break;
- }
-
- _offset = cursor_offset - _scale * (double)event->x();
+void SigView::wheelEvent(QWheelEvent *event)
+{
+ assert(event);
+ const double x = event->x() - LabelMarginWidth;
+ const double cursor_offset = _offset + _scale * x;
+ _scale *= powf(3.0/2.0, -event->delta() / 120);
+ _offset = cursor_offset - _scale * x;
update();
}
-void SigView::setupViewport(int width, int height)
+void SigView::setup_viewport(int width, int height)
{
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
}
-void SigView::paintRuler(QPainter &p)
+void SigView::paint_ruler(QPainter &p)
{
const double MinSpacing = 80;
// Draw the tick marks
p.setPen(Qt::black);
- double t = ceil(_offset / tick_period) * tick_period;
- double x = 0.0;
- while((x = (t - _offset) / _scale + LabelMarginWidth) < width())
+ const double minor_tick_period = tick_period / MinorTickSubdivision;
+ const double first_major_division = floor(_offset / tick_period);
+ const double first_minor_division = ceil(_offset / minor_tick_period);
+ const double t0 = first_major_division * tick_period;
+
+ int division = (int)round(first_minor_division -
+ first_major_division * MinorTickSubdivision);
+ while(1)
{
- QString s;
- QTextStream ts(&s);
- ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
- p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
- Qt::TextDontClip, s);
- p.drawLine(x, text_height, x, RulerHeight);
- t += tick_period;
+ const double t = t0 + division * minor_tick_period;
+ const double x = (t - _offset) / _scale + LabelMarginWidth;
+
+ if(x >= width())
+ break;
+
+ if(division % MinorTickSubdivision == 0)
+ {
+ // Draw a major tick
+ QString s;
+ QTextStream ts(&s);
+ ts << (t / order_decimal) << SIPrefixes[prefix] << "s";
+ p.drawText(x, 0, 0, text_height, Qt::AlignCenter | Qt::AlignTop |
+ Qt::TextDontClip, s);
+ p.drawLine(x, text_height, x, RulerHeight);
+ }
+ else
+ {
+ // Draw a minor tick
+ p.drawLine(x, (text_height + RulerHeight) / 2, x, RulerHeight);
+ }
+
+ division++;
}
}