#include "mainwindow.h"
#include <QColorDialog>
#include <colorselector.h>
#include <iostream>
#include <color_dialog.hpp>

using namespace std;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    control = new ControlSeleccio();
    colordialog = new Color_Dialog();

    initUI();
    initRcInteractiveComponents();

    mask = false;
    onlymask =false;

   // cout<< this->layoutStack2->widget()->size().width()<<" "<<this->layoutStack2->widget()->size().height()<<endl;
  //  cout<<"dwew"<< this->glWidgetArea3->size().width()<<" "<<this->glWidgetArea3->size().height()<<endl;

}

void MainWindow::initRcInteractiveComponents(){

   // slidersLayout2 = new QGridLayout;

    umbralSampling = new QSlider(Qt::Horizontal);
    umbralOpacitat = new QSlider(Qt::Horizontal);
    umbralSaturacio = new QSlider(Qt::Horizontal);
    umbralGammaMIDA = new QSlider(Qt::Horizontal);
    umbralBlending = new QSlider(Qt::Horizontal);

    cremQ = new QSlider(Qt::Horizontal);

    opacitatLabelGPU = new QLabel(QString::number(opacitat*100) + "%");
    saturacioLabelGPU = new QLabel(QString::number(saturacio) + "%");
    samplingLabelGPU = new QLabel(QString::number(sampling*100) + "%");
    cremLabelGPU = new QLabel(QString::number(crem*10) + "%");
    gammaLabel = new QLabel(QString::number(gamma/100));
    Klabel = new QLabel(QString::number(K/100));

    //settejem un rang per tal de restringir els valors possibles
    umbralOpacitat->setRange(0,100);
    umbralSaturacio->setRange(0,100);
    cremQ->setRange(0,60);
    umbralSampling->setRange(0,100);
    umbralGammaMIDA->setRange(-100,100);
    umbralBlending->setRange(0,100);

    //settejem un size per tal que al fer slide el valor que obtinguem sigui més acurat
    umbralOpacitat->setMaximumHeight(20);
    umbralSaturacio->setMaximumHeight(20);
    umbralSampling->setMaximumHeight(20);
    cremQ->setMaximumHeight(20);
    umbralGammaMIDA->setMaximumHeight(20);
    umbralBlending->setMaximumHeight(20);

    //settejem els valors anteriors per tal de tenir un control més acurat
    umbralOpacitat->setValue(opacitat*100);
    umbralSaturacio->setValue(saturacio);
    umbralSampling->setValue(sampling*100);
    cremQ->setValue(crem*10);
    umbralBlending->setValue(K);

    //els fem actius
    umbralOpacitat->setEnabled(true);
    umbralSaturacio->setEnabled(true);
    umbralSampling->setEnabled(true);
    cremQ->setEnabled(true);
    umbralGammaMIDA->setEnabled(true);
    umbralBlending->setEnabled(true);

    slidersLayout = new QGridLayout;
    samp = new QLabel("Valor de mostreig del raig");
    slidersLayout->addWidget(samp,0,0);
    slidersLayout->addWidget(umbralSampling, 1, 0);
    slidersLayout->addWidget(samplingLabelGPU, 2, 0);

    op = new QLabel("Opacitat");
    slidersLayout->addWidget(op,3,0);
    slidersLayout->addWidget(umbralOpacitat, 4, 0);
    slidersLayout->addWidget(opacitatLabelGPU, 5, 0);

    sat = new QLabel("Threshold");
    slidersLayout->addWidget(sat,6,0);
    slidersLayout->addWidget(umbralSaturacio, 7, 0);
    slidersLayout->addWidget(saturacioLabelGPU, 8, 0);

    cre = new QLabel("Contrast"); // Cremacio
    slidersLayout->addWidget(cre,0,1);
    slidersLayout->addWidget(cremQ,1, 1);
    slidersLayout->addWidget(cremLabelGPU,2, 1);

    gama = new QLabel("Gamma (MIDA)");
    slidersLayout->addWidget(gama,3,1);
    slidersLayout->addWidget(umbralGammaMIDA,4, 1);
    slidersLayout->addWidget(gammaLabel,5, 1);

    Klabel = new QLabel("K (blending)");
    slidersLayout->addWidget(label4,8,1);
    slidersLayout->addWidget(umbralBlending,7, 1);
    slidersLayout->addWidget(Klabel,6, 1);

    QVector<QPair<qreal, QColor> > initramp;
    initramp.push_back(QPair<qreal, QColor>(0.0,QColor(255,0,0)));
    initramp.push_back(QPair<qreal, QColor>(255.0,QColor(0,0,255)));

    ramph = new QColorRampEditor(NULL, Qt::Horizontal);

    ramph->setSlideUpdate(true);
    ramph->setMappingTextVisualize(true);
    ramph->setMappingTextColor(Qt::black);
    ramph->setMappingTextAccuracy(1);
    ramph->setMaximumHeight(20);
    ramph->setRamp(initramp);
   // ramph->setFixedHeight(40);


    Color_Dialog *cdialeg = new Color_Dialog();
    ramph->setColorChoose(cdialeg);

    QVector<QRgb> aux = ramph->getColorTable();
    control->escena->setInitialLUT(aux);


    QObject::connect(ramph, SIGNAL(rampChanged()), this, SLOT(onRampChanged()));

    ramph->hide();

    centralLayout->addLayout(slidersLayout, 3,0,1,1,Qt::AlignCenter);

    QObject::connect(umbralOpacitat, SIGNAL(sliderMoved(int)),this, SLOT(setOpacitatGPU(int)));
    QObject::connect(umbralSaturacio, SIGNAL(sliderMoved(int)),this, SLOT(setSaturacioGPU(int)));
    QObject::connect(umbralSampling, SIGNAL(sliderMoved(int)),this, SLOT(setSamplingGPU(int)));
    QObject::connect(cremQ, SIGNAL(sliderMoved(int)),this, SLOT(setCremGPU(int)));
    QObject::connect(umbralGammaMIDA, SIGNAL(sliderMoved(int)),this, SLOT(setGammaMida(int)));
    QObject::connect(umbralBlending,SIGNAL(sliderMoved(int)),this,SLOT(Kblending(int)));

}

