FormEditorWidget.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/HashTable.h>
  28. #include <LibGUI/ScrollableWidget.h>
  29. class FormWidget;
  30. class Tool;
  31. class WidgetTreeModel;
  32. class FormEditorWidget final : public GUI::ScrollableWidget {
  33. C_OBJECT(FormEditorWidget)
  34. public:
  35. virtual ~FormEditorWidget() override;
  36. FormWidget& form_widget() { return *m_form_widget; }
  37. const FormWidget& form_widget() const { return *m_form_widget; }
  38. Tool& tool() { return *m_tool; }
  39. const Tool& tool() const { return *m_tool; }
  40. void set_tool(NonnullOwnPtr<Tool>);
  41. WidgetTreeModel& model();
  42. class WidgetSelection {
  43. public:
  44. Function<void(GUI::Widget&)> on_remove;
  45. Function<void(GUI::Widget&)> on_add;
  46. Function<void()> on_clear;
  47. void enable_hooks() { m_hooks_enabled = true; }
  48. void disable_hooks() { m_hooks_enabled = false; }
  49. bool is_empty() const
  50. {
  51. return m_widgets.is_empty();
  52. }
  53. bool contains(GUI::Widget& widget) const
  54. {
  55. return m_widgets.contains(&widget);
  56. }
  57. void toggle(GUI::Widget& widget)
  58. {
  59. if (contains(widget))
  60. remove(widget);
  61. else
  62. add(widget);
  63. }
  64. void set(GUI::Widget& widget)
  65. {
  66. clear();
  67. add(widget);
  68. }
  69. void remove(GUI::Widget& widget)
  70. {
  71. ASSERT(m_widgets.contains(&widget));
  72. m_widgets.remove(&widget);
  73. if (m_hooks_enabled && on_remove)
  74. on_remove(widget);
  75. }
  76. void add(GUI::Widget& widget)
  77. {
  78. m_widgets.set(&widget);
  79. if (m_hooks_enabled && on_add)
  80. on_add(widget);
  81. }
  82. void clear()
  83. {
  84. m_widgets.clear();
  85. if (m_hooks_enabled && on_clear)
  86. on_clear();
  87. }
  88. template<typename Callback>
  89. void for_each(Callback callback)
  90. {
  91. for (auto& it : m_widgets) {
  92. if (callback(*it) == IterationDecision::Break)
  93. break;
  94. }
  95. }
  96. WidgetSelection() {}
  97. private:
  98. HashTable<GUI::Widget*> m_widgets;
  99. bool m_hooks_enabled { true };
  100. };
  101. WidgetSelection& selection() { return m_selection; }
  102. private:
  103. virtual void paint_event(GUI::PaintEvent&) override;
  104. explicit FormEditorWidget(GUI::Widget* parent);
  105. RefPtr<FormWidget> m_form_widget;
  106. RefPtr<WidgetTreeModel> m_widget_tree_model;
  107. NonnullOwnPtr<Tool> m_tool;
  108. WidgetSelection m_selection;
  109. };