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-2012, 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.unistra.fr/                                           *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/
24

25
#include "Utils/Qt/qtInputs.h"
Sylvain Thery's avatar
Sylvain Thery committed
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 84
#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
85
	for (unsigned int i = 0; i < nb; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
86 87 88
		params[i]->updateFrom(m_wid[i]);
}

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

Sylvain Thery's avatar
Sylvain Thery committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
// 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
111 112
VarBool::VarBool(bool& val, const std::string& label) :
	m_val(val)
Sylvain Thery's avatar
Sylvain Thery committed
113
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
114
	m_label = label;
Sylvain Thery's avatar
Sylvain Thery committed
115 116
}

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

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
139
VarInteger::VarInteger(int min, int max, int& val, const std::string& label):
Sylvain Thery's avatar
Sylvain Thery committed
140 141
		m_min(min),m_max(max), m_val(val)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
142
	m_label = label;
Sylvain Thery's avatar
Sylvain Thery committed
143 144
}

145

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

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

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


// class VarDbl

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
169 170
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
171
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
172
	m_label = label;
Sylvain Thery's avatar
Sylvain Thery committed
173 174
}

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

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


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 225

// 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
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
// 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
248
void VarSlider::updateFrom(QWidget* widg) const
Sylvain Thery's avatar
Sylvain Thery committed
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 275
{
	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
276
		size_t pos2 = m_choices.find(';', pos);
Sylvain Thery's avatar
Sylvain Thery committed
277 278
		if (pos2!= std::string::npos)
		{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
279
			std::string choice = m_choices.substr(pos, pos2 - pos);
Sylvain Thery's avatar
Sylvain Thery committed
280
			combo->addItem(QString(choice.c_str()));
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
281
			pos = pos2 + 1;
Sylvain Thery's avatar
Sylvain Thery committed
282 283 284
		}
		else
		{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
285
			std::string choice = m_choices.substr(pos, pos2);
Sylvain Thery's avatar
Sylvain Thery committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
			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
307
	while (ptr != NULL)
Sylvain Thery's avatar
Sylvain Thery committed
308 309 310 311 312
	{
		params.push_back(ptr);
		ptr = ptr->next();
	}

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

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

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

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