void MainWindow::initUI(){

    centralWidget = new QWidget;
    setCentralWidget(centralWidget);

    glWidgetArea2 = new QScrollArea;
    this->layoutStack2 = new QStackedLayout();
    glWidgetArea2->setLayout(layoutStack2);
    glWidgetArea2->setWidgetResizable(true);
    glWidgetArea2->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    glWidgetArea2->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    glWidgetArea2->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    glWidgetArea2->setMinimumSize(50, 50);

    glWidgetArea3 = new QScrollArea;
    this->layoutStack3 = new QStackedLayout();
    glWidgetArea3->setLayout(layoutStack3);
    glWidgetArea3->setWidgetResizable(true);
    glWidgetArea3->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    glWidgetArea3->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    glWidgetArea3->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    glWidgetArea3->setMinimumSize(50, 50);

    createActions();
    createMenus();

    // Layout central
    centralLayout = new QGridLayout;
    Widgets = new QGridLayout;
    Widgets->addWidget(glWidgetArea2, 1, 1);
    Widgets->addWidget(glWidgetArea3, 1, 0);

    label2 = new QLabel("RayCasting CPU Output");
   // Widgets->addWidget(label2, 0,1);

    label3 = new QLabel("RayCasting GPU Output");
   // centralLayout->addWidget(label3, 0,0);

    label4 = new QLabel("0.0");

    setWindowTitle(tr("Medical Data Visualitzation - UB"));
    resize(700, 750);

    centralLayout->addLayout(Widgets,1,0);

    layoutStack2->addWidget(control->texturewidget);
    layoutStack2->setCurrentWidget(control->texturewidget);
    layoutStack3->addWidget(control->escena);
    layoutStack3->setCurrentWidget(control->escena);

    centralWidget->setLayout(centralLayout);



    opacitat = 1.0;
    saturacio = 0;
    sampling = 1.0;
    opacitat = 1.0;
    saturacio = 0;
    opacitat = 1.0;
    saturacio = 0;
    sampling = 1.0;
    crem = 3.0;
    gamma = 0.0;

}

