1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:37:44 +00:00

Everywhere: Replace Model::update() with Model::invalidate()

Most of the models were just calling did_update anyway, which is
pointless since it can be unified to the base Model class. Instead, code
calling update() will now call invalidate(), which functions identically
and is more obvious in what it does.

Additionally, a default implementation is provided, which removes the
need to add empty implementations of update() for each model subclass.

Co-Authored-By: Ali Mohammad Pur <ali.mpfard@gmail.com>
This commit is contained in:
sin-ack 2021-05-25 14:13:19 +00:00 committed by Andreas Kling
parent 5cd2e0f3a2
commit ca2c81251a
100 changed files with 116 additions and 261 deletions

View file

@ -146,7 +146,7 @@ BookmarksBarWidget::BookmarksBarWidget(const String& bookmarks_file, bool enable
fields.empend("title", "Title", Gfx::TextAlignment::CenterLeft);
fields.empend("url", "Url", Gfx::TextAlignment::CenterRight);
set_model(GUI::JsonArrayModel::create(bookmarks_file, move(fields)));
model()->update();
model()->invalidate();
}
BookmarksBarWidget::~BookmarksBarWidget()

View file

@ -98,10 +98,6 @@ AddEventDialog::MonthListModel::~MonthListModel()
{
}
void AddEventDialog::MonthListModel::update()
{
}
int AddEventDialog::MonthListModel::row_count(const GUI::ModelIndex&) const
{
return 12;

View file

@ -39,7 +39,6 @@ private:
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Count; }
virtual String column_name(int) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual void update() override;
private:
MonthListModel();

View file

@ -360,7 +360,7 @@ void DirectoryView::open(String const& path)
auto real_path = Core::File::real_path_for(path);
if (model().root_path() == real_path) {
model().update();
model().invalidate();
return;
}
@ -382,7 +382,7 @@ void DirectoryView::open_parent_directory()
void DirectoryView::refresh()
{
model().update();
model().invalidate();
}
void DirectoryView::open_previous_directory()

View file

@ -500,7 +500,7 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
};
auto refresh_tree_view = [&] {
directories_model->update();
directories_model->invalidate();
auto current_path = directory_view.path();

View file

@ -172,8 +172,3 @@ TriState ManualModel::data_matches(const GUI::ModelIndex& index, const GUI::Vari
return view_result.value().contains(term.as_string()) ? TriState::True : TriState::False;
}
void ManualModel::update()
{
did_update();
}

View file

@ -32,7 +32,6 @@ public:
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual TriState data_matches(const GUI::ModelIndex&, const GUI::Variant&) const override;
virtual void update() override;
virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
virtual GUI::ModelIndex index(int row, int column = 0, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override;

View file

@ -109,11 +109,11 @@ int main(int argc, char* argv[])
if (auto model = search_list_view.model()) {
auto& search_model = *static_cast<GUI::FilteringProxyModel*>(model);
search_model.set_filter_term(search_box.text());
search_model.update();
search_model.invalidate();
}
};
search_list_view.set_model(GUI::FilteringProxyModel::construct(model));
search_list_view.model()->update();
search_list_view.model()->invalidate();
tree_view.set_model(model);
left_tab_bar.set_fixed_width(200);

View file

@ -75,8 +75,6 @@ public:
return {};
}
virtual void update() override { }
private:
Vector<Match> m_matches;
};

View file

