Mercurial > hg > octave-lyh
comparison gui/src/Plot2dWidget.cpp @ 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 |
comparison
equal
deleted
inserted
replaced
13469:a20f8763105f | 13470:f7356554594c |
---|---|
1 #include "Plot2dWidget.h" | 1 #include "Plot2dWidget.h" |
2 #include <QVBoxLayout> | 2 #include <QVBoxLayout> |
3 #include <QHBoxLayout> | 3 #include <QHBoxLayout> |
4 #include <QPushButton> | 4 #include <QPushButton> |
5 #include <QTimer> | |
6 #include <math.h> | |
5 | 7 |
6 Plot2dView::Plot2dView(QWidget *parent) | 8 Plot2dView::Plot2dView(QWidget *parent) |
7 : QGLWidget(parent) { | 9 : QGLWidget(parent) { |
8 construct(); | 10 construct(); |
9 } | 11 } |
10 | 12 |
11 void Plot2dView::construct() { | 13 void Plot2dView::construct() { |
14 QTimer *animationTimer = new QTimer(this); | |
15 animationTimer->setInterval(20); | |
16 animationTimer->start(); | |
17 m_zoom = 1.0; | |
18 m_scrollX = 0.0; | |
19 m_scrollY = 0.0; | |
20 m_leftMouseButtonDown = false; | |
21 connect(animationTimer, SIGNAL(timeout()), this, SLOT(animate())); | |
12 } | 22 } |
13 | 23 |
14 void Plot2dView::initializeGL() { | 24 void Plot2dView::initializeGL() { |
15 glClearColor(0.9, 0.9, 0.9, 0.0); | 25 glClearColor(0.0,0.0, 0.0, 0.0); |
16 glEnable(GL_POINT_SMOOTH); | 26 glEnable(GL_POINT_SMOOTH); |
17 // glEnable(GL_LINE_SMOOTH); | 27 // glEnable(GL_LINE_SMOOTH); |
18 glEnable(GL_POLYGON_SMOOTH); | 28 glEnable(GL_POLYGON_SMOOTH); |
19 glEnable(GL_BLEND); | 29 glEnable(GL_BLEND); |
20 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); | 30 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); |
21 } | 31 } |
22 | 32 |
23 void Plot2dView::paintGL() { | 33 void Plot2dView::paintGL() { |
34 glMatrixMode(GL_MODELVIEW_MATRIX); | |
35 glLoadIdentity(); | |
36 glScaled(m_zoom, m_zoom, 0.0); | |
37 glTranslated(-0.5 - m_scrollX, -0.5 - m_scrollY, 0.0); | |
38 | |
24 glClear(GL_COLOR_BUFFER_BIT); | 39 glClear(GL_COLOR_BUFFER_BIT); |
25 glBegin(GL_LINES); | 40 glBegin(GL_LINES); |
26 glColor3d(0.0, 0.0, 0.0); | 41 glColor3d(1.0, 1.0, 1.0); |
27 glVertex2d(0.1, 0.1); | 42 glVertex2d(0.1, 0.1); |
28 glVertex2d(0.9, 0.1); | 43 glVertex2d(0.9, 0.1); |
29 glVertex2d(0.1, 0.1); | 44 glVertex2d(0.1, 0.1); |
30 glVertex2d(0.1, 0.9); | 45 glVertex2d(0.1, 0.9); |
31 glEnd(); | 46 glEnd(); |
32 | 47 |
33 glBegin(GL_POLYGON); | 48 glBegin(GL_POLYGON); |
34 glVertex2d(0.092, 0.9); | 49 glVertex2d(0.092, 0.9); |
35 glVertex2d(0.108, 0.9); | 50 glVertex2d(0.108, 0.9); |
36 glVertex2d(0.1, 0.93); | 51 glVertex2d(0.1, 0.92); |
37 glEnd(); | 52 glEnd(); |
38 glBegin(GL_POLYGON); | 53 glBegin(GL_POLYGON); |
39 glVertex2d(0.9, 0.092); | 54 glVertex2d(0.9, 0.092); |
40 glVertex2d(0.9, 0.108); | 55 glVertex2d(0.9, 0.108); |
41 glVertex2d(0.93, 0.1); | 56 glVertex2d(0.92, 0.1); |
42 glEnd(); | 57 glEnd(); |
43 | 58 |
44 renderText(0.8, 0.05, 0.0, "axis"); | 59 renderText(0.8, 0.05, 0.0, "axis"); |
45 } | 60 } |
46 | 61 |
49 glMatrixMode(GL_MODELVIEW_MATRIX); | 64 glMatrixMode(GL_MODELVIEW_MATRIX); |
50 glLoadIdentity(); | 65 glLoadIdentity(); |
51 | 66 |
52 glMatrixMode(GL_PROJECTION_MATRIX); | 67 glMatrixMode(GL_PROJECTION_MATRIX); |
53 glLoadIdentity(); | 68 glLoadIdentity(); |
54 glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, 100.0); | 69 glOrtho(-1.0, 1.0, -1.0, 1.0, 0.0, 100.0); |
55 } | 70 } |
56 | 71 |
57 Plot2dWidget::Plot2dWidget(QWidget *parent) : | 72 void Plot2dView::wheelEvent(QWheelEvent *wheelEvent) { |
58 QWidget(parent) { | 73 m_zoomAcceleration += ((double)wheelEvent->delta()) / 5000; |
74 wheelEvent->accept(); | |
75 updateGL(); | |
76 } | |
77 | |
78 void Plot2dView::mousePressEvent(QMouseEvent *mouseEvent) { | |
79 if(mouseEvent->button() == Qt::LeftButton) { | |
80 m_leftMouseButtonDown = true; | |
81 m_lastMouseButtonDownX = mouseEvent->x(); | |
82 m_lastMouseButtonDownY = mouseEvent->y(); | |
83 mouseEvent->accept(); | |
84 } | |
85 } | |
86 | |
87 void Plot2dView::mouseReleaseEvent(QMouseEvent *mouseEvent) { | |
88 if(mouseEvent->button() == Qt::LeftButton) { | |
89 m_leftMouseButtonDown = false; | |
90 mouseEvent->accept(); | |
91 } | |
92 } | |
93 | |
94 void Plot2dView::mouseMoveEvent(QMouseEvent *mouseEvent) { | |
95 if(m_leftMouseButtonDown) { | |
96 m_scrollX -= ((double)mouseEvent->x() - m_lastMouseButtonDownX) / 100; | |
97 m_scrollY += ((double)mouseEvent->y() - m_lastMouseButtonDownY) / 100; | |
98 m_lastMouseButtonDownX = (double)mouseEvent->x(); | |
99 m_lastMouseButtonDownY = (double)mouseEvent->y(); | |
100 } | |
101 updateGL(); | |
102 } | |
103 | |
104 void Plot2dView::animate() { | |
105 m_zoom += m_zoomAcceleration; | |
106 if(m_zoom < 0) | |
107 m_zoom = 0; | |
108 m_zoomAcceleration *= 0.2; | |
109 if(abs(m_zoomAcceleration) < 0.01) | |
110 m_zoomAcceleration = 0; | |
111 updateGL(); | |
112 } | |
113 | |
114 Plot2dWidget::Plot2dWidget(QWidget *parent) | |
115 : QWidget(parent) { | |
59 construct(); | 116 construct(); |
60 } | 117 } |
61 | 118 |
62 void Plot2dWidget::construct() { | 119 void Plot2dWidget::construct() { |
63 QVBoxLayout *layout = new QVBoxLayout(); | 120 QVBoxLayout *layout = new QVBoxLayout(); |
64 m_plot2dView = new Plot2dView(this); | 121 m_plot2dView = new Plot2dView(this); |
65 m_plot2dView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); | 122 m_plot2dView->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); |
66 layout->addWidget(m_plot2dView); | 123 layout->addWidget(m_plot2dView); |
67 QWidget *buttonBar = new QWidget(this); | 124 |
68 QHBoxLayout *buttonBarLayout = new QHBoxLayout(this); | 125 m_tabWidget = new QTabWidget(this); |
69 QPushButton *exportButton = new QPushButton(tr("Export"), this); | 126 m_tabWidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); |
70 exportButton->setEnabled(false); | 127 layout->addWidget(m_tabWidget); |
71 buttonBarLayout->addWidget(exportButton); | 128 |
72 buttonBarLayout->addStretch(); | 129 m_dataSourceTab = new QWidget(this); |
73 buttonBarLayout->setMargin(1); | 130 m_verticalAxisTab = new QWidget(this); |
74 buttonBar->setLayout(buttonBarLayout); | 131 m_horizontalAxisTab = new QWidget(this); |
75 buttonBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum); | 132 m_tabWidget->addTab(m_dataSourceTab, tr("Data Source")); |
76 layout->addWidget(buttonBar); | 133 m_tabWidget->addTab(m_verticalAxisTab, tr("Vertical Axis")); |
134 m_tabWidget->addTab(m_horizontalAxisTab, tr("Horizontal Axis")); | |
135 | |
136 // Build data source tab. | |
137 QHBoxLayout *dataSourceTabLayout = new QHBoxLayout(); | |
138 | |
139 m_dataSourceTypeComboBox = new QComboBox(this); | |
140 m_dataSourceTypeComboBox->addItem(tr("Parameterized")); | |
141 m_dataSourceTypeComboBox->addItem(tr("Sampled")); | |
142 dataSourceTabLayout->addWidget(m_dataSourceTypeComboBox); | |
143 dataSourceTabLayout->addStretch(); | |
144 m_dataSourceTab->setLayout(dataSourceTabLayout); | |
145 | |
77 layout->setMargin(0); | 146 layout->setMargin(0); |
78 setLayout(layout); | 147 setLayout(layout); |
148 | |
79 } | 149 } |