qtinputs.cpp 7.85 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Sylvain Thery's avatar
Sylvain Thery committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
24
#include "Utils/Qt/qtInputs.h"
Sylvain Thery's avatar
Sylvain Thery committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#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();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
84
	for (unsigned int i = 0; i < nb; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
85 86 87
		params[i]->updateFrom(m_wid[i]);
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
88

Sylvain Thery's avatar
Sylvain Thery committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
// 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

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
110 111
VarBool::VarBool(bool& val, const std::string& label) :
	m_val(val)
Sylvain Thery's avatar
Sylvain Thery committed
112
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
113
	m_label = label;
Sylvain Thery's avatar
Sylvain Thery committed
114 115
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
116 117
VarBool::VarBool(bool& val, const std::string& label, const Var& var) :
	Var(var), m_val(val)
Sylvain Thery's avatar
Sylvain Thery committed
118
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
119
	m_label = label;
Sylvain Thery's avatar
Sylvain Thery committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
}

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

thery's avatar
thery committed
138
VarInteger::VarInteger(int min, int max, int& val, const std::string& label):
Sylvain Thery's avatar
Sylvain Thery committed
139 140
		m_min(min),m_max(max), m_val(val)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
141
	m_label = label;
Sylvain Thery's avatar
Sylvain Thery committed
142 143
}

144

thery's avatar
thery committed
145
VarInteger::VarInteger(int min, int max, int& val, const std::string& label, const Var& var):
Sylvain Thery's avatar
Sylvain Thery committed
146 147
		Var(var), m_min(min),m_max(max), m_val(val)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
148
	m_label = label;
Sylvain Thery's avatar
Sylvain Thery committed
149 150
}

thery's avatar
thery committed
151
QWidget* VarInteger::createInput() const
Sylvain Thery's avatar
Sylvain Thery committed
152 153 154 155 156 157 158
{
	QSpinBox *spin = new QSpinBox();
	spin->setRange(m_min, m_max);
	spin->setValue(m_val);
	return spin;
}

thery's avatar
thery committed
159
void VarInteger::updateFrom( QWidget* widg) const
Sylvain Thery's avatar
Sylvain Thery committed
160 161 162 163 164 165 166 167
{
	QSpinBox* spin = dynamic_cast<QSpinBox*>(widg);
	m_val = spin->value();
}


// class VarDbl

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
168 169
VarDbl::VarDbl(double min, double max, double& val, const std::string& label) :
	m_min(min), m_max(max), m_val(val)
Sylvain Thery's avatar
Sylvain Thery committed
170
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
171
	m_label = label;
Sylvain Thery's avatar
Sylvain Thery committed
172 173
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
174 175
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)
Sylvain Thery's avatar
Sylvain Thery committed
176
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
177
	m_label = label;
Sylvain Thery's avatar
Sylvain Thery committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
}

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();
}


195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224

// class VarFloat

VarFloat::VarFloat(float min, float max, float& val, const std::string& label) :
	m_min(min), m_max(max), m_val(val)
{
	m_label = label;
}

VarFloat::VarFloat(float min, float max, float& val, const std::string& label, const Var& var) :
	Var(var), m_min(min), m_max(max), m_val(val)
{
	m_label = label;
}

QWidget* VarFloat::createInput() const
{
	QDoubleSpinBox *spin = new QDoubleSpinBox();
	spin->setRange(double(m_min), double(m_max));
	spin->setValue(double(m_val));
	return spin;
}

void VarFloat::updateFrom( QWidget* widg) const
{
	QDoubleSpinBox* spin = dynamic_cast<QDoubleSpinBox*>(widg);
	m_val = float(spin->value());
}


Sylvain Thery's avatar
Sylvain Thery committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
// 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;
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
247
void VarSlider::updateFrom(QWidget* widg) const
Sylvain Thery's avatar
Sylvain Thery committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
{
	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)
	{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
275
		size_t pos2 = m_choices.find(';', pos);
Sylvain Thery's avatar
Sylvain Thery committed
276 277
		if (pos2!= std::string::npos)
		{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
278
			std::string choice = m_choices.substr(pos, pos2 - pos);
Sylvain Thery's avatar
Sylvain Thery committed
279
			combo->addItem(QString(choice.c_str()));
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
280
			pos = pos2 + 1;
Sylvain Thery's avatar
Sylvain Thery committed
281 282 283
		}
		else
		{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
284
			std::string choice = m_choices.substr(pos, pos2);
Sylvain Thery's avatar
Sylvain Thery committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
			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;
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
306
	while (ptr != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
307 308 309 310 311
	{
		params.push_back(ptr);
		ptr = ptr->next();
	}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
312
	CGoGNDialog dialog(params, title);
Sylvain Thery's avatar
Sylvain Thery committed
313
	int ret = dialog.exec();
314 315
	if (ret == QDialog::Accepted)
	{
Sylvain Thery's avatar
Sylvain Thery committed
316
		dialog.getResults(params);
317 318 319
		return true;
	}
	return false;
Sylvain Thery's avatar
Sylvain Thery committed
320 321
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
322
} // namespace QT
Sylvain Thery's avatar
Sylvain Thery committed
323

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
324
} // namespace Utils
Sylvain Thery's avatar
Sylvain Thery committed
325

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
326
} // namespace CGoGN