AbstractView.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Function.h>
  28. #include <LibGUI/ModelSelection.h>
  29. #include <LibGUI/ScrollableWidget.h>
  30. namespace GUI {
  31. class AbstractView : public ScrollableWidget {
  32. C_OBJECT_ABSTRACT(AbstractView)
  33. friend class Model;
  34. public:
  35. void set_model(RefPtr<Model>);
  36. Model* model() { return m_model.ptr(); }
  37. const Model* model() const { return m_model.ptr(); }
  38. ModelSelection& selection() { return m_selection; }
  39. const ModelSelection& selection() const { return m_selection; }
  40. virtual void select_all() = 0;
  41. bool is_editable() const { return m_editable; }
  42. void set_editable(bool editable) { m_editable = editable; }
  43. virtual bool accepts_focus() const override { return true; }
  44. virtual void did_update_model();
  45. virtual void did_update_selection();
  46. virtual Gfx::Rect content_rect(const ModelIndex&) const { return {}; }
  47. virtual ModelIndex index_at_event_position(const Gfx::Point&) const = 0;
  48. void begin_editing(const ModelIndex&);
  49. void stop_editing();
  50. void set_activates_on_selection(bool b) { m_activates_on_selection = b; }
  51. bool activates_on_selection() const { return m_activates_on_selection; }
  52. Function<void()> on_selection_change;
  53. Function<void(const ModelIndex&)> on_activation;
  54. Function<void(const ModelIndex&)> on_selection;
  55. Function<void(const ModelIndex&, const ContextMenuEvent&)> on_context_menu_request;
  56. Function<void(const ModelIndex&, const DropEvent&)> on_drop;
  57. Function<OwnPtr<ModelEditingDelegate>(const ModelIndex&)> aid_create_editing_delegate;
  58. void notify_selection_changed(Badge<ModelSelection>);
  59. NonnullRefPtr<Gfx::Font> font_for_index(const ModelIndex&) const;
  60. protected:
  61. AbstractView();
  62. virtual ~AbstractView() override;
  63. virtual void mousedown_event(MouseEvent&) override;
  64. virtual void mousemove_event(MouseEvent&) override;
  65. virtual void mouseup_event(MouseEvent&) override;
  66. virtual void doubleclick_event(MouseEvent&) override;
  67. virtual void context_menu_event(ContextMenuEvent&) override;
  68. virtual void drop_event(DropEvent&) override;
  69. virtual void did_scroll() override;
  70. void activate(const ModelIndex&);
  71. void activate_selected();
  72. void update_edit_widget_position();
  73. bool m_editable { false };
  74. ModelIndex m_edit_index;
  75. RefPtr<Widget> m_edit_widget;
  76. Gfx::Rect m_edit_widget_content_rect;
  77. Gfx::Point m_left_mousedown_position;
  78. bool m_might_drag { false };
  79. ModelIndex m_hovered_index;
  80. private:
  81. RefPtr<Model> m_model;
  82. OwnPtr<ModelEditingDelegate> m_editing_delegate;
  83. ModelSelection m_selection;
  84. bool m_activates_on_selection { false };
  85. };
  86. }