Commit 5a1c79c1 authored by Sylvain Thery's avatar Sylvain Thery

Ajout Utils::QT::inputValues

parent 61426fcd
......@@ -40,11 +40,11 @@
#include "Algo/Geometry/normal.h"
#include "Algo/Modelisation/polyhedron.h"
#include "Algo/Parallel/parallel_foreach.h"
// for file input
#include <QDialog>
#include "Utils/qtInputs.h"
using namespace CGoGN ;
......@@ -398,5 +398,25 @@ int main(int argc, char **argv)
sqt.statusMsg("Neww to create a sphere or Load for a mesh file");
CGoGNStream::allToConsole(&sqt);
int xx = 3;
double yy = 2.5;
bool zz=true;
int kk=32;
int cc=2;
{using namespace CGoGN::Utils::QT;
inputValues( VarInt(0,20,xx, "Entier",
VarBool(zz, "Bool",
VarDbl(0.314,3.14,yy,"Double",
VarSlider(10,100,kk,"Slider",
VarCombo("Riri;Fifi;Loulou;Donald",cc,"Combo") )))));
}
std::cout << "Int:" << xx << " Bool:"<<zz<< std::endl;
std::cout << "Dbl:" << yy << " Slider:"<<kk<< std::endl;
std::cout << "Combo:" << cc << std::endl;
return app.exec();
}
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#ifndef QTINPUTS_H_
#define QTINPUTS_H_
//#include <QApplication>
#include <QDesktopWidget>
//#include <QMainWindow>
#include <QWidget>
#include <QtGui>
#include <QDialog>
#include <QLabel>
namespace CGoGN
{
namespace Utils
{
namespace QT
{
/**
* Abstract class for Variable input in dialog window
*/
class Var
{
protected:
const Var* m_next;
std::string m_label;
public:
Var(const Var& v);
Var();
const Var* next() const;
QLabel* label() const;
virtual QWidget* createInput() const =0;
virtual void updateFrom( QWidget* widg) const =0;
};
/**
* Class for boolean input in dialog window
* Use: VarBool(ref_to_val, "label" [,nextVar])
*/
class VarBool: public Var
{
public:
bool& m_val;
public:
VarBool(bool& val, const std::string& label);
VarBool(bool& val, const std::string& label, const Var& var);
QWidget* createInput() const;
void updateFrom( QWidget* widg) const;
};
/**
* Class for integer input in dialog window (with spinbox)
* Use: VarBool(min,max,ref_to_val, "label" [,nextVar])
*/
class VarInt: public Var
{
public:
int m_min;
int m_max;
int& m_val;
public:
VarInt(int min, int max, int& val, const std::string& label);
VarInt(int min, int max, int& val, const std::string& label, const Var& var);
QWidget* createInput() const;
void updateFrom( QWidget* widg) const;
};
/**
* Class for double input in dialog window (with spinbox)
* Use: VarBool(min,max,ref_to_val, "label" [,nextVar])
*/
class VarDbl: public Var
{
public:
double m_min;
double m_max;
double& m_val;
public:
VarDbl(double min, double max, double& val, const std::string& label);
VarDbl(double min, double max, double& val, const std::string& label, const Var& var);
QWidget* createInput() const;
void updateFrom( QWidget* widg) const;
};
/**
* Class for integer input in dialog window (with slider)
* Use: VarBool(min,max,ref_to_val, "label" [,nextVar])
*/
class VarSlider: public Var
{
public:
int m_min;
int m_max;
int& m_val;
public:
VarSlider(int min, int max, int& val, const std::string& label);
VarSlider(int min, int max, int& val, const std::string& label, const Var& var);
QWidget* createInput() const;
void updateFrom( QWidget* widg) const;
};
/**
* Class for combo input (interger in dialog window (with spinbox)
* Use: VarBool("list_of_item, ref_to_val, "label" [,nextVar])
* item are separated by ; in list_of_item
*/
class VarCombo: public Var
{
public:
const std::string& m_choices;
int& m_val;
public:
VarCombo(const std::string& choices, int& v, const std::string& label);
VarCombo(const std::string& choices, int& v, const std::string& label, const Var& var);
QWidget* createInput() const;
void updateFrom( QWidget* widg) const;
};
/**
* Open a QtDialog for inputs of all chain Var defined
* &param v1 a Var object (that chain other Var objects
* Example:
* {using namespace Utils::QT
* inputValues(VarInt(0,20,x, "Entier",
* VarBool(b, "Bool",
* VarDbl(0.314,3.14,d,"Double",
* VarSlider(10,100,s,"Slider",
* VarCombo("Riri;Fifi;Loulou;Donald",c,"Combo"))))) );
* } // limit scope of using namespace
*/
bool inputValues(const Var& v1, const std::string& title ="input data");
}
} // end namespaces
}
#endif
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#include "Utils/qtInputs.h"
#include <iostream>
namespace CGoGN
{
namespace Utils
{
namespace QT
{
// class CGoGNDialog
// only used in this cpp so ...
class CGoGNDialog: public QDialog
{
protected:
std::vector<QWidget*> m_wid;
public:
CGoGNDialog(std::vector<const Var*>& params, const std::string& title);
void getResults(std::vector<const Var*>& params);
};
CGoGNDialog::CGoGNDialog(std::vector<const Var*>& params, const std::string& title)
{
int nbr=0;
QVBoxLayout *layout = new QVBoxLayout(this);
QGridLayout *m_layout = new QGridLayout();
m_layout->setColumnStretch(0,1);
m_layout->setColumnStretch(1,2);
layout->addLayout(m_layout);
// ADD ALL CREATED WIDGETS
for (std::vector<const Var*>::iterator it = params.begin(); it != params.end(); ++it)
{
m_layout->addWidget((*it)->label(),nbr,0);
m_wid.push_back((*it)->createInput());
m_layout->addWidget(m_wid.back(),nbr++,1);
}
// ADD OK / CANCEL
QHBoxLayout *layButtons = new QHBoxLayout();
QPushButton* okbutton = new QPushButton( "OK", this );
QObject::connect( okbutton, SIGNAL( clicked() ), this, SLOT( accept() ) );
QPushButton* cancelbutton = new QPushButton( "Cancel", this );
QObject::connect( cancelbutton, SIGNAL( clicked() ), this, SLOT( reject() ) );
// TO LAYOUT
layButtons->addWidget(okbutton);
layButtons->addWidget(cancelbutton);
layout->addLayout(layButtons);
setWindowTitle(QString(title.c_str()));
}
void CGoGNDialog::getResults(std::vector<const Var*>& params)
{
unsigned int nb = params.size();
for (unsigned int i=0; i<nb; ++i)
params[i]->updateFrom(m_wid[i]);
}
// class Var
Var::Var(): m_next(NULL)
{}
Var::Var(const Var& v): m_next(&v)
{}
const Var* Var::next() const
{
return m_next;
}
QLabel* Var::label() const
{
return new QLabel(QString(m_label.c_str()));
}
// class VarBool
VarBool::VarBool(bool& val, const std::string& label):
m_val(val)
{
m_label= label;
}
VarBool::VarBool(bool& val, const std::string& label, const Var& var):
Var(var), m_val(val)
{
m_label= label;
}
QWidget* VarBool::createInput() const
{
QCheckBox* check = new QCheckBox();
check->setChecked(m_val);
return check;
}
void VarBool::updateFrom( QWidget* widg) const
{
QCheckBox* check = dynamic_cast<QCheckBox*>(widg);
m_val = (check->checkState() == Qt::Checked);
}
//class VarInt
VarInt::VarInt(int min, int max, int& val, const std::string& label):
m_min(min),m_max(max), m_val(val)
{
m_label= label;
}
VarInt::VarInt(int min, int max, int& val, const std::string& label, const Var& var):
Var(var), m_min(min),m_max(max), m_val(val)
{
m_label= label;
}
QWidget* VarInt::createInput() const
{
QSpinBox *spin = new QSpinBox();
spin->setRange(m_min, m_max);
spin->setValue(m_val);
return spin;
}
void VarInt::updateFrom( QWidget* widg) const
{
QSpinBox* spin = dynamic_cast<QSpinBox*>(widg);
m_val = spin->value();
}
// class VarDbl
VarDbl::VarDbl(double min, double max, double& val, const std::string& label):
m_min(min),m_max(max), m_val(val)
{
m_label= label;
}
VarDbl::VarDbl(double min, double max, double& val, const std::string& label, const Var& var):
Var(var), m_min(min),m_max(max), m_val(val)
{
m_label= label;
}
QWidget* VarDbl::createInput() const
{
QDoubleSpinBox *spin = new QDoubleSpinBox();
spin->setRange(m_min, m_max);
spin->setValue(m_val);
return spin;
}
void VarDbl::updateFrom( QWidget* widg) const
{
QDoubleSpinBox* spin = dynamic_cast<QDoubleSpinBox*>(widg);
m_val = spin->value();
}
// class VarSlider
VarSlider::VarSlider(int min, int max, int& val, const std::string& label):
m_min(min),m_max(max), m_val(val)
{
m_label= label;
}
VarSlider::VarSlider(int min, int max, int& val, const std::string& label, const Var& var):
Var(var),m_min(min),m_max(max), m_val(val)
{
m_label= label;
}
QWidget* VarSlider::createInput() const
{
QSlider *slider = new QSlider(Qt::Horizontal);
slider->setRange(m_min, m_max);
slider->setSliderPosition(m_val);
return slider;
}
void VarSlider::updateFrom( QWidget* widg) const
{
QSlider* slider = dynamic_cast<QSlider*>(widg);
m_val = slider->value();
}
// class VarCombo
VarCombo::VarCombo(const std::string& choices, int& v, const std::string& label):
m_choices(choices),m_val(v)
{
m_label= label;
}
VarCombo::VarCombo(const std::string& choices, int& v, const std::string& label, const Var& var):
Var(var), m_choices(choices),m_val(v)
{
m_label= label;
}
QWidget* VarCombo::createInput() const
{
QComboBox *combo = new QComboBox();
size_t pos = 0;
while (pos != std::string::npos)
{
size_t pos2 = m_choices.find(';',pos);
if (pos2!= std::string::npos)
{
std::string choice = m_choices.substr(pos,pos2-pos);
combo->addItem(QString(choice.c_str()));
pos = pos2+1;
}
else
{
std::string choice = m_choices.substr(pos,pos2);
combo->addItem(QString(choice.c_str()));
pos = pos2;
}
}
combo->setCurrentIndex(m_val);
return combo;
}
void VarCombo::updateFrom( QWidget* widg) const
{
QComboBox* combo = dynamic_cast<QComboBox*>(widg);
m_val = combo->currentIndex();
}
bool inputValues(const Var& v1, const std::string& title)
{
std::vector<const Var*> params;
const Var* ptr = &v1;
while (ptr !=NULL)
{
params.push_back(ptr);
ptr = ptr->next();
}
CGoGNDialog dialog(params,title);
int ret = dialog.exec();
if ( ret != 0)
dialog.getResults(params);
return true;
}
}
} // end namespaces
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment