displays_panel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, 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 <QTimer>
31 #include <QHBoxLayout>
32 #include <QVBoxLayout>
33 #include <QPushButton>
34 #include <QInputDialog>
35 #include <QApplication>
36 
37 #include <boost/bind.hpp>
38 
39 #include "rviz/display_factory.h"
40 #include "rviz/display.h"
46 
47 #include "rviz/displays_panel.h"
48 
49 namespace rviz
50 {
51 
52 DisplaysPanel::DisplaysPanel( QWidget* parent )
53  : Panel( parent )
54 {
57 
58  QPushButton* add_button = new QPushButton( "Add" );
59  add_button->setShortcut( QKeySequence( QString( "Ctrl+N" )));
60  add_button->setToolTip( "Add a new display, Ctrl+N" );
61  duplicate_button_ = new QPushButton( "Duplicate" );
62  duplicate_button_->setShortcut( QKeySequence( QString( "Ctrl+D" )));
63  duplicate_button_->setToolTip( "Duplicate a display, Ctrl+D" );
64  duplicate_button_->setEnabled( false );
65  remove_button_ = new QPushButton( "Remove" );
66  remove_button_->setShortcut( QKeySequence( QString( "Ctrl+X" )));
67  remove_button_->setToolTip( "Remove displays, Ctrl+X" );
68  remove_button_->setEnabled( false );
69  rename_button_ = new QPushButton( "Rename" );
70  rename_button_->setShortcut( QKeySequence( QString( "Ctrl+R" )));
71  rename_button_->setToolTip( "Rename a display, Ctrl+R" );
72  rename_button_->setEnabled( false );
73 
74  QHBoxLayout* button_layout = new QHBoxLayout;
75  button_layout->addWidget( add_button );
76  button_layout->addWidget( duplicate_button_ );
77  button_layout->addWidget( remove_button_ );
78  button_layout->addWidget( rename_button_ );
79  button_layout->setContentsMargins( 2, 0, 2, 2 );
80 
81  QVBoxLayout* layout = new QVBoxLayout;
82  layout->setContentsMargins( 0, 0, 0, 2 );
83  layout->addWidget( tree_with_help_ );
84  layout->addLayout( button_layout );
85 
86  setLayout( layout );
87 
88  connect( add_button, SIGNAL( clicked( bool )), this, SLOT( onNewDisplay() ));
89  connect( duplicate_button_, SIGNAL( clicked( bool )), this, SLOT( onDuplicateDisplay() ));
90  connect( remove_button_, SIGNAL( clicked( bool )), this, SLOT( onDeleteDisplay() ));
91  connect( rename_button_, SIGNAL( clicked( bool )), this, SLOT( onRenameDisplay() ));
92  connect( property_grid_, SIGNAL( selectionHasChanged() ), this, SLOT( onSelectionChanged() ));
93 }
94 
96 {
97 }
98 
100 {
102 }
103 
105 {
106  QString lookup_name;
107  QString display_name;
108  QString topic;
109  QString datatype;
110 
111  QStringList empty;
112 
113  QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
115  "Display",
116  empty, empty,
117  &lookup_name,
118  &display_name,
119  &topic,
120  &datatype );
121  QApplication::restoreOverrideCursor();
122 
124  if( dialog->exec() == QDialog::Accepted )
125  {
126  Display *disp = vis_manager_->createDisplay( lookup_name, display_name, true );
127  if ( !topic.isEmpty() && !datatype.isEmpty() )
128  {
129  disp->setTopic( topic, datatype );
130  }
131  }
133  activateWindow(); // Force keyboard focus back on main window.
134  delete dialog;
135 }
136 
138 {
139  QList<Display*> displays_to_duplicate = property_grid_->getSelectedObjects<Display>();
140 
141  QList<Display*> duplicated_displays;
142 
143  for( int i = 0; i < displays_to_duplicate.size(); i++ )
144  {
145  // initialize display
146  QString lookup_name = displays_to_duplicate[ i ]->getClassId();
147  QString display_name = displays_to_duplicate[ i ]->getName();
148  Display *disp = vis_manager_->createDisplay( lookup_name, display_name, true );
149  // duplicate config
150  Config config;
151  displays_to_duplicate[ i ]->save(config);
152  disp->load(config);
153  duplicated_displays.push_back(disp);
154  }
155  // make sure the newly duplicated displays are selected.
156  if (duplicated_displays.size() > 0) {
157  QModelIndex first = property_grid_->getModel()->indexOf(duplicated_displays.front());
158  QModelIndex last = property_grid_->getModel()->indexOf(duplicated_displays.back());
159  QItemSelection selection(first, last);
160  property_grid_->selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);
161  }
163  activateWindow(); // Force keyboard focus back on main window.
164 }
165 
167 {
168  QList<Display*> displays_to_delete = property_grid_->getSelectedObjects<Display>();
169 
170  QModelIndex new_selected;
171 
172  for( int i = 0; i < displays_to_delete.size(); i++ )
173  {
174  if (i == 0) {
175  QModelIndex first = property_grid_->getModel()->indexOf(displays_to_delete[i]);
176  // This is safe because the first few rows cannot be deleted (they aren't "displays").
177  new_selected = first.sibling(first.row() - 1, first.column());
178  }
179  // Displays can emit signals from other threads with self pointers. We're
180  // freeing the display now, so ensure no one is listening to those signals.
181  displays_to_delete[ i ]->disconnect();
182  // Delete display later in case there are pending signals to it.
183  displays_to_delete[ i ]->deleteLater();
184  }
185 
186  QItemSelection selection(new_selected, new_selected);
187  property_grid_->selectionModel()->select(selection, QItemSelectionModel::ClearAndSelect);
188 
190 }
191 
193 {
194  QList<Display*> displays = property_grid_->getSelectedObjects<Display>();
195 
196  int num_displays_selected = displays.size();
197 
198  duplicate_button_->setEnabled( num_displays_selected > 0 );
199  remove_button_->setEnabled( num_displays_selected > 0 );
200  rename_button_->setEnabled( num_displays_selected == 1 );
201 }
202 
204 {
205  QList<Display*> displays = property_grid_->getSelectedObjects<Display>();
206  if( displays.size() == 0 )
207  {
208  return;
209  }
210  Display* display_to_rename = displays[ 0 ];
211 
212  if( !display_to_rename )
213  {
214  return;
215  }
216 
217  QString old_name = display_to_rename->getName();
218  QString new_name = QInputDialog::getText( this, "Rename Display", "New Name?", QLineEdit::Normal, old_name );
219 
220  if( new_name.isEmpty() || new_name == old_name )
221  {
222  return;
223  }
224 
225  display_to_rename->setName( new_name );
226 }
227 
228 void DisplaysPanel::save( Config config ) const
229 {
230  Panel::save( config );
231  tree_with_help_->save( config );
232 }
233 
234 void DisplaysPanel::load( const Config& config )
235 {
236  Panel::load( config );
237  tree_with_help_->load( config );
238 }
239 
240 } // namespace rviz
void setModel(PropertyTreeModel *model)
Set the data model this widget should view.
QPushButton * duplicate_button_
VisualizationManager * vis_manager_
Definition: panel.h:92
PropertyTreeWidget * getTree()
QList< Type * > getSelectedObjects()
Return the list of objects of a given type which are currently selected.
PropertyTreeModel * getModel() const
virtual void save(Config config) const
Write state to the given Config object.
Display * createDisplay(const QString &class_lookup_name, const QString &name, bool enabled)
Create and add a display to this panel, by class lookup name.
config
void onDeleteDisplay()
Called when the "Remove" button is pressed.
virtual void setTopic(const QString &topic, const QString &datatype)
Set the ROS topic to listen to for this display.
Definition: display.h:116
void onNewDisplay()
Called when the "Add" button is pressed.
DisplaysPanel(QWidget *parent=0)
virtual void load(const Config &config)
Read state from the given Config.
Configuration data storage class.
Definition: config.h:125
QModelIndex indexOf(Property *property) const
void save(Config config) const
Write state to the given Config.
virtual QString getClassId() const
Return the class identifier which was used to create this instance. This version just returns whateve...
Definition: display.h:85
QPushButton * remove_button_
void onRenameDisplay()
Called when the "Rename" button is pressed.
PropertyTreeWithHelp * tree_with_help_
virtual DisplayFactory * getDisplayFactory() const
Return a factory for creating Display subclasses based on a class id string.
PropertyTreeModel * getDisplayTreeModel() const
virtual void load(const Config &config)
Load the settings for this display from the given Config node, which must be a map.
Definition: display.cpp:242
QPushButton * rename_button_
void setName(const QString &name)
Overridden from Property to set associated widget title to the new name.
Definition: display.cpp:410
PropertyTreeWidget * property_grid_
void load(const Config &config)
Read state from the given Config.
void notifyConfigChanged()
Notify this VisualizationManager that something about its display configuration has changed...
virtual void save(Config config) const
Override to save configuration data. This version saves the name and class ID of the panel...
Definition: panel.cpp:52
virtual void load(const Config &config)
Override to load configuration data. This version loads the name of the panel.
Definition: panel.cpp:58
void onDuplicateDisplay()
Called when the "copy" button is pressed.
virtual void onInitialize()
void startUpdate()
Start timers. Creates and starts the update and idle timers, both set to 30Hz (33ms).
virtual QString getName() const
Return the name of this Property as a QString.
Definition: property.cpp:159


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat Apr 27 2019 02:33:41