ErrorDialog.cpp 3.4 KB

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