Add initial scrolling support with a QAbstractScrollArea
[pulseview.git] / sigview.cpp
index 565e67e2eb8a4d968c5fcf8d0be78727168797a3..4f2436cc5f152b08831841efc9ada481f7da48dd 100644 (file)
  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
+#include <assert.h>
+#include <math.h>
+
+#include <boost/foreach.hpp>
+
+#include <QEvent>
+#include <QScrollBar>
+
 #include "sigview.h"
 
+#include "logicdata.h"
+#include "logicdatasnapshot.h"
 #include "sigsession.h"
-#include "signal.h"
-
-#include <boost/foreach.hpp>
+#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;
 
 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),
+       _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()));
+       setViewport(_viewport);
+}
+
+double SigView::scale() const
+{
+       return _scale;
+}
+
+double SigView::offset() const
+{
+       return _offset;
+}
+
+int SigView::v_offset() const
+{
+       return _v_offset;
+}
+
+void SigView::zoom(double steps)
 {
-       connect(&_session, SIGNAL(dataUpdated()),
-               this, SLOT(dataUpdated()));
+       zoom(steps, (width() - LabelMarginWidth) / 2);
+}
 
-       setMouseTracking(true);
+void SigView::set_scale_offset(double scale, double offset)
+{
+       _scale = scale;
+       _offset = offset;
+       update_scroll();
+       _viewport->update();
 }
 
-void SigView::initializeGL()
+void SigView::update_scroll()
 {
-       glDisable(GL_TEXTURE_2D);
-       glDisable(GL_DEPTH_TEST);
-       glDisable(GL_COLOR_MATERIAL);
-       glEnable(GL_BLEND);
-       glEnable(GL_POLYGON_SMOOTH);
-       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-       glClearColor(1.0, 1.0, 1.0, 0);
+       assert(_viewport);
+
+       const QSize areaSize = _viewport->size();
+
+       // 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;
+       }
+
+       horizontalScrollBar()->setPageStep(areaSize.width());
+       horizontalScrollBar()->setRange(0,
+               max((int)(length - areaSize.width()), 0));
+       horizontalScrollBar()->setSliderPosition(offset);
+
+       // Set the vertical scrollbar
+       verticalScrollBar()->setPageStep(areaSize.height());
+       verticalScrollBar()->setRange(0,
+               _viewport->get_total_height() - areaSize.height());
 }
 
-void SigView::resizeGL(int width, int height)
+void SigView::zoom(double steps, int offset)
 {
-       glViewport(0, 0, (GLint)width, (GLint)height);
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       glOrtho(0, width, height, 0, -1, 1);
-       glMatrixMode(GL_MODELVIEW);
+       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::paintGL()
+bool SigView::viewportEvent(QEvent *e)
 {
-       glClear(GL_COLOR_BUFFER_BIT);
-
-       QRect rect(0, 0, width(), SignalHeight);
-       const vector< shared_ptr<Signal> > &sigs =
-               _session.get_signals();
-       BOOST_FOREACH(const shared_ptr<Signal> s, sigs)
-       {
-               assert(s);
-               s->paint(*this, rect);
-               rect.translate(0, SignalHeight);
+       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::dataUpdated()
+void SigView::resizeEvent(QResizeEvent *e)
 {
-       update();
+       update_scroll();
 }
 
+void SigView::h_scroll_value_changed(int value)
+{
+       _offset = _scale * value;
+       _viewport->update();
+}
+
+void SigView::v_scroll_value_changed(int value)
+{
+       _v_offset = value;
+       _viewport->update();
+}
+
+void SigView::data_updated()
+{
+       // 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();
+}