new_object_dialog.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <map>
31 
32 #include <boost/filesystem.hpp>
33 
34 #include <ros/package.h>
35 #include <ros/ros.h>
36 
37 #include <QGroupBox>
38 #include <QTreeWidget>
39 #include <QLabel>
40 #include <QLineEdit>
41 #include <QTextBrowser>
42 #include <QVBoxLayout>
43 #include <QDialogButtonBox>
44 #include <QPushButton>
45 
46 #include "new_object_dialog.h"
47 #include "rviz/load_resource.h"
48 
49 namespace rviz
50 {
52  const QString& object_type,
53  const QStringList& disallowed_display_names,
54  const QStringList& disallowed_class_lookup_names,
55  QString* lookup_name_output,
56  QString* display_name_output,
57  QWidget* parent)
58  : QDialog(parent)
59  , factory_(factory)
60  , disallowed_display_names_(disallowed_display_names)
61  , disallowed_class_lookup_names_(disallowed_class_lookup_names)
62  , lookup_name_output_(lookup_name_output)
63  , display_name_output_(display_name_output)
64 {
65  //***** Layout
66 
67  // Display Type group
68  QGroupBox* type_box = new QGroupBox(object_type + " Type");
69 
70  QTreeWidget* tree = new QTreeWidget;
71  tree->setHeaderHidden(true);
72  fillTree(tree);
73 
74  QLabel* description_label = new QLabel("Description:");
75  description_ = new QTextBrowser;
76  description_->setMaximumHeight(100);
77  description_->setOpenExternalLinks(true);
78 
79  QVBoxLayout* type_layout = new QVBoxLayout;
80  type_layout->addWidget(tree);
81  type_layout->addWidget(description_label);
82  type_layout->addWidget(description_);
83 
84  type_box->setLayout(type_layout);
85 
86  // Display Name group
87  QGroupBox* name_box = nullptr;
89  {
90  name_box = new QGroupBox(object_type + " Name");
91  name_editor_ = new QLineEdit;
92  QVBoxLayout* name_layout = new QVBoxLayout;
93  name_layout->addWidget(name_editor_);
94  name_box->setLayout(name_layout);
95  }
96 
97  // Buttons
98  button_box_ = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal);
99 
100  QVBoxLayout* main_layout = new QVBoxLayout;
101  main_layout->addWidget(type_box);
103  {
104  main_layout->addWidget(name_box);
105  }
106  main_layout->addWidget(button_box_);
107  setLayout(main_layout);
108 
109  //***** Connections
110  connect(tree, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), this,
111  SLOT(onDisplaySelected(QTreeWidgetItem*)));
112  connect(tree, SIGNAL(itemActivated(QTreeWidgetItem*, int)), this, SLOT(accept()));
113  connect(button_box_, SIGNAL(accepted()), this, SLOT(accept()));
114  connect(button_box_, SIGNAL(rejected()), this, SLOT(reject()));
115 
117  {
118  connect(name_editor_, SIGNAL(textEdited(const QString&)), this, SLOT(onNameChanged()));
119  }
120 }
121 
123 {
124  return (QSize(500, 660));
125 }
126 
127 void NewObjectDialog::fillTree(QTreeWidget* tree)
128 {
129  QIcon default_package_icon = loadPixmap("package://rviz/icons/default_package_icon.png");
130 
131  QStringList classes = factory_->getDeclaredClassIds();
132  classes.sort();
133 
134  // Map from package names to the corresponding top-level tree widget items.
135  std::map<QString, QTreeWidgetItem*> package_items;
136 
137  for (int i = 0; i < classes.size(); i++)
138  {
139  QString lookup_name = classes[i];
140  QString package = factory_->getClassPackage(lookup_name);
141  QString description = factory_->getClassDescription(lookup_name);
142  QString name = factory_->getClassName(lookup_name);
143 
144  QTreeWidgetItem* package_item;
145 
146  std::map<QString, QTreeWidgetItem*>::iterator mi;
147  mi = package_items.find(package);
148  if (mi == package_items.end())
149  {
150  package_item = new QTreeWidgetItem(tree);
151  package_item->setText(0, package);
152  package_item->setIcon(0, default_package_icon);
153 
154  package_item->setExpanded(true);
155  package_items[package] = package_item;
156  }
157  else
158  {
159  package_item = (*mi).second;
160  }
161  QTreeWidgetItem* class_item = new QTreeWidgetItem(package_item);
162 
163  class_item->setIcon(0, factory_->getIcon(lookup_name));
164 
165  class_item->setText(0, name);
166  class_item->setWhatsThis(0, description);
167  // Store the lookup name for each class in the UserRole of the item.
168  class_item->setData(0, Qt::UserRole, lookup_name);
169  class_item->setDisabled(disallowed_class_lookup_names_.contains(lookup_name));
170  }
171 }
172 
173 void NewObjectDialog::onDisplaySelected(QTreeWidgetItem* selected_item)
174 {
175  QString html = "<html><body>" + selected_item->whatsThis(0) + "</body></html>";
176  description_->setHtml(html);
177 
178  // We stored the lookup name for the class in the UserRole of the items.
179  QVariant user_data = selected_item->data(0, Qt::UserRole);
180  bool selection_is_valid = user_data.isValid();
181  if (selection_is_valid)
182  {
183  lookup_name_ = user_data.toString();
185  {
186  QString display_name = selected_item->text(0);
187 
188  int counter = 1;
189  QString name;
190  do
191  {
192  name = display_name;
193  if (counter > 1)
194  {
195  name += QString::number(counter);
196  }
197  ++counter;
198 
199  } while (disallowed_display_names_.contains(name));
200 
201  name_editor_->setText(name);
202  }
203  }
204  else
205  {
206  lookup_name_ = "";
208  {
209  name_editor_->setText("");
210  }
211  }
212  button_box_->button(QDialogButtonBox::Ok)->setEnabled(isValid());
213 }
214 
216 {
217  if (lookup_name_.size() == 0)
218  {
219  setError("Select a Display type.");
220  return false;
221  }
223  {
224  QString display_name = name_editor_->text();
225  if (display_name.size() == 0)
226  {
227  setError("Enter a name for the display.");
228  return false;
229  }
230  if (disallowed_display_names_.contains(display_name))
231  {
232  setError("Name in use. Display names must be unique.");
233  return false;
234  }
235  }
236  setError("");
237  return true;
238 }
239 
240 void NewObjectDialog::setError(const QString& error_text)
241 {
242  button_box_->button(QDialogButtonBox::Ok)->setToolTip(error_text);
243 }
244 
246 {
247  button_box_->button(QDialogButtonBox::Ok)->setEnabled(isValid());
248 }
249 
251 {
252  if (isValid())
253  {
256  {
258  }
259  QDialog::accept();
260  }
261 }
262 
263 } // namespace rviz
QSize sizeHint() const override
string package
NewObjectDialog(Factory *factory, const QString &object_type, const QStringList &disallowed_display_names, const QStringList &disallowed_class_lookup_names, QString *lookup_name_output, QString *display_name_output=nullptr, QWidget *parent=nullptr)
void onDisplaySelected(QTreeWidgetItem *selected_item)
virtual QString getClassName(const QString &class_id) const =0
const QStringList & disallowed_display_names_
QTextBrowser * description_
void setError(const QString &error_text)
description
virtual QString getClassDescription(const QString &class_id) const =0
const QStringList & disallowed_class_lookup_names_
tree
Abstract superclass representing the ability to get a list of class IDs and the ability to get name...
Definition: factory.h:42
QDialogButtonBox * button_box_
void fillTree(QTreeWidget *tree)
virtual QIcon getIcon(const QString &class_id) const =0
virtual QStringList getDeclaredClassIds()=0
QPixmap loadPixmap(QString url, bool fill_cache)


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:24