[Bf-blender-cvs] [348ec37f524] master: UI: Add AbstractViewItem base class

Julian Eisel noreply at git.blender.org
Tue Jul 19 16:32:34 CEST 2022


Commit: 348ec37f52452614cb26baa8be40a161e1446b15
Author: Julian Eisel
Date:   Mon Jul 18 16:51:57 2022 +0200
Branches: master
https://developer.blender.org/rB348ec37f52452614cb26baa8be40a161e1446b15

UI: Add AbstractViewItem base class

No user visible changes expected.

Similar to rBc355be6faeac, but for view items now instead of the view.
Not much of the item code is ported to use it yet, it's actually a bit
tricky for the most part. But just introducing the base class already
allows me to start unifying the view item buttons (`uiButTreeRow` and
`uiButGridTile`). This would be a nice improvement.

===================================================================

M	source/blender/editors/include/UI_abstract_view.hh
M	source/blender/editors/include/UI_grid_view.hh
M	source/blender/editors/include/UI_tree_view.hh
M	source/blender/editors/interface/CMakeLists.txt
A	source/blender/editors/interface/abstract_view_item.cc
M	source/blender/editors/interface/grid_view.cc
M	source/blender/editors/interface/tree_view.cc

===================================================================

diff --git a/source/blender/editors/include/UI_abstract_view.hh b/source/blender/editors/include/UI_abstract_view.hh
index 82f81f1702b..fdb7069e7c4 100644
--- a/source/blender/editors/include/UI_abstract_view.hh
+++ b/source/blender/editors/include/UI_abstract_view.hh
@@ -15,12 +15,17 @@
 #include <array>
 #include <memory>
 
+#include "DNA_defs.h"
+
 #include "BLI_span.hh"
 
 struct wmNotifier;
+struct uiBlock;
 
 namespace blender::ui {
 
+class AbstractViewItem;
+
 class AbstractView {
   bool is_reconstructed_ = false;
   /**
@@ -66,4 +71,26 @@ class AbstractView {
   bool is_reconstructed() const;
 };
 
+class AbstractViewItem {
+ protected:
+  bool is_active_ = false;
+
+ public:
+  virtual ~AbstractViewItem() = default;
+
+ protected:
+  AbstractViewItem() = default;
+
+  /**
+   * Copy persistent state (e.g. active, selection, etc.) from a matching item of
+   * the last redraw to this item. If sub-classes introduce more advanced state they should
+   * override this and make it update their state accordingly.
+   *
+   * \note Always call the base class implementation when overriding this!
+   */
+  virtual void update_from_old(const AbstractViewItem &old);
+
+  void set_view(AbstractView &view);
+};
+
 }  // namespace blender::ui
diff --git a/source/blender/editors/include/UI_grid_view.hh b/source/blender/editors/include/UI_grid_view.hh
index cabc49e411c..74ce16ccd85 100644
--- a/source/blender/editors/include/UI_grid_view.hh
+++ b/source/blender/editors/include/UI_grid_view.hh
@@ -32,14 +32,12 @@ class AbstractGridView;
 /** \name Grid-View Item Type
  * \{ */
 