void MainWindow::createActions()
{

    addVolumen = new QAction(tr("&OpenVolumen"), this);
    addVolumen->setShortcut(QKeySequence::Open);
    addVolumen->setStatusTip(tr("Abrir el Volumen"));
    connect(addVolumen, SIGNAL(triggered()), this, SLOT(cargarVolumen()));

    backgroundblack = new QAction(tr("&Background"),this);
    backgroundblack->setStatusTip(tr("Cambiar color background"));
    connect(backgroundblack, SIGNAL(triggered()), this, SLOT(cambiarbackground()));

    menucontrol = new QAction(tr("&Controls"),this);
    menucontrol->setStatusTip(tr(" "));
    connect(menucontrol, SIGNAL(triggered()), this, SLOT(mostrarinterficie()));


    maximizeGPU = new QAction(tr("&Maximize GPU"),this);
    maximizeGPU->setStatusTip(tr("Cambiar el tamany de la finestra"));
    connect(maximizeGPU, SIGNAL(triggered()), this, SLOT(cambiartamanyglwidgetescena()));


    maximizeCPU = new QAction(tr("&Maximize CPU"),this);
    maximizeCPU->setStatusTip(tr("Cambiar el tamany de la finestra"));
    connect(maximizeCPU, SIGNAL(triggered()), this, SLOT(cambiartamanyglwidgettexture()));


    exitAct = new QAction(tr("&Exit"), this);
    exitAct->setShortcut(tr("Ctrl+Q"));
    exitAct->setStatusTip(tr("Sorti del programa"));
    connect(exitAct, SIGNAL(triggered()), qApp, SLOT(closeAllWindows()));

    // accions buto Raycasting GPU

    ArcgGS = new QAction(tr("&Gray Scale"), this);
    ArcgGS->setShortcut(tr("Ctrl+A"));
    connect(ArcgGS, SIGNAL(triggered()), this, SLOT(rcgGS()));

    ArcgLut = new QAction(tr("&Lut"), this);
    ArcgLut->setShortcut(tr("Ctrl+S"));
    connect(ArcgLut, SIGNAL(triggered()), this, SLOT(rcgLut()));

    ArcgPB = new QAction(tr("&Phong Blinn"), this);
    ArcgPB->setShortcut(tr("Ctrl+S"));
    connect(ArcgPB, SIGNAL(triggered()), this, SLOT(rcgPB()));

    ArcgMip = new QAction(tr("&Mip"), this);
    ArcgMip->setShortcut(tr("Ctrl+S"));
    connect(ArcgMip, SIGNAL(triggered()), this, SLOT(rcgMip()));

    ArcgMida = new QAction(tr("&Mida"), this);
    ArcgMida->setShortcut(tr("Ctrl+S"));
    connect(ArcgMida, SIGNAL(triggered()), this, SLOT(rcgMida()));

    ArcgMida2 = new QAction(tr("&Mida 2"), this);
    ArcgMida2->setShortcut(tr("Ctrl+S"));
    connect(ArcgMida2, SIGNAL(triggered()), this, SLOT(rcgMida2()));

    // accions buto Raycasting CPU

    ArccGS = new QAction(tr("&Gray Scale"), this);
    ArccGS->setShortcut(tr("Ctrl+A"));
    connect(ArccGS, SIGNAL(triggered()), this, SLOT(rccGS()));

    ArccLut = new QAction(tr("&Lut"), this);
    ArccLut->setShortcut(tr("Ctrl+S"));
    connect(ArccLut, SIGNAL(triggered()), this, SLOT(rccLut()));

    ArccPB = new QAction(tr("&Phong Blinn"), this);
    ArccPB->setShortcut(tr("Ctrl+S"));
    connect(ArccPB, SIGNAL(triggered()), this, SLOT(rccPB()));

    ArccMip = new QAction(tr("&Mip"), this);
    ArccMip->setShortcut(tr("Ctrl+S"));
    connect(ArccMip, SIGNAL(triggered()), this, SLOT(rccMip()));

    ArccMida = new QAction(tr("&Mida"), this);
    ArccMida->setShortcut(tr("Ctrl+S"));
    connect(ArccMida, SIGNAL(triggered()), this, SLOT(rccMida()));


    MaskAction = new QAction(tr("&Enable/Disable"), this);
    MaskAction->setShortcut(tr("Ctrl+S"));
    connect(MaskAction, SIGNAL(triggered()), this, SLOT(activaMask()));


    OnlyMaskAction = new QAction(tr("&Only Mask"), this);
    OnlyMaskAction->setShortcut(tr("Ctrl+S"));
    connect(OnlyMaskAction, SIGNAL(triggered()), this, SLOT(onlyMask()));

    SelectColorMask = new QAction(tr("&Select Color"), this);
    SelectColorMask->setShortcut(tr("Ctrl+Ñ"));
    connect(SelectColorMask, SIGNAL(triggered()), this, SLOT(selectColorMask()));

    QObject::connect(colordialog,SIGNAL(colorChanged(QColor)),this,SLOT(cambicolor(QColor)));

  /*  QAshowLut = new QAction(tr("&Colors Lut"), this);
    QAshowLut->setShortcut(tr("Ctrl+Ñ"));
    connect(QAshowLut, SIGNAL(triggered()), this, SLOT(showLut()));
*/
}
// Funcions Signals
void MainWindow::setOpacitatGPU(int p){
    opacitatGPU = (float)p;
    opacitat =(float)p;
    opacitat = opacitat/10;
    control->setOpacitatGPU(opacitat,saturacio,sampling,crem);
}

