VPTissue Reference Manual
WorkspaceWizard.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 "WorkspaceWizard.h"
21 
23 #include "workspace/IWorkspace.h"
24 #include "util/misc/Exception.h"
25 
26 #include <QDir>
27 #include <QHBoxLayout>
28 #include <QLineEdit>
29 #include <QMessageBox>
30 #include <QPushButton>
31 #include <QSettings>
32 #include <QVariant>
33 #include <QWizardPage>
34 
35 #include <iostream>
36 #include <typeinfo>
37 
38 namespace SimShell {
39 namespace Gui {
40 
41 using namespace std;
42 using namespace Ws;
43 using namespace SimPT_Sim::Util;
44 
45 WorkspaceWizard::WorkspaceWizard(const shared_ptr<IWorkspaceFactory>& f, QWidget* parent)
46  : QWizard(parent), m_workspace_factory(f)
47 {
48  setPage(Page_Path, new PathPage(m_workspace_factory));
49  setPage(Page_Existing, new ExistingPage(m_workspace_factory));
50  setPage(Page_Init, new InitPage(m_workspace_factory));
51  setPage(Page_Done, new DonePage(m_workspace_factory));
52  setWindowTitle("Workspace Setup");
53 }
54 
56 {
57  if (field("workspace_path").isValid()) {
58  return field("workspace_path").toString().toStdString();
59  } else {
60  return string();
61  }
62 }
63 
64 WorkspaceWizard::PathPage::PathPage(const shared_ptr<IWorkspaceFactory>& f)
65  : m_workspace_factory(f)
66 {
67  setTitle("Workspace Setup");
68  setSubTitle("Please specify a workspace path.");
69 
70  dialog = 0;
71 
72  QLabel* label = new QLabel("Path");
73  QLineEdit* edit_path = new QLineEdit;
74  edit_path->setText(QDir::homePath() + '/' + QString::fromStdString(m_workspace_factory->GetDefaultWorkspaceName()));
75  label->setBuddy(edit_path);
76  QPushButton* button_browse = new QPushButton("Browse...");
77 
78  QHBoxLayout* layout = new QHBoxLayout;
79  layout->addWidget(label);
80  layout->addWidget(edit_path);
81  layout->addWidget(button_browse);
82  setLayout(layout);
83 
84  registerField("workspace_path", edit_path);
85  connect(button_browse, SIGNAL(clicked()), this, SLOT(showBrowseDialog()));
86 }
87 
88 int WorkspaceWizard::PathPage::nextId() const
89 {
90 
91  int id = 0;
92  string workspace_path = field("workspace_path").toString().toStdString();
93  try {
94  m_workspace_factory->CreateWorkspace(workspace_path); // may throw
95  id = Page_Existing;
96  }
97  catch (Exception &) {
98  id = Page_Init;
99  }
100  catch(std::exception const& e) {
101  id = Page_Init;
102  } catch (...){
103  id = Page_Init;
104  }
105  return id;
106 }
107 
108 void WorkspaceWizard::PathPage::showBrowseDialog()
109 {
110  if (dialog) {
111  dialog->exec();
112  } else {
113  dialog = new QFileDialog(this, "Browse", QDir::homePath());
114  dialog->setAcceptMode(QFileDialog::AcceptOpen);
115  dialog->setFileMode(QFileDialog::Directory);
116  dialog->setOptions(QFileDialog::ShowDirsOnly);
117  connect(dialog, SIGNAL(fileSelected(const QString &)), this, SLOT(pathSelected(const QString &)));
118  dialog->exec();
119  }
120 }
121 
122 void WorkspaceWizard::PathPage::pathSelected(const QString& path)
123 {
124  setField("workspace_path", path);
125 }
126 
127 WorkspaceWizard::ExistingPage::ExistingPage(const shared_ptr<IWorkspaceFactory>& f)
128  : m_workspace_factory(f)
129 {
130  setTitle("Existing workspace");
131  QLabel* label = new QLabel("This workspace contains the following projects:");
132  projects = new QListWidget;
133  QVBoxLayout* layout = new QVBoxLayout;
134  layout->addWidget(label);
135  layout->addWidget(projects);
136  setLayout(layout);
137 }
138 
139 void WorkspaceWizard::ExistingPage::initializePage()
140 {
141  string workspace_path = field("workspace_path").toString().toStdString();
142  try {
143  auto workspace = m_workspace_factory->CreateWorkspace(workspace_path);
144  projects->clear();
145  for (auto& project : *workspace) {
146  projects->addItem(QString::fromStdString(project.first));
147  }
148  setSubTitle(workspace_path.c_str());
149  }
150  catch (Exception &) {
151  }
152 }
153 
154 int WorkspaceWizard::ExistingPage::nextId() const
155 {
156  return Page_Done;
157 }
158 
159 WorkspaceWizard::InitPage::InitPage(const shared_ptr<IWorkspaceFactory>& f)
160  : m_workspace_factory(f)
161 {
162  setTitle("New workspace");
163  directory_label = new QLabel("Given path is not an existing directory. Directory will be created for you.");
164  QVBoxLayout* layout = new QVBoxLayout;
165  layout->addWidget(new QLabel("A new workspace will be created at the given path."));
166  layout->addWidget(directory_label);
167  setLayout(layout);
168 }
169 
170 void WorkspaceWizard::InitPage::initializePage()
171 {
172  QString workspace_path = field("workspace_path").toString();
173  setSubTitle(workspace_path);
174  QDir d(workspace_path);
175  directory_label->setVisible(!d.exists());
176 }
177 
178 int WorkspaceWizard::InitPage::nextId() const
179 {
180  return Page_Done;
181 }
182 
183 bool WorkspaceWizard::InitPage::validatePage()
184 {
185  // Actually initialize workspace now
186  // Create directory if it doesn't exist
187  QString workspace_path = field("workspace_path").toString();
188  QDir d(workspace_path);
189  if (!d.exists()) {
190  if (!d.mkpath(workspace_path)) {
191  QMessageBox::warning(this, "Error", "Could not create directory.");
192  return false;
193  }
194  }
195  try {
196  m_workspace_factory->InitWorkspace(workspace_path.toStdString());
197  } catch (Exception& e) {
198  QMessageBox::warning(this, "Error", QString("Could not initialize workspace: ") + e.what());
199  return false;
200  }
201  return true;
202 }
203 
204 WorkspaceWizard::DonePage::DonePage(const shared_ptr<IWorkspaceFactory>& f)
205  : m_workspace_factory(f)
206 {
207  setTitle("All Done!");
208  check_default = new QCheckBox("Remember as default workspace.");
209  check_default->setChecked(true);
210  QVBoxLayout* layout = new QVBoxLayout;
211  layout->addWidget(check_default);
212  setLayout(layout);
213 }
214 
215 void WorkspaceWizard::DonePage::initializePage()
216 {
217  QString workspace_path = field("workspace_path").toString();
218  setSubTitle(workspace_path);
219 }
220 
221 int WorkspaceWizard::DonePage::nextId() const
222 {
223  return -1;
224 }
225 
226 bool WorkspaceWizard::DonePage::validatePage()
227 {
228  QString workspace_path = field("workspace_path").toString();
229  if (check_default->isChecked()) {
230  QSettings settings;
231  settings.setValue("workspace", workspace_path);
232  }
233  return true;
234 }
235 
236 } // end of namespace Gui
237 } // end of namespace SimShell
Interface for WorkspaceWizard.
STL namespace.
std::string GetWorkspaceDir() const
After WorkspaceWizard::exec() returns QDialog::Accepted, this function will return workspace director...
Namespace for miscellaneous utilities.
Definition: PTreeFile.cpp:44
virtual const char * what() const noexcept
Return error message.
Definition: Exception.h:38
Extremely simple Exception root class.
Definition: Exception.h:28
Interface for IWorkspaceFactory.
see the online Qt documentation
Interface for IWorkspace.
see the online Qt documentation
Header file for Exception class.
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32