* 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::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(&_session, SIGNAL(dataUpdated()),
- this, SLOT(dataUpdated()));
-
- setMouseTracking(true);
- setAutoFillBackground(false);
+ 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);
}
-void SigView::initializeGL()
+double SigView::scale() const
{
+ return _scale;
}
-void SigView::resizeGL(int width, int height)
+double SigView::offset() const
{
- setupViewport(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();
-
- setupViewport(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
- paintRuler(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::dataUpdated()
+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::mouseMoveEvent(QMouseEvent *event)
+bool SigView::viewportEvent(QEvent *e)
{
- assert(event);
+ 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::mousePressEvent(QMouseEvent *event)
+void SigView::resizeEvent(QResizeEvent *e)
{
- assert(event);
+ update_scroll();
}
-void SigView::mouseReleaseEvent(QMouseEvent *event)
+void SigView::h_scroll_value_changed(int value)
{
- 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();
-
- update();
+ _offset = _scale * value;
+ _viewport->update();
}
-void SigView::setupViewport(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::paintRuler(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);
-
- double t = ceil(_offset / tick_period) * tick_period;
- double x = 0.0;
- while((x = (t - _offset) / _scale + LabelMarginWidth) < width())
- {
- 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;
+ // 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();
}