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, &QTreeWidget::currentItemChanged, this, &NewObjectDialog::onDisplaySelected);
111  connect(tree, &QTreeWidget::itemActivated, this, &NewObjectDialog::accept);
112  connect(button_box_, &QDialogButtonBox::accepted, this, &NewObjectDialog::accept);
113  connect(button_box_, &QDialogButtonBox::rejected, this, &NewObjectDialog::reject);
114 
116  {
117  connect(name_editor_, &QLineEdit::textEdited, this, &NewObjectDialog::onNameChanged);
118  }
119 }
120 
122 {
123  return (QSize(500, 660));
124 }
125 
126 void NewObjectDialog::fillTree(QTreeWidget* tree)
127 {
128  QIcon default_package_icon = loadPixmap("package://rviz/icons/default_package_icon.png");
129 
130  QStringList classes = factory_->getDeclaredClassIds();
131  classes.sort();
132 
133  // Map from package names to the corresponding top-level tree widget items.
134  std::map<QString, QTreeWidgetItem*> package_items;
135 
136  for (int i = 0; i < classes.size(); i++)
137  {
138  QString lookup_name = classes[i];
139  QString package = factory_->getClassPackage(lookup_name);
140  QString description = factory_->getClassDescription(lookup_name);
141  QString name = factory_->getClassName(lookup_name);
142 
143  QTreeWidgetItem* package_item;
144 
145  std::map<QString, QTreeWidgetItem*>::iterator mi;
146  mi = package_items.find(package);
147  if (mi == package_items.end())
148  {
149  package_item = new QTreeWidgetItem(tree);
150  package_item->setText(0, package);
151  package_item->setIcon(0, default_package_icon);
152 
153  package_item->setExpanded(true);
154  package_items[package] = package_item;
155  }
156  else
157  {
158  package_item = (*mi).second;
159  }
160  QTreeWidgetItem* class_item = new QTreeWidgetItem(package_item);
161 
162  class_item->setIcon(0, factory_->getIcon(lookup_name));
163 
164  class_item->setText(0, name);
165  class_item->setWhatsThis(0, description);
166  // Store the lookup name for each class in the UserRole of the item.
167  class_item->setData(0, Qt::UserRole, lookup_name);
168  class_item->setDisabled(disallowed_class_lookup_names_.contains(lookup_name));
169  }
170 }
171 
172 void NewObjectDialog::onDisplaySelected(QTreeWidgetItem* selected_item)
173 {
174  QString html = "<html><body>" + selected_item->whatsThis(0) + "</body></html>";
175  description_->setHtml(html);
176 
177  // We stored the lookup name for the class in the UserRole of the items.
178  QVariant user_data = selected_item->data(0, Qt::UserRole);
179  bool selection_is_valid = user_data.isValid();
180  if (selection_is_valid)
181  {
182  lookup_name_ = user_data.toString();
184  {
185  QString display_name = selected_item->text(0);
186 
187  int counter = 1;
188  QString name;
189  do
190  {
191  name = display_name;
192  if (counter > 1)
193  {
194  name += QString::number(counter);
195  }
196  ++counter;
197 
198  } while (disallowed_display_names_.contains(name));
199 
200  name_editor_->setText(name);
201  }
202  }
203  else
204  {
205  lookup_name_ = "";
207  {
208  name_editor_->setText("");
209  }
210  }
211  button_box_->button(QDialogButtonBox::Ok)->setEnabled(isValid());
212 }
213 
215 {
216  if (lookup_name_.size() == 0)
217  {
218  setError("Select a Display type.");
219  return false;
220  }
222  {
223  QString display_name = name_editor_->text();
224  if (display_name.size() == 0)
225  {
226  setError("Enter a name for the display.");
227  return false;
228  }
229  if (disallowed_display_names_.contains(display_name))
230  {
231  setError("Name in use. Display names must be unique.");
232  return false;
233  }
234  }
235  setError("");
236  return true;
237 }
238 
239 void NewObjectDialog::setError(const QString& error_text)
240 {
241  button_box_->button(QDialogButtonBox::Ok)->setToolTip(error_text);
242 }
243 
245 {
246  button_box_->button(QDialogButtonBox::Ok)->setEnabled(isValid());
247 }
248 
250 {
251  if (isValid())
252  {
255  {
257  }
258  QDialog::accept();
259  }
260 }
261 
262 } // namespace rviz
rviz::NewObjectDialog::onDisplaySelected
void onDisplaySelected(QTreeWidgetItem *selected_item)
Definition: new_object_dialog.cpp:172
rviz::NewObjectDialog::sizeHint
QSize sizeHint() const override
Definition: new_object_dialog.cpp:121
rviz::NewObjectDialog::NewObjectDialog
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)
Definition: new_object_dialog.cpp:51
rviz::NewObjectDialog::lookup_name_output_
QString * lookup_name_output_
Definition: new_object_dialog.h:98
ros.h
description
description
rviz::loadPixmap
QPixmap loadPixmap(const QString &url, bool fill_cache)
Definition: load_resource.cpp:65
rviz::NewObjectDialog::display_name_output_
QString * display_name_output_
Definition: new_object_dialog.h:99
rviz::NewObjectDialog::onNameChanged
void onNameChanged()
Definition: new_object_dialog.cpp:244
rviz::NewObjectDialog::description_
QTextBrowser * description_
Definition: new_object_dialog.h:102
package
string package
tree
tree
rviz::NewObjectDialog::disallowed_display_names_
const QStringList & disallowed_display_names_
Definition: new_object_dialog.h:95
rviz::NewObjectDialog::setError
void setError(const QString &error_text)
Definition: new_object_dialog.cpp:239
rviz
Definition: add_display_dialog.cpp:54
rviz::NewObjectDialog::name_editor_
QLineEdit * name_editor_
Definition: new_object_dialog.h:104
new_object_dialog.h
rviz::Factory::getClassDescription
virtual QString getClassDescription(const QString &class_id) const =0
rviz::NewObjectDialog::disallowed_class_lookup_names_
const QStringList & disallowed_class_lookup_names_
Definition: new_object_dialog.h:96
rviz::Factory::getDeclaredClassIds
virtual QStringList getDeclaredClassIds()=0
package.h
rviz::NewObjectDialog::isValid
bool isValid()
Definition: new_object_dialog.cpp:214
rviz::Factory::getClassName
virtual QString getClassName(const QString &class_id) const =0
rviz::NewObjectDialog::fillTree
void fillTree(QTreeWidget *tree)
Definition: new_object_dialog.cpp:126
load_resource.h
rviz::NewObjectDialog::factory_
Factory * factory_
Definition: new_object_dialog.h:94
rviz::Factory
Abstract superclass representing the ability to get a list of class IDs and the ability to get name,...
Definition: factory.h:42
rviz::NewObjectDialog::button_box_
QDialogButtonBox * button_box_
Definition: new_object_dialog.h:107
rviz::Factory::getIcon
virtual QIcon getIcon(const QString &class_id) const =0
rviz::NewObjectDialog::lookup_name_
QString lookup_name_
Definition: new_object_dialog.h:111
rviz::NewObjectDialog::accept
void accept() override
Definition: new_object_dialog.cpp:249


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10