ToolsConfigPage.cpp 24 KB

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