CmEditorApplication.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 "CmSceneWidgetFactory.h"
  9. #include "CmHierarchyWidgetFactory.h"
  10. #include "CmFileSystem.h"
  11. #include "CmException.h"
  12. #include "CmDataStream.h"
  13. #include <QtWidgets/QApplication>
  14. #include <QtCore/QDir>
  15. namespace CamelotEditor
  16. {
  17. const QString EditorApplication::PROJECT_PREFS_FILE_NAME = "CamelotProject.xml";
  18. const QString EditorApplication::EDITOR_PREFS_FILE_NAME = "Editor.xml";
  19. const QString EditorApplication::STYLE_SHEETS_DIRECTORY_NAME = "Styles";
  20. struct EditorApplication::PImpl
  21. {
  22. QApplication* mApp;
  23. QtEditor* mEditor;
  24. };
  25. EditorApplication::EditorApplication()
  26. :p(new PImpl())
  27. {
  28. p->mApp = nullptr;
  29. p->mEditor = nullptr;
  30. }
  31. EditorApplication::~EditorApplication()
  32. {
  33. delete p;
  34. }
  35. void EditorApplication::startUp()
  36. {
  37. EditorPrefs::startUp(new EditorPrefs());
  38. if(FileSystem::fileExists(getEditorPrefsPath().toStdString()))
  39. gEditorPrefs().load(getEditorPrefsPath());
  40. ProjectPrefs::startUp(new ProjectPrefs());
  41. startUpQt();
  42. EditorWindowManager::startUp(new EditorWindowManager());
  43. WindowDockManager::startUp(new WindowDockManager(p->mEditor->getCentralWidget(), p->mEditor->getDockOverlayWidget()));
  44. gEditorWindowManager().registerWidgetFactory(new SceneWidgetFactory());
  45. gEditorWindowManager().registerWidgetFactory(new HierarchyWidgetFactory());
  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. gEditorWindowManager().restoreWindowsFromPrefs();
  59. p->mEditor->setProjectName(gProjectPrefs().getProjectName());
  60. p->mEditor->show();
  61. p->mApp->exec();
  62. }
  63. void EditorApplication::shutDown()
  64. {
  65. gEditorWindowManager().saveWindowsToPrefs();
  66. WindowDockManager::shutDown();
  67. EditorWindowManager::shutDown();
  68. shutDownQt();
  69. ProjectPrefs::shutDown();
  70. gEditorPrefs().save(getEditorPrefsPath());
  71. EditorPrefs::shutDown();
  72. }
  73. void EditorApplication::startUpQt()
  74. {
  75. int argc = 0;
  76. p->mApp = new QApplication(argc, nullptr);
  77. p->mEditor = new QtEditor();
  78. loadStyleSheets();
  79. }
  80. void EditorApplication::shutDownQt()
  81. {
  82. delete p->mApp;
  83. }
  84. void EditorApplication::loadStyleSheets()
  85. {
  86. QDir styleSheetsDir = getStyleSheetsDirectoryPath();
  87. if(!styleSheetsDir.exists())
  88. return;
  89. QStringList nameFilters;
  90. nameFilters<<"*.css";
  91. QFileInfoList fileInfos = styleSheetsDir.entryInfoList(nameFilters, QDir::Files | QDir::Readable);
  92. QString styleSheet;
  93. for(auto iter = fileInfos.begin(); iter != fileInfos.end(); ++iter)
  94. {
  95. DataStreamPtr dataStream = FileSystem::open(iter->absoluteFilePath().toStdString());
  96. styleSheet += QString::fromStdString(dataStream->getAsString());
  97. }
  98. p->mApp->setStyleSheet(styleSheet);
  99. }
  100. void EditorApplication::loadProject(const QString& absProjPath)
  101. {
  102. if(!isValidProject(absProjPath))
  103. CM_EXCEPT(InternalErrorException, "Cannot load project at path: " + absProjPath.toStdString());
  104. QString projectPrefsPath = getProjectPrefsPath(absProjPath);
  105. gProjectPrefs().load(projectPrefsPath);
  106. }
  107. void EditorApplication::createProject(const QString& absProjDir, const QString& projName)
  108. {
  109. QString fullProjPath = getFullProjectPath(absProjDir, projName);
  110. String fullProjPathStd = fullProjPath.toStdString();
  111. if(FileSystem::dirExists(fullProjPathStd))
  112. CM_EXCEPT(InternalErrorException, "Directory you have choosen for your project already exists: " + fullProjPathStd);
  113. FileSystem::createDir(fullProjPathStd);
  114. QString projPrefsPath = getProjectPrefsPath(fullProjPath);
  115. gProjectPrefs().setProjectName(projName);
  116. gProjectPrefs().save(projPrefsPath);
  117. }
  118. void EditorApplication::deleteProject(const QString& absProjPath)
  119. {
  120. FileSystem::deleteDir(absProjPath.toStdString());
  121. }
  122. bool EditorApplication::isValidProject(const QString& absProjDir) const
  123. {
  124. return projectExists(absProjDir);
  125. }
  126. bool EditorApplication::isValidProjectName(const QString& name) const
  127. {
  128. return FileSystem::isValidFileName(name.toStdString());
  129. }
  130. QString EditorApplication::getFullProjectPath(const QString& absProjDir, const QString& projName) const
  131. {
  132. QString dir = absProjDir;
  133. return QDir::cleanPath(QDir::toNativeSeparators(absProjDir + QDir::separator() + projName));
  134. }
  135. bool EditorApplication::projectExists(const QString& absProjPath) const
  136. {
  137. QString filePath = getProjectPrefsPath(absProjPath);
  138. if(FileSystem::fileExists(filePath.toStdString()))
  139. return true;
  140. return false;
  141. }
  142. QString EditorApplication::getProjectPrefsPath(const QString& absProjPath) const
  143. {
  144. return QDir::cleanPath(QDir::toNativeSeparators(absProjPath + QDir::separator() + PROJECT_PREFS_FILE_NAME));
  145. }
  146. QString EditorApplication::getEditorRootPath() const
  147. {
  148. return QString::fromStdString(FileSystem::getCurrentPath());
  149. }
  150. QString EditorApplication::getEditorPrefsPath() const
  151. {
  152. return QDir::cleanPath(QDir::toNativeSeparators(getEditorRootPath() + QDir::separator() + EDITOR_PREFS_FILE_NAME));
  153. }
  154. QtEditor* EditorApplication::getMainWindow() const
  155. {
  156. return p->mEditor;
  157. }
  158. QString EditorApplication::getStyleSheetsDirectoryPath() const
  159. {
  160. return QDir::cleanPath(QDir::toNativeSeparators(getEditorRootPath() + QDir::separator() + STYLE_SHEETS_DIRECTORY_NAME));
  161. }
  162. EditorApplication& gEditorApp()
  163. {
  164. static EditorApplication application;
  165. return application;
  166. }
  167. }