-class AbstractGridViewItem {
+class AbstractGridViewItem : public AbstractViewItem {
   friend class AbstractGridView;
   friend class GridViewLayoutBuilder;
 
   const AbstractGridView *view_;
 
-  bool is_active_ = false;
-
  protected:
   /** Reference to a string that uniquely identifies this item in the view. */
   StringRef identifier_{};
@@ -77,13 +75,6 @@ class AbstractGridViewItem {
    */
   virtual std::optional<bool> should_be_active() const;
 
-  /**
-   * Copy persistent state (e.g. active, selection, etc.) from a matching item of
-   * the last redraw to this item. If sub-classes introduce more advanced state they should
-   * override this and make it update their state accordingly.
-   */
-  virtual void update_from_old(const AbstractGridViewItem &old);
-
   /**
    * Activates this item, deactivates other items, and calls the
    * #AbstractGridViewItem::on_activate() function.
diff --git a/source/blender/editors/include/UI_tree_view.hh b/source/blender/editors/include/UI_tree_view.hh
index 9527df590b7..fa427d96dd8 100644
--- a/source/blender/editors/include/UI_tree_view.hh
+++ b/source/blender/editors/include/UI_tree_view.hh
@@ -153,7 +153,7 @@ class AbstractTreeView : public AbstractView, public TreeViewItemContainer {
  * It also stores state information that needs to be persistent over redraws, like the collapsed
  * state.
  */
-class AbstractTreeViewItem : public TreeViewItemContainer {
+class AbstractTreeViewItem : public AbstractViewItem, public TreeViewItemContainer {
   friend class AbstractTreeView;
   friend class TreeViewLayoutBuilder;
   /* Higher-level API. */
@@ -161,7 +161,6 @@ class AbstractTreeViewItem : public TreeViewItemContainer {
 
  private:
   bool is_open_ = false;
-  bool is_active_ = false;
   bool is_renaming_ = false;
 
  protected:
@@ -222,12 +221,8 @@ class AbstractTreeViewItem : public TreeViewItemContainer {
    */
   virtual bool supports_collapsing() const;
 
-  /**
-   * Copy persistent state (e.g. is-collapsed flag, selection, etc.) from a matching item of
-   * the last redraw to this item. If sub-classes introduce more advanced state they should
-   * override this and make it update their state accordingly.
-   */
-  virtual void update_from_old(const AbstractTreeViewItem &old);
+  /** See #AbstractViewItem::update_from_old(). */
+  virtual void update_from_old(const AbstractViewItem &old) override;
 
   /**
    * Compare this item to \a other to check if they represent the same data.
diff --git a/source/blender/editors/interface/CMakeLists.txt b/source/blender/editors/interface/CMakeLists.txt
index 1bdec57ac59..c6c9f1f80c8 100644
--- a/source/blender/editors/interface/CMakeLists.txt
+++ b/source/blender/editors/interface/CMakeLists.txt
@@ -26,6 +26,7 @@ set(INC
 
 set(SRC
   abstract_view.cc
+  abstract_view_item.cc
   grid_view.cc
   interface.cc
   interface_align.c
diff --git a/source/blender/editors/interface/abstract_view_item.cc b/source/blender/editors/interface/abstract_view_item.cc
new file mode 100644
index 00000000000..fc71dbe8b95
--- /dev/null
+++ b/source/blender/editors/interface/abstract_view_item.cc
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+
+/** \file
+ * \ingroup edinterface
+ */
+
+#include "UI_abstract_view.hh"
+
+namespace blender::ui {
+
+/* ---------------------------------------------------------------------- */
+/** \name View Reconstruction
+ * \{ */
+
+void AbstractViewItem::update_from_old(const AbstractViewItem &old)
+{
+  is_active_ = old.is_active_;
+}
+
+/** \} */
+
+}  // namespace blender::ui
diff --git a/source/blender/editors/interface/grid_view.cc b/source/blender/editors/interface/grid_view.cc
index 19a2326fba1..362642b0846 100644
--- a/source/blender/editors/interface/grid_view.cc
+++ b/source/blender/editors/interface/grid_view.cc
@@ -158,11 +158,6 @@ void AbstractGridViewItem::change_state_delayed()
   }
 }
 
-void AbstractGridViewItem::update_from_old(const AbstractGridViewItem &old)
-{
-  is_active_ = old.is_active_;
-}
-
 void AbstractGridViewItem::activate()
 {
   BLI_assert_msg(get_view().is_reconstructed(),
diff --git a/source/blender/editors/interface/tree_view.cc b/source/blender/editors/interface/tree_view.cc
index d29cf367e59..ec1140e8efb 100644
--- a/source/blender/editors/interface/tree_view.cc
+++ b/source/blender/editors/interface/tree_view.cc
@@ -339,11 +339,13 @@ void AbstractTreeViewItem::build_context_menu(bContext & /*C*/, uiLayout & /*col
   /* No context menu by default. */
 }
 
-void AbstractTreeViewItem::update_from_old(const AbstractTreeViewItem &old)
+void AbstractTreeViewItem::update_from_old(const AbstractViewItem &old)
 {
-  is_open_ = old.is_open_;
-  is_active_ = old.is_active_;
-  is_renaming_ = old.is_renaming_;
+  AbstractViewItem::update_from_old(old);
+
+  const AbstractTreeViewItem &old_tree_item = dynamic_cast<const AbstractTreeViewItem &>(old);
+  is_open_ = old_tree_item.is_open_;
+  is_renaming_ = old_tree_item.is_renaming_;
 }
 
 bool AbstractTreeViewItem::matches(const AbstractTreeViewItem &other) const



More information about the Bf-blender-cvs mailing list