MultiplotConfigWidget.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2015 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <QFileDialog>
20 #include <QFileInfo>
21 #include <QMessageBox>
22 #include <QSettings>
23 
24 #include <ros/console.h>
25 #include <ros/package.h>
26 
28 
29 #include <ui_MultiplotConfigWidget.h>
30 
32 
33 namespace rqt_multiplot {
34 
35 /*****************************************************************************/
36 /* Constructors and Destructor */
37 /*****************************************************************************/
38 
40  maxHistoryLength) :
41  QWidget(parent),
42  ui_(new Ui::MultiplotConfigWidget()),
43  config_(0),
44  currentConfigModified_(false),
45  maxHistoryLength_(maxHistoryLength) {
46  ui_->setupUi(this);
47 
48  ui_->pushButtonClearHistory->setIcon(
49  QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
50  append("/resource/16x16/clear_history.png"))));
51  ui_->pushButtonNew->setIcon(
52  QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
53  append("/resource/16x16/add.png"))));
54  ui_->pushButtonOpen->setIcon(
55  QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
56  append("/resource/16x16/open.png"))));
57  ui_->pushButtonSave->setIcon(
58  QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
59  append("/resource/16x16/save.png"))));
60  ui_->pushButtonSaveAs->setIcon(
61  QIcon(QString::fromStdString(ros::package::getPath("rqt_multiplot").
62  append("/resource/16x16/save_as.png"))));
63 
64  ui_->pushButtonClearHistory->setEnabled(false);
65  ui_->pushButtonSave->setEnabled(false);
66 
67  connect(ui_->configComboBox, SIGNAL(editTextChanged(const QString&)),
68  this, SLOT(configComboBoxEditTextChanged(const QString&)));
69  connect(ui_->configComboBox, SIGNAL(currentUrlChanged(const QString&)),
70  this, SLOT(configComboBoxCurrentUrlChanged(const QString&)));
71 
72  connect(ui_->pushButtonClearHistory, SIGNAL(clicked()), this,
74  connect(ui_->pushButtonNew, SIGNAL(clicked()), this,
75  SLOT(pushButtonNewClicked()));
76  connect(ui_->pushButtonOpen, SIGNAL(clicked()), this,
77  SLOT(pushButtonOpenClicked()));
78  connect(ui_->pushButtonSave, SIGNAL(clicked()), this,
79  SLOT(pushButtonSaveClicked()));
80  connect(ui_->pushButtonSaveAs, SIGNAL(clicked()), this,
81  SLOT(pushButtonSaveAsClicked()));
82 }
83 
85  delete ui_;
86 }
87 
88 /*****************************************************************************/
89 /* Accessors */
90 /*****************************************************************************/
91 
93  if (config != config_) {
94  if (config_)
95  disconnect(config_, SIGNAL(changed()), this, SLOT(configChanged()));
96 
97  config_ = config;
98 
99  if (config)
100  connect(config, SIGNAL(changed()), this, SLOT(configChanged()));
101  }
102 }
103 
105  return config_;
106 }
107 
108 void MultiplotConfigWidget::setCurrentConfigUrl(const QString& url, bool
109  updateHistory) {
110  if (url != currentConfigUrl_) {
111  currentConfigUrl_ = url;
112 
113  if (updateHistory)
115 
116  ui_->configComboBox->setCurrentUrl(url);
117 
118  emit currentConfigUrlChanged(url);
119  }
120 }
121 
123  return ui_->configComboBox->getCurrentUrl();
124 }
125 
127  if (modified != currentConfigModified_) {
128  currentConfigModified_ = modified;
129 
130  ui_->pushButtonSave->setEnabled(!currentConfigUrl_.isEmpty() &&
131  (ui_->configComboBox->getCurrentUrl() == currentConfigUrl_) &&
132  modified);
133 
134  emit currentConfigModifiedChanged(modified);
135  }
136  return true;
137 }
138 
140  return currentConfigModified_;
141 }
142 
144  if (length != maxHistoryLength_) {
145  maxHistoryLength_ = length;
146 
147  while (ui_->configComboBox->count() > length)
148  ui_->configComboBox->removeItem(ui_->configComboBox->count()-1);
149  }
150 }
151 
153  return maxHistoryLength_;
154 }
155 
156 void MultiplotConfigWidget::setConfigUrlHistory(const QStringList& history) {
157  ui_->configComboBox->clear();
158 
159  for (size_t i = 0; (i < history.count()) && (i < maxHistoryLength_); ++i)
160  ui_->configComboBox->addItem(history[i]);
161 }
162 
164  QStringList history;
165 
166  for (size_t i = 0; i < ui_->configComboBox->count(); ++i)
167  history.append(ui_->configComboBox->itemText(i));
168 
169  return history;
170 }
171 
172 bool MultiplotConfigWidget::isFile(const QString& url) const {
173  if (!url.isEmpty()) {
174  UrlItemModel* model = ui_->configComboBox->getCompleter()->getModel();
175  QString filePath = model->getFilePath(url);
176 
177  if (!filePath.isEmpty()) {
178  QFileInfo fileInfo(filePath);
179 
180  return fileInfo.isFile();
181  }
182  }
183 
184  return false;
185 }
186 
187 /*****************************************************************************/
188 /* Methods */
189 /*****************************************************************************/
190 
191 bool MultiplotConfigWidget::loadConfig(const QString& url) {
192  if (config_) {
193  UrlItemModel* model = ui_->configComboBox->getCompleter()->getModel();
194  QString filePath = model->getFilePath(url);
195 
196  if (!filePath.isEmpty()) {
197  QFileInfo fileInfo(filePath);
198 
199  if (fileInfo.isReadable()) {
200  QSettings settings(filePath, XmlSettings::format);
201 
202  if (settings.status() == QSettings::NoError) {
203  settings.beginGroup("rqt_multiplot");
204  config_->load(settings);
205  settings.endGroup();
206 
207  setCurrentConfigUrl(url);
209 
210  ROS_INFO_STREAM("Loaded configuration from [" <<
211  url.toStdString() << "]");
212 
213  return true;
214  }
215  }
216  }
217  }
218 
219  ROS_ERROR_STREAM("Failed to load configuration from [" <<
220  url.toStdString() << "]");
221 
222  return false;
223 }
224 
226  if (!currentConfigUrl_.isEmpty())
228 
229  return false;
230 }
231 
232 bool MultiplotConfigWidget::saveConfig(const QString& url) {
233  if (config_) {
234  UrlItemModel* model = ui_->configComboBox->getCompleter()->getModel();
235  QString filePath = model->getFilePath(url);
236 
237  if (!filePath.isEmpty()) {
238  QSettings settings(filePath, XmlSettings::format);
239 
240  if (settings.isWritable()) {
241  settings.clear();
242 
243  settings.beginGroup("rqt_multiplot");
244  config_->save(settings);
245  settings.endGroup();
246 
247  settings.sync();
248 
249  if (settings.status() == QSettings::NoError) {
250  setCurrentConfigUrl(url);
252 
253  ROS_INFO_STREAM("Saved configuration to [" <<
254  url.toStdString() << "]");
255 
256  return true;
257  }
258  }
259  }
260  }
261 
262  ROS_ERROR_STREAM("Failed to save configuration to [" <<
263  url.toStdString() << "]");
264 
265  return false;
266 }
267 
269  if (config_) {
270  config_->reset();
271 
272  setCurrentConfigUrl(QString(), false);
274  }
275 }
276 
279  QMessageBox messageBox;
280  QMessageBox::StandardButtons buttons = QMessageBox::Save |
281  QMessageBox::Discard;
282 
283  if (canCancel)
284  buttons |= QMessageBox::Cancel;
285 
286  messageBox.setText("The configuration has been modified.");
287  messageBox.setInformativeText("Do you want to save your changes?");
288  messageBox.setStandardButtons(buttons);
289  messageBox.setDefaultButton(QMessageBox::Save);
290 
291  switch (messageBox.exec()) {
292  case QMessageBox::Save:
293  if (currentConfigUrl_.isEmpty()) {
294  QFileDialog dialog(this, "Save Configuration", QDir::homePath(),
295  "Multiplot configurations (*.xml)");
296 
297  dialog.setAcceptMode(QFileDialog::AcceptSave);
298  dialog.setFileMode(QFileDialog::AnyFile);
299  dialog.selectFile("rqt_multiplot.xml");
300 
301  if (dialog.exec() == QDialog::Accepted)
302  return saveConfig("file://"+dialog.selectedFiles().first());
303  else
304  return false;
305  }
306  else
307  return saveCurrentConfig();
308  case QMessageBox::Discard:
309  return true;
310  case QMessageBox::Cancel:
311  return false;
312  default:
313  return false;
314  }
315  }
316 
317  return true;
318 }
319 
321  if (!url.isEmpty()) {
322  int index = ui_->configComboBox->findText(url);
323 
324  ui_->configComboBox->blockSignals(true);
325 
326  if (index < 0) {
327  while (ui_->configComboBox->count()+1 > maxHistoryLength_)
328  ui_->configComboBox->removeItem(ui_->configComboBox->count()-1);
329  }
330  else
331  ui_->configComboBox->removeItem(index);
332 
333  ui_->configComboBox->insertItem(0, url);
334 
335  ui_->configComboBox->blockSignals(false);
336 
337  ui_->pushButtonClearHistory->setEnabled(true);
338  }
339 }
340 
342  ui_->configComboBox->blockSignals(true);
343 
344  QString url = ui_->configComboBox->currentText();
345 
346  while (ui_->configComboBox->count())
347  ui_->configComboBox->removeItem(ui_->configComboBox->count()-1);
348 
349  if (!url.isEmpty())
350  ui_->configComboBox->insertItem(0, url);
351 
352  ui_->configComboBox->blockSignals(false);
353 
354  ui_->pushButtonClearHistory->setEnabled(false);
355 }
356 
357 /*****************************************************************************/
358 /* Slots */
359 /*****************************************************************************/
360 
363 }
364 
366  QString& text) {
367  if (!currentConfigUrl_.isEmpty()) {
368  if (text != currentConfigUrl_)
369  ui_->pushButtonSave->setEnabled(!isFile(text));
370  else
371  ui_->pushButtonSave->setEnabled(currentConfigModified_);
372  }
373  else
374  ui_->pushButtonSave->setEnabled(false);
375 }
376 
378  QString& url) {
379  if (url != currentConfigUrl_) {
380  if (!isFile(url)) {
381  if (!currentConfigUrl_.isEmpty()) {
382  setCurrentConfigUrl(url, false);
384 
385  ui_->pushButtonSave->setEnabled(true);
386  }
387  else
388  ui_->pushButtonSave->setEnabled(false);
389  }
390  else {
391  loadConfig(url);
392  ui_->pushButtonSave->setEnabled(false);
393  }
394  }
395 }
396 
398  QMessageBox messageBox;
399 
400  messageBox.setText("The configuration file history will be cleared.");
401  messageBox.setInformativeText("Do you want to proceed?");
402  messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
403  messageBox.setDefaultButton(QMessageBox::No);
404 
405  if (messageBox.exec() == QMessageBox::Yes)
407 }
408 
410  if (confirmSave())
411  resetConfig();
412 }
413 
415  if (confirmSave()) {
416  QFileDialog dialog(this, "Open Configuration", QDir::homePath(),
417  "Multiplot configurations (*.xml)");
418 
419  dialog.setAcceptMode(QFileDialog::AcceptOpen);
420  dialog.setFileMode(QFileDialog::ExistingFile);
421 
422  if (dialog.exec() == QDialog::Accepted)
423  loadConfig("file://"+dialog.selectedFiles().first());
424  }
425 }
426 
428  if (currentConfigUrl_ == ui_->configComboBox->getCurrentUrl())
430  else
431  saveConfig(ui_->configComboBox->getCurrentUrl());
432 }
433 
435  QFileDialog dialog(this, "Save Configuration", QDir::homePath(),
436  "Multiplot configurations (*.xml)");
437 
438  dialog.setAcceptMode(QFileDialog::AcceptSave);
439  dialog.setFileMode(QFileDialog::AnyFile);
440  dialog.selectFile("rqt_multiplot.xml");
441 
442  if (dialog.exec() == QDialog::Accepted)
443  saveConfig("file://"+dialog.selectedFiles().first());
444 }
445 
446 }
MultiplotConfigWidget(QWidget *parent=0, size_t maxHistoryLength=10)
bool isFile(const QString &url) const
void load(QSettings &settings)
void save(QSettings &settings) const
void setCurrentConfigUrl(const QString &url, bool updateHistory=true)
QString getFilePath(const QModelIndex &index) const
void currentConfigUrlChanged(const QString &url)
void configComboBoxEditTextChanged(const QString &text)
void configComboBoxCurrentUrlChanged(const QString &url)
static const QSettings::Format format
Definition: XmlSettings.h:28
ROSLIB_DECL std::string getPath(const std::string &package_name)
#define ROS_INFO_STREAM(args)
void setConfigUrlHistory(const QStringList &history)
ROSCPP_DECL std::string append(const std::string &left, const std::string &right)
void currentConfigModifiedChanged(bool modified)
#define ROS_ERROR_STREAM(args)
void setConfig(MultiplotConfig *config)


rqt_multiplot_plugin
Author(s): Ralf Kaestner
autogenerated on Fri Jan 15 2021 03:47:53