File: ccItemSelectionDlg.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 (139 lines) | stat: -rw-r--r-- 4,158 bytes parent folder | download | duplicates (3)
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
//##########################################################################
//#                                                                        #
//#                              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 "ccItemSelectionDlg.h"

//Qt
#include <QDialog>
#include <QListWidgetItem>

ccItemSelectionDlg::ccItemSelectionDlg(	bool multiSelectionEnabled,
										QWidget* parent/*=0*/,
										QString itemName/*="entities"*/,
										QString labelStr/*=QString()*/)
	: QDialog(parent, Qt::Tool)
	, Ui::ItemSelectionDlg()
{
	setupUi(this);

	//multi-selection mode
	if (multiSelectionEnabled)
	{
		label->setText(tr("Please select one or several %1:\n(press CTRL+A to select all)").arg(itemName));
		listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
	}
	else
	{
		label->setText(tr("Please select one %1").arg(itemName));
	}

	if (!labelStr.isNull())
	{
		label->setText(labelStr);
	}
}

void ccItemSelectionDlg::setItems(const QStringList& items, int defaultSelectedIndex)
{
	for (int i = 0; i < items.size(); ++i)
	{
		//add one line per entity in the combo-box
		listWidget->insertItem(static_cast<int>(i), new QListWidgetItem(items[i]));
	}

	//default selection
	if (defaultSelectedIndex >= 0 && defaultSelectedIndex < items.size())
	{
		listWidget->setItemSelected(listWidget->item(defaultSelectedIndex), true);
	}
}

int ccItemSelectionDlg::getSelectedIndex() const
{
	//get selected items
	QList<QListWidgetItem*> list = listWidget->selectedItems();
	return list.empty() ? -1 : listWidget->row(list.front());
}

void ccItemSelectionDlg::getSelectedIndexes(std::vector<int>& indexes) const
{
	//get selected items
	QList<QListWidgetItem*> list = listWidget->selectedItems();

	try
	{
		indexes.resize(static_cast<size_t>(list.size()));
	}
	catch (const std::bad_alloc&)
	{
		//not enough memory?!
		return;
	}
	
	for (int i = 0; i < list.size(); ++i)
	{
		indexes[i]  = listWidget->row(list[i]);
	}
}

int ccItemSelectionDlg::SelectEntity(const ccHObject::Container& entities,
									int selectedIndex/*=0*/,
									QWidget* parent/*=0*/,
									QString label/*=QString()*/)
{
	ccItemSelectionDlg epDlg(false, parent, tr("entity"), label);

	QStringList items;
	for (size_t i = 0; i < entities.size(); ++i)
	{
		//add one line per entity
		items << QString("%1 (ID=%2)").arg(entities[i]->getName()).arg(entities[i]->getUniqueID());
	}
	epDlg.setItems(items, selectedIndex);

	if (!epDlg.exec())
	{
		//cancelled by the user
		return -1;
	}

	return epDlg.getSelectedIndex();
}

bool ccItemSelectionDlg::SelectEntities(const ccHObject::Container& entities,
									  std::vector<int>& selectedIndexes,
									  QWidget* parent/*=0*/,
									  QString label/*=QString()*/)
{
	selectedIndexes.clear();

	ccItemSelectionDlg epDlg(true, parent, tr("entities"), label);

	QStringList items;
	for (size_t i = 0; i < entities.size(); ++i)
	{
		//add one line per entity
		items << QString("%1 (ID=%2)").arg(entities[i]->getName()).arg(entities[i]->getUniqueID());
	}
	epDlg.setItems(items, -1);

	if (!epDlg.exec())
		return false;

	epDlg.getSelectedIndexes(selectedIndexes);
	return true;
}