28 #include <QApplication>
30 #include <QCloseEvent>
32 #include <QFileDialog>
33 #include <QFormLayout>
38 #include <QMessageBox>
39 #include <QProgressDialog>
40 #include <QPushButton>
41 #include <QSignalMapper>
53 const QRegExp ConverterWindow::g_export_name_regex(
"[^\\x0-\\x1F<>:/\\\\|]*");
55 ConverterWindow::ConverterWindow(
57 std::vector<IConverterFormat*> formats,
58 shared_ptr<SimShell::Ws::MergedPreferences> prefs,
59 const string& window_title,
66 setWindowModality(Qt::WindowModal);
67 setWindowTitle(QString::fromStdString(window_title));
68 setMinimumSize(400, 500);
71 QVBoxLayout *layout =
new QVBoxLayout();
75 connect(m_conversion_list, SIGNAL(CheckedChanged()),
this, SLOT(CheckConversionEnabled()));
76 layout->addWidget(m_conversion_list);
79 QFrame *line =
new QFrame();
80 line->setFrameShape(QFrame::HLine);
81 line->setFrameShadow(QFrame::Sunken);
82 layout->addWidget(line);
85 QFormLayout *exportLayout =
new QFormLayout();
88 QHBoxLayout *comboLayout =
new QHBoxLayout();
89 m_export_format =
new QComboBox();
90 for (
auto format : m_formats) {
91 QString formatName = QString::fromStdString(format->GetName());
92 m_export_format->addItem(formatName);
94 connect(m_export_format, SIGNAL(currentIndexChanged(
const QString&)),
this, SLOT(SLOT_ExportFormatChanged(
const QString&)));
95 comboLayout->addWidget(m_export_format);
96 m_options_button =
new QPushButton(
"Options...");
97 m_options_button->setEnabled(
false);
98 connect(m_options_button, SIGNAL(clicked()),
this, SLOT(SLOT_OptionsTriggered()));
99 comboLayout->addWidget(m_options_button);
100 comboLayout->addStretch();
101 exportLayout->addRow(
new QLabel(
"Export Format"), comboLayout);
104 QHBoxLayout *exportPathLayout =
new QHBoxLayout();
106 m_export_path =
new QLineEdit();
107 m_export_path->setReadOnly(
true);
108 m_export_path->setText(QString::fromStdString(m_project->
GetPath()));
109 exportPathLayout->addWidget(m_export_path);
110 connect(m_export_path, SIGNAL(textChanged(
const QString&)),
this, SLOT(CheckConversionEnabled()));
112 QPushButton *browseExportPathButton =
new QPushButton(
"Browse...");
113 exportPathLayout->addWidget(browseExportPathButton);
114 connect(browseExportPathButton, SIGNAL(clicked()),
this, SLOT(BrowseExportPath()));
116 exportLayout->addRow(
"Export Path", exportPathLayout);
119 m_export_name =
new QLineEdit();
120 m_export_name->setText(
"leaf");
121 exportLayout->addRow(
"Export Prefix", m_export_name);
122 m_export_name->setValidator(
new QRegExpValidator(g_export_name_regex));
123 connect(m_export_name, SIGNAL(textChanged(
const QString&)),
this, SLOT(CheckConversionEnabled()));
125 layout->addLayout(exportLayout);
128 QHBoxLayout *buttonLayout =
new QHBoxLayout();
129 buttonLayout->addStretch();
131 QPushButton *closeButton =
new QPushButton(
"Close");
132 connect(closeButton, SIGNAL(clicked()),
this, SLOT(close()));
133 buttonLayout->addWidget(closeButton);
135 m_convert_button =
new QPushButton(
"Convert");
136 m_convert_button->setEnabled(
false);
137 m_convert_button->setDefault(
true);
138 connect(m_convert_button, SIGNAL(clicked()),
this, SLOT(Convert()));
139 buttonLayout->addWidget(m_convert_button);
142 layout->addLayout(buttonLayout);
154 void ConverterWindow::CheckConversionEnabled()
156 bool enabled = m_conversion_list->HasCheckedEntries() && !m_export_path->text().isEmpty() && QDir(m_export_path->text()).exists() && !m_export_name->text().isEmpty();
157 m_convert_button->setEnabled(enabled);
160 void ConverterWindow::BrowseExportPath()
162 QString exportPath = QFileDialog::getExistingDirectory(
this,
"Select export directory", !m_export_path->text().isEmpty() ? m_export_path->text() : QDir::homePath());
163 if (!exportPath.isEmpty()) {
164 m_export_path->setText(exportPath);
168 void ConverterWindow::Convert()
170 auto checked = m_conversion_list->GetCheckedEntries();
172 vector<int> timesteps;
173 for (
auto& checked_entry : checked) {
174 timesteps.push_back(checked_entry.timestep);
177 auto format = *find_if(m_formats.begin(), m_formats.end(), [
this] (
const IConverterFormat *format) ->
bool {
return format->GetName() == m_export_format->currentText().toStdString(); });
179 QProgressDialog progress(
"Converting...",
"Cancel", 0, checked.size(),
this);
180 progress.setWindowTitle(
"Please wait");
181 progress.setMinimumDuration(0);
182 progress.setWindowModality(Qt::WindowModal);
184 FileConversion conversion(
189 m_export_path->text().toStdString(),
190 m_export_name->text().toStdString());
193 conversion.Run([&](
int i) {
194 QApplication::processEvents();
195 progress.setValue(i);
196 if (progress.wasCanceled()) {
201 catch (exception &e) {
202 QMessageBox::critical(
this,
"Error exporting steps", e.what(), QMessageBox::Ok);
205 QMessageBox::critical(
this,
"Error exporting steps",
"Unknown exception occurred", QMessageBox::Ok);
211 m_conversion_list->
Clear();
212 m_step_to_name_map.clear();
213 m_step_to_file_map.clear();
215 for (
auto& file : *m_project) {
217 for (
auto step : sim_file->GetTimeSteps()) {
218 m_step_to_name_map[step].push_back(file.first);
219 m_step_to_file_map[step] = sim_file;
223 for (
auto& it : m_step_to_name_map) {
225 for (
auto file_it = it.second.begin(); file_it != it.second.end(); file_it++) {
226 if (file_it != it.second.begin()) {
231 m_conversion_list->
AddEntry({it.first, files});
234 m_convert_button->setEnabled(
false);
237 void ConverterWindow::SLOT_ExportFormatChanged(
const QString&)
243 void ConverterWindow::SLOT_OptionsTriggered()
virtual ~ConverterWindow()
Destructor.
Abstraction of project info on filesystem, as well as a maintainer of a work session.
Interface for ConversionList.
void AddEntry(const EntryType &)
Add entry.
Interface for Conversion.
Namespace for miscellaneous utilities.
Namespace for SimPT shell package.
Interface for ConverterWindow.
void Clear()
Clear all entries.
Base class representing the file types used to initiate a session.
Interface for StartupFileBase.
String manipulation utilities.
see the online Qt documentation
virtual const std::string & GetPath() const
Header file for Exception class.
Widget containing the list of steps in conversion and associated functionality.
see the online Qt documentation
void Refresh()
Refresh list of input files.