50 #include <QDialogButtonBox> 51 #include <QVBoxLayout> 52 #include <QMessageBox> 53 #include <QTreeWidget> 63 #ifndef _MIRA_SELECTIONLISTDIALOG_H_ 64 #define _MIRA_SELECTIONLISTDIALOG_H_ 113 template <
typename T>
136 void addItem(
const QStringList& labels, T data=T(),
bool selected=
false);
145 void addItem(
const QString& category,
const QStringList& labels, T data=T(),
146 bool selected=
false);
174 QTreeWidgetItem*
addCategory(
const QString& category);
187 std::map<const QTreeWidgetItem*, T>
mItems;
193 template <
typename T>
196 mOnlyOnePerCategory(false)
198 this->resize(640,480);
200 QVBoxLayout *mainLayout =
new QVBoxLayout;
212 mainLayout->addWidget(filter);
219 mButtonBox =
new QDialogButtonBox(QDialogButtonBox::Ok |
220 QDialogButtonBox::Cancel);
223 connect(
mButtonBox, SIGNAL(rejected()),
this, SLOT(reject()));
224 connect(
mTreeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,
int)),
this, SLOT(
accept()));
226 setLayout(mainLayout);
230 template <
typename T>
233 mTreeWidget->setHeaderLabels(labels);
236 template <
typename T>
239 QTreeWidgetItem* item;
241 item =
new QTreeWidgetItem(mTreeWidget, QStringList(category));
242 mTreeWidget->addTopLevelItem(item);
243 QFont font = item->font(0);
245 item->setFont(0,font);
246 item->setFlags(Qt::ItemIsEnabled);
247 item->setExpanded(
true);
248 mCategories[category] = item;
249 mTreeWidget->resizeColumnToContents(0);
253 template <
typename T>
255 const QStringList& labels, T data,
258 assert(!category.isEmpty());
260 QTreeWidgetItem* parent = NULL;
261 auto it = mCategories.find(category);
262 if(it != mCategories.end())
265 parent = addCategory(category);
266 mCategories.insert(std::make_pair(category, parent));
268 assert(parent!=NULL);
270 QTreeWidgetItem* item =
new QTreeWidgetItem(parent, labels);
271 parent->addChild(item);
272 item->setSelected(selected);
273 mItems.insert(std::make_pair(item,data));
274 mTreeWidget->resizeColumnToContents(0);
277 template <
typename T>
281 QTreeWidgetItem* item =
new QTreeWidgetItem(labels);
282 mTreeWidget->addTopLevelItem(item);
283 item->setSelected(selected);
284 mItems.insert(std::make_pair(item,data));
285 mTreeWidget->resizeColumnToContents(0);
288 template <
typename T>
291 std::set<QString> itemsPerCategory;
293 if (mOnlyOnePerCategory)
295 foreach(
const QTreeWidgetItem* i, mTreeWidget->selectedItems()) {
296 auto it = mItems.find(i);
297 if(it==mItems.end() || i->parent()==NULL)
299 if (itemsPerCategory.count(i->parent()->text(0)) > 0)
301 QMessageBox::critical(
this,
"Multiple items selected",
302 "You have selected more than one item per category");
305 itemsPerCategory.insert(i->parent()->text(0));
311 template <
typename T>
314 mOnlyOnePerCategory = onlyOne;
317 template <
typename T>
320 mTreeWidget->setSelectionMode(enable ? QAbstractItemView::MultiSelection :
321 QAbstractItemView::SingleSelection);
324 template <
typename T>
327 std::list<std::pair<QString, T>> r;
329 for(QTreeWidgetItemIterator i(mTreeWidget, QTreeWidgetItemIterator::Selected); *i; ++i)
331 auto it = mItems.find(*i);
335 r.push_back(std::make_pair((*i)->text(0), it->second));
Macro for iterating over all elements in a container.
std::map< const QTreeWidgetItem *, T > mItems
Definition: SelectionListDialog.h:187
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
QTreeWidgetItem * addCategory(const QString &category)
Definition: SelectionListDialog.h:237
void setExpandAll(bool expandAll)
If set to true, all (visible) nodes in the tree will be always expanded.
std::list< std::pair< QString, T > > selectedItems() const
Returns a list containing the selected items as pairs of strings and the data that was assigned in ad...
Definition: SelectionListDialog.h:325
QVBoxLayout * mAdditionalContentLayout
Definition: SelectionListDialog.h:178
A dialog for providing the a list, where the items can be grouped in categories.
Definition: SelectionListDialog.h:114
void setHeaderLabels(const QStringList &labels)
Sets the labels of the columns.
Definition: SelectionListDialog.h:231
Filter widget containing an line edit that filters the elements of an assigned TreeView.
bool mOnlyOnePerCategory
Definition: SelectionListDialog.h:184
void setMultiSelection(bool enable)
Definition: SelectionListDialog.h:318
void addItem(const QStringList &labels, T data=T(), bool selected=false)
Adds a new item to the list.
Definition: SelectionListDialog.h:278
QTreeWidget * mTreeWidget
Definition: SelectionListDialog.h:183
virtual void accept()
Definition: SelectionListDialog.h:289
SelectionListDialog(QWidget *parent=0)
Definition: SelectionListDialog.h:194
std::map< QString, QTreeWidgetItem * > mCategories
Definition: SelectionListDialog.h:186
void setSelectOnlyOneItemPerCategory(bool onlyOne)
If set to true the user is allowed to only select one item per category.
Definition: SelectionListDialog.h:312
QDialogButtonBox * mButtonBox
Definition: SelectionListDialog.h:182
Definition: TreeViewFilter.h:64