ErrorDialog.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 "ErrorDialog.h"
  10. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  11. #include <ui_ErrorDialog.h>
  12. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  13. namespace SandboxEditor
  14. {
  15. ErrorDialog::ErrorDialog(QWidget* parent)
  16. : QDialog(parent)
  17. , m_ui(new Ui::ErrorLogDialog())
  18. {
  19. m_ui->setupUi(this);
  20. connect(m_ui->okButton, &QPushButton::clicked, this, &ErrorDialog::OnOK);
  21. connect(
  22. m_ui->messages,
  23. &QTreeWidget::itemSelectionChanged,
  24. this,
  25. &ErrorDialog::MessageSelectionChanged);
  26. }
  27. ErrorDialog::~ErrorDialog()
  28. {
  29. }
  30. void ErrorDialog::OnOK()
  31. {
  32. close();
  33. }
  34. void ErrorDialog::AddMessages(MessageType messageType, const AZStd::list<QString>& messages)
  35. {
  36. AZ_Assert(m_ui != nullptr, "ErrorDialog's AddMessages cannot be used without a valid UI.");
  37. for (const QString& message : messages)
  38. {
  39. // filter out duplicate messages
  40. if (m_uniqueStrings.contains(message))
  41. {
  42. continue;
  43. }
  44. m_uniqueStrings.insert(message);
  45. // Attempt to split the message up by newline, so the list of errors can be kept shorter.
  46. // This returns a QStringList with one element, the original string, if the split fails.
  47. QStringList messageSplitPerLine = message.split('\n');
  48. QStringList messageList;
  49. // MessageColumn::MessageType
  50. messageList << GetMessageTypeString(messageType);
  51. // MessageColumn::ShortMessage
  52. messageList << messageSplitPerLine[0];
  53. // MessageColumn::DetailedMessage
  54. messageList << message;
  55. // Add the message to the tree widget at the root.
  56. m_ui->messages->insertTopLevelItem(0, new QTreeWidgetItem(messageList));
  57. }
  58. }
  59. void ErrorDialog::MessageSelectionChanged()
  60. {
  61. AZ_Assert(m_ui != nullptr, "ErrorDialog's MessageSelectionChanged cannot be used without a valid UI.");
  62. AZ_Assert(m_ui->messages != nullptr, "ErrorDialog's MessageSelectionChanged cannot be used without a valid messages QTreeWidget.");
  63. AZ_Assert(m_ui->details != nullptr, "ErrorDialog's MessageSelectionChanged cannot be used without a valid details QLabel.");
  64. QList<QTreeWidgetItem*> selectedItem = m_ui->messages->selectedItems();
  65. QTreeWidgetItem* firstSelected = !selectedItem.isEmpty() ? selectedItem.first() : nullptr;
  66. if (firstSelected)
  67. {
  68. m_ui->details->setText(firstSelected->text((int)MessageColumn::DetailedMessage));
  69. }
  70. }
  71. QString ErrorDialog::GetMessageTypeString(MessageType messageType) const
  72. {
  73. switch (messageType)
  74. {
  75. case ErrorDialog::MessageType::Warning:
  76. return QObject::tr("Warning");
  77. case MessageType::Error:
  78. return QObject::tr("Error");
  79. default:
  80. {
  81. AZ_Warning(
  82. "ErrorDialog",
  83. false,
  84. "No message type string is available for message type %u",
  85. messageType);
  86. return QObject::tr("Unknown");
  87. }
  88. }
  89. }
  90. }
  91. #include <moc_ErrorDialog.cpp>