void MainWindow::setSaturacioGPU(int p){
    saturacio = (float)(p);
    saturacio= saturacio/100;
    QString num = QString::number(saturacio, 'g',4);
    this->saturacioLabelGPU->setText(num);
    control->setSaturacioGPU(saturacio);
    //cout<<" saturacio/threshold "<<saturacio<<endl;
} // threshold !!

void MainWindow::setSamplingGPU(int p){control->setSamplingGPU(p);}

void MainWindow::setCremGPU(int p){
    crem = (float)p;
    crem = crem/10;
    //cout <<" cremacio "<<crem<<endl;
    control->setCremGPU(opacitat,saturacio,sampling,crem);
}

void MainWindow::setGammaMida(int p){
    gamma = (float)p;
    gamma = gamma/100.0;
    QString num = QString::number(gamma, 'g', 4);
    this->gammaLabel->setText(num);
    control->escena->setGamma(gamma);

}

void MainWindow::Kblending(int p){
    K = (float)p;
    K = K/100.0;
    QString num = QString::number(K, 'g', 4);
    control->setK(K);
    this->label4->setText(num);

}
void MainWindow::cargarVolumen(){


    //Abro una pantalla de eleccion de fichero y me guardo la ruta y se la envio al volumen
    char *fichero, *hdr;
    tamanyVolum = new vec4(128,128,128,1.0);


    fichero = (char *)malloc(500*sizeof(char));


    QString fi = QFileDialog::getOpenFileName(this);
   if( fi.isEmpty() )
      return;
    strcpy(fichero, fi.toStdString().c_str());

    printf(" EL FIXER CARREGAT EN DIALOG ES %s \n",fichero);




    // ALEX
     // HOME/DONA PRIM
  // std::string fit = "../TFGProject/datasets/spmpet1246094_20110228.img";
  //  std::string hdr2 = "../TFGProject/datasets/spmpet1246094_20110228.hdr";

      // DONA
  // std::string fit  = "../TFGProject/datasets/spmpet178225_20101202.img";
  // std::string hdr2 ="../TFGProject/datasets/spmpet178225_20101202.hdr";


    // HOME MULTITUMOR
 //  std::string fit  = "../TFGProject/datasets/spmpet602503_20120914.img";
 //  std::string hdr2 ="../TFGProject/datasets/spmpet602503_20120914.hdr";

    // HOME
  //  std::string fit  = "../TFGProject/datasets/spmpet983078_20110629.img";
  //  std::string hdr2 ="../TFGProject/datasets/spmpet983078_20110629.hdr";

    // llargarut
   //    std::string fit  = "../TFGProject/datasets/spmpet485184_20130220.img";
  //     std::string hdr2 ="../TFGProject/datasets/spmpet485184_20130220.hdr";

    // GORDO amb intesti
    //   std::string fit  = "../TFGProject/datasets/spmpet1475575_20130606.img";
     //  std::string hdr2 ="../TFGProject/datasets/spmpet1475575_20130606.hdr";

  //  char* img= "";


  /*  fichero = new char[fit.length() + 1];
    strcpy(fichero, fit.c_str());

    hdr = new char[hdr2.length() + 1];
    strcpy(hdr, hdr2.c_str());
*/
   char* img;
  img = (char *)malloc(strlen(fichero)*sizeof(char));
  hdr = (char *)malloc(strlen(fichero)*sizeof(char));

    Arxiu *ar = new Arxiu();
    ar->img2vecstring(fichero, img, hdr);
    ar->getTamanyHDR(hdr, tamanyVolum);

    this->canvioDeAtributos();
    control->escena->setTamanyVolum(tamanyVolum);

   // char* nF;
   // nF = (char *)malloc(200*sizeof(char));

    int anch = 0;
    int alt = 0;
    int profu = 0;
    QString tipoUns = "";

    anch = integerSpinBox1->value();
    alt = integerSpinBox2->value();
    profu = integerSpinBox3->value();
    tipoUns = unsig->currentText();

  //  control->cargarVolumen(img,anch,alt,profu,tipoUns);
 //   control->cargarVolumen(fichero,anch,alt,profu,tipoUns);
    control->cargarVolumen(img,tamanyVolum->x,tamanyVolum->y, tamanyVolum->z,tipoUns);
    control->escena->setTamanyVolum(tamanyVolum);
    control->setColorMascara(QColor(255,0,0,128));

    mask = true;
    window->close();

    this->rcgGS();

}

