File: PlyOpenDlg.cpp

package info (click to toggle)
cloudcompare 2.10.1-2
  • links: PTS
  • area: main
  • in suites: buster
  • size: 55,916 kB
  • sloc: cpp: 219,837; ansic: 29,944; makefile: 67; sh: 45
file content (469 lines) | stat: -rw-r--r-- 13,386 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//##########################################################################
//#                                                                        #
//#                              CLOUDCOMPARE                              #
//#                                                                        #
//#  This program is free software; you can redistribute it and/or modify  #
//#  it under the terms of the GNU General Public License as published by  #
//#  the Free Software Foundation; version 2 or later of the License.      #
//#                                                                        #
//#  This program 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 General Public License for more details.                          #
//#                                                                        #
//#          COPYRIGHT: EDF R&D / TELECOM ParisTech (ENST-TSI)             #
//#                                                                        #
//##########################################################################

#include "PlyOpenDlg.h"

//Qt
#include <QMessageBox>
#include <QStringList>

//qCC_db
#include <ccLog.h>

//System
#include <string.h>
#include <assert.h>

//! Ply dialog loading context
struct PlyLoadingContext
{
	PlyLoadingContext()
		: ignoredProps(0)
		, valid(false)
		, applyAll(false)
	{}
	
	QStringList allProperties;
	std::vector<QString> standardCombosProperties;
	std::vector<QString> sfCombosProperties;
	std::vector<QString> listCombosProperties;
	std::vector<QString> singleCombosProperties;
	int ignoredProps;
	bool valid;
	bool applyAll;
};
//! Last loading context
static PlyLoadingContext s_lastContext;

PlyOpenDlg::PlyOpenDlg(QWidget* parent)
	: QDialog(parent)
	, Ui::PlyOpenDlg()
{
	setupUi(this);

	try
	{
		m_standardCombos.push_back(xComboBox);
		m_standardCombos.push_back(yComboBox);
		m_standardCombos.push_back(zComboBox);
		m_standardCombos.push_back(rComboBox);
		m_standardCombos.push_back(gComboBox);
		m_standardCombos.push_back(bComboBox);
		m_standardCombos.push_back(iComboBox);
		m_standardCombos.push_back(nxComboBox);
		m_standardCombos.push_back(nyComboBox);
		m_standardCombos.push_back(nzComboBox);

		m_sfCombos.push_back(sfComboBox);

		m_listCombos.push_back(facesComboBox);
		m_listCombos.push_back(textCoordsComboBox);

		m_singleCombos.push_back(texIndexComboBox);
	}
	catch (const std::bad_alloc&)
	{
		//not enough memory?! What can we do...
	}

	connect(addSFToolButton,	&QAbstractButton::clicked,	this,	&PlyOpenDlg::addSFComboBox);
	connect(applyButton,		&QAbstractButton::clicked,	this,	&PlyOpenDlg::apply);
	connect(applyAllButton,		&QAbstractButton::clicked,	this,	&PlyOpenDlg::applyAll);
	connect(cancelButton,		&QAbstractButton::clicked,	this,	&QDialog::reject);
	connect(this,				&PlyOpenDlg::fullyAccepted,	this,	&QDialog::accept);
}

void PlyOpenDlg::setDefaultComboItems(const QStringList& stdPropsText)
{
	m_stdPropsText = stdPropsText;
	int stdPropsCount = stdPropsText.count();

	for (QComboBox* combo : m_standardCombos)
	{
		assert(combo);
		combo->addItems(m_stdPropsText);
		combo->setMaxVisibleItems(stdPropsCount);
	}

	for (QComboBox* combo : m_sfCombos)
	{
		assert(combo);
		combo->addItems(m_stdPropsText);
		combo->setMaxVisibleItems(stdPropsCount);
	}
}

void PlyOpenDlg::setListComboItems(const QStringList& listPropsText)
{
	m_listPropsText = listPropsText;
	int listPropsCount = listPropsText.count();

	for (QComboBox* combo : m_listCombos)
	{
		assert(combo);
		combo->addItems(m_listPropsText);
		combo->setMaxVisibleItems(listPropsCount);
	}
}

void PlyOpenDlg::setSingleComboItems(const QStringList& singlePropsText)
{
	m_singlePropsText = singlePropsText;
	int singlePropsCount = singlePropsText.count();

	for (QComboBox* combo : m_singleCombos)
	{
		assert(combo);
		combo->addItems(m_singlePropsText);
		combo->setMaxVisibleItems(singlePropsCount);
	}
}

void PlyOpenDlg::ResetApplyAll()
{
	s_lastContext.applyAll = false;
}

bool PlyOpenDlg::restorePreviousContext(bool& hasAPreviousContext)
{
	hasAPreviousContext = s_lastContext.valid;
	if (!hasAPreviousContext)
		return false;

	int unassignedProps = 0;
	int mismatchProps = 0;
	bool restored = restoreContext(&s_lastContext, unassignedProps, mismatchProps);

	//auto-stop: we can't keep 'apply all' if something has changed
	if (!restored || mismatchProps != 0/* || unassignedProps > 0*/)
	{
		s_lastContext.applyAll = false;
		return false;
	}

	return true;
}

