ToolsConfigPage.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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 "ToolsConfigPage.h"
  10. // Qt
  11. #include <QCompleter>
  12. #include <QMessageBox>
  13. // AzToolsFramework
  14. #include <AzToolsFramework/API/EditorPythonConsoleBus.h>
  15. // AzQtComponents
  16. #include <AzQtComponents/Components/Widgets/TabWidget.h>
  17. // Editor
  18. #include "Settings.h"
  19. #include "MainWindow.h"
  20. #include "ToolBox.h"
  21. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  22. #include <ui_ToolsConfigPage.h>
  23. #include <ui_IconListDialog.h>
  24. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  25. namespace
  26. {
  27. QColor COLOR_FOR_EDITOR_COMMAND = QColor(0, 255, 0);
  28. QColor COLOR_FOR_CONSOLE_COMMAND = QColor(0, 0, 255);
  29. QColor COLOR_FOR_TOGGLE_COMMAND = QColor(128, 0, 255);
  30. QColor COLOR_FOR_INVALID_COMMAND = QColor(255, 0, 0);
  31. };
  32. class IconListModel
  33. : public QAbstractListModel
  34. {
  35. public:
  36. IconListModel(QObject* parent = nullptr)
  37. : QAbstractListModel(parent)
  38. {
  39. IFileUtil::FileArray pngFiles;
  40. // Search for the png files in Editor/UI/Icons folder
  41. // and add them to the image list.
  42. const int iconSize = 32; // Currently, accepts the image of this size only.
  43. if (gSettings.searchPaths[EDITOR_PATH_UI_ICONS].empty())
  44. {
  45. return;
  46. }
  47. const QString iconsDir = gSettings.searchPaths[EDITOR_PATH_UI_ICONS][0];
  48. CFileUtil::ScanDirectory(iconsDir, "*.png", pngFiles);
  49. m_iconImages.reserve(static_cast<int>(pngFiles.size()));
  50. m_iconFiles.reserve(static_cast<int>(pngFiles.size()));
  51. for (size_t i = 0; i < pngFiles.size(); ++i)
  52. {
  53. const QString path = Path::Make(iconsDir, pngFiles[i].filename);
  54. QPixmap bm(path);
  55. if (bm.isNull())
  56. {
  57. continue;
  58. }
  59. if (bm.width() == iconSize && bm.height() == iconSize)
  60. {
  61. QIcon icon(bm);
  62. icon.addPixmap(bm, QIcon::Selected);
  63. m_iconImages.push_back(icon);
  64. m_iconFiles.push_back(path);
  65. }
  66. }
  67. }
  68. int rowCount(const QModelIndex& parent = QModelIndex()) const override
  69. {
  70. return parent.isValid() ? 0 : m_iconImages.count();
  71. }
  72. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override
  73. {
  74. if (!index.isValid() || index.row() >= rowCount(index.parent()))
  75. {
  76. return QVariant();
  77. }
  78. switch (role)
  79. {
  80. case Qt::DisplayRole:
  81. return m_iconFiles[index.row()];
  82. case Qt::DecorationRole:
  83. return m_iconImages[index.row()];
  84. default:
  85. return QVariant();
  86. }
  87. }
  88. private:
  89. QVector<QIcon> m_iconImages;
  90. QStringList m_iconFiles;
  91. };
  92. CIconListDialog::CIconListDialog(QWidget* pParent /* = nullptr */)
  93. : QDialog(pParent)
  94. , m_ui(new Ui::IconListDialog)
  95. {
  96. m_ui->setupUi(this);
  97. m_ui->m_iconListCtrl->setModel(new IconListModel(this));
  98. }
  99. bool CIconListDialog::GetSelectedIconPath(QString& path) const
  100. {
  101. if (!m_ui->m_iconListCtrl->currentIndex().isValid())
  102. {
  103. return false;
  104. }
  105. path = m_ui->m_iconListCtrl->currentIndex().data().toString();
  106. return true;
  107. }
  108. class CommandModel
  109. : public QAbstractListModel
  110. {
  111. public:
  112. CommandModel(QObject* parent = nullptr)
  113. : QAbstractListModel(parent)
  114. {
  115. }
  116. void setMacroIndex(const QModelIndex& macroIndex)
  117. {
  118. if (m_macroIndex == macroIndex)
  119. {
  120. return;
  121. }
  122. beginResetModel();
  123. m_macroIndex = macroIndex;
  124. endResetModel();
  125. }
  126. bool addRow()
  127. {
  128. if (macro() == nullptr)
  129. {
  130. return false;
  131. }
  132. beginInsertRows(QModelIndex(), rowCount(), rowCount());
  133. macro()->AddCommand(CToolBoxCommand::eT_INVALID_COMMAND, "nop");
  134. endInsertRows();
  135. return true;
  136. }
  137. bool moveRow(int row, bool up)
  138. {
  139. const int targetRow = up ? row - 1 : row + 1;
  140. if (row < 0 || row >= rowCount() || targetRow < 0 || targetRow >= rowCount())
  141. {
  142. return false;
  143. }
  144. if (up)
  145. {
  146. beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1);
  147. }
  148. else
  149. {
  150. beginMoveRows(QModelIndex(), row + 1, row + 1, QModelIndex(), row);
  151. }
  152. macro()->SwapCommand(row, targetRow);
  153. endMoveRows();
  154. return true;
  155. }
  156. bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override
  157. {
  158. if (row < 0 || row + count - 1 >= rowCount(parent))
  159. {
  160. return false;
  161. }
  162. beginRemoveRows(QModelIndex(), row, row + count - 1);
  163. for (int r = row + count - 1; r >= row; --r)
  164. {
  165. macro()->RemoveCommand(r);
  166. }
  167. endRemoveRows();
  168. return true;
  169. }
  170. int rowCount(const QModelIndex& parent = QModelIndex()) const override
  171. {
  172. return parent.isValid() || macro() == nullptr ? 0 : macro()->GetCommandCount();
  173. }
  174. bool setData(const QModelIndex& index, [[maybe_unused]] const QVariant& value, int role = Qt::EditRole) override
  175. {
  176. if (!index.isValid() || index.row() >= rowCount(index.parent()))
  177. {
  178. return false;
  179. }
  180. if (role != Qt::UserRole)
  181. {
  182. return false;
  183. }
  184. // this has already been set
  185. emit dataChanged(index, index);
  186. return true;
  187. }
  188. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override
  189. {
  190. if (!index.isValid() || index.row() >= rowCount(index.parent()))
  191. {
  192. return QVariant();
  193. }
  194. auto command = macro()->GetCommandAt(index.row());
  195. switch (role)
  196. {
  197. case Qt::DisplayRole:
  198. case Qt::EditRole:
  199. return command->m_text;
  200. case Qt::ForegroundRole:
  201. switch (command->m_type)
  202. {
  203. case CToolBoxCommand::eT_SCRIPT_COMMAND:
  204. return QVariant::fromValue(COLOR_FOR_EDITOR_COMMAND);
  205. case CToolBoxCommand::eT_CONSOLE_COMMAND:
  206. return command->m_bVariableToggle ? QVariant::fromValue(COLOR_FOR_TOGGLE_COMMAND) : QVariant::fromValue(COLOR_FOR_CONSOLE_COMMAND);
  207. default:
  208. return QVariant::fromValue(COLOR_FOR_INVALID_COMMAND);
  209. }
  210. case Qt::UserRole:
  211. return QVariant::fromValue(command);
  212. default:
  213. return QVariant();
  214. }
  215. }
  216. private:
  217. CToolBoxMacro* macro() const
  218. {
  219. return m_macroIndex.data(Qt::UserRole).value<CToolBoxMacro*>();
  220. }
  221. QPersistentModelIndex m_macroIndex;
  222. };
  223. class MacroModel
  224. : public QAbstractListModel
  225. {
  226. public:
  227. MacroModel(QObject* parent = nullptr)
  228. : QAbstractListModel(parent)
  229. , m_hasEmptyRow(false)
  230. , m_currentlyRemovingRows(false)
  231. {
  232. }
  233. bool moveRow(int row, bool up)
  234. {
  235. if (m_hasEmptyRow)
  236. {
  237. return false;
  238. }
  239. const int targetRow = up ? row - 1 : row + 1;
  240. if (row < 0 || row >= rowCount() || targetRow < 0 || targetRow >= rowCount())
  241. {
  242. return false;
  243. }
  244. if (up)
  245. {
  246. beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1);
  247. }
  248. else
  249. {
  250. beginMoveRows(QModelIndex(), row + 1, row + 1, QModelIndex(), row);
  251. }
  252. GetIEditor()->GetToolBoxManager()->SwapMacro(row, targetRow, true);
  253. endMoveRows();
  254. return true;
  255. }
  256. int rowCount(const QModelIndex& parent = QModelIndex()) const override
  257. {
  258. return parent.isValid() ? 0 : (GetIEditor()->GetToolBoxManager()->GetMacroCount(true) + (m_hasEmptyRow ? 1 : 0));
  259. }
  260. QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override
  261. {
  262. if (!index.isValid() || index.row() >= rowCount(index.parent()))
  263. {
  264. return QVariant();
  265. }
  266. if (isEmptyRow(index))
  267. {
  268. switch (role)
  269. {
  270. case Qt::DisplayRole:
  271. case Qt::EditRole:
  272. return QString();
  273. default:
  274. return QVariant();
  275. }
  276. }
  277. auto macro = GetIEditor()->GetToolBoxManager()->GetMacro(index.row(), true);
  278. switch (role)
  279. {
  280. case Qt::DisplayRole:
  281. case Qt::EditRole:
  282. return macro->GetTitle();
  283. case Qt::UserRole:
  284. return QVariant::fromValue(macro);
  285. default:
  286. return QVariant();
  287. }
  288. }
  289. bool setData(const QModelIndex& index, const QVariant& data, [[maybe_unused]] int role = Qt::EditRole) override
  290. {
  291. if (!index.isValid() || index.row() >= rowCount(index.parent()))
  292. {
  293. return false;
  294. }
  295. // Retrieve the dialog parent widget for this model so we can display
  296. // error popups properly (if needed)
  297. QWidget* parentWidget = qobject_cast<QWidget*>(parent());
  298. // isNull and isValid in Qvariant/QString in this case cannot detect null input
  299. // check null data input
  300. if (data.toString().isEmpty())
  301. {
  302. if (!m_currentlyRemovingRows)
  303. {
  304. QMessageBox::critical(parentWidget, QString(), tr("Please enter a valid name!"));
  305. // If this is a newly added empty row, then just delete it
  306. // Otherwise if the user was renaming an existing row, the previous
  307. // value will be restored
  308. if (isEmptyRow(index))
  309. {
  310. removeRow(index.row());
  311. assert(!m_hasEmptyRow);
  312. }
  313. }
  314. return false;
  315. }
  316. if (isEmptyRow(index))
  317. {
  318. auto macro = GetIEditor()->GetToolBoxManager()->NewMacro(data.toString(), true, nullptr);
  319. if (macro == nullptr)
  320. {
  321. QMessageBox::critical(parentWidget, QString(), tr("There is a macro of that name, already!"));
  322. removeRow(index.row());
  323. assert(!m_hasEmptyRow);
  324. return false;
  325. }
  326. emit dataChanged(index, index);
  327. m_hasEmptyRow = false;
  328. }
  329. else if (GetIEditor()->GetToolBoxManager()->SetMacroTitle(index.row(), data.toString(), true))
  330. {
  331. emit dataChanged(index, index);
  332. return true;
  333. }
  334. else
  335. {
  336. QMessageBox::critical(parentWidget, QString(), tr("There is a macro of that name, already!"));
  337. }
  338. return false;
  339. }
  340. Qt::ItemFlags flags(const QModelIndex& index) const override
  341. {
  342. return QAbstractListModel::flags(index) | Qt::ItemIsEditable;
  343. }
  344. bool addRow()
  345. {
  346. if (m_hasEmptyRow)
  347. {
  348. return false;
  349. }
  350. beginInsertRows(QModelIndex(), rowCount(), rowCount());
  351. m_hasEmptyRow = true;
  352. endInsertRows();
  353. return true;
  354. }
  355. bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override
  356. {
  357. if (row < 0 || row + count - 1 >= rowCount(parent))
  358. {
  359. return false;
  360. }
  361. m_currentlyRemovingRows = true;
  362. beginRemoveRows(QModelIndex(), row, row + count - 1);
  363. auto tools = GetIEditor()->GetToolBoxManager();
  364. for (int r = row + count - 1; r >= row; --r)
  365. {
  366. if (r == rowCount() - 1 && m_hasEmptyRow)
  367. {
  368. m_hasEmptyRow = false;
  369. }
  370. else
  371. {
  372. tools->RemoveMacro(r, true);
  373. }
  374. }
  375. endRemoveRows();
  376. m_currentlyRemovingRows = false;
  377. return true;
  378. }
  379. private:
  380. bool m_hasEmptyRow;
  381. bool m_currentlyRemovingRows;
  382. // Empty row is the last row in the list if the proper flag is set
  383. bool isEmptyRow(const QModelIndex& index) const
  384. {
  385. return m_hasEmptyRow && index.row() == rowCount() - 1;
  386. }
  387. };
  388. // CToolsConfigPage dialog
  389. ToolsConfigDialog::ToolsConfigDialog(QWidget* parent)
  390. : QDialog(parent)
  391. {
  392. setWindowTitle(tr("Configure ToolBox Macros"));
  393. setLayout(new QVBoxLayout);
  394. AzQtComponents::TabWidget* tabs = new AzQtComponents::TabWidget;
  395. AzQtComponents::TabWidget::applySecondaryStyle(tabs, false);
  396. layout()->addWidget(tabs);
  397. CToolsConfigPage* page = new CToolsConfigPage;
  398. tabs->addTab(page, page->windowTitle());
  399. QDialogButtonBox* buttons = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
  400. layout()->addWidget(buttons);
  401. connect(buttons, &QDialogButtonBox::accepted, page, &CToolsConfigPage::OnOK);
  402. connect(buttons, &QDialogButtonBox::rejected, page, &CToolsConfigPage::OnCancel);
  403. connect(buttons, &QDialogButtonBox::accepted, this, &QDialog::accept);
  404. connect(buttons, &QDialogButtonBox::rejected, this, &QDialog::reject);
  405. }
  406. void ToolsConfigDialog::reject()
  407. {
  408. // Revert to the original.
  409. GetIEditor()->GetToolBoxManager()->Load();
  410. QDialog::reject();
  411. }
  412. void ToolsConfigDialog::closeEvent(QCloseEvent* ev)
  413. {
  414. reject();
  415. QDialog::closeEvent(ev);
  416. }
  417. CToolsConfigPage::CToolsConfigPage(QWidget* parent)
  418. : QWidget(parent)
  419. , m_macroModel(new MacroModel(this))
  420. , m_commandModel(new CommandModel(this))
  421. , m_completionModel(new QStringListModel(this))
  422. , m_ui(new Ui::ToolsConfigPage)
  423. {
  424. m_ui->setupUi(this);
  425. m_ui->m_macroCmd->setCompleter(new QCompleter(m_completionModel));
  426. OnInitDialog();
  427. }
  428. CToolsConfigPage::~CToolsConfigPage()
  429. {
  430. }
  431. // CToolsConfigPage message handlers
  432. void CToolsConfigPage::OnInitDialog()
  433. {
  434. m_ui->m_macroList->setModel(m_macroModel);
  435. m_ui->m_commandList->setModel(m_commandModel);
  436. connect(m_ui->m_assignCommand, &QPushButton::clicked, this, &CToolsConfigPage::OnAssignCommand);
  437. connect(m_ui->m_selectIcon, &QPushButton::clicked, this, &CToolsConfigPage::OnSelectMacroIcon);
  438. connect(m_ui->m_clearIcon, &QPushButton::clicked, this, &CToolsConfigPage::OnClearMacroIcon);
  439. connect(m_ui->m_console, &QRadioButton::clicked, this, &CToolsConfigPage::OnConsoleCmd);
  440. connect(m_ui->m_script, &QRadioButton::clicked, this, &CToolsConfigPage::OnScriptCmd);
  441. connect(m_ui->m_macroList->selectionModel(), &QItemSelectionModel::currentChanged, this, &CToolsConfigPage::OnSelchangeMacroList);
  442. connect(m_ui->m_buttonMacroNew, &QToolButton::clicked, this, &CToolsConfigPage::OnNewMacroItem);
  443. connect(m_ui->m_buttonMacroUp, &QToolButton::clicked, this, &CToolsConfigPage::OnMoveMacroItemUp);
  444. connect(m_ui->m_buttonMacroDown, &QToolButton::clicked, this, &CToolsConfigPage::OnMoveMacroItemDown);
  445. connect(m_ui->m_buttonMacroDelete, &QToolButton::clicked, this, &CToolsConfigPage::OnDeleteMacroItem);
  446. connect(m_ui->m_commandList->selectionModel(), &QItemSelectionModel::currentChanged, this, &CToolsConfigPage::OnSelchangeCommandList);
  447. connect(m_ui->m_buttonCommandNew, &QToolButton::clicked, this, &CToolsConfigPage::OnNewCommandItem);
  448. connect(m_ui->m_buttonCommandUp, &QToolButton::clicked, this, &CToolsConfigPage::OnMoveCommandItemUp);
  449. connect(m_ui->m_buttonCommandDown, &QToolButton::clicked, this, &CToolsConfigPage::OnMoveCommandItemDown);
  450. connect(m_ui->m_buttonCommandDelete, &QToolButton::clicked, this, &CToolsConfigPage::OnDeleteCommandItem);
  451. m_consoleOrScript = 1; // To force the change in the next line
  452. OnConsoleCmd();
  453. // To ensure the proper disabling of controls
  454. OnSelchangeMacroList();
  455. OnSelchangeCommandList();
  456. }
  457. //////////////////////////////////////////////////////////////////////////
  458. void CToolsConfigPage::OnOK()
  459. {
  460. GetIEditor()->GetToolBoxManager()->Save();
  461. }
  462. //////////////////////////////////////////////////////////////////////////
  463. void CToolsConfigPage::OnCancel()
  464. {
  465. // Revert to the original.
  466. GetIEditor()->GetToolBoxManager()->Load();
  467. }
  468. //////////////////////////////////////////////////////////////////////////
  469. void CToolsConfigPage::OnSelchangeMacroList()
  470. {
  471. /// Update the command list.
  472. auto macro = m_ui->m_macroList->currentIndex().data(Qt::UserRole).value<CToolBoxMacro*>();
  473. m_commandModel->setMacroIndex(m_ui->m_macroList->currentIndex());
  474. if (m_ui->m_macroList->currentIndex().isValid())
  475. {
  476. /// Update the icon.
  477. const QPixmap icon(macro != nullptr ? macro->GetIconPath() : QString());
  478. m_ui->m_macroIcon->setPixmap(icon);
  479. m_ui->m_selectIcon->setEnabled(true);
  480. m_ui->m_clearIcon->setEnabled(true);
  481. }
  482. else
  483. {
  484. m_ui->m_selectIcon->setEnabled(false);
  485. m_ui->m_clearIcon->setEnabled(false);
  486. }
  487. m_ui->m_commandList->selectionModel()->clear();
  488. OnSelchangeCommandList();
  489. }
  490. //////////////////////////////////////////////////////////////////////////
  491. void CToolsConfigPage::OnNewMacroItem()
  492. {
  493. if (m_macroModel->addRow())
  494. {
  495. const QModelIndex index = m_macroModel->index(m_macroModel->rowCount() - 1, 0);
  496. m_ui->m_macroList->setCurrentIndex(index);
  497. m_ui->m_macroList->edit(index);
  498. }
  499. }
  500. //////////////////////////////////////////////////////////////////////////
  501. void CToolsConfigPage::OnSelchangeCommandList()
  502. {
  503. auto commandIndex = m_ui->m_commandList->currentIndex();
  504. if (!commandIndex.isValid())
  505. {
  506. m_ui->m_assignCommand->setEnabled(false);
  507. m_ui->m_macroCmd->setEnabled(false);
  508. m_ui->m_macroCmd->clear();
  509. m_ui->m_toggleVar->setEnabled(false);
  510. m_ui->m_toggleVar->setChecked(false);
  511. m_ui->m_console->setEnabled(false);
  512. m_ui->m_script->setEnabled(false);
  513. }
  514. else
  515. {
  516. m_ui->m_assignCommand->setEnabled(true);
  517. m_ui->m_macroCmd->setEnabled(true);
  518. m_ui->m_console->setEnabled(true);
  519. m_ui->m_script->setEnabled(true);
  520. CToolBoxCommand* pCommand = commandIndex.data(Qt::UserRole).value<CToolBoxCommand*>();
  521. if (pCommand->m_type == CToolBoxCommand::eT_SCRIPT_COMMAND)
  522. {
  523. m_ui->m_macroCmd->setText(pCommand->m_text);
  524. OnScriptCmd();
  525. }
  526. else if (pCommand->m_type == CToolBoxCommand::eT_CONSOLE_COMMAND)
  527. {
  528. m_ui->m_macroCmd->setText(pCommand->m_text);
  529. OnConsoleCmd();
  530. m_ui->m_toggleVar->setChecked(pCommand->m_bVariableToggle);
  531. m_ui->m_toggleVar->setEnabled(true);
  532. }
  533. else
  534. {
  535. m_ui->m_macroCmd->clear();
  536. OnConsoleCmd();
  537. m_ui->m_toggleVar->setChecked(false);
  538. m_ui->m_toggleVar->setEnabled(true);
  539. }
  540. }
  541. }
  542. //////////////////////////////////////////////////////////////////////////
  543. void CToolsConfigPage::OnNewCommandItem()
  544. {
  545. if (m_commandModel->addRow())
  546. {
  547. m_ui->m_commandList->setCurrentIndex(m_commandModel->index(m_commandModel->rowCount() - 1));
  548. }
  549. }
  550. //////////////////////////////////////////////////////////////////////////
  551. void CToolsConfigPage::OnAssignCommand()
  552. {
  553. auto commandIndex = m_ui->m_commandList->currentIndex();
  554. if (commandIndex.isValid())
  555. {
  556. CToolBoxCommand* pCommand = commandIndex.data(Qt::UserRole).value<CToolBoxCommand*>();
  557. pCommand->m_type = m_consoleOrScript ? CToolBoxCommand::eT_SCRIPT_COMMAND
  558. : CToolBoxCommand::eT_CONSOLE_COMMAND;
  559. pCommand->m_text = m_ui->m_macroCmd->text();
  560. if (pCommand->m_type == CToolBoxCommand::eT_CONSOLE_COMMAND)
  561. {
  562. pCommand->m_bVariableToggle = m_ui->m_toggleVar->isChecked();
  563. }
  564. else
  565. {
  566. pCommand->m_bVariableToggle = false;
  567. }
  568. m_commandModel->setData(commandIndex, QVariant::fromValue(pCommand), Qt::UserRole);
  569. }
  570. }
  571. //////////////////////////////////////////////////////////////////////////
  572. void CToolsConfigPage::OnMoveMacroItemUp()
  573. {
  574. auto macroIndex = m_ui->m_macroList->currentIndex();
  575. m_macroModel->moveRow(macroIndex.row(), true);
  576. }
  577. //////////////////////////////////////////////////////////////////////////
  578. void CToolsConfigPage::OnMoveMacroItemDown()
  579. {
  580. auto macroIndex = m_ui->m_macroList->currentIndex();
  581. m_macroModel->moveRow(macroIndex.row(), false);
  582. }
  583. //////////////////////////////////////////////////////////////////////////
  584. void CToolsConfigPage::OnMoveCommandItemUp()
  585. {
  586. auto commandIndex = m_ui->m_commandList->currentIndex();
  587. m_commandModel->moveRow(commandIndex.row(), true);
  588. }
  589. //////////////////////////////////////////////////////////////////////////
  590. void CToolsConfigPage::OnMoveCommandItemDown()
  591. {
  592. auto commandIndex = m_ui->m_commandList->currentIndex();
  593. m_commandModel->moveRow(commandIndex.row(), false);
  594. }
  595. //////////////////////////////////////////////////////////////////////////
  596. void CToolsConfigPage::OnDeleteMacroItem()
  597. {
  598. m_macroModel->removeRow(m_ui->m_macroList->currentIndex().row());
  599. }
  600. //////////////////////////////////////////////////////////////////////////
  601. void CToolsConfigPage::OnDeleteCommandItem()
  602. {
  603. m_commandModel->removeRow(m_ui->m_commandList->currentIndex().row());
  604. }
  605. //////////////////////////////////////////////////////////////////////////
  606. void CToolsConfigPage::OnSelectMacroIcon()
  607. {
  608. auto macroIndex = m_ui->m_macroList->currentIndex();
  609. if (!macroIndex.isValid())
  610. {
  611. return;
  612. }
  613. CToolBoxMacro* pMacro = macroIndex.data(Qt::UserRole).value<CToolBoxMacro*>();
  614. CIconListDialog iconListDlg;
  615. if (iconListDlg.exec() == QDialog::Accepted)
  616. {
  617. QString iconPath;
  618. if (iconListDlg.GetSelectedIconPath(iconPath))
  619. {
  620. const QPixmap pixmap(iconPath);
  621. m_ui->m_macroIcon->setPixmap(pixmap);
  622. pMacro->SetIconPath(iconPath.toUtf8().data());
  623. }
  624. }
  625. }
  626. //////////////////////////////////////////////////////////////////////////
  627. void CToolsConfigPage::OnClearMacroIcon()
  628. {
  629. auto macroIndex = m_ui->m_macroList->currentIndex();
  630. if (!macroIndex.isValid())
  631. {
  632. return;
  633. }
  634. CToolBoxMacro* pMacro = macroIndex.data(Qt::UserRole).value<CToolBoxMacro*>();
  635. m_ui->m_macroIcon->setPixmap(QPixmap());
  636. pMacro->SetIconPath("");
  637. }
  638. //////////////////////////////////////////////////////////////////////////
  639. void CToolsConfigPage::FillConsoleCmds()
  640. {
  641. QStringList commands;
  642. IConsole* console = GetIEditor()->GetSystem()->GetIConsole();
  643. AZStd::vector<AZStd::string_view> cmds;
  644. cmds.resize(console->GetNumVars());
  645. size_t cmdCount = console->GetSortedVars(cmds);
  646. for (int i = 0; i < cmdCount; ++i)
  647. {
  648. commands.push_back(cmds[i].data());
  649. }
  650. m_completionModel->setStringList(commands);
  651. }
  652. //////////////////////////////////////////////////////////////////////////
  653. void CToolsConfigPage::FillScriptCmds()
  654. {
  655. // Add module names to the auto-completion list.
  656. QStringList commands;
  657. using namespace AzToolsFramework;
  658. EditorPythonConsoleInterface* editorPythonConsoleInterface = AZ::Interface<EditorPythonConsoleInterface>::Get();
  659. if (editorPythonConsoleInterface)
  660. {
  661. EditorPythonConsoleInterface::GlobalFunctionCollection globalFunctionCollection;
  662. editorPythonConsoleInterface->GetGlobalFunctionList(globalFunctionCollection);
  663. commands.reserve(static_cast<int>(globalFunctionCollection.size()));
  664. for (const EditorPythonConsoleInterface::GlobalFunction& globalFunction : globalFunctionCollection)
  665. {
  666. const QString fullCmd = QString("%1.%2()").arg(globalFunction.m_moduleName.data()).arg(globalFunction.m_functionName.data());
  667. commands.push_back(fullCmd);
  668. }
  669. }
  670. m_completionModel->setStringList(commands);
  671. }
  672. //////////////////////////////////////////////////////////////////////////
  673. void CToolsConfigPage::OnConsoleCmd()
  674. {
  675. m_ui->m_console->setChecked(true);
  676. if (m_consoleOrScript == 0)
  677. {
  678. return;
  679. }
  680. m_consoleOrScript = 0;
  681. FillConsoleCmds();
  682. m_ui->m_toggleVar->setEnabled(true);
  683. m_ui->m_toggleVar->setChecked(false);
  684. }
  685. //////////////////////////////////////////////////////////////////////////
  686. void CToolsConfigPage::OnScriptCmd()
  687. {
  688. m_ui->m_script->setChecked(true);
  689. if (m_consoleOrScript == 1)
  690. {
  691. return;
  692. }
  693. m_consoleOrScript = 1;
  694. FillScriptCmds();
  695. m_ui->m_toggleVar->setEnabled(false);
  696. m_ui->m_toggleVar->setChecked(false);
  697. }
  698. #include <moc_ToolsConfigPage.cpp>