34 #include <QMessageBox>
42 namespace Controller {
44 WorkspaceController::WorkspaceController(shared_ptr<Ws::IWorkspaceFactory> f, AppController* m)
46 HasUnsavedChangesPrompt(
"Workspace"),
49 m_project_controller(m)
51 qRegisterMetaType< std::string >();
57 QAction* WorkspaceController::GetActionNewProject()
const
59 return m_a_new_project;
62 QAction* WorkspaceController::GetActionRefreshWorkspace()
const
64 return m_a_refresh_workspace;
67 QDockWidget* WorkspaceController::GetPreferencesDock()
const
69 return m_container_preferences->GetDock();
72 QMenu* WorkspaceController::GetPreferencesMenu()
const
74 return m_container_preferences->GetMenu();
77 ptree WorkspaceController::GetPTreeState()
const
80 state.put_child(
"project_controller", m_project_controller.GetPTreeState());
81 state.put_child(
"view", m_view->GetPTreeState());
82 state.put_child(
"container_preferences", m_container_preferences->GetPTreeState());
91 void WorkspaceController::InitActions()
93 m_a_new_project =
new QAction(QIcon::fromTheme(
"document-new"),
"New Project...",
this);
94 m_a_new_project->setShortcut(QKeySequence(
"Ctrl+N"));
95 m_a_new_project->setEnabled(
false);
96 connect(m_a_new_project, SIGNAL(triggered()),
this, SLOT(SLOT_NewProjectDialog()));
98 m_a_refresh_workspace =
new QAction(QIcon::fromTheme(
"view-refresh"),
"Refresh Workspace",
this);
99 m_a_refresh_workspace->setShortcut(QKeySequence(
"F5"));
100 m_a_refresh_workspace->setEnabled(
false);
101 connect(m_a_refresh_workspace, SIGNAL(triggered()),
this, SLOT(SLOT_RefreshWorkspace()));
103 m_enabled_actions.Set({ m_a_new_project, m_a_refresh_workspace });
106 void WorkspaceController::InitBaseClasses()
108 HasUnsavedChanges::SetChildren({
109 &m_project_controller,
110 m_container_preferences.get()
114 void WorkspaceController::InitWidgets()
116 m_view =
new WorkspaceView(
this);
118 m_container_preferences = make_shared<PTreeContainerPreferencesObserver>(
"Workspace Preferences");
119 m_container_preferences->SetOnlyEditData(
true);
120 connect(m_container_preferences.get(), SIGNAL(Applied(
const boost::property_tree::ptree&)),
121 this, SLOT(SLOT_ApplyPreferences(
const boost::property_tree::ptree&)));
124 void WorkspaceController::InternalForceClose()
126 m_enabled_actions.Disable();
127 m_view->setModel(
nullptr);
131 bool WorkspaceController::InternalIsClean()
const
136 void WorkspaceController::InternalPreForceClose()
138 m_model->Workspace()->SetUserData(
"main_controller", m_main_controller->GetPTreeState());
141 bool WorkspaceController::InternalSave()
146 bool WorkspaceController::IsOpened()
const
148 return (
bool) m_model;
151 bool WorkspaceController::Open(
const std::string& path)
157 auto workspace = m_factory->CreateWorkspace(path);
158 m_model = WorkspaceQtModel::Create(workspace, &m_project_controller);
161 m_container_preferences->Open(m_model->Workspace()->GetPreferences());
163 auto handler = bind(&PTreeContainerPreferencesObserver::Update,
164 m_container_preferences.get(), std::placeholders::_1);
166 std::weak_ptr<const void>>::Register(m_container_preferences, handler);
169 m_view->setModel(m_model.get());
171 m_enabled_actions.Enable();
173 m_main_controller->SetPTreeState(m_model->Workspace()->GetUserData(
"main_controller"));
182 ProjectController& WorkspaceController::Project()
184 return m_project_controller;
187 const ProjectController& WorkspaceController::Project()
const
189 return m_project_controller;
192 void WorkspaceController::SetPTreeState(
const ptree& state)
194 auto container_preferences_state = state.get_child_optional(
"container_preferences");
195 if (container_preferences_state) {
196 m_container_preferences->SetPTreeState(container_preferences_state.get());
199 auto view_state = state.get_child_optional(
"view");
201 m_view->SetPTreeState(view_state.get());
204 auto project_controller_state = state.get_child_optional(
"project_controller");
205 if (project_controller_state) {
206 m_project_controller.SetPTreeState(project_controller_state.get());
210 WorkspaceController::operator bool()
const
215 void WorkspaceController::SLOT_ApplyPreferences(
const ptree& p)
217 m_model->Workspace()->SetPreferences(p);
220 void WorkspaceController::SLOT_NewProjectDialog()
223 if (dialog.exec() == QDialog::Accepted) {
224 auto project_name = dialog.GetProjectName();
226 if (dialog.IsCopyProject()) {
227 auto src_project = dialog.GetSrcProject();
230 QDir dst_dir(QString::fromStdString(m_model->Workspace()->GetPath()));
231 dst_dir.mkdir(QString::fromStdString(project_name));
234 QDir src_dir(QString::fromStdString(src_project->second->GetPath()));
235 auto src_files = src_dir.entryList(QDir::Files | QDir::Hidden);
236 for (
auto& src_file : src_files) {
237 QFile::copy(QString::fromStdString(src_project->second->GetPath()) +
'/' + src_file,
238 QString::fromStdString(m_model->Workspace()->GetPath()) +
'/' + QString::fromStdString(project_name) +
'/' + src_file);
242 m_model->Workspace()->Add(
"project", project_name);
245 QMessageBox::warning(
this,
"Error", QString(
"Could not create project: ") + e.
what());
248 auto src_path = dialog.GetSrcPath();
249 auto src_file = QFileInfo(QString::fromStdString(src_path)).fileName().toStdString();
252 QDir dst_dir(QString::fromStdString(m_model->Workspace()->GetPath()));
253 dst_dir.mkdir(QString::fromStdString(project_name));
256 QFile::copy(QString::fromStdString(src_path),
257 QString::fromStdString(m_model->Workspace()->GetPath()) +
'/' + QString::fromStdString(project_name) +
'/' + QString::fromStdString(src_file));
260 m_model->Workspace()->Add(
"project", project_name);
263 QMessageBox::warning(
this,
"Error", QString(
"Could not create project: ") + e.
what());
269 void WorkspaceController::SLOT_RefreshWorkspace()
271 m_model->Workspace()->Refresh();
274 Ws::IWorkspace* WorkspaceController::operator->()
276 return m_model->Workspace().get();
279 const Ws::IWorkspace* WorkspaceController::operator->()
const
281 return m_model->Workspace().get();
284 shared_ptr<Ws::IWorkspace> WorkspaceController::operator*()
286 return m_model->Workspace();
289 shared_ptr<const Ws::IWorkspace> WorkspaceController::operator*()
const
291 return m_model->Workspace();
Event used to inform some observer that preferences have changed.
Namespace for miscellaneous utilities.
virtual const char * what() const noexcept
Return error message.
Interface for PTreeQtState.
Interface for NewProjectDialog.
Interface for PTreeContainerPreferencesObserver.
Dialog that asks user about information for setting up a new project.
Interface for WorkspaceQtModel.
Extremely simple Exception root class.
see the online Qt documentation
see the online Qt documentation
Interface for WorkspaceView.
see the online Qt documentation
TreeView widget that uses WorkspaceQtModel as a model.
Header file for Exception class.
Namespace for generic graphical shell for simulators.
WorkspaceController header.
Interface for PTreeContainer.