Blender  V3.3
spreadsheet_layout.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include <iomanip>
4 #include <sstream>
5 
6 #include "BLI_math_vec_types.hh"
7 
8 #include "BKE_geometry_set.hh"
9 
11 #include "spreadsheet_layout.hh"
12 
13 #include "DNA_collection_types.h"
14 #include "DNA_object_types.h"
15 #include "DNA_userdef_types.h"
16 
17 #include "UI_interface.h"
18 #include "UI_resources.h"
19 
20 #include "BLF_api.h"
21 
22 #include "BLT_translation.h"
23 
24 namespace blender::ed::spreadsheet {
25 
27  private:
28  const SpreadsheetLayout &spreadsheet_layout_;
29 
30  public:
31  SpreadsheetLayoutDrawer(const SpreadsheetLayout &spreadsheet_layout)
32  : spreadsheet_layout_(spreadsheet_layout)
33  {
34  tot_columns = spreadsheet_layout.columns.size();
35  tot_rows = spreadsheet_layout.row_indices.size();
36  left_column_width = spreadsheet_layout.index_column_width;
37  }
38 
39  void draw_top_row_cell(int column_index, const CellDrawParams &params) const final
40  {
41  const StringRefNull name = spreadsheet_layout_.columns[column_index].values->name();
42  uiBut *but = uiDefIconTextBut(params.block,
44  0,
45  ICON_NONE,
46  name.c_str(),
47  params.xmin,
48  params.ymin,
49  params.width,
50  params.height,
51  nullptr,
52  0,
53  0,
54  0,
55  0,
56  nullptr);
57  /* Center-align column headers. */
60  }
61 
62  void draw_left_column_cell(int row_index, const CellDrawParams &params) const final
63  {
64  const int real_index = spreadsheet_layout_.row_indices[row_index];
65  std::string index_str = std::to_string(real_index);
66  uiBut *but = uiDefIconTextBut(params.block,
68  0,
69  ICON_NONE,
70  index_str.c_str(),
71  params.xmin,
72  params.ymin,
73  params.width,
74  params.height,
75  nullptr,
76  0,
77  0,
78  0,
79  0,
80  nullptr);
81  /* Right-align indices. */
84  }
85 
86  void draw_content_cell(int row_index, int column_index, const CellDrawParams &params) const final
87  {
88  const int real_index = spreadsheet_layout_.row_indices[row_index];
89  const ColumnValues &column = *spreadsheet_layout_.columns[column_index].values;
90  if (real_index > column.size()) {
91  return;
92  }
93 
94  const GVArray &data = column.data();
95 
96  if (data.type().is<int>()) {
97  const int value = data.get<int>(real_index);
98  const std::string value_str = std::to_string(value);
99  uiBut *but = uiDefIconTextBut(params.block,
101  0,
102  ICON_NONE,
103  value_str.c_str(),
104  params.xmin,
105  params.ymin,
106  params.width,
107  params.height,
108  nullptr,
109  0,
110  0,
111  0,
112  0,
113  nullptr);
114  /* Right-align Integers. */
117  }
118  if (data.type().is<int8_t>()) {
119  const int8_t value = data.get<int8_t>(real_index);
120  const std::string value_str = std::to_string(value);
121  uiBut *but = uiDefIconTextBut(params.block,
123  0,
124  ICON_NONE,
125  value_str.c_str(),
126  params.xmin,
127  params.ymin,
128  params.width,
129  params.height,
130  nullptr,
131  0,
132  0,
133  0,
134  0,
135  nullptr);
136  /* Right-align Integers. */
139  }
140  else if (data.type().is<float>()) {
141  const float value = data.get<float>(real_index);
142  std::stringstream ss;
143  ss << std::fixed << std::setprecision(3) << value;
144  const std::string value_str = ss.str();
145  uiBut *but = uiDefIconTextBut(params.block,
147  0,
148  ICON_NONE,
149  value_str.c_str(),
150  params.xmin,
151  params.ymin,
152  params.width,
153  params.height,
154  nullptr,
155  0,
156  0,
157  0,
158  0,
159  nullptr);
160  /* Right-align Floats. */
163  }
164  else if (data.type().is<bool>()) {
165  const bool value = data.get<bool>(real_index);
166  const int icon = value ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
167  uiBut *but = uiDefIconTextBut(params.block,
169  0,
170  icon,
171  "",
172  params.xmin,
173  params.ymin,
174  params.width,
175  params.height,
176  nullptr,
177  0,
178  0,
179  0,
180  0,
181  nullptr);
183  }
184  else if (data.type().is<float2>()) {
185  const float2 value = data.get<float2>(real_index);
186  this->draw_float_vector(params, Span(&value.x, 2));
187  }
188  else if (data.type().is<float3>()) {
189  const float3 value = data.get<float3>(real_index);
190  this->draw_float_vector(params, Span(&value.x, 3));
191  }
192  else if (data.type().is<ColorGeometry4f>()) {
193  const ColorGeometry4f value = data.get<ColorGeometry4f>(real_index);
194  this->draw_float_vector(params, Span(&value.r, 4));
195  }
196  else if (data.type().is<ColorGeometry4b>()) {
197  const ColorGeometry4b value = data.get<ColorGeometry4b>(real_index);
198  this->draw_byte_color(params, value);
199  }
200  else if (data.type().is<InstanceReference>()) {
201  const InstanceReference value = data.get<InstanceReference>(real_index);
202  switch (value.type()) {
204  const Object &object = value.object();
205  uiDefIconTextBut(params.block,
207  0,
208  ICON_OBJECT_DATA,
209  object.id.name + 2,
210  params.xmin,
211  params.ymin,
212  params.width,
213  params.height,
214  nullptr,
215  0,
216  0,
217  0,
218  0,
219  nullptr);
220  break;
221  }
223  Collection &collection = value.collection();
224  uiDefIconTextBut(params.block,
226  0,
227  ICON_OUTLINER_COLLECTION,
228  collection.id.name + 2,
229  params.xmin,
230  params.ymin,
231  params.width,
232  params.height,
233  nullptr,
234  0,
235  0,
236  0,
237  0,
238  nullptr);
239  break;
240  }
242  uiDefIconTextBut(params.block,
244  0,
245  ICON_MESH_DATA,
246  "Geometry",
247  params.xmin,
248  params.ymin,
249  params.width,
250  params.height,
251  nullptr,
252  0,
253  0,
254  0,
255  0,
256  nullptr);
257  break;
258  }
260  break;
261  }
262  }
263  }
264  else if (data.type().is<std::string>()) {
265  uiDefIconTextBut(params.block,
267  0,
268  ICON_NONE,
269  data.get<std::string>(real_index).c_str(),
270  params.xmin,
271  params.ymin,
272  params.width,
273  params.height,
274  nullptr,
275  0,
276  0,
277  0,
278  0,
279  nullptr);
280  }
281  }
282 
283  void draw_float_vector(const CellDrawParams &params, const Span<float> values) const
284  {
285  BLI_assert(!values.is_empty());
286  const float segment_width = (float)params.width / values.size();
287  for (const int i : values.index_range()) {
288  std::stringstream ss;
289  const float value = values[i];
290  ss << " " << std::fixed << std::setprecision(3) << value;
291  const std::string value_str = ss.str();
292  uiBut *but = uiDefIconTextBut(params.block,
294  0,
295  ICON_NONE,
296  value_str.c_str(),
297  params.xmin + i * segment_width,
298  params.ymin,
299  segment_width,
300  params.height,
301  nullptr,
302  0,
303  0,
304  0,
305  0,
306  nullptr);
307  /* Right-align Floats. */
310  }
311  }
312 
314  {
315  const ColorGeometry4f float_color = color.decode();
316  Span<float> values(&float_color.r, 4);
317  const float segment_width = (float)params.width / values.size();
318  for (const int i : values.index_range()) {
319  std::stringstream ss;
320  const float value = values[i];
321  ss << " " << std::fixed << std::setprecision(3) << value;
322  const std::string value_str = ss.str();
323  uiBut *but = uiDefIconTextBut(params.block,
325  0,
326  ICON_NONE,
327  value_str.c_str(),
328  params.xmin + i * segment_width,
329  params.ymin,
330  segment_width,
331  params.height,
332  nullptr,
333  0,
334  0,
335  0,
336  0,
337  nullptr);
338  /* Right-align Floats. */
341 
342  /* Tooltip showing raw byte values. Encode values in pointer to avoid memory allocation. */
344  but,
345  [](bContext * /*C*/, void *argN, const char *UNUSED(tip)) {
346  const uint32_t uint_color = POINTER_AS_UINT(argN);
347  ColorGeometry4b color = *(ColorGeometry4b *)&uint_color;
348  return BLI_sprintfN(TIP_("Byte Color (sRGB encoded):\n%3d %3d %3d %3d"),
349  color.r,
350  color.g,
351  color.b,
352  color.a);
353  },
355  nullptr);
356  }
357  }
358 
359  int column_width(int column_index) const final
360  {
361  return spreadsheet_layout_.columns[column_index].width;
362  }
363 };
364 
365 std::unique_ptr<SpreadsheetDrawer> spreadsheet_drawer_from_layout(
366  const SpreadsheetLayout &spreadsheet_layout)
367 {
368  return std::make_unique<SpreadsheetLayoutDrawer>(spreadsheet_layout);
369 }
370 
371 } // namespace blender::ed::spreadsheet
typedef float(TangentPoint)[2]
#define BLI_assert(a)
Definition: BLI_assert.h:46
size_t size_t char * BLI_sprintfN(const char *__restrict format,...) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC ATTR_PRINTF_FORMAT(1
#define POINTER_AS_UINT(i)
#define UNUSED(x)
#define POINTER_FROM_UINT(i)
#define TIP_(msgid)
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert a color
@ UI_BUT_TEXT_RIGHT
Definition: UI_interface.h:261
@ UI_BUT_ICON_LEFT
Definition: UI_interface.h:260
@ UI_BUT_TEXT_LEFT
Definition: UI_interface.h:259
uiBut * uiDefIconTextBut(uiBlock *block, int type, int retval, int icon, const char *str, int x, int y, short width, short height, void *poin, float min, float max, float a1, float a2, const char *tip)
Definition: interface.cc:5623
void UI_but_func_tooltip_set(uiBut *but, uiButToolTipFunc func, void *arg, uiFreeArgFunc free_arg)
Definition: interface.cc:6029
void UI_but_drawflag_enable(uiBut *but, int flag)
Definition: interface.cc:5873
void UI_but_drawflag_disable(uiBut *but, int flag)
Definition: interface.cc:5878
@ UI_BTYPE_LABEL
Definition: UI_interface.h:354
Object & object() const
Collection & collection() const
ChannelStorageType r
Definition: BLI_color.hh:85
int64_t size() const
constexpr int64_t size() const
Definition: BLI_span.hh:240
constexpr IndexRange index_range() const
Definition: BLI_span.hh:401
constexpr bool is_empty() const
Definition: BLI_span.hh:248
constexpr const char * c_str() const
void draw_top_row_cell(int column_index, const CellDrawParams &params) const final
void draw_byte_color(const CellDrawParams &params, const ColorGeometry4b color) const
void draw_left_column_cell(int row_index, const CellDrawParams &params) const final
void draw_content_cell(int row_index, int column_index, const CellDrawParams &params) const final
void draw_float_vector(const CellDrawParams &params, const Span< float > values) const
SpreadsheetLayoutDrawer(const SpreadsheetLayout &spreadsheet_layout)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
std::unique_ptr< SpreadsheetDrawer > spreadsheet_drawer_from_layout(const SpreadsheetLayout &spreadsheet_layout)
std::string to_string(const T &n)
unsigned int uint32_t
Definition: stdint.h:80
signed char int8_t
Definition: stdint.h:75
char name[66]
Definition: DNA_ID.h:378