SettingsManagerDialog.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "EditorDefs.h"
  9. #include "SettingsManagerDialog.h"
  10. // Qt
  11. #include <QMessageBox>
  12. // AzToolsFramework
  13. #include <AzToolsFramework/API/ViewPaneOptions.h>
  14. // Editor
  15. #include "SettingsManager.h"
  16. #include "MainWindow.h"
  17. #include "QtViewPaneManager.h"
  18. #include "Util/AutoDirectoryRestoreFileDialog.h" // for CAutoDirectoryRestoreFileDialog
  19. #include "LyViewPaneNames.h" // for LyViewPane::
  20. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  21. #include "ui_SettingsManagerDialog.h"
  22. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  23. //////////////////////////////////////////////////////////////////////////
  24. CSettingsManagerDialog::CSettingsManagerDialog(QWidget* pParent)
  25. : QDialog(pParent)
  26. , ui(new Ui::SettingsManagerDialog)
  27. {
  28. ui->setupUi(this);
  29. ui->m_skipToolsChk->setChecked(false);
  30. ui->m_importSettingsChk->setChecked(false);
  31. connect(ui->m_exportBtn, &QAbstractButton::clicked, this, &CSettingsManagerDialog::OnExportBtnClick);
  32. connect(ui->m_readBtn, &QAbstractButton::clicked, this, &CSettingsManagerDialog::OnReadBtnClick);
  33. connect(ui->m_importBtn, &QAbstractButton::clicked, this, &CSettingsManagerDialog::OnImportBtnClick);
  34. connect(ui->m_closeAllToolsBtn, &QAbstractButton::clicked, this, &CSettingsManagerDialog::OnCloseAllTools);
  35. connect(ui->m_layoutListBox, &QListWidget::itemSelectionChanged, this, &CSettingsManagerDialog::OnSelectionChanged);
  36. // Disable import button until a layout is selected.
  37. ui->m_importBtn->setEnabled(false);
  38. }
  39. CSettingsManagerDialog::~CSettingsManagerDialog()
  40. {
  41. }
  42. const GUID& CSettingsManagerDialog::GetClassID()
  43. {
  44. // {64E0B47F-FA9B-46a9-AEF4-BDAC021B5B2F}
  45. static const GUID guid =
  46. {
  47. 0x64e0b47f, 0xfa9b, 0x46a9, { 0xae, 0xf4, 0xbd, 0xac, 0x2, 0x1b, 0x5b, 0x2f }
  48. };
  49. return guid;
  50. }
  51. void CSettingsManagerDialog::RegisterViewClass()
  52. {
  53. AzToolsFramework::ViewPaneOptions options;
  54. options.paneRect = QRect(5, 100, 210, 505);
  55. options.showInMenu = false;
  56. AzToolsFramework::RegisterViewPane<CSettingsManagerDialog>(LyViewPane::EditorSettingsManager, LyViewPane::CategoryOther, options);
  57. }
  58. void CSettingsManagerDialog::OnReadBtnClick()
  59. {
  60. char szFilters[] = "Editor Settings and Layout File (*.xml);;All files (*)";
  61. CAutoDirectoryRestoreFileDialog importFileSelectionDialog(QFileDialog::AcceptOpen, QFileDialog::ExistingFile, "xml", {}, szFilters, {}, {}, this);
  62. if (importFileSelectionDialog.exec())
  63. {
  64. m_importFileStr = importFileSelectionDialog.selectedFiles().first();
  65. ui->m_layoutListBox->clear();
  66. TToolNamesMap toolNames;
  67. XmlNodeRef dummyNode = nullptr;
  68. GetIEditor()->GetSettingsManager()->GetMatchingLayoutNames(toolNames, dummyNode, m_importFileStr);
  69. if (!toolNames.empty())
  70. {
  71. TToolNamesMap::const_iterator tIt = toolNames.begin();
  72. for (; tIt != toolNames.end(); ++tIt)
  73. {
  74. if (!tIt->second.isEmpty())
  75. {
  76. ui->m_layoutListBox->addItem(tIt->second);
  77. }
  78. else
  79. if (!tIt->first.isEmpty())
  80. {
  81. ui->m_layoutListBox->addItem(tIt->first);
  82. }
  83. }
  84. }
  85. ui->m_layoutListBox->selectAll();
  86. }
  87. }
  88. void CSettingsManagerDialog::OnExportBtnClick()
  89. {
  90. char szFilters[] = "Editor Settings and Layout File (*.xml);;All files (*)";
  91. CAutoDirectoryRestoreFileDialog exportFileSelectionDialog(QFileDialog::AcceptSave, QFileDialog::AnyFile, "xml", "ExportedEditor.xml", szFilters, {}, {}, this);
  92. if (exportFileSelectionDialog.exec())
  93. {
  94. QString file = exportFileSelectionDialog.selectedFiles().first();
  95. GetIEditor()->GetSettingsManager()->SetExportFileName(file);
  96. QtViewPaneManager::instance()->ClosePane(LyViewPane::EditorSettingsManager);
  97. GetIEditor()->GetSettingsManager()->Export();
  98. }
  99. }
  100. void CSettingsManagerDialog::OnImportBtnClick()
  101. {
  102. QString importFileStr = m_importFileStr;
  103. bool bImportSettings = ui->m_importSettingsChk->isChecked();
  104. QString ask = tr("This will close all opened Views. Make sure to save your projects and backup layout before continuing");
  105. QStringList layouts;
  106. for (auto& item : ui->m_layoutListBox->selectedItems())
  107. {
  108. layouts.append(item->text());
  109. }
  110. // Show warning window to the user in case he selects some layouts to import
  111. if (!layouts.isEmpty())
  112. {
  113. if (QMessageBox::question(this, tr("Editor"), ask) != QMessageBox::Yes)
  114. {
  115. return;
  116. }
  117. }
  118. // Import Settings
  119. if (bImportSettings)
  120. {
  121. ImportSettings(importFileStr);
  122. }
  123. QtViewPaneManager::instance()->CloseAllNonStandardPanes();
  124. // Import layout
  125. if (!layouts.isEmpty())
  126. {
  127. ImportLayouts(importFileStr, layouts);
  128. }
  129. }
  130. void CSettingsManagerDialog::ImportSettings(QString file)
  131. {
  132. if (QFile::exists(file))
  133. {
  134. GetIEditor()->GetSettingsManager()->ImportSettings(file);
  135. }
  136. }
  137. void CSettingsManagerDialog::ImportLayouts(QString file, const QStringList& layouts)
  138. {
  139. auto viewPaneManager = QtViewPaneManager::instance();
  140. if (!layouts.isEmpty())
  141. {
  142. TToolNamesMap& allToolNames = *GetIEditor()->GetSettingsManager()->GetToolNames();
  143. if (allToolNames.empty())
  144. {
  145. return;
  146. }
  147. TToolNamesMap toolNames;
  148. for (const QString& layoutStr : layouts)
  149. {
  150. auto it = std::find_if(allToolNames.begin(), allToolNames.end(),
  151. [&](const std::pair<QString, QString>& v)
  152. {
  153. return layoutStr == v.second;
  154. });
  155. if (it == allToolNames.end())
  156. {
  157. continue;
  158. }
  159. toolNames.insert(*it);
  160. }
  161. // Remove previous tool node
  162. XmlNodeRef layoutNode = XmlHelpers::CreateXmlNode(EDITOR_LAYOUT_NODE);
  163. // Find in file and add selected tool node
  164. GetIEditor()->GetSettingsManager()->GetMatchingLayoutNames(toolNames, layoutNode, file);
  165. // restore main window layout
  166. auto it = toolNames.find(QStringLiteral(MAINFRM_LAYOUT_NORMAL));
  167. if (it == toolNames.end())
  168. {
  169. it = toolNames.find(QStringLiteral(MAINFRM_LAYOUT_PREVIEW));
  170. }
  171. if (it != toolNames.end())
  172. {
  173. const QString& className = it->first;
  174. XmlNodeRef dockingLayoutNode = layoutNode->findChild(className.toUtf8().data());
  175. if (dockingLayoutNode)
  176. {
  177. viewPaneManager->DeserializeLayout(dockingLayoutNode);
  178. }
  179. }
  180. // restore tool panes layouts
  181. for (it = toolNames.begin(); it != toolNames.end(); ++it)
  182. {
  183. const QString& className = it->first;
  184. const QString& paneName = it->second;
  185. if (className == QStringLiteral(MAINFRM_LAYOUT_NORMAL) || className == QStringLiteral(MAINFRM_LAYOUT_PREVIEW))
  186. {
  187. continue;
  188. }
  189. XmlNodeRef dockingLayoutNode = layoutNode->findChild(className.toUtf8().data());
  190. if (!dockingLayoutNode)
  191. {
  192. continue;
  193. }
  194. auto toolPanel = FindViewPane<QMainWindow>(paneName);
  195. if (!toolPanel)
  196. {
  197. continue;
  198. }
  199. XmlNodeRef windowStateNode = dockingLayoutNode->findChild("WindowState");
  200. if (windowStateNode)
  201. {
  202. toolPanel->restoreState(QByteArray::fromHex(windowStateNode->getContent()));
  203. }
  204. }
  205. }
  206. }
  207. void CSettingsManagerDialog::OnCloseAllTools()
  208. {
  209. auto viewPaneManager = QtViewPaneManager::instance();
  210. viewPaneManager->CloseAllNonStandardPanes();
  211. viewPaneManager->SaveLayout();
  212. }
  213. void CSettingsManagerDialog::OnSelectionChanged()
  214. {
  215. ui->m_importBtn->setEnabled(ui->m_layoutListBox->selectedItems().count());
  216. }
  217. #include <moc_SettingsManagerDialog.cpp>