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 }
137 
139  return currentConfigModified_;
140 }
141 
143  if (length != maxHistoryLength_) {
144  maxHistoryLength_ = length;
145 
146  while (ui_->configComboBox->count() > length)
147  ui_->configComboBox->removeItem(ui_->configComboBox->count()-1);
148  }
149 }
150 
152  return maxHistoryLength_;
153 }
154 
155 void MultiplotConfigWidget::setConfigUrlHistory(const QStringList& history) {
156  ui_->configComboBox->clear();
157 
158  for (size_t i = 0; (i < history.count()) && (i < maxHistoryLength_); ++i)
159  ui_->configComboBox->addItem(history[i]);
160 }
161 
163  QStringList history;
164 
165  for (size_t i = 0; i < ui_->configComboBox->count(); ++i)
166  history.append(ui_->configComboBox->itemText(i));
167 
168  return history;
169 }
170 
171 bool MultiplotConfigWidget::isFile(const QString& url) const {
172  if (!url.isEmpty()) {
173  UrlItemModel* model = ui_->configComboBox->getCompleter()->getModel();
174  QString filePath = model->getFilePath(url);
175 
176  if (!filePath.isEmpty()) {
177  QFileInfo fileInfo(filePath);
178 
179  return fileInfo.isFile();
180  }
181  }
182 
183  return false;
184 }
185 
186 /*****************************************************************************/
187 /* Methods */
188 /*****************************************************************************/
189 
190 bool MultiplotConfigWidget::loadConfig(const QString& url) {
191  if (config_) {
192  UrlItemModel* model = ui_->configComboBox->getCompleter()->getModel();
193  QString filePath = model->getFilePath(url);
194 
195  if (!filePath.isEmpty()) {
196  QFileInfo fileInfo(filePath);
197 
198  if (fileInfo.isReadable()) {
199  QSettings settings(filePath, XmlSettings::format);
200 
201  if (settings.status() == QSettings::NoError) {
202  settings.beginGroup("rqt_multiplot");
203  config_->load(settings);
204  settings.endGroup();
205 
206  setCurrentConfigUrl(url);
208 
209  ROS_INFO_STREAM("Loaded configuration from [" <<
210  url.toStdString() << "]");
211 
212  return true;
213  }
214  }
215  }
216  }
217 
218  ROS_ERROR_STREAM("Failed to load configuration from [" <<
219  url.toStdString() << "]");
220 
221  return false;
222 }
223 
225  if (!currentConfigUrl_.isEmpty())
227 
228  return false;
229 }
230 
231 bool MultiplotConfigWidget::saveConfig(const QString& url) {
232  if (config_) {
233  UrlItemModel* model = ui_->configComboBox->getCompleter()->getModel();
234  QString filePath = model->getFilePath(url);
235 
236  if (!filePath.isEmpty()) {
237  QSettings settings(filePath, XmlSettings::format);
238 
239  if (settings.isWritable()) {
240  settings.clear();
241 
242  settings.beginGroup("rqt_multiplot");
243  config_->save(settings);
244  settings.endGroup();
245 
246  settings.sync();
247 
248  if (settings.status() == QSettings::NoError) {
249  setCurrentConfigUrl(url);
251 
252  ROS_INFO_STREAM("Saved configuration to [" <<
253  url.toStdString() << "]");
254 
255  return true;
256  }
257  }
258  }
259  }
260 
261  ROS_ERROR_STREAM("Failed to save configuration to [" <<
262  url.toStdString() << "]");
263 
264  return false;
265 }
266 
268  if (config_) {
269  config_->reset();
270 
271  setCurrentConfigUrl(QString(), false);
273  }
274 }
275 
278  QMessageBox messageBox;
279  QMessageBox::StandardButtons buttons = QMessageBox::Save |
280  QMessageBox::Discard;
281 
282  if (canCancel)
283  buttons |= QMessageBox::Cancel;
284 
285  messageBox.setText("The configuration has been modified.");
286  messageBox.setInformativeText("Do you want to save your changes?");
287  messageBox.setStandardButtons(buttons);
288  messageBox.setDefaultButton(QMessageBox::Save);
289 
290  switch (messageBox.exec()) {
291  case QMessageBox::Save:
292  if (currentConfigUrl_.isEmpty()) {
293  QFileDialog dialog(this, "Save Configuration", QDir::homePath(),
294  "Multiplot configurations (*.xml)");
295 
296  dialog.setAcceptMode(QFileDialog::AcceptSave);
297  dialog.setFileMode(QFileDialog::AnyFile);
298  dialog.selectFile("rqt_multiplot.xml");
299 
300  if (dialog.exec() == QDialog::Accepted)
301  return saveConfig("file://"+dialog.selectedFiles().first());
302  else
303  return false;
304  }
305  else
306  return saveCurrentConfig();
307  case QMessageBox::Discard:
308  return true;
309  case QMessageBox::Cancel:
310  return false;
311  default:
312  return false;
313  }
314  }
315 
316  return true;
317 }
318 
320  if (!url.isEmpty()) {
321  int index = ui_->configComboBox->findText(url);
322 
323  ui_->configComboBox->blockSignals(true);
324 
325  if (index < 0) {
326  while (ui_->configComboBox->count()+1 > maxHistoryLength_)
327  ui_->configComboBox->removeItem(ui_->configComboBox->count()-1);
328  }
329  else
330  ui_->configComboBox->removeItem(index);
331 
332  ui_->configComboBox->insertItem(0, url);
333 
334  ui_->configComboBox->blockSignals(false);
335 
336  ui_->pushButtonClearHistory->setEnabled(true);
337  }
338 }
339 
341  ui_->configComboBox->blockSignals(true);
342 
343  QString url = ui_->configComboBox->currentText();
344 
345  while (ui_->configComboBox->count())
346  ui_->configComboBox->removeItem(ui_->configComboBox->count()-1);
347 
348  if (!url.isEmpty())
349  ui_->configComboBox->insertItem(0, url);
350 
351  ui_->configComboBox->blockSignals(false);
352 
353  ui_->pushButtonClearHistory->setEnabled(false);
354 }
355 
356 /*****************************************************************************/
357 /* Slots */
358 /*****************************************************************************/
359 
362 }
363 
365  QString& text) {
366  if (!currentConfigUrl_.isEmpty()) {
367  if (text != currentConfigUrl_)
368  ui_->pushButtonSave->setEnabled(!isFile(text));
369  else
370  ui_->pushButtonSave->setEnabled(currentConfigModified_);
371  }
372  else
373  ui_->pushButtonSave->setEnabled(false);
374 }
375 
377  QString& url) {
378  if (url != currentConfigUrl_) {
379  if (!isFile(url)) {
380  if (!currentConfigUrl_.isEmpty()) {
381  setCurrentConfigUrl(url, false);
383 
384  ui_->pushButtonSave->setEnabled(true);
385  }
386  else
387  ui_->pushButtonSave->setEnabled(false);
388  }
389  else {
390  loadConfig(url);
391  ui_->pushButtonSave->setEnabled(false);
392  }
393  }
394 }
395 
397  QMessageBox messageBox;
398 
399  messageBox.setText("The configuration file history will be cleared.");
400  messageBox.setInformativeText("Do you want to proceed?");
401  messageBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
402  messageBox.setDefaultButton(QMessageBox::No);
403 
404  if (messageBox.exec() == QMessageBox::Yes)
406 }
407 
409  if (confirmSave())
410  resetConfig();
411 }
412 
414  if (confirmSave()) {
415  QFileDialog dialog(this, "Open Configuration", QDir::homePath(),
416  "Multiplot configurations (*.xml)");
417 
418  dialog.setAcceptMode(QFileDialog::AcceptOpen);
419  dialog.setFileMode(QFileDialog::ExistingFile);
420 
421  if (dialog.exec() == QDialog::Accepted)
422  loadConfig("file://"+dialog.selectedFiles().first());
423  }
424 }
425 
427  if (currentConfigUrl_ == ui_->configComboBox->getCurrentUrl())
429  else
430  saveConfig(ui_->configComboBox->getCurrentUrl());
431 }
432 
434  QFileDialog dialog(this, "Save Configuration", QDir::homePath(),
435  "Multiplot configurations (*.xml)");
436 
437  dialog.setAcceptMode(QFileDialog::AcceptSave);
438  dialog.setFileMode(QFileDialog::AnyFile);
439  dialog.selectFile("rqt_multiplot.xml");
440 
441  if (dialog.exec() == QDialog::Accepted)
442  saveConfig("file://"+dialog.selectedFiles().first());
443 }
444 
445 }
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
Author(s): Ralf Kaestner
autogenerated on Wed Jul 10 2019 03:49:44