Add initial scrolling support with a QAbstractScrollArea
[pulseview.git] / sigview.cpp
index 8461af6e455f37b1182ef9b9bc1a8197ada54d09..4f2436cc5f152b08831841efc9ada481f7da48dd 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
-#include "sigview.h"
-
-#include "sigsession.h"
-#include "signal.h"
+#include <assert.h>
+#include <math.h>
 
-#include "extdef.h"
+#include <boost/foreach.hpp>
 
-#include <QMouseEvent>
-#include <QTextStream>
+#include <QEvent>
+#include <QScrollBar>
 
-#include <math.h>
+#include "sigview.h"
 
-#include <boost/foreach.hpp>
+#include "logicdata.h"
+#include "logicdatasnapshot.h"
+#include "sigsession.h"
+#include "sigviewport.h"
 
 using namespace boost;
 using namespace std;
 
-const int SigView::SignalHeight = 50;
+const double SigView::MaxScale = 1e9;
+const double SigView::MinScale = 1e-15;
+
 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] =
-       {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
-const int SigView::FirstSIPrefixPower = -15;
-
 SigView::SigView(SigSession &session, QWidget *parent) :
-       QGLWidget(parent),
-        _session(session),
+       QAbstractScrollArea(parent),
+       _session(session),
+       _viewport(new SigViewport(*this)),
+       _data_length(0),
        _scale(1e-6),
-       _offset(0)
+       _offset(0),
+       _v_offset(0)
 {
+       connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
+               this, SLOT(h_scroll_value_changed(int)));
+       connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
+               this, SLOT(v_scroll_value_changed(int)));
        connect(&_session, SIGNAL(data_updated()),
                this, SLOT(data_updated()));
-
-       setMouseTracking(true);
-       setAutoFillBackground(false);
+       setViewport(_viewport);
 }
 
-void SigView::initializeGL()
+double SigView::scale() const
 {
+       return _scale;
 }
 
-void SigView::resizeGL(int width, int height)
+double SigView::offset() const
 {
-       setup_viewport(width, height);
+       return _offset;
 }
 
-void SigView::paintEvent(QPaintEvent *event)
+int SigView::v_offset() const
 {
-       int offset;
-
-       const vector< shared_ptr<Signal> > &sigs =
-               _session.get_signals();
-
-       // Prepare for OpenGL rendering
-       makeCurrent();
-       glMatrixMode(GL_MODELVIEW);
-       glPushMatrix();
-
-       setup_viewport(width(), height());
-
-       qglClearColor(Qt::white);
-       glClear(GL_COLOR_BUFFER_BIT);
-
-       // Plot the signal
-       offset = RulerHeight;
-       BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
-       {
-               assert(s);
-
-               const QRect signal_rect(LabelMarginWidth, offset,
-                       width() - LabelMarginWidth, SignalHeight);
-
-               s->paint(*this, signal_rect, _scale, _offset);
-
-               offset += SignalHeight;
-       }
+       return _v_offset;
+}
 
-       // Prepare for QPainter rendering
-       glMatrixMode(GL_MODELVIEW);
-       glPopMatrix();
+void SigView::zoom(double steps)
+{
+       zoom(steps, (width() - LabelMarginWidth) / 2);
+}
 
-       QPainter painter(this);
-       painter.setRenderHint(QPainter::Antialiasing);
+void SigView::set_scale_offset(double scale, double offset)
+{
+       _scale = scale;
+       _offset = offset;
+       update_scroll();
+       _viewport->update();
+}
 
