CmEditorApplication.cpp 4.2 KB

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