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 {
51 
53  const QString& object_type,
54  const QStringList& disallowed_display_names,
55  const QStringList& disallowed_class_lookup_names,
56  QString* lookup_name_output,
57  QString* display_name_output,
58  QWidget* parent )
59 : QDialog( parent )
60 , factory_( factory )
61 , disallowed_display_names_( disallowed_display_names )
62 , disallowed_class_lookup_names_( disallowed_class_lookup_names )
63 , lookup_name_output_( lookup_name_output )
64 , display_name_output_( display_name_output )
65 {
66  //***** Layout
67 
68  // Display Type group
69  QGroupBox* type_box = new QGroupBox( object_type + " Type" );
70 
71  QTreeWidget* tree = new QTreeWidget;
72  tree->setHeaderHidden( true );
73  fillTree( tree );
74 
75  QLabel* description_label = new QLabel( "Description:" );
76  description_ = new QTextBrowser;
77  description_->setMaximumHeight( 100 );
78  description_->setOpenExternalLinks( true );
79 
80  QVBoxLayout* type_layout = new QVBoxLayout;
81  type_layout->addWidget( tree );
82  type_layout->addWidget( description_label );
83  type_layout->addWidget( description_ );
84 
85  type_box->setLayout( type_layout );
86 
87  // Display Name group
88  QGroupBox* name_box;
90  {
91  name_box = new QGroupBox( object_type + " Name" );
92  name_editor_ = new QLineEdit;
93  QVBoxLayout* name_layout = new QVBoxLayout;
94  name_layout->addWidget( name_editor_ );
95  name_box->setLayout( name_layout );
96  }
97 
98  // Buttons
99  button_box_ = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel,
100  Qt::Horizontal );
101 
102  QVBoxLayout* main_layout = new QVBoxLayout;
103  main_layout->addWidget( type_box );
105  {
106  main_layout->addWidget( name_box );
107  }
108  main_layout->addWidget( button_box_ );
109  setLayout( main_layout );
110 
111  //***** Connections
112  connect( tree, SIGNAL( currentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* )),
113  this, SLOT( onDisplaySelected( QTreeWidgetItem* )));
114  connect( tree, SIGNAL( itemActivated( QTreeWidgetItem*, int )),
115  this, SLOT( accept() ));
116  connect( button_box_, SIGNAL( accepted() ), this, SLOT( accept() ));
117  connect( button_box_, SIGNAL( rejected() ), this, SLOT( reject() ));
118 
120  {
121  connect( name_editor_, SIGNAL( textEdited( const QString& )),
122  this, SLOT( onNameChanged() ));
123  }
124 }
125 
127 {
128  return( QSize(500,660) );
129 }
130 
131 void NewObjectDialog::fillTree( QTreeWidget* tree )
132 {
133  QIcon default_package_icon = loadPixmap( "package://rviz/icons/default_package_icon.png" );
134 
135  QStringList classes = factory_->getDeclaredClassIds();
136  classes.sort();
137 
138  // Map from package names to the corresponding top-level tree widget items.
139  std::map<QString, QTreeWidgetItem*> package_items;
140 
141  for( int i = 0; i < classes.size(); i++ )
142  {
143  QString lookup_name = classes[ i ];
144  QString package = factory_->getClassPackage( lookup_name );
145  QString description = factory_->getClassDescription( lookup_name );
146  QString name = factory_->getClassName( lookup_name );
147 
148  QTreeWidgetItem* package_item;
149 
150  std::map<QString, QTreeWidgetItem*>::iterator mi;
151  mi = package_items.find( package );
152  if( mi == package_items.end() )
153  {
154  package_item = new QTreeWidgetItem( tree );
155  package_item->setText( 0, package );
156  package_item->setIcon( 0, default_package_icon );
157 
158  package_item->setExpanded( true );
159  package_items[ package ] = package_item;
160  }
161  else
162  {
163  package_item = (*mi).second;
164  }
165  QTreeWidgetItem* class_item = new QTreeWidgetItem( package_item );
166 
167  class_item->setIcon( 0, factory_->getIcon( lookup_name ) );
168 
169  class_item->setText( 0, name );
170  class_item->setWhatsThis( 0, description );
171  // Store the lookup name for each class in the UserRole of the item.
172  class_item->setData( 0, Qt::UserRole, lookup_name );
173  class_item->setDisabled( disallowed_class_lookup_names_.contains( lookup_name ));
174  }
175 }
176 
177 void NewObjectDialog::onDisplaySelected( QTreeWidgetItem* selected_item )
178 {
179  QString html = "<html><body>" + selected_item->whatsThis( 0 ) + "</body></html>";
180  description_->setHtml( html );
181 
182  // We stored the lookup name for the class in the UserRole of the items.
183  QVariant user_data = selected_item->data( 0, Qt::UserRole );
184  bool selection_is_valid = user_data.isValid();
185  if( selection_is_valid )
186  {
187  lookup_name_ = user_data.toString();
189  {
190  QString display_name = selected_item->text( 0 );
191 
192  int counter = 1;
193  QString name;
194  do
195  {
196  name = display_name;
197  if( counter > 1 )
198  {
199  name += QString::number( counter );
200  }
201  ++counter;
202 
203  } while( disallowed_display_names_.contains( name ));
204 
205  name_editor_->setText( name );
206  }
207  }
208  else
209  {
210  lookup_name_ = "";
212  {
213  name_editor_->setText( "" );
214  }
215  }
216  button_box_->button( QDialogButtonBox::Ok )->setEnabled( isValid() );
217 }
218 
220 {
221  if( lookup_name_.size() == 0 )
222  {
223  setError( "Select a Display type." );
224  return false;
225  }
227  {
228  QString display_name = name_editor_->text();
229  if( display_name.size() == 0 )
230  {
231  setError( "Enter a name for the display." );
232  return false;
233  }
234  if( disallowed_display_names_.contains( display_name ))
235  {
236  setError( "Name in use. Display names must be unique." );
237  return false;
238  }
239  }
240  setError( "" );
241  return true;
242 }
243 
244 void NewObjectDialog::setError( const QString& error_text )
245 {
246  button_box_->button( QDialogButtonBox::Ok )->setToolTip( error_text );
247 }
248 
250 {
251  button_box_->button( QDialogButtonBox::Ok )->setEnabled( isValid() );
252 }
253 
255 {
256  if( isValid() )
257  {
260  {
262  }
263  QDialog::accept();
264  }
265 }
266 
267 } // rviz
268 
string package
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_
virtual QSize sizeHint() const
tree
Abstract superclass representing the ability to get a list of class IDs and the ability to get name...
Definition: factory.h:43
QDialogButtonBox * button_box_
void fillTree(QTreeWidget *tree)
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=0, QWidget *parent=0)
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 Apr 27 2019 02:33:41