select_frame_dialog.cpp
Go to the documentation of this file.
1 // *****************************************************************************
2 //
3 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
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 Southwest Research Institute® (SwRI®) nor the
14 // names of its contributors may be used to endorse or promote products
15 // derived from 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 <COPYRIGHT HOLDER> BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //
28 // *****************************************************************************
30 
31 #include <algorithm>
32 #include <set>
33 
34 #include <QListWidget>
35 #include <QLineEdit>
36 #include <QVBoxLayout>
37 #include <QHBoxLayout>
38 #include <QPushButton>
39 #include <QLabel>
40 #include <QTimerEvent>
41 
42 #include <tf/transform_listener.h>
43 
44 namespace mapviz
45 {
48  QWidget *parent)
49 {
50  SelectFrameDialog dialog(tf_listener, parent);
51  dialog.allowMultipleFrames(false);
52  if (dialog.exec() == QDialog::Accepted) {
53  return dialog.selectedFrame();
54  } else {
55  return "";
56  }
57 }
58 
59 std::vector<std::string> SelectFrameDialog::selectFrames(
61  QWidget *parent)
62 {
63  SelectFrameDialog dialog(tf_listener, parent);
64  dialog.allowMultipleFrames(true);
65  if (dialog.exec() == QDialog::Accepted) {
66  return dialog.selectedFrames();
67  } else {
68  return std::vector<std::string>();
69  }
70 }
71 
74  QWidget *parent)
75  :
76  tf_(tf_listener),
77  ok_button_(new QPushButton("&Ok")),
78  cancel_button_(new QPushButton("&Cancel")),
79  list_widget_(new QListWidget()),
80  name_filter_(new QLineEdit())
81 {
82  QHBoxLayout *filter_box = new QHBoxLayout();
83  filter_box->addWidget(new QLabel("Filter:"));
84  filter_box->addWidget(name_filter_);
85 
86  QHBoxLayout *button_box = new QHBoxLayout();
87  button_box->addStretch(1);
88  button_box->addWidget(cancel_button_);
89  button_box->addWidget(ok_button_);
90 
91  QVBoxLayout *vbox = new QVBoxLayout();
92  vbox->addWidget(list_widget_);
93  vbox->addLayout(filter_box);
94  vbox->addLayout(button_box);
95  setLayout(vbox);
96 
97  connect(ok_button_, SIGNAL(clicked(bool)),
98  this, SLOT(accept()));
99  connect(cancel_button_, SIGNAL(clicked(bool)),
100  this, SLOT(reject()));
101  connect(name_filter_, SIGNAL(textChanged(const QString &)),
102  this, SLOT(updateDisplayedFrames()));
103 
104  ok_button_->setDefault(true);
105 
106  allowMultipleFrames(false);
107  setWindowTitle("Select frames...");
108 
109  resize(600, 600);
110 
111  fetch_frames_timer_id_ = startTimer(1000);
112  fetchFrames();
113 }
114 
115 void SelectFrameDialog::timerEvent(QTimerEvent *event)
116 {
117  if (event->timerId() == fetch_frames_timer_id_) {
118  fetchFrames();
119  }
120 }
121 
122 void SelectFrameDialog::closeEvent(QCloseEvent *event)
123 {
124  // We don't need to keep making requests from the ROS master.
125  killTimer(fetch_frames_timer_id_);
126  QDialog::closeEvent(event);
127 }
128 
130  bool allow)
131 {
132  if (allow) {
133  list_widget_->setSelectionMode(QAbstractItemView::MultiSelection);
134  } else {
135  list_widget_->setSelectionMode(QAbstractItemView::SingleSelection);
136  }
137 }
138 
140 {
141  std::vector<std::string> selection = selectedFrames();
142  if (selection.empty()) {
143  return "";
144  } else {
145  return selection.front();
146  }
147 }
148 
149 std::vector<std::string> SelectFrameDialog::selectedFrames() const
150 {
151  QModelIndexList qt_selection = list_widget_->selectionModel()->selectedIndexes();
152 
153  std::vector<std::string> selection;
154  selection.resize(qt_selection.size());
155  for (int i = 0; i < qt_selection.size(); i++) {
156  if (!qt_selection[i].isValid()) {
157  continue;
158  }
159 
160  int row = qt_selection[i].row();
161  if (row < 0 || static_cast<size_t>(row) >= displayed_frames_.size()) {
162  continue;
163  }
164 
165  selection[i] = displayed_frames_[row];
166  }
167 
168  return selection;
169 }
170 
172 {
173  if (!tf_) { return; }
174 
175  known_frames_.clear();
176  tf_->getFrameStrings(known_frames_);
177  std::sort(known_frames_.begin(), known_frames_.end());
179 }
180 
181 std::vector<std::string> SelectFrameDialog::filterFrames(
182  const std::vector<std::string> &frames) const
183 {
184  QString frame_filter = name_filter_->text();
185  std::vector<std::string> filtered;
186 
187  for (size_t i = 0; i < frames.size(); i++) {
188  QString frame_name = QString::fromStdString(frames[i]);
189  if (!frame_filter.isEmpty() &&
190  !frame_name.contains(frame_filter, Qt::CaseInsensitive)) {
191  continue;
192  }
193 
194  filtered.push_back(frames[i]);
195  }
196 
197  return filtered;
198 }
199 
201 {
202  std::vector<std::string> next_displayed_frames = filterFrames(known_frames_);
203 
204  // It's a lot more work to keep track of the additions/removals like
205  // this compared to resetting the QListWidget's items each time, but
206  // it allows Qt to properly track the selection and current items
207  // across updates, which results in much less frustration for the user.
208 
209  std::set<std::string> prev_names;
210  prev_names.insert(displayed_frames_.begin(), displayed_frames_.end());
211 
212  std::set<std::string> next_names;
213  next_names.insert(next_displayed_frames.begin(), next_displayed_frames.end());
214 
215  std::set<std::string> added_names;
216  std::set_difference(next_names.begin(), next_names.end(),
217  prev_names.begin(), prev_names.end(),
218  std::inserter(added_names, added_names.end()));
219 
220  std::set<std::string> removed_names;
221  std::set_difference(prev_names.begin(), prev_names.end(),
222  next_names.begin(), next_names.end(),
223  std::inserter(removed_names, removed_names.end()));
224 
225  // Remove all the removed names
226  size_t removed = 0;
227  for (size_t i = 0; i < displayed_frames_.size(); i++) {
228  if (removed_names.count(displayed_frames_[i]) == 0) {
229  continue;
230  }
231 
232  QListWidgetItem *item = list_widget_->takeItem(i - removed);
233  delete item;
234  removed++;
235  }
236 
237  // Now we can add the new items.
238  for (size_t i = 0; i < next_displayed_frames.size(); i++) {
239  if (added_names.count(next_displayed_frames[i]) == 0) {
240  continue;
241  }
242 
243  list_widget_->insertItem(i, QString::fromStdString(next_displayed_frames[i]));
244  if (list_widget_->count() == 1) {
245  list_widget_->setCurrentRow(0);
246  }
247  }
248 
249  displayed_frames_.swap(next_displayed_frames);
250 }
251 } // namespace mapviz
SelectFrameDialog(boost::shared_ptr< tf::TransformListener > tf_listener, QWidget *parent=0)
boost::shared_ptr< tf::TransformListener > tf_
static std::string selectFrame(boost::shared_ptr< tf::TransformListener > tf_listener, QWidget *parent=0)
std::vector< std::string > selectedFrames() const
std::vector< std::string > displayed_frames_
std::vector< std::string > filterFrames(const std::vector< std::string > &) const
std::string selectedFrame() const
static std::vector< std::string > selectFrames(boost::shared_ptr< tf::TransformListener > tf_listener, QWidget *parent=0)
std::vector< std::string > known_frames_


mapviz
Author(s): Marc Alban
autogenerated on Thu Jun 6 2019 19:25:08