@ -62,7 +62,7 @@ void IRCAppWindow::setup_client()
return static_cast<IRCWindow*>(m_container->active_widget());
};
m_client->aid_update_window_list = [this] {
m_window_list->model()->update();
m_window_list->model()->invalidate();
};
m_client->on_nickname_changed = [this](const String&) {
update_title();

View file

@ -36,7 +36,7 @@ void IRCChannel::add_member(const String& name, char prefix)
}
}
m_members.append({ name, prefix });
m_member_model->update();
m_member_model->invalidate();
}
void IRCChannel::remove_member(const String& name)
@ -69,7 +69,7 @@ void IRCChannel::handle_join(const String& nick, const String& hostmask)
return;
}
add_member(nick, (char)0);
m_member_model->update();
m_member_model->invalidate();
if (m_client.show_join_part_messages())
add_message(String::formatted("*** {} [{}] has joined {}", nick, hostmask, m_name), Color::MidGreen);
}
@ -83,7 +83,7 @@ void IRCChannel::handle_part(const String& nick, const String& hostmask)
} else {
remove_member(nick);
}
m_member_model->update();
m_member_model->invalidate();
if (m_client.show_join_part_messages())
add_message(String::formatted("*** {} [{}] has parted from {}", nick, hostmask, m_name), Color::MidGreen);
}
@ -97,7 +97,7 @@ void IRCChannel::handle_quit(const String& nick, const String& hostmask, const S
} else {
remove_member(nick);
}
m_member_model->update();
m_member_model->invalidate();
add_message(String::formatted("*** {} [{}] has quit ({})", nick, hostmask, message), Color::MidGreen);
}
@ -114,7 +114,7 @@ void IRCChannel::notify_nick_changed(const String& old_nick, const String& new_n
for (auto& member : m_members) {
if (member.name == old_nick) {
member.name = new_nick;
m_member_model->update();
m_member_model->invalidate();
if (m_client.show_nick_change_messages())
add_message(String::formatted("~ {} changed nickname to {}", old_nick, new_nick), Color::MidMagenta);
return;

View file

@ -50,11 +50,6 @@ GUI::Variant IRCChannelMemberListModel::data(const GUI::ModelIndex& index, GUI::
return {};
}
void IRCChannelMemberListModel::update()
{
did_update();
}
String IRCChannelMemberListModel::nick_at(const GUI::ModelIndex& index) const
{
return data(index, GUI::ModelRole::Display).to_string();

View file

@ -23,7 +23,6 @@ public:
virtual int column_count(const GUI::ModelIndex&) const override;
virtual String column_name(int column) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual void update() override;
virtual String nick_at(const GUI::ModelIndex& index) const;
private:

View file

@ -817,7 +817,7 @@ void IRCClient::register_subwindow(IRCWindow& subwindow)
subwindow.set_log_buffer(*m_log);
}
m_windows.append(&subwindow);
m_client_window_list_model->update();
m_client_window_list_model->invalidate();
}
void IRCClient::unregister_subwindow(IRCWindow& subwindow)
@ -831,7 +831,7 @@ void IRCClient::unregister_subwindow(IRCWindow& subwindow)
break;
}
}
m_client_window_list_model->update();
m_client_window_list_model->invalidate();
}
void IRCClient::handle_user_command(const String& input)
@ -1079,7 +1079,7 @@ void IRCClient::handle_kick_user_action(const String& channel, const String& nic
void IRCClient::handle_close_query_action(const String& nick)
{
m_queries.remove(nick);
m_client_window_list_model->update();
m_client_window_list_model->invalidate();
}
void IRCClient::handle_join_action(const String& channel)

View file

@ -64,8 +64,3 @@ GUI::Variant IRCWindowListModel::data(const GUI::ModelIndex& index, GUI::ModelRo
}
return {};
}
void IRCWindowListModel::update()
{
did_update();
}

View file

@ -25,7 +25,6 @@ public:
virtual int column_count(const GUI::ModelIndex&) const override;
virtual String column_name(int column) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual void update() override;
private:
explicit IRCWindowListModel(IRCClient&);

View file

@ -39,11 +39,6 @@ public:
return {};
}
virtual void update() override
{
did_update();
}
private:
explicit CharacterMapFileListModel(Vector<String>& filenames)
: m_filenames(filenames)

View file

@ -75,5 +75,5 @@ void AccountHolder::add_account_with_name_and_mailboxes(String name, Vector<IMAP
void AccountHolder::rebuild_tree()
{
m_mailbox_tree_model->update();
m_mailbox_tree_model->invalidate();
}

View file

@ -43,8 +43,3 @@ GUI::Variant InboxModel::data(GUI::ModelIndex const& index, GUI::ModelRole role)
}
return {};
}
void InboxModel::update()
{
did_update();
}

View file

@ -33,7 +33,6 @@ public:
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return Column::__Count; }
virtual String column_name(int) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual void update() override;
private:
InboxModel(Vector<InboxEntry>);

View file

@ -113,8 +113,3 @@ GUI::Variant MailboxTreeModel::data(GUI::ModelIndex const& index, GUI::ModelRole
return {};
}
void MailboxTreeModel::update()
{
did_update();
}

View file

@ -25,7 +25,6 @@ public:
virtual GUI::Variant data(GUI::ModelIndex const&, GUI::ModelRole) const override;
virtual GUI::ModelIndex index(int row, int column, GUI::ModelIndex const& parent = GUI::ModelIndex()) const override;
virtual GUI::ModelIndex parent_index(GUI::ModelIndex const&) const override;
virtual void update() override;
private:
explicit MailboxTreeModel(AccountHolder const&);

