GModel.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #include <LibGUI/GAbstractView.h>
  2. #include <LibGUI/GModel.h>
  3. GModel::GModel()
  4. {
  5. }
  6. GModel::~GModel()
  7. {
  8. }
  9. void GModel::register_view(Badge<GAbstractView>, GAbstractView& view)
  10. {
  11. m_views.set(&view);
  12. }
  13. void GModel::unregister_view(Badge<GAbstractView>, GAbstractView& view)
  14. {
  15. m_views.remove(&view);
  16. }
  17. void GModel::for_each_view(Function<void(GAbstractView&)> callback)
  18. {
  19. for (auto* view : m_views)
  20. callback(*view);
  21. }
  22. void GModel::did_update()
  23. {
  24. if (on_update)
  25. on_update();
  26. for_each_view([](auto& view) {
  27. view.did_update_model();
  28. });
  29. }
  30. void GModel::set_selected_index(const GModelIndex& index)
  31. {
  32. if (m_selected_index == index)
  33. return;
  34. m_selected_index = index;
  35. if (on_selection_changed)
  36. on_selection_changed(index);
  37. for_each_view([](auto& view) {
  38. view.did_update_selection();
  39. });
  40. }
  41. GModelIndex GModel::create_index(int row, int column, const void* data) const
  42. {
  43. return GModelIndex(*this, row, column, const_cast<void*>(data));
  44. }
  45. GModelIndex GModel::sibling(int row, int column, const GModelIndex& parent) const
  46. {
  47. if (!parent.is_valid())
  48. return {};
  49. int row_count = this->row_count(parent);
  50. if (row < 0 || row > row_count)
  51. return {};
  52. return index(row, column, parent);
  53. }