void MainWindow::canvioDeAtributos(){

    integerSpinBox1 = new QSpinBox();
    integerSpinBox2 = new QSpinBox();
    integerSpinBox3 = new QSpinBox();

    unsig = new QComboBox();

    //Anyado los dos items que quiero
    unsig->addItem("unsigned short");
    unsig->addItem("unsigned char");


    //Creo una ventana nueva con dos botones uno de aplicar y otro de aceptar
    window = new QWidget;
    buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok);

    //Creo un layout para colocar los spinbox
    QFormLayout *layout = new QFormLayout();

    //Cambio el valor por defecto maximo de los spinbox
    integerSpinBox1->setRange(0,1000);
    integerSpinBox2->setRange(0,1000);
    integerSpinBox3->setRange(0,1000);
    /*integerSpinBox1->setValue(144);
    integerSpinBox2->setValue(144);
    integerSpinBox3->setValue(213);*/
    integerSpinBox1->setValue(tamanyVolum->x);
    integerSpinBox2->setValue(tamanyVolum->y);
    integerSpinBox3->setValue(tamanyVolum->z);


    //Pongo en el layout los spinbox con su nombre
    layout->addRow(new QLabel(tr("Anchura:")), integerSpinBox1);
    layout->addRow(new QLabel(tr("Altura:")), integerSpinBox2);
    layout->addRow(new QLabel(tr("Profundidad:")), integerSpinBox3);
    layout->addRow(new QLabel(tr("Unsigned")), unsig);
    //AÃ±ado el boton para aplicar el cambio
    layout->addWidget(buttonBox);



}
void MainWindow::onRampChanged(){
    control->onRampChanged(ramph->getColorTable());
   // showLut();
}

// functions Slot Raycasting GPU

void MainWindow::rcgGS(){control->rcgGS(opacitat,saturacio,sampling,crem);this->ramph->hide();}

void MainWindow::rcgLut(){
    control->rcgLut(opacitat,saturacio,sampling,crem);
    this->ramph->show();
    this->ramph->setEnabled(true);
    }

void MainWindow::rcgPB(){control->rcgPB(opacitat,saturacio,sampling,crem);this->ramph->hide();}

void MainWindow::rcgMip(){control->rcgMip(opacitat,saturacio,sampling,crem);this->ramph->hide();}

void MainWindow::rcgMida(){control->rcgMida(opacitat,saturacio,sampling,crem);this->ramph->hide();}

void MainWindow::rcgMida2(){control->rcgMida2(opacitat,saturacio,sampling,crem);this->ramph->hide();}

// functions Slot Raycasting CPU

void MainWindow::rccGS(){control->rccGS(opacitat,saturacio,sampling,crem);this->ramph->hide();}

void MainWindow::rccLut(){
    control->rccLut(opacitat,saturacio,sampling,crem);
    this->ramph->show();
    this->ramph->setEnabled(true);
}

void MainWindow::rccPB(){control->rccPB(opacitat,saturacio,sampling,crem);this->ramph->hide();}

void MainWindow::rccMip(){control->rccMip(opacitat,saturacio,sampling,crem);this->ramph->hide();}

void MainWindow::rccMida(){control->rccMida(opacitat,saturacio,sampling,crem);this->ramph->hide();}

void MainWindow::cambiarbackground(){
    control->cambibackground();
}

