ErrorReportTableModel.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 "ErrorReportTableModel.h"
  10. #include <QIcon>
  11. // Editor
  12. #include "ErrorReport.h"
  13. bool GetPositionFromString(QString er, float* x, float* y, float* z)
  14. {
  15. er = er.toLower();
  16. int ind = er.indexOf("pos:");
  17. int shift = 4;
  18. if (ind < 0)
  19. {
  20. ind = er.indexOf("position:");
  21. shift = 9;
  22. }
  23. if (ind >= 0)
  24. {
  25. er = er.mid(ind + shift);
  26. er.remove(QRegExp("^ *"));
  27. if (er[0] == '(')
  28. {
  29. er = er.mid(1);
  30. er.remove(QRegExp("^ *"));
  31. ind = er.indexOf(")");
  32. if (ind > 0)
  33. {
  34. er = er.mid(0, ind);
  35. er.remove(QRegExp(" *$"));
  36. ind = er.indexOf(" ");
  37. int ind2 = er.indexOf(",");
  38. if (ind < 0 || (ind2 > 0 && ind > ind2))
  39. {
  40. ind = ind2;
  41. }
  42. if (ind > 0)
  43. {
  44. *x = er.mid(0, ind).toFloat();
  45. er = er.mid(ind);
  46. er.remove(QRegExp("^[ ,]*"));
  47. ind = er.indexOf(" ");
  48. ind2 = er.indexOf(",");
  49. if (ind < 0 || (ind2 > 0 && ind > ind2))
  50. {
  51. ind = ind2;
  52. }
  53. if (ind > 0)
  54. {
  55. *y = er.mid(0, ind).toFloat();
  56. er = er.mid(ind);
  57. er.remove(QRegExp("^[ ,]*"));
  58. if (er.length())
  59. {
  60. *z = er.toFloat();
  61. return true;
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. return false;
  69. }
  70. CErrorReportTableModel::CErrorReportTableModel(QObject* parent)
  71. : AbstractSortModel(parent)
  72. {
  73. QIcon error_icon = QIcon(":/error_report_error.svg");
  74. error_icon.addFile(":/error_report_error.svg", QSize(), QIcon::Selected);
  75. m_imageList.push_back(error_icon);
  76. // VALIDATOR_ERROR_DBGBRK - this is never used but we need to space the icon list correctly.
  77. QIcon dbg_icon = QIcon(":/error_report_error.svg");
  78. dbg_icon.addFile(":/error_report_error.svg", QSize(), QIcon::Selected);
  79. m_imageList.push_back(dbg_icon);
  80. QIcon warning_icon = QIcon(":/error_report_warning.svg");
  81. warning_icon.addFile(":/error_report_warning.svg", QSize(), QIcon::Selected);
  82. m_imageList.push_back(warning_icon);
  83. QIcon comment_icon = QIcon(":/error_report_comment.svg");
  84. comment_icon.addFile(":/error_report_comment.svg", QSize(), QIcon::Selected);
  85. m_imageList.push_back(comment_icon);
  86. }
  87. CErrorReportTableModel::~CErrorReportTableModel()
  88. {
  89. }
  90. void CErrorReportTableModel::setErrorReport(CErrorReport* report)
  91. {
  92. beginResetModel();
  93. if (!m_errorRecords.empty())
  94. {
  95. m_errorRecords.clear();
  96. }
  97. if (report != nullptr)
  98. {
  99. const int count = report->GetErrorCount();
  100. m_errorRecords.reserve(count);
  101. for (int i = 0; i < count; ++i)
  102. {
  103. m_errorRecords.push_back(report->GetError(i));
  104. }
  105. }
  106. endResetModel();
  107. }
  108. int CErrorReportTableModel::rowCount(const QModelIndex& parent) const
  109. {
  110. return parent.isValid() ? 0 : static_cast<int>(m_errorRecords.size());
  111. }
  112. int CErrorReportTableModel::columnCount(const QModelIndex& parent) const
  113. {
  114. return parent.isValid() ? 0 : 8;
  115. }
  116. QVariant CErrorReportTableModel::data(const QModelIndex& index, int role) const
  117. {
  118. assert(index.row() < m_errorRecords.size());
  119. const CErrorRecord& record = m_errorRecords[index.row()];
  120. return data(record, index.column(), role);
  121. }
  122. QVariant CErrorReportTableModel::data(const CErrorRecord& record, int column, int role) const
  123. {
  124. switch (role)
  125. {
  126. case Qt::DisplayRole:
  127. {
  128. switch (column)
  129. {
  130. case ColumnCount:
  131. return record.count;
  132. case ColumnText:
  133. return QString(record.error).simplified();
  134. case ColumnFile:
  135. return record.file;
  136. case ColumnObject:
  137. if (!record.error.isEmpty())
  138. {
  139. float x, y, z;
  140. if (GetPositionFromString(record.error, &x, &y, &z))
  141. {
  142. return tr("Pos: (%1, %2, %3)").arg(x, 0, 'f').arg(y, 0, 'f').arg(z, 0, 'f');
  143. }
  144. }
  145. break;
  146. case ColumnModule:
  147. switch (record.module)
  148. {
  149. case VALIDATOR_MODULE_RENDERER:
  150. return tr("Renderer");
  151. case VALIDATOR_MODULE_3DENGINE:
  152. return tr("3DEngine");
  153. case VALIDATOR_MODULE_ASSETS:
  154. return tr("Assets");
  155. case VALIDATOR_MODULE_SYSTEM:
  156. return tr("System");
  157. case VALIDATOR_MODULE_AUDIO:
  158. return tr("Audio");
  159. case VALIDATOR_MODULE_MOVIE:
  160. return tr("Movie");
  161. case VALIDATOR_MODULE_EDITOR:
  162. return tr("Editor");
  163. case VALIDATOR_MODULE_NETWORK:
  164. return tr("Network");
  165. case VALIDATOR_MODULE_PHYSICS:
  166. return tr("Physics");
  167. case VALIDATOR_MODULE_FEATURETESTS:
  168. return tr("FeatureTests");
  169. default:
  170. return tr("Unknown");
  171. }
  172. case ColumnDescription:
  173. return record.description;
  174. case ColumnAssetScope:
  175. return record.assetScope;
  176. }
  177. break;
  178. }
  179. case Qt::DecorationRole:
  180. {
  181. if (column == ColumnSeverity)
  182. {
  183. return m_imageList[record.severity];
  184. }
  185. break;
  186. }
  187. case Qt::UserRole:
  188. {
  189. return QVariant::fromValue(&record);
  190. }
  191. case Qt::TextAlignmentRole:
  192. {
  193. return m_alignments.value(column, Qt::AlignLeft) | Qt::AlignVCenter;
  194. }
  195. case Qt::ForegroundRole:
  196. {
  197. if (column == ColumnObject)
  198. {
  199. return QPalette().color(QPalette::Link);
  200. }
  201. break;
  202. }
  203. case Qt::FontRole:
  204. {
  205. if (column == ColumnObject)
  206. {
  207. static QFont f;
  208. f.setUnderline(true);
  209. return f;
  210. }
  211. break;
  212. }
  213. case SeverityRole:
  214. return record.severity;
  215. }
  216. return QVariant();
  217. }
  218. QVariant CErrorReportTableModel::headerData(int section, Qt::Orientation orientation, int role) const
  219. {
  220. if (orientation != Qt::Horizontal)
  221. {
  222. return QVariant();
  223. }
  224. else if (role == Qt::TextAlignmentRole)
  225. {
  226. return m_alignments.value(section, Qt::AlignLeft) | Qt::AlignVCenter;
  227. }
  228. else if (role == Qt::DisplayRole)
  229. {
  230. switch (section)
  231. {
  232. case ColumnSeverity:
  233. return tr("");
  234. case ColumnCount:
  235. return tr("N");
  236. case ColumnText:
  237. return tr("Text");
  238. case ColumnFile:
  239. return tr("File");
  240. case ColumnObject:
  241. return tr("Object/Material");
  242. case ColumnModule:
  243. return tr("Module");
  244. case ColumnDescription:
  245. return tr("Description");
  246. case ColumnAssetScope:
  247. return tr("Scope");
  248. default:
  249. return QString();
  250. }
  251. }
  252. else
  253. {
  254. return QVariant();
  255. }
  256. }
  257. bool CErrorReportTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant& value, int role)
  258. {
  259. if (orientation == Qt::Horizontal && section >= 0 && section < columnCount() && value.canConvert<int>() && role == Qt::TextAlignmentRole)
  260. {
  261. m_alignments.insert(section, value.toInt());
  262. Q_EMIT headerDataChanged(orientation, section, section);
  263. const int rows = rowCount();
  264. if (rows > 0)
  265. {
  266. Q_EMIT dataChanged(index(0, section), index(rows - 1, section));
  267. }
  268. return true;
  269. }
  270. return QAbstractTableModel::setHeaderData(section, orientation, value, role);
  271. }
  272. bool CErrorReportTableModel::LessThan(const QModelIndex& lhs, const QModelIndex& rhs) const
  273. {
  274. int column = lhs.column();
  275. if (column == ColumnSeverity)
  276. {
  277. return lhs.data(SeverityRole).toInt() < rhs.data(SeverityRole).toInt();
  278. }
  279. const QVariant l = lhs.data();
  280. const QVariant r = rhs.data();
  281. bool ok;
  282. const int lInt = l.toInt(&ok);
  283. const int rInt = r.toInt(&ok);
  284. if (ok)
  285. {
  286. return lInt < rInt;
  287. }
  288. return l.toString() < r.toString();
  289. }
  290. #include <moc_ErrorReportTableModel.cpp>