Blender  V2.93
spreadsheet_column_values.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
19 #include "BLI_string_ref.hh"
20 
22 
23 namespace blender::ed::spreadsheet {
24 
29 class ColumnValues {
30  protected:
31  std::string name_;
32  int size_;
33 
34  public:
35  ColumnValues(std::string name, const int size) : name_(std::move(name)), size_(size)
36  {
37  }
38 
39  virtual ~ColumnValues() = default;
40 
41  virtual void get_value(int index, CellValue &r_cell_value) const = 0;
42 
44  {
45  return name_;
46  }
47 
48  int size() const
49  {
50  return size_;
51  }
52 
53  /* The default width of newly created columns, in UI units. */
54  float default_width = 0.0f;
55 };
56 
57 /* Utility class for the function below. */
58 template<typename GetValueF> class LambdaColumnValues : public ColumnValues {
59  private:
60  GetValueF get_value_;
61 
62  public:
63  LambdaColumnValues(std::string name, int size, GetValueF get_value)
64  : ColumnValues(std::move(name), size), get_value_(std::move(get_value))
65  {
66  }
67 
68  void get_value(int index, CellValue &r_cell_value) const final
69  {
70  get_value_(index, r_cell_value);
71  }
72 };
73 
74 /* Utility function that simplifies creating a spreadsheet column from a lambda function. */
75 template<typename GetValueF>
76 std::unique_ptr<ColumnValues> column_values_from_function(std::string name,
77  const int size,
78  GetValueF get_value,
79  const float default_width = 0.0f)
80 {
81  std::unique_ptr<ColumnValues> column_values = std::make_unique<LambdaColumnValues<GetValueF>>(
82  std::move(name), size, std::move(get_value));
83  column_values->default_width = default_width;
84  return column_values;
85 }
86 
87 static constexpr float default_float_column_width = 3;
91 
92 } // namespace blender::ed::spreadsheet
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
ColumnValues(std::string name, const int size)
virtual void get_value(int index, CellValue &r_cell_value) const =0
LambdaColumnValues(std::string name, int size, GetValueF get_value)
void get_value(int index, CellValue &r_cell_value) const final
std::unique_ptr< ColumnValues > column_values_from_function(std::string name, const int size, GetValueF get_value, const float default_width=0.0f)
static constexpr float default_float2_column_width
static constexpr float default_color_column_width
static constexpr float default_float_column_width
static constexpr float default_float3_column_width