VPTissue Reference Manual
FileConversion.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 "FileConversion.h"
21 
22 #include <QDir>
23 
24 using namespace std;
25 using namespace SimShell::Ws;
26 
27 namespace SimPT_Shell {
28 
29 FileConversion::FileConversion(
30  Ws::Project* project,
31  vector<int> timesteps,
32  shared_ptr<SimShell::Ws::MergedPreferences> prefs,
33  IConverterFormat* output_format,
34  string output_path,
35  string output_prefix)
36  : m_project(project),
37  m_timesteps(timesteps),
38  m_preferences(prefs),
39  m_output_format(output_format),
40  m_output_path(output_path),
41  m_output_prefix(output_prefix)
42 {
43  for (auto& file : *m_project) {
44  auto sim_file = static_pointer_cast<Ws::StartupFileBase>(file.second);
45  for (auto step : sim_file->GetTimeSteps()) {
46  m_step_to_file_map[step] = sim_file;
47  }
48  }
49 }
50 
51 void FileConversion::Run(function<void(int)> progress_callback)
52 {
53  // Pause filesystem watcher
54  m_project->SetWatchingDirectory(false);
55 
56  string path = m_output_path;
57  bool use_temp_dir = false;
58  QDir project_dir(QString::fromStdString(path));
59  if (path == m_project->GetPath()) {
60  project_dir.mkdir("temp");
61  path += "/temp";
62  use_temp_dir = true;
63  }
64  // begin batch conversion
65  m_output_format->PreConvert(path + '/' + m_output_prefix, m_preferences);
66  int i = 0;
67  for (auto timestep : m_timesteps) {
68  if (progress_callback)
69  progress_callback(i);
70  auto sim_state = m_step_to_file_map[timestep]->GetSimState(timestep);
71  m_output_format->Convert(sim_state);
72  i++;
73  }
74  // remove temp dir if there was one
75  if (use_temp_dir) {
76  QDir d(QString::fromStdString(path));
77  for (auto& entry : d.entryList()) {
78  QFile f(QString::fromStdString(m_project->GetPath()) + '/' + entry);
79  f.remove();
80  d.rename(entry, "../" + entry);
81  }
82  project_dir.rmdir("temp");
83  }
84  // end batch conversion
85  m_output_format->PostConvert();
86  if (progress_callback)
87  progress_callback(i);
88 
89  // Resume filesystem watcher
90  m_project->Refresh();
91  m_project->SetWatchingDirectory(true);
92 }
93 
94 } // namespace
STL namespace.
Interface for Conversion.
Namespace for SimPT shell package.
Definition: Client.cpp:50
Namespace for generic workspace classes.
Definition: SimSession.h:32