27 #include <boost/property_tree/exceptions.hpp>
28 #include <boost/property_tree/xml_parser.hpp>
30 #include <QFileDialog>
33 #include <QListWidget>
34 #include <QMessageBox>
35 #include <QPushButton>
36 #include <QRadioButton>
39 #include <QVBoxLayout>
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)
53 setTitle(
"Select files");
54 setSubTitle(
"Select all the files for the exploration.");
56 QVBoxLayout *layout =
new QVBoxLayout;
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);
63 QHBoxLayout *buttonsLayout =
new QHBoxLayout;
65 QPushButton *addButton =
new QPushButton(
"Add...");
66 connect(addButton, SIGNAL(clicked()),
this, SLOT(AddFiles()));
67 buttonsLayout->addWidget(addButton);
69 m_remove_button =
new QPushButton(
"Remove");
70 connect(m_remove_button, SIGNAL(clicked()),
this, SLOT(RemoveFiles()));
71 buttonsLayout->addWidget(m_remove_button);
73 layout->addLayout(buttonsLayout);
79 void FilesPage::UpdateRemoveDisabled()
81 m_remove_button->setEnabled(!m_files_widget->selectedItems().isEmpty());
85 void FilesPage::AddFiles()
89 if (settings.contains(
"last_leaf_file"))
90 path = settings.value(
"last_leaf_file").toString();
92 path = settings.value(
"workspace").toString();
94 QStringList files = QFileDialog::getOpenFileNames(
this,
"Browse files", path);
99 for (
auto file : files) {
100 if (!QFile(file).exists())
105 read_xml(file.toStdString(), pt, trim_whitespace);
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);
112 m_files.insert(found, std::make_pair(file, pt));
113 m_files_widget->addItem(
new QListWidgetItem(file));
115 }
catch (ptree_bad_path& e) {
116 QMessageBox::critical(
this,
"Read Error",
"Error in file", QMessageBox::Ok);
118 }
catch (ptree_bad_data& e) {
119 QMessageBox::critical(
this,
"Read Error",
"Error in file.", QMessageBox::Ok);
121 }
catch (xml_parser_error& e) {
122 QMessageBox::critical(
this,
"Error",
"Not a valid file.", QMessageBox::Ok);
124 }
catch (std::exception& e) {
125 QMessageBox::critical(
this,
"Error",
"Could not read file.", QMessageBox::Ok);
128 QMessageBox::critical(
this,
"Error",
"Unknown Exception", QMessageBox::Ok);
133 settings.setValue(
"last_leaf_file", files.first());
139 void FilesPage::RemoveFiles()
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");
147 m_files.erase(selectedIt);
151 UpdateRemoveDisabled();
158 bool FilesPage::isComplete()
const
160 return m_files_widget->count() > 0;
164 bool FilesPage::validatePage()
166 assert(!m_files.empty() &&
"m_files cannot be empty");
168 std::vector<std::pair<std::string, ptree>> files;
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())));
174 m_exploration = std::make_shared<FileExploration>(
"Untitled", files, m_preferences);
Interface for RangeSweep.
Namespace for SimPT parameter explorer package.
Interface for ParameterExploration.
Interface for FileExploration.