GModel.cpp 1010 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. GModelIndex GModel::create_index(int row, int column, const void* data) const
  31. {
  32. return GModelIndex(*this, row, column, const_cast<void*>(data));
  33. }
  34. GModelIndex GModel::sibling(int row, int column, const GModelIndex& parent) const
  35. {
  36. if (!parent.is_valid())
  37. return {};
  38. int row_count = this->row_count(parent);
  39. if (row < 0 || row > row_count)
  40. return {};
  41. return index(row, column, parent);
  42. }