Mercurial > hg > octave-terminal
changeset 13470:f7356554594c
Plot can be moved around with mouse and zoomed with scrollwheel.
author | Jacob Dawid <jacob.dawid@googlemail.com> |
---|---|
date | Wed, 20 Apr 2011 09:59:04 +0200 |
parents | a20f8763105f |
children | 4baf5e6bba13 |
files | gui/src/Plot2dWidget.cpp gui/src/Plot2dWidget.h |
diffstat | 2 files changed, 112 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/gui/src/Plot2dWidget.cpp +++ b/gui/src/Plot2dWidget.cpp @@ -2,6 +2,8 @@ #include <QVBoxLayout> #include <QHBoxLayout> #include <QPushButton> +#include <QTimer> +#include <math.h> Plot2dView::Plot2dView(QWidget *parent) : QGLWidget(parent) { @@ -9,10 +11,18 @@ } void Plot2dView::construct() { + QTimer *animationTimer = new QTimer(this); + animationTimer->setInterval(20); + animationTimer->start(); + m_zoom = 1.0; + m_scrollX = 0.0; + m_scrollY = 0.0; + m_leftMouseButtonDown = false; + connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate())); } void Plot2dView::initializeGL() { - glClearColor(0.9, 0.9, 0.9, 0.0); + glClearColor(0.0,0.0, 0.0, 0.0); glEnable(GL_POINT_SMOOTH); // glEnable(GL_LINE_SMOOTH); glEnable(GL_POLYGON_SMOOTH); @@ -21,9 +31,14 @@ } void Plot2dView::paintGL() { + glMatrixMode(GL_MODELVIEW_MATRIX); + glLoadIdentity(); + glScaled(m_zoom, m_zoom, 0.0); + glTranslated(-0.5 - m_scrollX, -0.5 - m_scrollY, 0.0); + glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_LINES); - glColor3d(0.0, 0.0, 0.0); + glColor3d(1.0, 1.0, 1.0); glVertex2d(0.1, 0.1); glVertex2d(0.9, 0.1); glVertex2d(0.1, 0.1); @@ -33,12 +48,12 @@ glBegin(GL_POLYGON); glVertex2d(0.092, 0.9); glVertex2d(0.108, 0.9); - glVertex2d(0.1, 0.93); + glVertex2d(0.1, 0.92); glEnd(); glBegin(GL_POLYGON); glVertex2d(0.9, 0.092); glVertex2d(0.9, 0.108); - glVertex2d(0.93, 0.1); + glVertex2d(0.92, 0.1); glEnd(); renderText(0.8, 0.05, 0.0, "axis"); @@ -51,11 +66,53 @@ glMatrixMode(GL_PROJECTION_MATRIX); glLoadIdentity(); - glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 100.0); + glOrtho(-1.0, 1.0, -1.0, 1.0, 0.0, 100.0); +} + +void Plot2dView::wheelEvent(QWheelEvent *wheelEvent) { + m_zoomAcceleration += ((double)wheelEvent->delta()) / 5000; + wheelEvent->accept(); + updateGL(); +} + +void Plot2dView::mousePressEvent(QMouseEvent *mouseEvent) { + if(mouseEvent->button() == Qt::LeftButton) { + m_leftMouseButtonDown = true; + m_lastMouseButtonDownX = mouseEvent->x(); + m_lastMouseButtonDownY = mouseEvent->y(); + mouseEvent->accept(); + } } -Plot2dWidget::Plot2dWidget(QWidget *parent) : - QWidget(parent) { +void Plot2dView::mouseReleaseEvent(QMouseEvent *mouseEvent) { + if(mouseEvent->button() == Qt::LeftButton) { + m_leftMouseButtonDown = false; + mouseEvent->accept(); + } +} + +void Plot2dView::mouseMoveEvent(QMouseEvent *mouseEvent) { + if(m_leftMouseButtonDown) { + m_scrollX -= ((double)mouseEvent->x() - m_lastMouseButtonDownX) / 100; + m_scrollY += ((double)mouseEvent->y() - m_lastMouseButtonDownY) / 100; + m_lastMouseButtonDownX = (double)mouseEvent->x(); + m_lastMouseButtonDownY = (double)mouseEvent->y(); + } + updateGL(); +} + +void Plot2dView::animate() { + m_zoom += m_zoomAcceleration; + if(m_zoom < 0) + m_zoom = 0; + m_zoomAcceleration *= 0.2; + if(abs(m_zoomAcceleration) < 0.01) + m_zoomAcceleration = 0; + updateGL(); +} + +Plot2dWidget::Plot2dWidget(QWidget *parent) + : QWidget(parent) { construct(); } @@ -64,16 +121,29 @@ m_plot2dView = new Plot2dView(this); m_plot2dView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); layout->addWidget(m_plot2dView); - QWidget *buttonBar = new QWidget(this); - QHBoxLayout *buttonBarLayout = new QHBoxLayout(this); - QPushButton *exportButton = new QPushButton(tr("Export"), this); - exportButton->setEnabled(false); - buttonBarLayout->addWidget(exportButton); - buttonBarLayout->addStretch(); - buttonBarLayout->setMargin(1); - buttonBar->setLayout(buttonBarLayout); - buttonBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); - layout->addWidget(buttonBar); + + m_tabWidget = new QTabWidget(this); + m_tabWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); + layout->addWidget(m_tabWidget); + + m_dataSourceTab = new QWidget(this); + m_verticalAxisTab = new QWidget(this); + m_horizontalAxisTab = new QWidget(this); + m_tabWidget->addTab(m_dataSourceTab, tr("Data Source")); + m_tabWidget->addTab(m_verticalAxisTab, tr("Vertical Axis")); + m_tabWidget->addTab(m_horizontalAxisTab, tr("Horizontal Axis")); + + // Build data source tab. + QHBoxLayout *dataSourceTabLayout = new QHBoxLayout(); + + m_dataSourceTypeComboBox = new QComboBox(this); + m_dataSourceTypeComboBox->addItem(tr("Parameterized")); + m_dataSourceTypeComboBox->addItem(tr("Sampled")); + dataSourceTabLayout->addWidget(m_dataSourceTypeComboBox); + dataSourceTabLayout->addStretch(); + m_dataSourceTab->setLayout(dataSourceTabLayout); + layout->setMargin(0); setLayout(layout); + }
--- a/gui/src/Plot2dWidget.h +++ b/gui/src/Plot2dWidget.h @@ -3,8 +3,13 @@ #include <QWidget> #include <QGLWidget> +#include <QTabWidget> +#include <QComboBox> +#include <QWheelEvent> +#include <QMouseEvent> class Plot2dView : public QGLWidget { + Q_OBJECT public: explicit Plot2dView(QWidget *parent = 0); @@ -12,9 +17,23 @@ void initializeGL(); void paintGL(); void resizeGL(int w, int h); + void wheelEvent(QWheelEvent *wheelEvent); + void mousePressEvent(QMouseEvent *mouseEvent); + void mouseReleaseEvent(QMouseEvent *mouseEvent); + void mouseMoveEvent(QMouseEvent *mouseEvent); + +private slots: + void animate(); private: void construct(); + bool m_leftMouseButtonDown; + double m_lastMouseButtonDownX; + double m_lastMouseButtonDownY; + double m_scrollX; + double m_scrollY; + double m_zoom; + double m_zoomAcceleration; }; class Plot2dWidget : public QWidget @@ -31,6 +50,12 @@ void construct(); Plot2dView *m_plot2dView; + QTabWidget *m_tabWidget; + QWidget *m_dataSourceTab; + QWidget *m_verticalAxisTab; + QWidget *m_horizontalAxisTab; + QComboBox *m_dataSourceTypeComboBox; + }; #endif // PLOT2DWIDGET_H