1 /*************************************************************************************
2  *  Copyright (C) 2010-2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> *
3  *  Copyright (C) 2007 by Abderrahman Taha: Basic OpenGL calls like scene, lights    *
4  *                                          and mouse behaviour taken from K3DSurf   *
5  *                                                                                   *
6  *  This program is free software; you can redistribute it and/or                    *
7  *  modify it under the terms of the GNU General Public License                      *
8  *  as published by the Free Software Foundation; either version 2                   *
9  *  of the License, or (at your option) any later version.                           *
10  *                                                                                   *
11  *  This program is distributed in the hope that it will be useful,                  *
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
14  *  GNU General Public License for more details.                                     *
15  *                                                                                   *
16  *  You should have received a copy of the GNU General Public License                *
17  *  along with this program; if not, write to the Free Software                      *
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
19  *************************************************************************************/
20 
21 #include "plotsview3d_es.h"
22 #include <analitzaplot/plotsmodel.h>
23 #include <analitzaplot/surface.h>
24 #include <analitzaplot/spacecurve.h>
25 #include <QVector3D>
26 #include <QItemSelectionModel>
27 #include <QVector>
28 #include <QDebug>
29 
30 using namespace Analitza;
31 
PlotsView3DES(QWidget * parent)32 PlotsView3DES::PlotsView3DES(QWidget *parent)
33     : QOpenGLWidget(parent), m_selection(nullptr), old_x(-1), old_y(-1)
34 {
35     setFocusPolicy(Qt::ClickFocus);
36 }
37 
~PlotsView3DES()38 PlotsView3DES::~PlotsView3DES()
39 {}
40 
setSelectionModel(QItemSelectionModel * selection)41 void PlotsView3DES::setSelectionModel(QItemSelectionModel* selection)
42 {
43     Q_ASSERT(selection);
44 
45     m_selection = selection;
46 //     connect(m_selection,SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(forceRepaint()));
47 }
48 
addFuncs(const QModelIndex & parent,int start,int end)49 void PlotsView3DES::addFuncs(const QModelIndex & parent, int start, int end)
50 {
51     updatePlots(parent, start, end);
52 }
53 
removeFuncs(const QModelIndex & parent,int start,int end)54 void PlotsView3DES::removeFuncs(const QModelIndex & parent, int start, int end)
55 {
56     updatePlots(parent, start, end);
57 }
58 
59 //NOTE
60 //si hay un cambio aki es desetvisible (que no es necesario configurar en el plotitem) o es del
61 //setinterval (que si es necesario configurarlo en el plotitem)
62 //el enfoque es: si hay un cambio borro el displaylist y lo genero de nuevo (no lo genero si el item no es visible)
63 //TODO cache para exp e interval ... pues del resto es solo cuestion de update
updateFuncs(const QModelIndex & start,const QModelIndex & end)64 void PlotsView3DES::updateFuncs(const QModelIndex& start, const QModelIndex& end)
65 {
66     updatePlots(QModelIndex(), start.row(), end.row());
67 }
68 
paintGL()69 void PlotsView3DES::paintGL()
70 {
71     drawPlots();
72 }
73 
initializeGL()74 void PlotsView3DES::initializeGL()
75 {
76     initGL();
77 }
78 
resizeGL(int newwidth,int newheight)79 void PlotsView3DES::resizeGL(int newwidth, int newheight)
80 {
81     setViewport(QRectF(0,0,newwidth,newheight));
82 }
83 
mousePressEvent(QMouseEvent * e)84 void PlotsView3DES::mousePressEvent(QMouseEvent *e)
85 {
86     buttons = e->buttons();
87 
88     old_y = e->y();
89     old_x = e->x();
90     CartesianAxis axis = selectAxisArrow(e->x(), e->y());
91     showAxisArrowHint(axis);
92 
93     if (isRotationFixed() && axis != InvalidAxis) {
94         fixRotation(QVector3D());
95         hideAxisHint();
96     } else switch (axis)
97     {
98         case XAxis:
99             fixRotation(QVector3D(1,0,0));
100             break;
101         case YAxis:
102             fixRotation(QVector3D(0,1,0));
103             break;
104         case ZAxis:
105             fixRotation(QVector3D(0,0,1));
106             break;
107         case InvalidAxis:
108             break;
109     }
110 
111 }
112 
modelChanged()113 void PlotsView3DES::modelChanged()
114 {
115 //     if (model()) {
116 //         disconnect(model(), &QAbstractItemModel::dataChanged, this, &PlotsView3DES::updateFuncs);
117 //         disconnect(model(), &QAbstractItemModel::rowsInserted, this, &PlotsView3DES::addFuncs);
118 //         disconnect(model(), &QAbstractItemModel::rowsRemoved, this, &PlotsView3DES::removeFuncs);
119 //     }
120 
121     addFuncs({}, 0, model()->rowCount());
122 
123     connect(model(), &QAbstractItemModel::dataChanged, this, &PlotsView3DES::updateFuncs);
124     connect(model(), &QAbstractItemModel::rowsInserted, this, &PlotsView3DES::addFuncs);
125     connect(model(), &QAbstractItemModel::rowsRemoved, this, &PlotsView3DES::removeFuncs);
126 
127     update();
128 }
129 
renderGL()130 void PlotsView3DES::renderGL()
131 {
132     update();
133 }
134 
wheelEvent(QWheelEvent * ev)135 void PlotsView3DES::wheelEvent(QWheelEvent* ev)
136 {
137     scale(1.f-ev->angleDelta().y()/1000.f);
138 }
139 
mouseMoveEvent(QMouseEvent * e)140 void PlotsView3DES::mouseMoveEvent(QMouseEvent *e)
141 {
142 //TODO translate with middle btn
143 // Rotational function :
144     if(buttons & Qt::LeftButton)
145         rotate(old_x - e->x(), old_y - e->y());
146 
147     old_y = e->y();
148     old_x = e->x();
149 }
150 
keyPressEvent(QKeyEvent * ev)151 void PlotsView3DES::keyPressEvent(QKeyEvent* ev)
152 {
153     switch(ev->key()) {
154         case Qt::Key_S:
155             scale(1.1);
156             break;
157         case Qt::Key_W:
158             scale(0.9);
159             break;
160         case Qt::Key_Left:
161             rotate(-10, 0);
162             break;
163         case Qt::Key_Right:
164             rotate(10, 0);
165             break;
166         case Qt::Key_Up:
167             rotate(0, -10);
168             break;
169         case Qt::Key_Down:
170             rotate(0, 10);
171             break;
172     }
173 }
174 
grabImage()175 QImage PlotsView3DES::grabImage()
176 {
177     return grabFramebuffer();
178 }
179