serenity/Userland/DevTools/HackStudio/ToDoEntriesWidget.cpp

112 lines
3.1 KiB
C++
Raw Normal View History

2021-05-22 14:14:49 +02:00
/*
* Copyright (c) 2021, Federico Guerinoni <guerinoni.federico@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ToDoEntriesWidget.h"
#include "HackStudio.h"
#include "ToDoEntries.h"
#include <LibGUI/BoxLayout.h>
#include <LibGfx/Font/FontDatabase.h>
2021-05-22 14:14:49 +02:00
namespace HackStudio {
class ToDoEntriesModel final : public GUI::Model {
public:
enum Column {
Filename,
Text,
Line,
Column,
2021-05-22 14:14:49 +02:00
__Count
};
explicit ToDoEntriesModel(Vector<Cpp::Parser::TodoEntry> const&& matches)
2021-05-22 14:14:49 +02:00
: m_matches(move(matches))
{
}
virtual int row_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return m_matches.size(); }
virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return Column::__Count; }
2021-05-22 14:14:49 +02:00
virtual String column_name(int column) const override
{
switch (column) {
case Column::Filename:
return "Filename";
case Column::Text:
return "Text";
case Column::Line:
return "Line";
case Column::Column:
return "Col";
2021-05-22 14:14:49 +02:00
default:
VERIFY_NOT_REACHED();
}
}
virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role) const override
2021-05-22 14:14:49 +02:00
{
if (role == GUI::ModelRole::TextAlignment)
return Gfx::TextAlignment::CenterLeft;
if (role == GUI::ModelRole::Font) {
if (index.column() == Column::Text)
return Gfx::FontDatabase::default_fixed_width_font();
return {};
}
if (role == GUI::ModelRole::Display) {
auto& match = m_matches.at(index.row());
switch (index.column()) {
case Column::Filename:
return match.filename;
case Column::Text:
return match.content;
case Column::Line:
return String::formatted("{}", match.line + 1);
case Column::Column:
return String::formatted("{}", match.column);
2021-05-22 14:14:49 +02:00
}
}
return {};
}
virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& = GUI::ModelIndex()) const override
{
if (row < 0 || row >= (int)m_matches.size())
return {};
if (column < 0 || column >= Column::__Count)
return {};
return create_index(row, column, &m_matches.at(row));
}
private:
Vector<Cpp::Parser::TodoEntry> m_matches;
2021-05-22 14:14:49 +02:00
};
void ToDoEntriesWidget::refresh()
{
2022-04-01 20:58:27 +03:00
auto const& entries = ToDoEntries::the().get_entries();
2021-05-22 14:14:49 +02:00
auto results_model = adopt_ref(*new ToDoEntriesModel(move(entries)));
m_result_view->set_model(results_model);
}
void ToDoEntriesWidget::clear()
{
ToDoEntries::the().clear_entries();
refresh();
}
2021-05-22 14:14:49 +02:00
ToDoEntriesWidget::ToDoEntriesWidget()
{
set_layout<GUI::VerticalBoxLayout>();
m_result_view = add<GUI::TableView>();
m_result_view->on_activation = [](auto& index) {
auto& match = *(Cpp::Parser::TodoEntry const*)index.internal_data();
open_file(match.filename, match.line, match.column);
};
2021-05-22 14:14:49 +02:00
}
}