View file

@ -61,11 +61,6 @@ GUI::Variant OutlineModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
}
}
void OutlineModel::update()
{
did_update();
}
GUI::ModelIndex OutlineModel::parent_index(const GUI::ModelIndex& index) const
{
if (!index.is_valid())

View file

@ -19,7 +19,6 @@ public:
virtual int row_count(const GUI::ModelIndex&) const override;
virtual int column_count(const GUI::ModelIndex&) const override;
virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role) const override;
virtual void update() override;
virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override;
virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex&) const override;

View file

@ -59,8 +59,6 @@ public:
return {};
}
virtual void update() override { }
private:
NonnullRefPtrVector<Desktop::AppFile> m_apps;
};

View file

@ -92,10 +92,6 @@ String PlaylistModel::column_name(int column) const
VERIFY_NOT_REACHED();
}
void PlaylistModel::update()
{
}
void PlaylistTableView::doubleclick_event(GUI::MouseEvent& event)
{
AbstractView::doubleclick_event(event);

View file

@ -24,7 +24,6 @@ public:
int row_count(const GUI::ModelIndex&) const override { return m_playlist_items.size(); }
int column_count(const GUI::ModelIndex&) const override { return 6; }
GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
void update() override;
String column_name(int column) const override;
Vector<M3UEntry>& items() { return m_playlist_items; }

View file

@ -258,7 +258,7 @@ void SoundPlayerWidgetAdvancedView::read_playlist(StringView path)
for (auto& item : *items)
m_playlist_model->items().append(item);
set_playlist_visible(true);
m_playlist_model->update();
m_playlist_model->invalidate();
open_file(items->at(0).path);

View file

@ -27,7 +27,6 @@ public:
virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return m_keys.size(); }
virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override { return 1; }
virtual void update() override { }
virtual GUI::Variant data(const GUI::ModelIndex& index, GUI::ModelRole role = GUI::ModelRole::Display) const override
{
@ -46,7 +45,7 @@ public:
object.for_each_member([this](auto& name, auto&) {
m_keys.append(name);
});
did_update();
invalidate();
}
private:

View file

@ -146,7 +146,7 @@ void SheetModel::set_data(const GUI::ModelIndex& index, const GUI::Variant& valu
auto& cell = m_sheet->ensure({ (size_t)index.column(), (size_t)index.row() });
cell.set_data(value.to_string());
update();
invalidate();
}
void SheetModel::update()

View file

@ -23,11 +23,12 @@ public:
virtual RefPtr<Core::MimeData> mime_data(const GUI::ModelSelection&) const override;
virtual bool is_editable(const GUI::ModelIndex&) const override;
virtual void set_data(const GUI::ModelIndex&, const GUI::Variant&) override;
virtual void update() override;
virtual bool is_column_sortable(int) const override { return false; }
virtual StringView drag_data_type() const override { return "text/x-spreadsheet-data"; }
Sheet& sheet() { return *m_sheet; }
void update();
private:
explicit SheetModel(Sheet& sheet)
: m_sheet(sheet)

View file

@ -144,7 +144,7 @@ void InfinitelyScrollableTableView::mouseup_event(GUI::MouseEvent& event)
void SpreadsheetView::update_with_model()
{
m_table_view->model()->update();
m_table_view->model()->invalidate();
m_table_view->update();
}

View file

@ -118,8 +118,9 @@ GUI::Variant DevicesModel::data(const GUI::ModelIndex& index, GUI::ModelRole rol
return {};
}
void DevicesModel::update()
void DevicesModel::invalidate()
{
// FIXME: granularly update this.
auto proc_devices = Core::File::construct("/proc/devices");
if (!proc_devices->open(Core::OpenMode::ReadOnly))
VERIFY_NOT_REACHED();
@ -172,5 +173,5 @@ void DevicesModel::update()
fill_in_paths_from_dir("/dev");
fill_in_paths_from_dir("/dev/pts");
did_update();
Model::invalidate();
}

View file

@ -28,7 +28,9 @@ public:
virtual int column_count(const GUI::ModelIndex&) const override;
virtual String column_name(int column) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual void update() override;
// FIXME: This should be moved to granularly updating itself.
virtual void invalidate() override;
private:
DevicesModel();

View file

