range_list_selection.cpp
Go to the documentation of this file.
2 
3 namespace ram_qt_guis
4 {
6  const QString help_string,
7  const unsigned min_val,
8  const unsigned max_val,
9  std::vector<unsigned> locked) :
10  layout_(layout),
11  help_string_(help_string),
12  min_val_(min_val),
13  max_val_(max_val)
14 {
15  setObjectName("Modify range list selection");
16  layout_->addWidget(new QLabel(help_string_));
17 
18  QTabWidget *tab_widget = new QTabWidget;
19  layout_->addWidget(tab_widget);
20 
21  QHBoxLayout *all_none_buttons = new QHBoxLayout;
22  all_button_ = new QPushButton("All");
23  none_button_ = new QPushButton("None");
24  all_none_buttons->addWidget(all_button_);
25  all_none_buttons->addWidget(none_button_);
26  layout_->addLayout(all_none_buttons);
27 
28  QWidget *range_select = new QWidget;
29  QVBoxLayout *range_select_layout = new QVBoxLayout(range_select);
30  range_select->setLayout(range_select_layout);
31 
32  range_select_layout->addWidget(new QLabel("Select first and last:"));
33  min_box_ = new QSpinBox;
34  min_box_->setRange(min_val_, max_val_);
35  max_box_ = new QSpinBox;
36  max_box_->setRange(min_val_, max_val_);
37  connect(min_box_, SIGNAL(valueChanged(int)), this, SLOT(tweakRangeMin()));
38  connect(max_box_, SIGNAL(valueChanged(int)), this, SLOT(tweakRangeMax()));
39 
40  QHBoxLayout *min_box_layout = new QHBoxLayout;
41  min_box_layout->addWidget(new QLabel("First"));
42  min_box_layout->addWidget(min_box_);
43  QHBoxLayout *max_box_layout = new QHBoxLayout;
44  max_box_layout->addWidget(new QLabel("Last"));
45  max_box_layout->addWidget(max_box_);
46 
47  range_select_layout->addLayout(min_box_layout);
48  range_select_layout->addLayout(max_box_layout);
49 
50  QHBoxLayout *add_remove_invert_buttons = new QHBoxLayout;
51  add_button_ = new QPushButton("Add");
52  remove_button_ = new QPushButton("Remove");
53  invert_button_ = new QPushButton("Invert");
54  add_remove_invert_buttons->addWidget(add_button_);
55  add_remove_invert_buttons->addWidget(remove_button_);
56  add_remove_invert_buttons->addWidget(invert_button_);
57 
58  range_select_layout->addLayout(add_remove_invert_buttons);
59  range_select_layout->addStretch(1);
60 
61  QWidget *scroll_widget = new QWidget;
62  QVBoxLayout *checkboxes_layout = new QVBoxLayout;
63  scroll_widget->setLayout(checkboxes_layout);
64  QScrollArea *scroll_area = new QScrollArea;
65  scroll_area->setWidget(scroll_widget);
66  scroll_area->setWidgetResizable(true);
67  scroll_area->setFrameShape(QFrame::NoFrame);
68 
69  tab_widget->addTab(scroll_area, "Check-boxes");
70  tab_widget->addTab(range_select, "Range select");
71 
72  for (unsigned i(min_val_); i <= max_val_; ++i)
73  {
74  QCheckBox *checkbox = new QCheckBox;
75  checkbox->setText(QString::fromStdString(std::to_string(i)));
76  checkboxes.push_back(checkbox);
77  }
78 
79  for (auto checkbox : checkboxes)
80  {
81  checkboxes_layout->addWidget(checkbox);
82  connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(updateSelectionFromTicks()));
83  }
84 
85  selection_.resize(checkboxes.size());
86 
87  if (!locked.empty())
88  {
89  std::sort(locked.begin(), locked.end());
90  if (locked.back() < checkboxes.size())
91  {
92  for (unsigned i(0); i < locked.size(); ++i)
93  {
94  checkboxes[locked[i]]->setEnabled(false);
95  checkboxes[locked[i]]->setChecked(true);
96  }
97  }
99  }
100 
101  button_box_ = new QDialogButtonBox(QDialogButtonBox::Ok
102  | QDialogButtonBox::Cancel);
103  layout_->addStretch(1);
104  layout_->addWidget(button_box_);
105 
106  connect(add_button_, SIGNAL(clicked()), this, SLOT(addButton()));
107  connect(remove_button_, SIGNAL(clicked()), this, SLOT(removeButton()));
108  connect(invert_button_, SIGNAL(clicked()), this, SLOT(invertButton()));
109  connect(all_button_, SIGNAL(clicked()), this, SLOT(selectAll()));
110  connect(none_button_, SIGNAL(clicked()), this, SLOT(selectNone()));
111 }
112 
114 {
115 }
116 
117 std::vector<unsigned>
119 {
120  std::lock_guard<std::recursive_mutex> lock(selection_mutex_);
121  std::vector<unsigned> selection;
122 
123  for (unsigned i(0); i < selection_.size(); ++i)
124  if (selection_[i] == true)
125  selection.push_back(i);
126 
127  return selection;
128 }
129 
130 void
132 {
133  std::lock_guard<std::recursive_mutex> lock(selection_mutex_);
134 
135  Q_EMIT layout_->parentWidget()->setEnabled(false);
136 
137  // Block signals from check boxes to prevent nested infinite calls
138  std::vector<QSignalBlocker> blockers;
139  for (auto checkbox : checkboxes)
140  blockers.push_back(QSignalBlocker(checkbox));
141 
142  for (unsigned i(0); i < checkboxes.size(); ++i)
143  checkboxes[i]->setChecked(selection_[i]);
144 
145  Q_EMIT layout_->parentWidget()->setEnabled(true);
146  Q_EMIT selectionChanged(getSelection());
147 }
148 
150 {
151  if (min_box_->value() > max_box_->value())
152  max_box_->setValue(min_box_->value());
153 }
154 
156 {
157  if (min_box_->value() > max_box_->value())
158  min_box_->setValue(max_box_->value());
159 }
160 
161 void
163 {
164  std::lock_guard<std::recursive_mutex> lock(selection_mutex_);
165 
166  Q_EMIT layout_->parentWidget()->setEnabled(false);
167 
168  for (unsigned i(0); i < selection_.size(); ++i)
169  selection_[i] = checkboxes[i]->isChecked();
170 
171  Q_EMIT layout_->parentWidget()->setEnabled(true);
172  Q_EMIT selectionChanged(getSelection());
173 }
174 
175 void
177 {
178  {
179  std::lock_guard<std::recursive_mutex> lock(selection_mutex_);
180 
181  if (!checkRange())
182  return;
183 
184  for (unsigned i(min_box_->value()); i <= (unsigned)max_box_->value(); ++i)
185  selection_[i] = true;
186  }
187 
189 }
190 
191 void
193 {
194  {
195  std::lock_guard<std::recursive_mutex> lock(selection_mutex_);
196 
197  if (!checkRange())
198  return;
199 
200  for (unsigned i(min_box_->value()); i <= (unsigned)max_box_->value(); ++i)
201  if (checkboxes[i]->isEnabled())
202  selection_[i] = false;
203  }
204 
206 }
207 
208 void
210 {
211  {
212  std::lock_guard<std::recursive_mutex> lock(selection_mutex_);
213 
214  if (!checkRange())
215  return;
216 
217  for (unsigned i(min_box_->value()); i <= (unsigned)max_box_->value(); ++i)
218  if (checkboxes[i]->isEnabled())
219  selection_[i] = !selection_[i];
220  }
221 
223 }
224 
225 void
227 {
228  {
229  std::lock_guard<std::recursive_mutex> lock(selection_mutex_);
230  std::fill(selection_.begin(), selection_.end(), true);
231  }
233 }
234 
235 void
237 {
238  {
239  std::lock_guard<std::recursive_mutex> lock(selection_mutex_);
240  std::fill(selection_.begin(), selection_.end(), false);
241  }
243 }
244 
245 bool
247 {
248  if (min_box_->value() > max_box_->value())
249  {
250  QMessageBox::warning(this, ("Wrong range"),
251  ("Wrong selection, the \"last\" value must be superior or equal to the \"first\" value."),
252  QMessageBox::Ok);
253  return false;
254  }
255 
256  return true;
257 }
258 
259 }
ModifyRangeListSelection(QVBoxLayout *layout, const QString help_string="", const unsigned min_val=0, const unsigned max_val=0, std::vector< unsigned > locked=std::vector< unsigned >())
void selectionChanged(std::vector< unsigned > selection)
ram_display::UpdateSelection::Request selection


ram_qt_guis
Author(s): Victor Lamoine - Institut Maupertuis
autogenerated on Mon Jun 10 2019 14:50:11