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