VPTissue Reference Manual
NewProjectDialog.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  */
21 #include "workspace/IWorkspace.h"
22 #include "workspace/IProject.h"
23 #include "workspace/IFile.h"
24 #include "NewProjectDialog.h"
25 
26 #include <QLabel>
27 #include <QGridLayout>
28 #include <QHBoxLayout>
29 
30 namespace SimShell {
31 namespace Gui {
32 
33 using namespace std;
34 using namespace Ws;
35 
36 NewProjectDialog::NewProjectDialog(const shared_ptr<IWorkspaceFactory>& f, QWidget* parent)
37  : QDialog(parent),
38  m_workspace_model(f->CreateWorkspace(f->GetWorkspaceTemplatePath()))
39 {
40  setWindowTitle("New Project");
41 
42  m_dialog = 0; // browse dialog
43 
44  // "global" layout
45  QVBoxLayout* layout = new QVBoxLayout;
46 
47  QHBoxLayout* layout_name = new QHBoxLayout;
48  QLabel* label_name = new QLabel("Name:");
49  m_edit_name = new QLineEdit;
50  m_edit_name->setText("untitled");
51  m_edit_name->selectAll();
52  label_name->setBuddy(m_edit_name);
53  layout_name->addWidget(label_name);
54  layout_name->addWidget(m_edit_name);
55  layout->addLayout(layout_name);
56 
57  QLabel* label_info = new QLabel("Please specify a file to initialize project with:");
58  layout->addWidget(label_info);
59 
60  QVBoxLayout* layout_f = new QVBoxLayout;
61  QHBoxLayout* layout_default = new QHBoxLayout;
62  m_radio_default = new QRadioButton("Copy project from template workspace:");
63  m_radio_default->setChecked(true);
64  m_combo_models = new QComboBox;
65  vector<string> project_list;
66  for (auto& project : *m_workspace_model) {
67  project_list.push_back(project.first);
68  }
69  for (vector<string>::const_iterator it = project_list.begin(); it != project_list.end(); it++) {
70  m_combo_models->addItem(it->c_str());
71  }
72  layout_default->addWidget(m_radio_default);
73  layout_default->addWidget(m_combo_models);
74  layout_f->addLayout(layout_default);
75  QHBoxLayout* layout_custom = new QHBoxLayout;
76  m_radio_custom = new QRadioButton("Use the following file:");
77  m_edit_custom = new QLineEdit;
78  m_edit_custom->setEnabled(false);
79  QPushButton* button_browse = new QPushButton("Browse...");
80  button_browse->setEnabled(false);
81  layout_custom->addWidget(m_radio_custom);
82  layout_custom->addWidget(m_edit_custom);
83  layout_custom->addWidget(button_browse);
84  layout_f->addLayout(layout_custom);
85  layout->addLayout(layout_f);
86 
87  layout->addStretch();
88 
89  QHBoxLayout* layout_buttons = new QHBoxLayout;
90  m_button_ok = new QPushButton("OK");
91  m_button_ok->setDefault(true);
92  m_button_cancel = new QPushButton("Cancel");
93  layout_buttons->addStretch();
94  layout_buttons->addWidget(m_button_cancel);
95  layout_buttons->addWidget(m_button_ok);
96  layout->addLayout(layout_buttons);
97 
98  setLayout(layout);
99 
100  connect(m_edit_name, SIGNAL(textChanged(const QString &)), this, SLOT(ProjectNameChanged(const QString &)));
101  connect(m_radio_custom, SIGNAL(toggled(bool)), m_edit_custom, SLOT(setEnabled(bool)));
102  connect(m_radio_custom, SIGNAL(toggled(bool)), button_browse, SLOT(setEnabled(bool)));
103  connect(m_radio_default, SIGNAL(toggled(bool)), this, SLOT(FileChanged(bool)));
104  connect(m_edit_custom, SIGNAL(textChanged(const QString &)), this, SLOT(FileChanged(const QString &)));
105  connect(button_browse, SIGNAL(clicked()), this, SLOT(ShowBrowseDialog()));
106  connect(m_button_ok, SIGNAL(clicked()), this, SLOT(Ok()));
107  connect(m_button_cancel, SIGNAL(clicked()), this, SLOT(Cancel()));
108 
109  project_name_ok = true;
110  file_ok = true;
111 }
112 
113 void NewProjectDialog::Cancel()
114 {
115  reject();
116 }
117 
118 string NewProjectDialog::GetSrcPath() const
119 {
120  return m_edit_custom->text().toStdString();
121 }
122 
123 IWorkspace::ConstProjectIterator NewProjectDialog::GetSrcProject() const
124 {
125  std::string project_name = m_combo_models->currentText().toStdString();
126  return m_workspace_model->Find(project_name);
127 }
128 
129 string NewProjectDialog::GetProjectName() const
130 {
131  return m_edit_name->text().toStdString();
132 }
133 
134 void NewProjectDialog::FileChanged(bool use_default)
135 {
136  file_ok = use_default || m_edit_custom->text().length();
137  ValidateForm();
138 }
139 
140 void NewProjectDialog::FileChanged(QString const& f)
141 {
142  file_ok = m_radio_default->isChecked() || f.length();
143  ValidateForm();
144 }
145 
146 bool NewProjectDialog::IsCopyProject() const
147 {
148  return m_radio_default->isChecked();
149 }
150 
151 void NewProjectDialog::ProjectNameChanged(QString const& name)
152 {
153  project_name_ok = name.length();
154  ValidateForm();
155 }
156 
157 void NewProjectDialog::Ok()
158 {
159  accept();
160 }
161 
162 void NewProjectDialog::ShowBrowseDialog()
163 {
164  if (m_dialog) {
165  m_dialog->exec();
166  } else {
167  m_dialog = new QFileDialog(this, "Browse", QDir::homePath(), "XML files (*.xml)");
168  m_dialog->setAcceptMode(QFileDialog::AcceptOpen);
169  m_dialog->setFileMode(QFileDialog::ExistingFile);
170  connect(m_dialog, SIGNAL(fileSelected(const QString &)), m_edit_custom, SLOT(setText(const QString &)));
171  m_dialog->exec();
172  }
173 }
174 
175 void NewProjectDialog::ValidateForm()
176 {
177  m_button_ok->setEnabled(project_name_ok && file_ok);
178 }
179 
180 } // namespace
181 } // namespace
Interface for IFile.
STL namespace.
Interface for NewProjectDialog.
Interface for IWorkspaceFactory.
Interface for IProject.
Interface for IWorkspace.
see the online Qt documentation
Namespace for generic graphical shell for simulators.
Definition: SimSession.h:32
see the online Qt documentation