GModel.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <LibGUI/GModel.h>
  2. #include <LibGUI/GAbstractView.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_model_update)
  25. on_model_update(*this);
  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. if (m_activates_on_selection && is_valid(index))
  38. activate(index);
  39. }
  40. GModelIndex GModel::create_index(int row, int column, void* data) const
  41. {
  42. return GModelIndex(*this, row, column, data);
  43. }
  44. GModelIndex GModel::sibling(int row, int column, const GModelIndex& parent) const
  45. {
  46. if (!parent.is_valid())
  47. return { };
  48. int row_count = this->row_count(parent);
  49. if (row < 0 || row > row_count)
  50. return { };
  51. return index(row, column, parent);
  52. }