ErrorReportDialog.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 "ErrorReportDialog.h"
  10. // Qt
  11. #include <QClipboard>
  12. #include <QDesktopServices>
  13. #include <QFile>
  14. #include <QKeyEvent>
  15. #include <QMenu>
  16. #include <QUrl>
  17. #include <QDateTime>
  18. // AzToolsFramework
  19. #include <AzToolsFramework/API/ToolsApplicationAPI.h>
  20. #include <AzToolsFramework/API/ViewPaneOptions.h>
  21. // Editor
  22. #include "ErrorReportTableModel.h"
  23. #include "Viewport.h"
  24. #include "Util/Mailer.h"
  25. #include "GameEngine.h"
  26. #include "LyViewPaneNames.h"
  27. AZ_PUSH_DISABLE_DLL_EXPORT_MEMBER_WARNING
  28. #include <ui_ErrorReportDialog.h>
  29. AZ_POP_DISABLE_DLL_EXPORT_MEMBER_WARNING
  30. //////////////////////////////////////////////////////////////////////////
  31. CErrorReportDialog* CErrorReportDialog::m_instance = nullptr;
  32. // CErrorReportDialog dialog
  33. //////////////////////////////////////////////////////////////////////////
  34. void CErrorReportDialog::RegisterViewClass()
  35. {
  36. AzToolsFramework::ViewPaneOptions options;
  37. options.showInMenu = false;
  38. AzToolsFramework::RegisterViewPane<CErrorReportDialog>(LyViewPane::ErrorReport, LyViewPane::CategoryOther, options);
  39. }
  40. CErrorReportDialog::CErrorReportDialog(QWidget* parent)
  41. : QWidget(parent)
  42. , ui(new Ui::CErrorReportDialog)
  43. , m_errorReportModel(new CErrorReportTableModel(this))
  44. , m_sortIndicatorColumn(-1)
  45. , m_sortIndicatorOrder(Qt::AscendingOrder)
  46. {
  47. ui->setupUi(this);
  48. ui->treeView->setModel(m_errorReportModel);
  49. ui->treeView->header()->setContextMenuPolicy(Qt::CustomContextMenu);
  50. ui->treeView->header()->setSectionsMovable(true);
  51. ui->treeView->viewport()->setMouseTracking(true);
  52. ui->treeView->viewport()->installEventFilter(this);
  53. ui->treeView->header()->setSectionResizeMode(CErrorReportTableModel::ColumnSeverity, QHeaderView::ResizeToContents);
  54. ui->treeView->header()->resizeSection(CErrorReportTableModel::ColumnCount, 30);
  55. ui->treeView->header()->resizeSection(CErrorReportTableModel::ColumnText, 200);
  56. ui->treeView->header()->resizeSection(CErrorReportTableModel::ColumnFile, 150);
  57. ui->treeView->header()->resizeSection(CErrorReportTableModel::ColumnObject, 150);
  58. ui->treeView->header()->resizeSection(CErrorReportTableModel::ColumnModule, 100);
  59. ui->treeView->header()->resizeSection(CErrorReportTableModel::ColumnDescription, 100);
  60. ui->treeView->header()->resizeSection(CErrorReportTableModel::ColumnAssetScope, 200);
  61. connect(ui->treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(OnReportItemClick(QModelIndex)));
  62. connect(ui->treeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(OnReportItemDblClick(QModelIndex)));
  63. connect(ui->treeView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(OnReportItemRClick()));
  64. connect(ui->treeView->header(), SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(OnReportColumnRClick()));
  65. connect(ui->treeView->header(), SIGNAL(sortIndicatorChanged(int,Qt::SortOrder)), this, SLOT(OnSortIndicatorChanged(int,Qt::SortOrder)));
  66. ui->treeView->AddGroup(CErrorReportTableModel::ColumnModule);
  67. ui->treeView->header()->setSortIndicator(-1, Qt::AscendingOrder);
  68. m_instance = this;
  69. //CErrorReport *report,
  70. //m_pErrorReport = report;
  71. m_pErrorReport = nullptr;
  72. }
  73. CErrorReportDialog::~CErrorReportDialog()
  74. {
  75. m_instance = nullptr;
  76. }
  77. //////////////////////////////////////////////////////////////////////////
  78. void CErrorReportDialog::Open(CErrorReport* pReport)
  79. {
  80. if (!m_instance)
  81. {
  82. GetIEditor()->OpenView(LyViewPane::ErrorReport);
  83. }
  84. if (!m_instance)
  85. {
  86. return;
  87. }
  88. m_instance->SetReport(pReport);
  89. m_instance->UpdateErrors();
  90. m_instance->setFocus();
  91. }
  92. //////////////////////////////////////////////////////////////////////////
  93. void CErrorReportDialog::Close()
  94. {
  95. if (m_instance)
  96. {
  97. /*
  98. CCryMemFile memFile( new BYTE[256], 256, 256 );
  99. CArchive ar( &memFile, CArchive::store );
  100. m_instance->m_wndReport.SerializeState( ar );
  101. ar.Close();
  102. UINT nSize = (UINT)memFile.GetLength();
  103. LPBYTE pbtData = memFile.Detach();
  104. CXTRegistryManager regManager;
  105. regManager.WriteProfileBinary( "Dialogs\\ErrorReport", "Configuration", pbtData, nSize);
  106. if ( pbtData )
  107. delete [] pbtData;
  108. */
  109. m_instance->close();
  110. }
  111. }
  112. //////////////////////////////////////////////////////////////////////////
  113. void CErrorReportDialog::Clear()
  114. {
  115. if (m_instance)
  116. {
  117. m_instance->SetReport(nullptr);
  118. m_instance->UpdateErrors();
  119. }
  120. }
  121. bool CErrorReportDialog::eventFilter(QObject* watched, QEvent* event)
  122. {
  123. if (event->type() == QEvent::MouseMove && watched == ui->treeView->viewport())
  124. {
  125. QMouseEvent* ev = static_cast<QMouseEvent*>(event);
  126. const QModelIndex index = ui->treeView->indexAt(ev->pos());
  127. QRect rect = ui->treeView->visualRect(index);
  128. rect.moveTopLeft(ui->treeView->viewport()->mapToGlobal(rect.topLeft()));
  129. const QRect target = fontMetrics().boundingRect(rect, index.data(Qt::TextAlignmentRole).toInt(), index.data().toString());
  130. if (index.column() == CErrorReportTableModel::ColumnObject && target.contains(QCursor::pos()))
  131. {
  132. ui->treeView->viewport()->setCursor(Qt::PointingHandCursor);
  133. }
  134. else
  135. {
  136. ui->treeView->viewport()->setCursor(Qt::ArrowCursor);
  137. }
  138. }
  139. return QWidget::eventFilter(watched, event);
  140. }
  141. // CErrorReportDialog message handlers
  142. void CErrorReportDialog::SetReport(CErrorReport* report)
  143. {
  144. m_pErrorReport = report;
  145. m_errorReportModel->setErrorReport(report);
  146. }
  147. void CErrorReportDialog::UpdateErrors()
  148. {
  149. m_errorReportModel->setErrorReport(m_pErrorReport);
  150. }
  151. //////////////////////////////////////////////////////////////////////////
  152. #define ID_REMOVE_ITEM 1
  153. #define ID_SORT_ASC 2
  154. #define ID_SORT_DESC 3
  155. #define ID_GROUP_BYTHIS 4
  156. #define ID_SHOW_GROUPBOX 5
  157. #define ID_SHOW_FIELDCHOOSER 6
  158. #define ID_COLUMN_BESTFIT 7
  159. #define ID_COLUMN_ARRANGEBY 100
  160. #define ID_COLUMN_ALIGMENT 200
  161. #define ID_COLUMN_ALIGMENT_LEFT ID_COLUMN_ALIGMENT + 1
  162. #define ID_COLUMN_ALIGMENT_RIGHT ID_COLUMN_ALIGMENT + 2
  163. #define ID_COLUMN_ALIGMENT_CENTER ID_COLUMN_ALIGMENT + 3
  164. #define ID_COLUMN_SHOW 500
  165. void CErrorReportDialog::OnReportColumnRClick()
  166. {
  167. QHeaderView* header = ui->treeView->header();
  168. int column = header->logicalIndexAt(ui->treeView->mapFromGlobal(QCursor::pos()));
  169. if (column < 0)
  170. {
  171. return;
  172. }
  173. QMenu menu;
  174. QAction* actionSortAscending = menu.addAction(tr("Sort &Ascending"));
  175. QAction* actionSortDescending = menu.addAction(tr("Sort Des&cending"));
  176. menu.addSeparator();
  177. QAction* actionGroupByThis = menu.addAction(tr("&Group by this field"));
  178. QAction* actionGroupByBox = menu.addAction(tr("Group &by box"));
  179. menu.addSeparator();
  180. QAction* actionRemoveItem = menu.addAction(tr("&Remove column"));
  181. QAction* actionFieldChooser = menu.addAction(tr("Field &Chooser"));
  182. menu.addSeparator();
  183. QAction* actionBestFit = menu.addAction(tr("Best &Fit"));
  184. actionGroupByBox->setCheckable(true);
  185. actionGroupByBox->setChecked(ui->treeView->IsGroupsShown());
  186. // create arrange by items
  187. QMenu menuArrange;
  188. const int nColumnCount = m_errorReportModel->columnCount();
  189. for (int nColumn = 0; nColumn < nColumnCount; nColumn++)
  190. {
  191. if (!header->isSectionHidden(nColumn))
  192. {
  193. const QString sCaption = m_errorReportModel->headerData(nColumn, Qt::Horizontal).toString();
  194. if (!sCaption.isEmpty())
  195. {
  196. menuArrange.addAction(sCaption)->setData(nColumn);
  197. }
  198. }
  199. }
  200. menuArrange.addSeparator();
  201. QAction* actionClearGroups = menuArrange.addAction(tr("Clear groups"));
  202. menuArrange.setTitle(tr("Arrange By"));
  203. menu.insertMenu(actionSortAscending, &menuArrange);
  204. // create columns items
  205. QMenu menuColumns;
  206. for (int nColumn = 0; nColumn < nColumnCount; nColumn++)
  207. {
  208. const QString sCaption = m_errorReportModel->headerData(nColumn, Qt::Horizontal).toString();
  209. //if (!sCaption.isEmpty())
  210. QAction* action = menuColumns.addAction(sCaption);
  211. action->setCheckable(true);
  212. action->setChecked(!ui->treeView->header()->isSectionHidden(nColumn));
  213. }
  214. menuColumns.setTitle(tr("Columns"));
  215. menu.insertMenu(menuArrange.menuAction(), &menuColumns);
  216. //create Text alignment submenu
  217. QMenu menuAlign;
  218. QAction* actionAlignLeft = menuAlign.addAction(tr("Align Left"));
  219. QAction* actionAlignRight = menuAlign.addAction(tr("Align Right"));
  220. QAction* actionAlignCenter = menuAlign.addAction(tr("Align Center"));
  221. actionAlignLeft->setCheckable(true);
  222. actionAlignRight->setCheckable(true);
  223. actionAlignCenter->setCheckable(true);
  224. const int alignment = m_errorReportModel->headerData(column, Qt::Horizontal, Qt::TextAlignmentRole).toInt();
  225. actionAlignLeft->setChecked(alignment & Qt::AlignLeft);
  226. actionAlignRight->setChecked(alignment & Qt::AlignRight);
  227. actionAlignCenter->setChecked(alignment & Qt::AlignHCenter);
  228. menuAlign.setTitle(tr("&Alignment"));
  229. menu.insertMenu(actionBestFit, &menuAlign);
  230. // track menu
  231. QAction* nMenuResult = menu.exec(QCursor::pos());
  232. // arrange by items
  233. if (menuArrange.actions().contains(nMenuResult))
  234. {
  235. // group by item
  236. if (actionClearGroups == nMenuResult)
  237. {
  238. ui->treeView->ClearGroups();
  239. }
  240. else
  241. {
  242. column = nMenuResult->data().toInt();
  243. ui->treeView->ToggleSortOrder(column);
  244. }
  245. }
  246. // process Alignment options
  247. if (nMenuResult == actionAlignLeft)
  248. {
  249. m_errorReportModel->setHeaderData(column, Qt::Horizontal, Qt::AlignLeft, Qt::TextAlignmentRole);
  250. }
  251. else if (nMenuResult == actionAlignRight)
  252. {
  253. m_errorReportModel->setHeaderData(column, Qt::Horizontal, Qt::AlignRight, Qt::TextAlignmentRole);
  254. }
  255. else if (nMenuResult == actionAlignCenter)
  256. {
  257. m_errorReportModel->setHeaderData(column, Qt::Horizontal, Qt::AlignCenter, Qt::TextAlignmentRole);
  258. }
  259. // process column selection item
  260. if (menuColumns.actions().contains(nMenuResult))
  261. {
  262. ui->treeView->header()->setSectionHidden(menuColumns.actions().indexOf(nMenuResult), !nMenuResult->isChecked());
  263. }
  264. // other general items
  265. if (nMenuResult == actionSortAscending || nMenuResult == actionSortDescending)
  266. {
  267. ui->treeView->sortByColumn(column, nMenuResult == actionSortAscending ? Qt::AscendingOrder : Qt::DescendingOrder);
  268. }
  269. else if (nMenuResult == actionFieldChooser)
  270. {
  271. //OnShowFieldChooser();
  272. }
  273. else if (nMenuResult == actionBestFit)
  274. {
  275. ui->treeView->resizeColumnToContents(column);
  276. }
  277. else if (nMenuResult == actionRemoveItem)
  278. {
  279. ui->treeView->header()->setSectionHidden(column, true);
  280. }
  281. // other general items
  282. else if (nMenuResult == actionGroupByThis)
  283. {
  284. ui->treeView->AddGroup(column);
  285. ui->treeView->ShowGroups(true);
  286. }
  287. else if (nMenuResult == actionGroupByBox)
  288. {
  289. ui->treeView->ShowGroups(!ui->treeView->IsGroupsShown());
  290. }
  291. }
  292. #define ID_POPUP_COLLAPSEALLGROUPS 1
  293. #define ID_POPUP_EXPANDALLGROUPS 2
  294. //////////////////////////////////////////////////////////////////////////
  295. void CErrorReportDialog::CopyToClipboard()
  296. {
  297. QString str;
  298. const QModelIndexList selRows = ui->treeView->selectionModel()->selectedRows();
  299. for (const QModelIndex& index : selRows)
  300. {
  301. const CErrorRecord* pRecord = index.data(Qt::UserRole).value<const CErrorRecord*>();
  302. if (pRecord)
  303. {
  304. str += pRecord->GetErrorText();
  305. str += QString::fromLatin1("\r\n");
  306. }
  307. }
  308. if (!str.isEmpty())
  309. {
  310. QApplication::clipboard()->setText(str);
  311. }
  312. }
  313. //////////////////////////////////////////////////////////////////////////
  314. void CErrorReportDialog::OnReportItemRClick()
  315. {
  316. const QModelIndex index = ui->treeView->indexAt(ui->treeView->viewport()->mapFromGlobal(QCursor::pos()));
  317. if (!index.isValid())
  318. {
  319. return;
  320. }
  321. if (ui->treeView->model()->hasChildren(index))
  322. {
  323. QMenu menu;
  324. menu.addAction(tr("Collapse &All Groups"), ui->treeView, SLOT(collapseAll()));
  325. menu.addAction(tr("E&xpand All Groups"), ui->treeView, SLOT(expandAll()));
  326. // track menu
  327. menu.exec(QCursor::pos());
  328. }
  329. else
  330. {
  331. QMenu menu;
  332. //menu.addAction(tr("Select Object(s)")); // TODO: does nothing?
  333. menu.addAction(tr("Copy Warning(s) To Clipboard"), this, SLOT(CopyToClipboard()));
  334. menu.addAction(tr("E-mail Error Report"), this, SLOT(SendInMail()));
  335. menu.addAction(tr("Open in Excel"), this, SLOT(OpenInExcel()));
  336. // track menu
  337. menu.exec(QCursor::pos());
  338. }
  339. }
  340. //////////////////////////////////////////////////////////////////////////
  341. void CErrorReportDialog::SendInMail()
  342. {
  343. if (!m_pErrorReport)
  344. {
  345. return;
  346. }
  347. // Send E-Mail.
  348. QString textMsg;
  349. for (int i = 0; i < m_errorReportModel->rowCount(); ++i)
  350. {
  351. const CErrorRecord* err = m_errorReportModel->index(i, 0).data(Qt::UserRole).value<const CErrorRecord*>();
  352. textMsg += err->GetErrorText() + QString::fromLatin1("\n");
  353. }
  354. std::vector<const char*> who;
  355. const QString subject = QString::fromLatin1("Level %1 Error Report").arg(GetIEditor()->GetGameEngine()->GetLevelPath());
  356. CMailer::SendMail(subject.toUtf8().data(), textMsg.toUtf8().data(), who, who, true);
  357. }
  358. //////////////////////////////////////////////////////////////////////////
  359. void CErrorReportDialog::OpenInExcel()
  360. {
  361. if (!m_pErrorReport)
  362. {
  363. return;
  364. }
  365. const QString levelName = Path::GetFileName(GetIEditor()->GetGameEngine()->GetLevelName());
  366. const QString filename = QString::fromLatin1("ErrorList_%1_%2.csv").arg(levelName).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss"));
  367. // Save to temp file.
  368. QFile f(filename);
  369. if (f.open(QIODevice::WriteOnly))
  370. {
  371. for (int i = 0; i < m_errorReportModel->rowCount(); ++i)
  372. {
  373. const CErrorRecord* err = m_errorReportModel->index(i, 0).data(Qt::UserRole).value<const CErrorRecord*>();
  374. QString text = err->GetErrorText();
  375. text.replace(',', '.');
  376. text.replace('\t', ',');
  377. f.write((text + QString::fromLatin1("\n")).toUtf8().data());
  378. }
  379. f.close();
  380. QDesktopServices::openUrl(QUrl::fromLocalFile(filename));
  381. }
  382. else
  383. {
  384. Warning("Failed to save %s", (const char*)filename.toUtf8().data());
  385. }
  386. }
  387. //////////////////////////////////////////////////////////////////////////
  388. void CErrorReportDialog::OnReportItemClick(const QModelIndex& index)
  389. {
  390. QRect rect = ui->treeView->visualRect(index);
  391. rect.moveTopLeft(ui->treeView->viewport()->mapToGlobal(rect.topLeft()));
  392. const QRect target = fontMetrics().boundingRect(rect, index.data(Qt::TextAlignmentRole).toInt(), index.data().toString());
  393. if (index.column() == CErrorReportTableModel::ColumnObject && target.contains(QCursor::pos()))
  394. {
  395. OnReportHyperlink(index);
  396. }
  397. /*
  398. if (pItemNotify->pColumn->GetItemIndex() == COLUMN_CHECK)
  399. {
  400. m_wndReport.Populate();
  401. }
  402. */
  403. }
  404. //////////////////////////////////////////////////////////////////////////
  405. void CErrorReportDialog::keyPressEvent(QKeyEvent* event)
  406. {
  407. if (event->matches(QKeySequence::Copy))
  408. {
  409. CopyToClipboard();
  410. event->accept();
  411. }
  412. else
  413. {
  414. QWidget::keyPressEvent(event);
  415. }
  416. }
  417. //////////////////////////////////////////////////////////////////////////
  418. void CErrorReportDialog::OnReportItemDblClick(const QModelIndex& index)
  419. {
  420. const CErrorRecord* pError = index.data(Qt::UserRole).value<const CErrorRecord*>();
  421. if (pError && GetIEditor()->GetActiveView())
  422. {
  423. float x, y, z;
  424. if (GetPositionFromString(pError->error, &x, &y, &z))
  425. {
  426. CViewport* vp = GetIEditor()->GetActiveView();
  427. Matrix34 tm = vp->GetViewTM();
  428. tm.SetTranslation(Vec3(x, y, z));
  429. vp->SetViewTM(tm);
  430. }
  431. }
  432. }
  433. void CErrorReportDialog::OnSortIndicatorChanged(int logicalIndex, Qt::SortOrder order)
  434. {
  435. if (logicalIndex == 0)
  436. {
  437. ui->treeView->header()->setSortIndicator(m_sortIndicatorColumn, m_sortIndicatorOrder);
  438. }
  439. else
  440. {
  441. m_sortIndicatorColumn = logicalIndex;
  442. m_sortIndicatorOrder = order;
  443. }
  444. }
  445. //////////////////////////////////////////////////////////////////////////
  446. void CErrorReportDialog::OnReportHyperlink(const QModelIndex& index)
  447. {
  448. const CErrorRecord* pError = index.data(Qt::UserRole).value<const CErrorRecord*>();
  449. if (pError && GetIEditor()->GetActiveView())
  450. {
  451. float x, y, z;
  452. if (GetPositionFromString(pError->error, &x, &y, &z))
  453. {
  454. CViewport* vp = GetIEditor()->GetActiveView();
  455. Matrix34 tm = vp->GetViewTM();
  456. tm.SetTranslation(Vec3(x, y, z));
  457. vp->SetViewTM(tm);
  458. }
  459. }
  460. }
  461. /*
  462. //////////////////////////////////////////////////////////////////////////
  463. void CErrorReportDialog::OnShowFieldChooser()
  464. {
  465. CMainFrm* pMainFrm = (CMainFrame*)AfxGetMainWnd();
  466. if (pMainFrm)
  467. {
  468. bool bShow = !pMainFrm->m_wndFieldChooser.IsVisible();
  469. pMainFrm->ShowControlBar(&pMainFrm->m_wndFieldChooser, bShow, false);
  470. }
  471. }
  472. */
  473. #include <moc_ErrorReportDialog.cpp>