-       // Paint the labels
-       offset = RulerHeight;
-       BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
-       {
-               assert(s);
+void SigView::update_scroll()
+{
+       assert(_viewport);
 
-               const QRect label_rect(0, offset,
-                       LabelMarginWidth, SignalHeight);
-               s->paint_label(painter, label_rect);
+       const QSize areaSize = _viewport->size();
 
-               offset += SignalHeight;
+       // Set the horizontal scroll bar
+       double length = 0, offset = 0;
+       const shared_ptr<SignalData> sig_data = _session.get_data();
+       if(sig_data) {
+               length = _data_length /
+                       (sig_data->get_samplerate() * _scale);
+               offset = _offset / _scale;
        }
 
-       // Paint the ruler
-       paint_ruler(painter);
+       horizontalScrollBar()->setPageStep(areaSize.width());
+       horizontalScrollBar()->setRange(0,
+               max((int)(length - areaSize.width()), 0));
+       horizontalScrollBar()->setSliderPosition(offset);
 
-       painter.end();
+       // Set the vertical scrollbar
+       verticalScrollBar()->setPageStep(areaSize.height());
+       verticalScrollBar()->setRange(0,
+               _viewport->get_total_height() - areaSize.height());
 }
 
-void SigView::data_updated()
+void SigView::zoom(double steps, int offset)
 {
-       update();
+       const double cursor_offset = _offset + _scale * offset;
+       _scale *= pow(3.0/2.0, -steps);
+       _scale = max(min(_scale, MaxScale), MinScale);
+       _offset = cursor_offset - _scale * offset;
+       _viewport->update();
+       update_scroll();
 }
 
-void SigView::mousePressEvent(QMouseEvent *event)
+bool SigView::viewportEvent(QEvent *e)
 {
-       assert(event);
-
-       _mouse_down_point = event->pos();
-       _mouse_down_offset = _offset;
-}
-
-void SigView::mouseMoveEvent(QMouseEvent *event)
-{
-       assert(event);
-
-       if(event->buttons() & Qt::LeftButton)
-       {
-               _offset = _mouse_down_offset + (_mouse_down_point - event->pos()).x() * _scale;
-               update();
+       switch(e->type()) {
+       case QEvent::Paint:
+       case QEvent::MouseButtonPress:
+       case QEvent::MouseButtonRelease:
+       case QEvent::MouseButtonDblClick:
+       case QEvent::MouseMove:
+       case QEvent::Wheel:
+               return false;
+
+       default:
+               return QAbstractScrollArea::viewportEvent(e);
        }
 }
 
-void SigView::mouseReleaseEvent(QMouseEvent *event)
+void SigView::resizeEvent(QResizeEvent *e)
 {
-       assert(event);
+       update_scroll();
 }
 
-void SigView::wheelEvent(QWheelEvent *event)
+void SigView::h_scroll_value_changed(int value)
 {
-       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();
+       _offset = _scale * value;
+       _viewport->update();
 }
 
-void SigView::setup_viewport(int width, int height)
+void SigView::v_scroll_value_changed(int value)
 {
-       glViewport(0, 0, (GLint)width, (GLint)height);
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       glOrtho(0, width, height, 0, -1, 1);
-       glMatrixMode(GL_MODELVIEW);
+       _v_offset = value;
+       _viewport->update();
 }
 
-void SigView::paint_ruler(QPainter &p)
+void SigView::data_updated()
 {
-       const double MinSpacing = 80;
-
-       const double min_period = _scale * MinSpacing;
-
-       const int order = (int)floorf(log10f(min_period));
-       const double order_decimal = pow(10, order);
-
-       int unit = 0;
-       double tick_period = 0.0f;
-
-       do
-       {
-               tick_period = order_decimal * ScaleUnits[unit++];
-       } while(tick_period < min_period && unit < countof(ScaleUnits));
-
-       const int prefix = (order - FirstSIPrefixPower) / 3;
-       assert(prefix >= 0);
-       assert(prefix < countof(SIPrefixes));
-
-       const int text_height = p.boundingRect(0, 0, INT_MAX, INT_MAX,
-               Qt::AlignLeft | Qt::AlignTop, "8").height();
-
-       // Draw the tick marks
-       p.setPen(Qt::black);
-
-       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)
-       {
-               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++;
+       // Get the new data length
+       _data_length = 0;
+       shared_ptr<LogicData> sig_data = _session.get_data();
+       if(sig_data) {
+               deque< shared_ptr<LogicDataSnapshot> > &snapshots =
+                       sig_data->get_snapshots();
+               BOOST_FOREACH(shared_ptr<LogicDataSnapshot> s, snapshots)
+                       if(s)
+                               _data_length = max(_data_length,
+                                       s->get_sample_count());
        }
+
+       // Update the scroll bars
+       update_scroll();
+
+       // Repaint the view
+       _viewport->update();
 }