void PlyOpenDlg::saveContext(PlyLoadingContext* context)
{
	if (!context)
	{
		assert(false);
		return;
	}
	context->valid = false;

	//create the list of all properties
	context->allProperties.clear();
	assert(m_standardCombos.front());
	if (m_standardCombos.front())
		for (int i = 1; i < m_standardCombos.front()->count(); ++i) //the first item is always 'NONE'
			context->allProperties.append(m_standardCombos.front()->itemText(i));
	assert(m_listCombos.front());
	if (m_listCombos.front())
		for (int i = 1; i < m_listCombos.front()->count(); ++i) //the first item is always 'NONE'
			context->allProperties.append(m_listCombos.front()->itemText(i));
	assert(m_singleCombos.front());
	if (m_singleCombos.front())
		for (int i = 1; i < m_singleCombos.front()->count(); ++i) //the first item is always 'NONE'
			context->allProperties.append(m_singleCombos.front()->itemText(i));

	//now remember how each combo-box is mapped
	int assignedProps = 0;
	try
	{
		//standard combos
		{
			context->standardCombosProperties.resize(m_standardCombos.size());
			std::fill(context->standardCombosProperties.begin(), context->standardCombosProperties.end(), QString());
			for (size_t i = 0; i < m_standardCombos.size(); ++i)
			{
				if (m_standardCombos[i] && m_standardCombos[i]->currentIndex() > 0) //currentIndex == 0 means 'NONE'!!!
				{
					context->standardCombosProperties[i] = m_standardCombos[i]->currentText();
					++assignedProps;
				}
			}
		}
		//list combos
		{
			context->listCombosProperties.resize(m_listCombos.size());
			std::fill(context->listCombosProperties.begin(), context->listCombosProperties.end(), QString());
			for (size_t i = 0; i < m_listCombos.size(); ++i)
			{
				if (m_listCombos[i] && m_listCombos[i]->currentIndex() > 0)
				{
					context->listCombosProperties[i] = m_listCombos[i]->currentText();
					++assignedProps;
				}
			}
		}
		//single combos
		{
			context->singleCombosProperties.resize(m_singleCombos.size());
			std::fill(context->singleCombosProperties.begin(), context->singleCombosProperties.end(), QString());
			for (size_t i = 0; i < m_singleCombos.size(); ++i)
			{
				if (m_singleCombos[i] && m_singleCombos[i]->currentIndex() > 0)
				{
					context->singleCombosProperties[i] = m_singleCombos[i]->currentText();
					++assignedProps;
				}
			}
		}
		//additional SF combos
		{
			context->sfCombosProperties.clear();
			for (size_t i = 0; i < m_sfCombos.size(); ++i)
			{
				//we only copy the valid ones!
				if (m_sfCombos[i] && m_sfCombos[i]->currentIndex() > 0)
				{
					context->sfCombosProperties.push_back(m_sfCombos[i]->currentText());
					++assignedProps;
				}
			}
		}
	}
	catch (const std::bad_alloc&)
	{
		//not enough memory
		return;
	}

	context->ignoredProps = context->allProperties.size() - assignedProps;
	context->valid = true;
}

