CmEditorApplication.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include "CmEditorApplication.h"
  2. #include "CmEditorPrefs.h"
  3. #include "CmProjectPrefs.h"
  4. #include "CmQtEditor.h"
  5. #include "CmQtProjectSelection.h"
  6. #include "CmEditorWindowManager.h"
  7. #include "CmWindowDockManager.h"
  8. #include "CmSceneWindowFactory.h"
  9. #include "CmHierarchyWindowFactory.h"
  10. #include "CmFileSystem.h"
  11. #include "CmException.h"
  12. #include <QtWidgets/QApplication>
  13. #include <QtCore/QDir>
  14. namespace CamelotEditor
  15. {
  16. const QString EditorApplication::PROJECT_PREFS_FILE_NAME = "CamelotProject.xml";
  17. const QString EditorApplication::EDITOR_PREFS_FILE_NAME = "Editor.xml";
  18. struct EditorApplication::PImpl
  19. {
  20. QApplication* mApp;
  21. QtEditor* mEditor;
  22. };
  23. EditorApplication::EditorApplication()
  24. :p(new PImpl())
  25. {
  26. p->mApp = nullptr;
  27. p->mEditor = nullptr;
  28. }
  29. EditorApplication::~EditorApplication()
  30. {
  31. delete p;
  32. }
  33. void EditorApplication::startUp()
  34. {
  35. EditorPrefs::startUp(new EditorPrefs());
  36. if(FileSystem::fileExists(getEditorPrefsPath().toStdString()))
  37. gEditorPrefs().load(getEditorPrefsPath());
  38. ProjectPrefs::startUp(new ProjectPrefs());
  39. int argc = 0;
  40. p->mApp = new QApplication(argc, nullptr);
  41. p->mEditor = new QtEditor();
  42. EditorWindowManager::startUp(new EditorWindowManager());
  43. WindowDockManager::startUp(new WindowDockManager(p->mEditor->getCentralWidget(), p->mEditor->getDockOverlayWidget()));
  44. gEditorWindowManager().registerWindowFactory(new SceneWindowFactory());
  45. gEditorWindowManager().registerWindowFactory(new HierarchyWindowFactory());
  46. vector<QString>::type windowTypes = gEditorWindowManager().getAvailableWindowTypes();
  47. for(auto iter = windowTypes.begin(); iter != windowTypes.end(); ++iter)
  48. {
  49. p->mEditor->addMenuItemCallback("Windows", *iter, gEditorWindowManager().getOpenCallback(*iter));
  50. }
  51. }
  52. void EditorApplication::run()
  53. {
  54. QtProjectSelection projSelection;
  55. projSelection.onProjectSelected.connect(boost::bind(&EditorApplication::loadProject, this, _1));
  56. if(projSelection.exec() == QDialog::Rejected)
  57. return;
  58. p->mEditor->setProjectName(gProjectPrefs().getProjectName());
  59. p->mEditor->show();
  60. p->mApp->exec();
  61. }
  62. void EditorApplication::shutDown()
  63. {
  64. WindowDockManager::shutDown();
  65. EditorWindowManager::shutDown();
  66. delete p->mApp;
  67. ProjectPrefs::shutDown();
  68. gEditorPrefs().save(getEditorPrefsPath());
  69. EditorPrefs::shutDown();
  70. }
  71. void EditorApplication::loadProject(const QString& absProjPath)
  72. {
  73. if(!isValidProject(absProjPath))
  74. CM_EXCEPT(InternalErrorException, "Cannot load project at path: " + absProjPath.toStdString());
  75. QString projectPrefsPath = getProjectPrefsPath(absProjPath);
  76. gProjectPrefs().load(projectPrefsPath);
  77. }
  78. void EditorApplication::createProject(const QString& absProjDir, const QString& projName)
  79. {
  80. QString fullProjPath = getFullProjectPath(absProjDir, projName);
  81. String fullProjPathStd = fullProjPath.toStdString();
  82. if(FileSystem::dirExists(fullProjPathStd))
  83. CM_EXCEPT(InternalErrorException, "Directory you have choosen for your project already exists: " + fullProjPathStd);
  84. FileSystem::createDir(fullProjPathStd);
  85. QString projPrefsPath = getProjectPrefsPath(fullProjPath);
  86. gProjectPrefs().setProjectName(projName);
  87. gProjectPrefs().save(projPrefsPath);
  88. }
  89. void EditorApplication::deleteProject(const QString& absProjPath)
  90. {
  91. FileSystem::deleteDir(absProjPath.toStdString());
  92. }
  93. bool EditorApplication::isValidProject(const QString& absProjDir) const
  94. {
  95. return projectExists(absProjDir);
  96. }
  97. bool EditorApplication::isValidProjectName(const QString& name) const
  98. {
  99. return FileSystem::isValidFileName(name.toStdString());
  100. }
  101. QString EditorApplication::getFullProjectPath(const QString& absProjDir, const QString& projName) const
  102. {
  103. QString dir = absProjDir;
  104. return QDir::cleanPath(QDir::toNativeSeparators(absProjDir + QDir::separator() + projName));
  105. }
  106. bool EditorApplication::projectExists(const QString& absProjPath) const
  107. {
  108. QString filePath = getProjectPrefsPath(absProjPath);
  109. if(FileSystem::fileExists(filePath.toStdString()))
  110. return true;
  111. return false;
  112. }
  113. QString EditorApplication::getProjectPrefsPath(const QString& absProjPath) const
  114. {
  115. return QDir::cleanPath(QDir::toNativeSeparators(absProjPath + QDir::separator() + PROJECT_PREFS_FILE_NAME));
  116. }
  117. QString EditorApplication::getEditorRootPath() const
  118. {
  119. return QString::fromStdString(FileSystem::getCurrentPath());
  120. }
  121. QString EditorApplication::getEditorPrefsPath() const
  122. {
  123. return QDir::cleanPath(QDir::toNativeSeparators(getEditorRootPath() + QDir::separator() + EDITOR_PREFS_FILE_NAME));
  124. }
  125. QtEditor* EditorApplication::getMainWindow() const
  126. {
  127. return p->mEditor;
  128. }
  129. EditorApplication& gEditorApp()
  130. {
  131. static EditorApplication application;
  132. return application;
  133. }
  134. }