// creador de menus
void MainWindow::createMenus()
{
    // buto menu File
    fileMenu = menuBar()->addMenu(tr("&File"));

    fileMenu->addSeparator();
    fileMenu->addAction(addVolumen);

    fileMenu->addSeparator();
    fileMenu->addAction(backgroundblack);

    fileMenu->addSeparator();
    fileMenu->addAction(menucontrol);

    fileMenu->addSeparator();
    fileMenu->addAction(maximizeGPU);

    fileMenu->addSeparator();
    fileMenu->addAction(maximizeCPU);

    fileMenu->addSeparator();
    fileMenu->addAction(exitAct);

    // buto menu Ray Casting GPU
    rcMenu = menuBar()->addMenu(tr("&RayCasting GPU"));

    rcMenu->addSeparator();
    rcMenu->addAction(ArcgGS);

    rcMenu->addSeparator();
    rcMenu->addAction(ArcgLut);

    rcMenu->addSeparator();
    rcMenu->addAction(ArcgPB);

    rcMenu->addSeparator();
    rcMenu->addAction(ArcgMip);

    rcMenu->addSeparator();
    rcMenu->addAction(ArcgMida);

    rcMenu->addSeparator();
    rcMenu->addAction(ArcgMida2);

    // buto menu RayCasting CPU
    cmMenu = menuBar()->addMenu(tr("&RayCasting CPU"));

    cmMenu->addSeparator();
    cmMenu->addAction(ArccGS);

    cmMenu->addSeparator();
    cmMenu->addAction(ArccLut);

    cmMenu->addSeparator();
    cmMenu->addAction(ArccPB);

    cmMenu->addSeparator();
    cmMenu->addAction(ArccMip);

    cmMenu->addSeparator();
    cmMenu->addAction(ArccMida);

    maskMenu = menuBar()->addMenu(tr("&Mascara"));

    maskMenu->addSeparator();
    maskMenu->addAction(MaskAction);

    maskMenu->addSeparator();
    maskMenu->addAction(OnlyMaskAction);

    maskMenu->addSeparator();
    maskMenu->addAction(SelectColorMask);

   /* maskMenu->addSeparator();
    maskMenu->addAction(QAshowLut);*/

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::activaMask(){
    if (mask == true){
        mask = false;

    }
    else {
        mask = true;

    }
    control->activaMask(mask);
    control->ActualitzarRC(opacitat,saturacio,sampling,crem);

}

void MainWindow::onlyMask(){
    if (onlymask == true){
        onlymask = false;

    }
    else {
        onlymask = true;

    }
    control->onlyMask(onlymask);
    control->ActualitzarRC(opacitat,saturacio,sampling,crem);

}

void MainWindow::selectColorMask(){

       colordialog->setColor(QColor(64,172,143,128));
       colordialog->show();

}

void MainWindow::cambicolor(QColor c){

    //cout << " Color "<<c.red()<<" "<<c.green() <<" "<< c.blue()<<" "<<c.alpha()<<endl;

    control->setColorMascara(c);
}
void MainWindow::showLut(){
    control->escena->showLut();

}

void MainWindow::cambiartamanyglwidgetescena(){


    this->glWidgetArea2->hide();
    this->glWidgetArea3->show();

    this->control->escena->resizeGL(this->size().width(),this->glWidgetArea3->size().height());
    this->control->escena->CalculCamera(false, true);

    this->control->escena->updateGL();
    this->update();

 //   cout << "tamany viewport "<<control->escena->size().width()<<" "<<control->escena->size().height()<<endl;
}

void MainWindow::cambiartamanyglwidgettexture(){

    this->glWidgetArea3->hide();
    this->glWidgetArea2->show();
    this->control->texturewidget->resizeGL(this->size().width(),this->glWidgetArea2->size().height());
    this->control->escena->CalculCamera(false, true);

    this->control->texturewidget->updateGL();
    this->update();
}

void MainWindow::mostrarinterficie(){

    this->glWidgetArea2->show();
    this->glWidgetArea3->show();

    this->control->escena->resizeGL(this->glWidgetArea2->size().width(),this->glWidgetArea2->size().height());
    this->control->escena->CalculCamera(false, true);

    this->control->escena->updateGL();
    this->control->texturewidget->updateGL();
    this->update();
}

void MainWindow::iniescena(){
    //this->cargarVolumen();
   // this->rcgMida();
}

void MainWindow::reescalar(){
    this->control->escena->resizeGL(this->glWidgetArea3->size().width(),this->glWidgetArea3->size().height());
   // this->glWidgetArea3->layout()->update();
    this->control->escena->CalculCamera(false,true);
    this->update();
    this->control->escena->refresh();
    this->control->escena->updateGL();
    this->control->escena->update();
    this->update();
}
void  MainWindow::keyPressEvent(QKeyEvent* e){
    //cout <<" press event "<<endl;

    if ( e->key() == Qt::Key_P){
        control->escena->setYRotation(1);
    }
  /*  if ( e->key() == Qt::Key_W){
        int t = (int)(this->gamma*100)+5;
        this->setGammaMida(t);

    }
    if ( e->key() == Qt::Key_S){
        int t = (int)(this->gamma*100)-5;
        this->setGammaMida(t);


    }*/
    control->escena->updateGL();
    control->escena->repaint();
    this->repaint();
}
