File indexing completed on 2024-04-21 03:40:42

0001 /*************************************************************************************
0002  *  Copyright (C) 2010-2012 by Percy Camilo T. Aucahuasi <percy.camilo.ta@gmail.com> *
0003  *  Copyright (C) 2007 by Abderrahman Taha: Basic OpenGL calls like scene, lights    *
0004  *                                          and mouse behaviour taken from K3DSurf   *
0005  *                                                                                   *
0006  *  This program is free software; you can redistribute it and/or                    *
0007  *  modify it under the terms of the GNU General Public License                      *
0008  *  as published by the Free Software Foundation; either version 2                   *
0009  *  of the License, or (at your option) any later version.                           *
0010  *                                                                                   *
0011  *  This program is distributed in the hope that it will be useful,                  *
0012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0014  *  GNU General Public License for more details.                                     *
0015  *                                                                                   *
0016  *  You should have received a copy of the GNU General Public License                *
0017  *  along with this program; if not, write to the Free Software                      *
0018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0019  *************************************************************************************/
0020 
0021 #include "plotsview3d_es.h"
0022 #include <analitzaplot/plotsmodel.h>
0023 #include <analitzaplot/surface.h>
0024 #include <analitzaplot/spacecurve.h>
0025 #include <QVector3D>
0026 #include <QItemSelectionModel>
0027 #include <QVector>
0028 #include <QDebug>
0029 
0030 using namespace Analitza;
0031 
0032 PlotsView3DES::PlotsView3DES(QWidget *parent)
0033     : QOpenGLWidget(parent), m_selection(nullptr), old_x(-1), old_y(-1)
0034 {
0035     setFocusPolicy(Qt::ClickFocus);
0036 }
0037 
0038 PlotsView3DES::~PlotsView3DES()
0039 {}
0040 
0041 void PlotsView3DES::setSelectionModel(QItemSelectionModel* selection)
0042 {
0043     Q_ASSERT(selection);
0044 
0045     m_selection = selection;
0046 //     connect(m_selection,SIGNAL(currentChanged(QModelIndex,QModelIndex)), SLOT(forceRepaint()));
0047 }
0048 
0049 void PlotsView3DES::addFuncs(const QModelIndex & parent, int start, int end)
0050 {
0051     updatePlots(parent, start, end);
0052 }
0053 
0054 void PlotsView3DES::removeFuncs(const QModelIndex & parent, int start, int end)
0055 {
0056     updatePlots(parent, start, end);
0057 }
0058 
0059 //NOTE
0060 //si hay un cambio aki es desetvisible (que no es necesario configurar en el plotitem) o es del
0061 //setinterval (que si es necesario configurarlo en el plotitem)
0062 //el enfoque es: si hay un cambio borro el displaylist y lo genero de nuevo (no lo genero si el item no es visible)
0063 //TODO cache para exp e interval ... pues del resto es solo cuestion de update
0064 void PlotsView3DES::updateFuncs(const QModelIndex& start, const QModelIndex& end)
0065 {
0066     updatePlots(QModelIndex(), start.row(), end.row());
0067 }
0068 
0069 void PlotsView3DES::paintGL()
0070 {
0071     drawPlots();
0072 }
0073 
0074 void PlotsView3DES::initializeGL()
0075 {
0076     initGL();
0077 }
0078 
0079 void PlotsView3DES::resizeGL(int newwidth, int newheight)
0080 {
0081     setViewport(QRectF(0,0,newwidth,newheight));
0082 }
0083 
0084 void PlotsView3DES::mousePressEvent(QMouseEvent *e)
0085 {
0086     buttons = e->buttons();
0087 
0088     old_y = e->y();
0089     old_x = e->x();
0090     CartesianAxis axis = selectAxisArrow(e->x(), e->y());
0091     showAxisArrowHint(axis);
0092 
0093     if (isRotationFixed() && axis != InvalidAxis) {
0094         fixRotation(QVector3D());
0095         hideAxisHint();
0096     } else switch (axis)
0097     {
0098         case XAxis: 
0099             fixRotation(QVector3D(1,0,0));
0100             break;
0101         case YAxis: 
0102             fixRotation(QVector3D(0,1,0));
0103             break;
0104         case ZAxis: 
0105             fixRotation(QVector3D(0,0,1));
0106             break;
0107         case InvalidAxis:
0108             break;
0109     }
0110 
0111 }
0112 
0113 void PlotsView3DES::modelChanged()
0114 {
0115 //     if (model()) {
0116 //         disconnect(model(), &QAbstractItemModel::dataChanged, this, &PlotsView3DES::updateFuncs);
0117 //         disconnect(model(), &QAbstractItemModel::rowsInserted, this, &PlotsView3DES::addFuncs);
0118 //         disconnect(model(), &QAbstractItemModel::rowsRemoved, this, &PlotsView3DES::removeFuncs);
0119 //     }
0120 
0121     addFuncs({}, 0, model()->rowCount());
0122 
0123     connect(model(), &QAbstractItemModel::dataChanged, this, &PlotsView3DES::updateFuncs);
0124     connect(model(), &QAbstractItemModel::rowsInserted, this, &PlotsView3DES::addFuncs);
0125     connect(model(), &QAbstractItemModel::rowsRemoved, this, &PlotsView3DES::removeFuncs);
0126 
0127     update();
0128 }
0129 
0130 void PlotsView3DES::renderGL()
0131 {
0132     update();
0133 }
0134 
0135 void PlotsView3DES::wheelEvent(QWheelEvent* ev)
0136 {
0137     scale(1.f-ev->angleDelta().y()/1000.f);
0138 }
0139 
0140 void PlotsView3DES::mouseMoveEvent(QMouseEvent *e)
0141 {
0142 //TODO translate with middle btn
0143 // Rotational function :
0144     if(buttons & Qt::LeftButton)
0145         rotate(old_x - e->x(), old_y - e->y());
0146 
0147     old_y = e->y();
0148     old_x = e->x();
0149 }
0150 
0151 void PlotsView3DES::keyPressEvent(QKeyEvent* ev)
0152 {
0153     switch(ev->key()) {
0154         case Qt::Key_S:
0155             scale(1.1);
0156             break;
0157         case Qt::Key_W:
0158             scale(0.9);
0159             break;
0160         case Qt::Key_Left:
0161             rotate(-10, 0);
0162             break;
0163         case Qt::Key_Right:
0164             rotate(10, 0);
0165             break;
0166         case Qt::Key_Up:
0167             rotate(0, -10);
0168             break;
0169         case Qt::Key_Down:
0170             rotate(0, 10);
0171             break;
0172     }
0173 }
0174 
0175 QImage PlotsView3DES::grabImage()
0176 {
0177     return grabFramebuffer();
0178 }
0179 
0180 #include "moc_plotsview3d_es.cpp"