ToDoEntriesWidget.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2021, Federico Guerinoni <guerinoni.federico@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ToDoEntriesWidget.h"
  7. #include "HackStudio.h"
  8. #include "ToDoEntries.h"
  9. #include <LibGUI/BoxLayout.h>
  10. #include <LibGfx/FontDatabase.h>
  11. namespace HackStudio {
  12. class ToDoEntriesModel final : public GUI::Model {
  13. public:
  14. enum Column {
  15. Filename,
  16. Text,
  17. Line,
  18. Column,
  19. __Count
  20. };
  21. explicit ToDoEntriesModel(Vector<Cpp::Parser::TodoEntry> const&& matches)
  22. : m_matches(move(matches))
  23. {
  24. }
  25. virtual int row_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return m_matches.size(); }
  26. virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return Column::__Count; }
  27. virtual String column_name(int column) const override
  28. {
  29. switch (column) {
  30. case Column::Filename:
  31. return "Filename";
  32. case Column::Text:
  33. return "Text";
  34. case Column::Line:
  35. return "Line";
  36. case Column::Column:
  37. return "Col";
  38. default:
  39. VERIFY_NOT_REACHED();
  40. }
  41. }
  42. virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
  43. {
  44. if (role == GUI::ModelRole::TextAlignment)
  45. return Gfx::TextAlignment::CenterLeft;
  46. if (role == GUI::ModelRole::Font) {
  47. if (index.column() == Column::Text)
  48. return Gfx::FontDatabase::default_fixed_width_font();
  49. return {};
  50. }
  51. if (role == GUI::ModelRole::Display) {
  52. auto& match = m_matches.at(index.row());
  53. switch (index.column()) {
  54. case Column::Filename:
  55. return match.filename;
  56. case Column::Text:
  57. return match.content;
  58. case Column::Line:
  59. return String::formatted("{}", match.line + 1);
  60. case Column::Column:
  61. return String::formatted("{}", match.column);
  62. }
  63. }
  64. return {};
  65. }
  66. virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& = GUI::ModelIndex()) const override
  67. {
  68. if (row < 0 || row >= (int)m_matches.size())
  69. return {};
  70. if (column < 0 || column >= Column::__Count)
  71. return {};
  72. return create_index(row, column, &m_matches.at(row));
  73. }
  74. private:
  75. Vector<Cpp::Parser::TodoEntry> m_matches;
  76. };
  77. void ToDoEntriesWidget::refresh()
  78. {
  79. const auto& entries = ToDoEntries::the().get_entries();
  80. auto results_model = adopt_ref(*new ToDoEntriesModel(move(entries)));
  81. m_result_view->set_model(results_model);
  82. }
  83. ToDoEntriesWidget::ToDoEntriesWidget()
  84. {
  85. set_layout<GUI::VerticalBoxLayout>();
  86. m_result_view = add<GUI::TableView>();
  87. m_result_view->on_activation = [](auto& index) {
  88. auto& match = *(Cpp::Parser::TodoEntry const*)index.internal_data();
  89. open_file(match.filename, match.line, match.column);
  90. };
  91. }
  92. }