bool PlyOpenDlg::restoreContext(PlyLoadingContext* context, int& unassignedProps, int& mismatchProps)
{
	if (!context || !context->valid)
	{
		assert(false);
		return false;
	}

	//first check if all new properties are in the old properties set
	mismatchProps = 0;
	int totalProps = 0;
	{
		assert(m_standardCombos.front());
		if (m_standardCombos.front())
			for (int i = 1; i < m_standardCombos.front()->count(); ++i) //the first item is always 'NONE'
			{
				++totalProps;
				if (!context->allProperties.contains(m_standardCombos.front()->itemText(i)))
					++mismatchProps;
			}
		assert(m_listCombos.front());
		if (m_listCombos.front())
			for (int i = 1; i < m_listCombos.front()->count(); ++i) //the first item is always 'NONE'
			{
				++totalProps;
				if (!context->allProperties.contains(m_listCombos.front()->itemText(i)))
					++mismatchProps;
			}
		assert(m_singleCombos.front());
		if (m_singleCombos.front())
			for (int i = 1; i < m_singleCombos.front()->count(); ++i) //the first item is always 'NONE'
			{
				++totalProps;
				if (!context->allProperties.contains(m_singleCombos.front()->itemText(i)))
					++mismatchProps;
			}
	}

	int assignedEntries = 0;

	//standard combos
	assert(m_standardCombos.size() == context->standardCombosProperties.size());
	{
		for (size_t i = 0; i < m_standardCombos.size(); ++i)
		{
			if (m_standardCombos[i])
			{
				m_standardCombos[i]->setCurrentIndex(0);
				//if a specific property was defined for this field
				if (!context->standardCombosProperties[i].isNull())
				{
					//try to find it in the new property list!
					int idx = m_standardCombos[i]->findText(context->standardCombosProperties[i]);
					if (idx >= 0)
					{
						++assignedEntries;
						m_standardCombos[i]->setCurrentIndex(idx);
					}
				}
			}
		}
	}

	//list combos
	assert(m_listCombos.size() == context->listCombosProperties.size());
	{
		for (size_t i = 0; i < m_listCombos.size(); ++i)
		{
			if (m_listCombos[i])
			{
				m_listCombos[i]->setCurrentIndex(0);
				//if a specific property was defined for this field
				if (!context->listCombosProperties[i].isNull())
				{
					//try to find it in the new property list!
					int idx = m_listCombos[i]->findText(context->listCombosProperties[i]);
					if (idx >= 0)
					{
						++assignedEntries;
						m_listCombos[i]->setCurrentIndex(idx);
					}
				}
			}
		}
	}

	//single combox
	assert(m_singleCombos.size() == context->singleCombosProperties.size());
	{
		for (size_t i = 0; i < m_singleCombos.size(); ++i)
		{
			if (m_singleCombos[i])
			{
				m_singleCombos[i]->setCurrentIndex(0);
				//if a specific property was defined for this field
				if (!context->singleCombosProperties[i].isNull())
				{
					//try to find it in the new property list!
					int idx = m_singleCombos[i]->findText(context->singleCombosProperties[i]);
					if (idx >= 0)
					{
						++assignedEntries;
						m_singleCombos[i]->setCurrentIndex(idx);
					}
				}
			}
		}
	}
	
	//additional SF combos
	assert(m_sfCombos.size() == 1);
	{
		m_sfCombos.front()->setCurrentIndex(0);
		bool firstSF = true;
		for (size_t i = 0; i < context->sfCombosProperties.size(); ++i)
		{
			//try to find it in the new property list!
			int idx = m_sfCombos.front()->findText(context->sfCombosProperties[i]);
			if (idx >= 0)
			{
				++assignedEntries;
				//we use the default sf combo-box by default
				if (firstSF)
					m_sfCombos.front()->setCurrentIndex(idx);
				else
					addSFComboBox(idx);
				firstSF = false;
			}
		}
	}

	assert(assignedEntries <= totalProps);
	unassignedProps = totalProps - assignedEntries;

	return true;
}

void PlyOpenDlg::apply()
{
	if (isValid())
	{
		saveContext(&s_lastContext);
		s_lastContext.applyAll = false;
		emit fullyAccepted();
	}
}

void PlyOpenDlg::applyAll()
{
	if (isValid())
	{
		saveContext(&s_lastContext);
		s_lastContext.applyAll = true;
		emit fullyAccepted();
	}
}

bool PlyOpenDlg::isValid(bool displayErrors/*=true*/) const
{
	//we need at least two coordinates per point (i.e. 2D)
	int zeroCoord = 0;
	if (xComboBox->currentIndex() == 0) ++zeroCoord;
	if (yComboBox->currentIndex() == 0) ++zeroCoord;
	if (zComboBox->currentIndex() == 0) ++zeroCoord;

	if (zeroCoord > 1)
	{
		if (displayErrors)
			QMessageBox::warning(0, "Error", "At least two vertex coordinates (X,Y,Z) must be defined!");
		return false;
	}

	//we must ensure that no property is assigned to more than one field
	int n = m_stdPropsText.size();
	int p = m_listPropsText.size();
	int q = m_singlePropsText.size();

	assert(n + p + q >= 2);
	std::vector<int> assignedIndexCount(n + p + q, 0);

	for (size_t i = 0; i < m_standardCombos.size(); ++i)
		++assignedIndexCount[m_standardCombos[i]->currentIndex()];
	for (size_t j = 0; j < m_listCombos.size(); ++j)
		++assignedIndexCount[m_listCombos[j]->currentIndex() > 0 ? n + m_listCombos[j]->currentIndex() : 0];
	for (size_t k = 0; k < m_singleCombos.size(); ++k)
		++assignedIndexCount[m_singleCombos[k]->currentIndex() > 0 ? n + p + m_singleCombos[k]->currentIndex() : 0];
	for (size_t l = 0; l < m_sfCombos.size(); ++l)
		++assignedIndexCount[m_sfCombos[l]->currentIndex()];

	for (int i = 1; i < n + p + q; ++i)
	{
		if (assignedIndexCount[i] > 1)
		{
			if (displayErrors)
				QMessageBox::warning(0, "Error", QString("Can't assign same property to multiple fields! (%1)").arg(xComboBox->itemText(i)));
			return false;
		}
	}

	return true;
}

bool PlyOpenDlg::canBeSkipped() const
{
	return s_lastContext.valid && s_lastContext.applyAll && isValid(false);
}

void PlyOpenDlg::addSFComboBox(int selectedIndex/*=0*/)
{
	//create a new combo-box
	m_sfCombos.push_back(new QComboBox());
	formLayout->addRow(QString("Scalar #%1").arg(m_sfCombos.size()), m_sfCombos.back());

	//fill it with default items
	m_sfCombos.back()->addItems(m_stdPropsText);
	m_sfCombos.back()->setMaxVisibleItems(m_stdPropsText.size());
	m_sfCombos.back()->setCurrentIndex(selectedIndex);
}