@ -44,5 +44,5 @@ InterruptsWidget::~InterruptsWidget()
void InterruptsWidget::update_model()
{
m_interrupt_table_view->model()->update();
m_interrupt_model->invalidate();
}

View file

@ -117,7 +117,7 @@ NetworkStatisticsWidget::~NetworkStatisticsWidget()
void NetworkStatisticsWidget::update_models()
{
m_adapter_table_view->model()->update();
m_tcp_socket_table_view->model()->update();
m_udp_socket_table_view->model()->update();
m_adapter_model->invalidate();
m_tcp_socket_model->invalidate();
m_udp_socket_model->invalidate();
}

View file

@ -122,5 +122,5 @@ void ProcessMemoryMapWidget::set_pid(pid_t pid)
void ProcessMemoryMapWidget::refresh()
{
if (m_pid != -1)
m_json_model->update();
m_json_model->invalidate();
}

View file

@ -60,8 +60,8 @@ public:
virtual int column_count(const GUI::ModelIndex&) const override;
virtual String column_name(int column) const override;
virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
virtual void update() override;
virtual bool is_column_sortable(int column_index) const override { return column_index != Column::Icon; }
void update();
struct CpuInfo {
u32 id;

View file

@ -57,11 +57,6 @@ public:
return {};
}
virtual void update() override
{
did_update(GUI::Model::DontInvalidateIndices);
}
virtual void model_did_update([[maybe_unused]] unsigned flags) override
{
refresh();
@ -77,7 +72,7 @@ public:
break;
}
}
update();
invalidate();
}
private:

View file

@ -225,11 +225,11 @@ int main(int argc, char** argv)
process_table_view.set_column_visible(ProcessModel::Column::DirtyPrivate, true);
process_table_view.set_key_column_and_sort_order(ProcessModel::Column::CPU, GUI::SortOrder::Descending);
process_table_view.model()->update();
process_model->update();
auto& refresh_timer = window->add<Core::Timer>(
3000, [&] {
process_table_view.model()->update();
process_model->update();
if (auto* memory_stats_widget = MemoryStatsWidget::the())
memory_stats_widget->refresh();
});
@ -567,11 +567,12 @@ NonnullRefPtr<GUI::Widget> build_file_systems_tab()
df_fields.empend("free_inode_count", "Free inodes", Gfx::TextAlignment::CenterRight);
df_fields.empend("total_inode_count", "Total inodes", Gfx::TextAlignment::CenterRight);
df_fields.empend("block_size", "Block size", Gfx::TextAlignment::CenterRight);
fs_table_view.set_model(GUI::SortingProxyModel::create(GUI::JsonArrayModel::create("/proc/df", move(df_fields))));
fs_table_view.set_column_painting_delegate(3, make<ProgressbarPaintingDelegate>());
fs_table_view.model()->update();
fs_table_view.model()->invalidate();
};
return fs_widget;
}
@ -629,7 +630,7 @@ NonnullRefPtr<GUI::Widget> build_pci_devices_tab()
});
pci_table_view.set_model(GUI::SortingProxyModel::create(GUI::JsonArrayModel::create("/proc/pci", move(pci_fields))));
pci_table_view.model()->update();
pci_table_view.model()->invalidate();
};
return pci_widget;
@ -645,7 +646,7 @@ NonnullRefPtr<GUI::Widget> build_devices_tab()
auto& devices_table_view = self.add<GUI::TableView>();
devices_table_view.set_model(GUI::SortingProxyModel::create(DevicesModel::create()));
devices_table_view.model()->update();
devices_table_view.model()->invalidate();
};
return devices_widget;
@ -748,8 +749,9 @@ NonnullRefPtr<GUI::Widget> build_processors_tab()
processors_field.empend("type", "Type", Gfx::TextAlignment::CenterRight);
auto& processors_table_view = self.add<GUI::TableView>();
processors_table_view.set_model(GUI::JsonArrayModel::create("/proc/cpuinfo", move(processors_field)));
processors_table_view.model()->update();
auto json_model = GUI::JsonArrayModel::create("/proc/cpuinfo", move(processors_field));
processors_table_view.set_model(json_model);
json_model->invalidate();
};
return processors_widget;

View file

@ -26,7 +26,6 @@ public:
return Gfx::to_string(m_color_roles[(size_t)index.row()]);
return {};
}
virtual void update() { did_update(); }
explicit ColorRoleModel(const Vector<Gfx::ColorRole>& color_roles)
: m_color_roles(color_roles)