VPTissue Reference Manual
FilesPage.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "FilesPage.h"
21 
26 
27 #include <boost/property_tree/exceptions.hpp>
28 #include <boost/property_tree/xml_parser.hpp>
29 #include <QComboBox>
30 #include <QFileDialog>
31 #include <QLabel>
32 #include <QListView>
33 #include <QListWidget>
34 #include <QMessageBox>
35 #include <QPushButton>
36 #include <QRadioButton>
37 #include <QSettings>
38 #include <QString>
39 #include <QVBoxLayout>
40 
41 #include <cctype>
42 #include <locale>
43 #include <memory>
44 
45 using namespace boost::property_tree;
46 using namespace boost::property_tree::xml_parser;
47 
48 namespace SimPT_Parex {
49 
50 FilesPage::FilesPage(std::shared_ptr<Exploration>& exploration, boost::property_tree::ptree& preferences)
51  : m_exploration(exploration), m_preferences(preferences), m_files(), m_files_widget(nullptr), m_remove_button(nullptr)
52 {
53  setTitle("Select files");
54  setSubTitle("Select all the files for the exploration.");
55 
56  QVBoxLayout *layout = new QVBoxLayout;
57 
58  m_files_widget = new QListWidget;
59  m_files_widget->setSelectionMode(QListWidget::MultiSelection);
60  connect(m_files_widget, SIGNAL(itemSelectionChanged()), this, SLOT(UpdateRemoveDisabled()));
61  layout->addWidget(m_files_widget);
62 
63  QHBoxLayout *buttonsLayout = new QHBoxLayout;
64 
65  QPushButton *addButton = new QPushButton("Add...");
66  connect(addButton, SIGNAL(clicked()), this, SLOT(AddFiles()));
67  buttonsLayout->addWidget(addButton);
68 
69  m_remove_button = new QPushButton("Remove");
70  connect(m_remove_button, SIGNAL(clicked()), this, SLOT(RemoveFiles()));
71  buttonsLayout->addWidget(m_remove_button);
72 
73  layout->addLayout(buttonsLayout);
74 
75  setLayout(layout);
76 }
77 
78 
79 void FilesPage::UpdateRemoveDisabled()
80 {
81  m_remove_button->setEnabled(!m_files_widget->selectedItems().isEmpty());
82 }
83 
84 
85 void FilesPage::AddFiles()
86 {
87  QSettings settings;
88  QString path;
89  if (settings.contains("last_leaf_file"))
90  path = settings.value("last_leaf_file").toString();
91  else
92  path = settings.value("workspace").toString();
93 
94  QStringList files = QFileDialog::getOpenFileNames(this, "Browse files", path);
95 
96  if (files.empty())
97  return;
98 
99  for (auto file : files) {
100  if (!QFile(file).exists())
101  return;
102 
103  try {
104  ptree pt;
105  read_xml(file.toStdString(), pt, trim_whitespace);
106 
107  auto found = m_files.find(file);
108  if (found != m_files.end()) {
109  QMessageBox::warning(this, "Already added",
110  "The file '" + file + "' was already added.\nNot adding it again.", QMessageBox::Ok);
111  } else {
112  m_files.insert(found, std::make_pair(file, pt));
113  m_files_widget->addItem(new QListWidgetItem(file));
114  }
115  } catch (ptree_bad_path& e) {
116  QMessageBox::critical(this, "Read Error", "Error in file", QMessageBox::Ok);
117  return;
118  } catch (ptree_bad_data& e) {
119  QMessageBox::critical(this, "Read Error", "Error in file.", QMessageBox::Ok);
120  return;
121  } catch (xml_parser_error& e) {
122  QMessageBox::critical(this, "Error", "Not a valid file.", QMessageBox::Ok);
123  return;
124  } catch (std::exception& e) {
125  QMessageBox::critical(this, "Error", "Could not read file.", QMessageBox::Ok);
126  return;
127  } catch (...) {
128  QMessageBox::critical(this, "Error", "Unknown Exception", QMessageBox::Ok);
129  return;
130  }
131  }
132 
133  settings.setValue("last_leaf_file", files.first());
134 
135  completeChanged();
136 }
137 
138 
139 void FilesPage::RemoveFiles()
140 {
141  if (QMessageBox::warning(this, "Remove files", "Are you sure you want to remove the selected item(s)?", QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) {
142  auto selectedItems = m_files_widget->selectedItems();
143  for (auto selectedItem : selectedItems) {
144  auto selectedIt = m_files.find(selectedItem->text());
145  assert(selectedIt != m_files.end() && "Selected item should be in files map");
146 
147  m_files.erase(selectedIt);
148  delete selectedItem;
149  }
150 
151  UpdateRemoveDisabled();
152 
153  completeChanged();
154  }
155 }
156 
157 
158 bool FilesPage::isComplete() const
159 {
160  return m_files_widget->count() > 0;
161 }
162 
163 
164 bool FilesPage::validatePage()
165 {
166  assert(!m_files.empty() && "m_files cannot be empty");
167 
168  std::vector<std::pair<std::string, ptree>> files;
169 
170  for (int i = 0; i < m_files_widget->count(); ++i) {
171  auto item = m_files_widget->item(i);
172  files.push_back(std::make_pair(item->text().toStdString(), m_files.at(item->text())));
173  }
174  m_exploration = std::make_shared<FileExploration>("Untitled", files, m_preferences);
175 
176  return true;
177 }
178 
179 
180 } // namespace
Interface for FilesPage.
Interface for RangeSweep.
Interface for ListSweep.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
Interface for ParameterExploration.
Interface for FileExploration.