Blender V5.3
spreadsheet_layout.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <iomanip>
6#include <sstream>
7
8#include <fmt/format.h>
9
10#include "BLF_api.hh"
11
12#include "BLI_color.hh"
13#include "BLI_math_matrix.hh"
16#include "BLI_string.hh"
17
18#include "BKE_instances.hh"
19#include "BKE_lib_id.hh"
20
22
23#include "ED_outliner.hh"
24
26
29#include "spreadsheet_layout.hh"
30
32#include "DNA_meshdata_types.h"
33#include "DNA_sound_types.h"
34#include "DNA_vfont_types.h"
35
36#include "UI_interface.hh"
37#include "UI_resources.hh"
38
39#include "BLT_translation.hh"
40
41/* Need to do our own padding in some cases because we use low-level ui code to draw the
42 * spreadsheet. */
43#define CELL_PADDING_X (0.15f * SPREADSHEET_WIDTH_UNIT)
44
46
47static std::string format_matrix_to_grid(const float4x4 &matrix)
48{
49 auto format_element = [](float value) {
50 if (math::abs(value) < 1e-4f) {
51 return fmt::format("{:.3}", value);
52 }
53 return fmt::format("{:.6}", value);
54 };
55
56 /* Transpose to be able to print row by row. */
57 const float4x4 t_matrix = math::transpose(matrix);
58 std::array<std::array<std::string, 4>, 4> formatted_elements;
59 std::array<size_t, 4> column_widths = {};
60 for (const int row_i : IndexRange(4)) {
61 for (const int col_i : IndexRange(4)) {
62 formatted_elements[row_i][col_i] = format_element(t_matrix[row_i][col_i]);
63 column_widths[col_i] = std::max(column_widths[col_i],
64 formatted_elements[row_i][col_i].length());
65 }
66 }
67
68 fmt::memory_buffer buf;
69 for (const int row_i : IndexRange(4)) {
70 for (const int col_i : IndexRange(4)) {
71 fmt::format_to(
72 fmt::appender(buf), "{:>{}}", formatted_elements[row_i][col_i], column_widths[col_i]);
73 if (col_i < 3) {
74 fmt::format_to(fmt::appender(buf), " ");
75 }
76 }
77 if (row_i < 3) {
78 fmt::format_to(fmt::appender(buf), "\n");
79 }
80 }
81 return fmt::to_string(buf);
82}
83
85 private:
86 const SpreadsheetLayout &spreadsheet_layout_;
87
88 public:
89 SpreadsheetLayoutDrawer(const SpreadsheetLayout &spreadsheet_layout)
90 : spreadsheet_layout_(spreadsheet_layout)
91 {
92 tot_columns = spreadsheet_layout.columns.size();
93 tot_rows = spreadsheet_layout.row_indices.size();
94 left_column_width = spreadsheet_layout.index_column_width;
95 }
96
97 void draw_top_row_cell(int column_index, const CellDrawParams &params) const final
98 {
99 const ColumnValues &values = *spreadsheet_layout_.columns[column_index].values;
100 const StringRefNull name = values.name();
101 ui::Button *but = uiDefIconTextBut(params.block,
103 ICON_NONE,
104 name,
105 params.xmin,
106 params.ymin,
107 params.width,
108 params.height,
109 nullptr,
110 std::nullopt);
111 std::string description = values.description().is_empty() ?
112 std::string(name) :
113 fmt::format("{}\n{}", name, values.description());
114 button_func_tooltip_set(
115 but,
116 [](bContext * /*C*/, void *arg, StringRef /*tip*/) {
117 return *static_cast<std::string *>(arg);
118 },
119 MEM_new<std::string>(__func__, std::move(description)),
120 [](void *arg) { MEM_delete(static_cast<std::string *>(arg)); });
121 /* Center-align column headers. */
122 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
123 button_drawflag_disable(but, ui::BUT_TEXT_RIGHT);
124 }
125
126 void draw_left_column_cell(int row_index, const CellDrawParams &params) const final
127 {
128 const int real_index = spreadsheet_layout_.row_indices[row_index];
129 std::string index_str = std::to_string(real_index);
130 ui::Button *but = uiDefIconTextBut(params.block,
132 ICON_NONE,
133 index_str,
134 params.xmin,
135 params.ymin,
136 params.width,
137 params.height,
138 nullptr,
139 std::nullopt);
140 /* Right-align indices. */
141 button_drawflag_enable(but, ui::BUT_TEXT_RIGHT);
142 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
143 }
144
145 void draw_content_cell(int row_index, int column_index, const CellDrawParams &params) const final
146 {
147 const int real_index = spreadsheet_layout_.row_indices[row_index];
148 const ColumnValues &column = *spreadsheet_layout_.columns[column_index].values;
149 if (real_index > column.size()) {
150 return;
151 }
152
153 const GVArray &data = column.data();
154 const CPPType &type = data.type();
155 BUFFER_FOR_CPP_TYPE_VALUE(type, buffer);
156 data.get_to_uninitialized(real_index, buffer);
157 this->draw_content_cell_value(GPointer(type, buffer), params, column);
158 type.destruct(buffer);
159 }
160
161 void draw_content_cell_value(const GPointer value_ptr,
162 const CellDrawParams &params,
163 const ColumnValues &column) const
164 {
165 const CPPType &type = *value_ptr.type();
166 if (type.is<int>()) {
167 this->draw_int(params, *value_ptr.get<int>(), column.display_hint());
168 return;
169 }
170 if (type.is<int64_t>()) {
171 this->draw_int(params, *value_ptr.get<int64_t>(), column.display_hint());
172 return;
173 }
174 if (type.is<int8_t>()) {
175 const int8_t value = *value_ptr.get<int8_t>();
176 const std::string value_str = std::to_string(value);
177 ui::Button *but = uiDefIconTextBut(params.block,
179 ICON_NONE,
180 value_str,
181 params.xmin,
182 params.ymin,
183 params.width,
184 params.height,
185 nullptr,
186 std::nullopt);
187 /* Right-align Integers. */
188 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
189 button_drawflag_enable(but, ui::BUT_TEXT_RIGHT);
190 return;
191 }
192 if (type.is<short2>()) {
193 const int2 value = int2(*value_ptr.get<short2>());
194 this->draw_int_vector(params, Span(&value.x, 2));
195 return;
196 }
197 if (type.is<int2>()) {
198 const int2 value = *value_ptr.get<int2>();
199 this->draw_int_vector(params, Span(&value.x, 2));
200 return;
201 }
202 if (type.is<int3>()) {
203 const int3 value = *value_ptr.get<int3>();
204 this->draw_int_vector(params, Span(&value.x, 3));
205 return;
206 }
207 if (type.is<float>()) {
208 const float value = *value_ptr.get<float>();
209 std::stringstream ss;
210 ss << std::fixed << std::setprecision(3) << value;
211 const std::string value_str = ss.str();
212 ui::Button *but = uiDefIconTextBut(params.block,
214 ICON_NONE,
215 value_str,
216 params.xmin,
217 params.ymin,
218 params.width,
219 params.height,
220 nullptr,
221 std::nullopt);
222 button_func_tooltip_set(
223 but,
224 [](bContext * /*C*/, void *argN, const StringRef /*tip*/) {
225 return fmt::format("{:f}", *(static_cast<float *>(argN)));
226 },
227 MEM_new<float>(__func__, value),
229 /* Right-align Floats. */
230 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
231 button_drawflag_enable(but, ui::BUT_TEXT_RIGHT);
232 return;
233 }
234 if (type.is<bool>()) {
235 const bool value = *value_ptr.get<bool>();
236 const int icon = value ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
237 ui::Button *but = uiDefIconTextBut(params.block,
239 icon,
240 "",
241 params.xmin,
242 params.ymin,
243 params.width,
244 params.height,
245 nullptr,
246 std::nullopt);
247 button_drawflag_disable(but, ui::BUT_ICON_LEFT);
248 return;
249 }
250 if (type.is<float2>()) {
251 const float2 value = *value_ptr.get<float2>();
252 this->draw_float_vector(params, Span(&value.x, 2));
253 return;
254 }
255 if (type.is<float3>()) {
256 const float3 value = *value_ptr.get<float3>();
257 this->draw_float_vector(params, Span(&value.x, 3));
258 return;
259 }
260 if (type.is<float4>()) {
261 const float4 value = *value_ptr.get<float4>();
262 this->draw_float_vector(params, Span(&value.x, 4));
263 return;
264 }
265 if (type.is<ColorGeometry4f>()) {
266 const ColorGeometry4f value = *value_ptr.get<ColorGeometry4f>();
267 this->draw_float_vector(params, Span(&value.r, 4));
268 return;
269 }
270 if (type.is<ColorGeometry4b>()) {
271 const ColorGeometry4b value = *value_ptr.get<ColorGeometry4b>();
272 this->draw_byte_color(params, value);
273 return;
274 }
275 if (type.is<math::Quaternion>()) {
276 const float4 value = float4(*value_ptr.get<math::Quaternion>());
277 this->draw_float_vector(params, Span(&value.x, 4));
278 return;
279 }
280 if (type.is<float4x4>()) {
281 this->draw_float4x4(params, *value_ptr.get<float4x4>());
282 return;
283 }
284 if (type.is<bke::InstanceReference>()) {
285 const bke::InstanceReference value = *value_ptr.get<bke::InstanceReference>();
286 const StringRefNull name = value.name().is_empty() ? IFACE_("(Geometry)") : value.name();
287 const int icon = get_instance_reference_icon(value);
288 uiDefIconTextBut(params.block,
290 icon,
291 name.c_str(),
292 params.xmin,
293 params.ymin,
294 params.width,
295 params.height,
296 nullptr,
297 std::nullopt);
298 return;
299 }
300 if (type.is<std::string>()) {
301 uiDefIconTextBut(params.block,
303 ICON_NONE,
304 *value_ptr.get<std::string>(),
305 params.xmin + CELL_PADDING_X,
306 params.ymin,
307 params.width - 2.0f * CELL_PADDING_X,
308 params.height,
309 nullptr,
310 std::nullopt);
311 return;
312 }
313 if (type.is<MStringProperty>()) {
315 *prop = *value_ptr.get<MStringProperty>();
316 ui::Button *but = uiDefIconTextBut(params.block,
318 ICON_NONE,
319 StringRef(prop->s, prop->s_len),
320 params.xmin + CELL_PADDING_X,
321 params.ymin,
322 params.width - 2.0f * CELL_PADDING_X,
323 params.height,
324 nullptr,
325 std::nullopt);
326
327 button_func_tooltip_set(
328 but,
329 [](bContext * /*C*/, void *argN, const StringRef /*tip*/) {
330 const MStringProperty &prop = *static_cast<MStringProperty *>(argN);
331 return std::string(StringRef(prop.s, prop.s_len));
332 },
333 prop,
335 return;
336 }
337 if (type.is<nodes::BundleItemValue>()) {
338 const nodes::BundleItemValue &value = *value_ptr.get<nodes::BundleItemValue>();
339 if (const nodes::BundleItemSocketValue *socket_value =
340 std::get_if<nodes::BundleItemSocketValue>(&value.value))
341 {
342 const bke::SocketValueVariant &value_variant = socket_value->value;
343 this->draw_content_cell_value(&value_variant, params, column);
344 return;
345 }
346 this->draw_undrawable(params);
347 return;
348 }
349 if (type.is<Object *>()) {
350 this->draw_data_block(params, *value_ptr.get<Object *>());
351 return;
352 }
353 if (type.is<Collection *>()) {
354 const Collection *collection = *value_ptr.get<Collection *>();
355 /* Using original collection because changing the color tag does not cause the eval copy to
356 * be updated. */
357 const Collection *orig_collection = DEG_get_original(collection);
358 if (orig_collection) {
359 int icon = ED_outliner_icon_from_id(orig_collection->id);
360 if (orig_collection->color_tag != COLLECTION_COLOR_NONE) {
361 icon = int(ICON_COLLECTION_COLOR_01) + int(orig_collection->color_tag);
362 }
363 this->draw_data_block(params, collection, icon);
364 return;
365 }
366 this->draw_data_block(params, collection);
367 return;
368 }
369 if (type.is<Material *>()) {
370 this->draw_data_block(params, *value_ptr.get<Material *>());
371 return;
372 }
373 if (type.is<VFont *>()) {
374 this->draw_data_block(params, *value_ptr.get<VFont *>());
375 return;
376 }
377 if (type.is<bSound *>()) {
378 this->draw_data_block(params, *value_ptr.get<bSound *>());
379 return;
380 }
381 if (type.is<Image *>()) {
382 this->draw_data_block(params, *value_ptr.get<Image *>());
383 return;
384 }
385 if (type.is<bke::SocketValueVariant>()) {
386 const bke::SocketValueVariant &value_variant = *value_ptr.get<bke::SocketValueVariant>();
387 if (value_variant.is_single()) {
388 const GPointer single_value_ptr = value_variant.get();
389 this->draw_content_cell_value(single_value_ptr, params, column);
390 return;
391 }
392 this->draw_undrawable(params);
393 return;
394 }
395 this->draw_undrawable(params);
396 }
397
398 void draw_float_vector(const CellDrawParams &params, const Span<float> values) const
399 {
400 BLI_assert(!values.is_empty());
401 const float segment_width = float(params.width) / values.size();
402 for (const int i : values.index_range()) {
403 std::stringstream ss;
404 const float value = values[i];
405 ss << " " << std::fixed << std::setprecision(3) << value;
406 const std::string value_str = ss.str();
407 ui::Button *but = uiDefIconTextBut(params.block,
409 ICON_NONE,
410 value_str,
411 params.xmin + i * segment_width,
412 params.ymin,
413 segment_width,
414 params.height,
415 nullptr,
416 std::nullopt);
417
418 button_func_tooltip_set(
419 but,
420 [](bContext * /*C*/, void *argN, const StringRef /*tip*/) {
421 return fmt::format("{:f}", *(static_cast<float *>(argN)));
422 },
423 MEM_new<float>(__func__, value),
425 /* Right-align Floats. */
426 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
427 button_drawflag_enable(but, ui::BUT_TEXT_RIGHT);
428 }
429 }
430
431 void draw_int(const CellDrawParams &params,
432 const int64_t value,
433 const ColumnValueDisplayHint display_hint) const
434 {
435 std::string value_str;
436 switch (display_hint) {
439 BLI_str_format_byte_unit(dst, value, true);
440 value_str = dst;
441 break;
442 }
443 default: {
446 value_str = dst;
447 break;
448 }
449 }
450 ui::Button *but = uiDefIconTextBut(params.block,
452 ICON_NONE,
453 value_str,
454 params.xmin,
455 params.ymin,
456 params.width,
457 params.height,
458 nullptr,
459 std::nullopt);
460 switch (display_hint) {
462 button_func_tooltip_set(
463 but,
464 [](bContext * /*C*/, void *argN, const StringRef /*tip*/) {
466 BLI_str_format_int64_grouped(dst, *static_cast<int64_t *>(argN));
467 return fmt::format("{} {}", dst, TIP_("bytes"));
468 },
469 MEM_new<int64_t>(__func__, value),
471 break;
472 }
473 default: {
474 button_func_tooltip_set(
475 but,
476 [](bContext * /*C*/, void *argN, const StringRef /*tip*/) {
477 return fmt::format("{}", *static_cast<int64_t *>(argN));
478 },
479 MEM_new<int64_t>(__func__, value),
481 break;
482 }
483 }
484 /* Right-align Integers. */
485 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
486 button_drawflag_enable(but, ui::BUT_TEXT_RIGHT);
487 }
488
489 void draw_int_vector(const CellDrawParams &params, const Span<int> values) const
490 {
491 BLI_assert(!values.is_empty());
492 const float segment_width = float(params.width) / values.size();
493 for (const int i : values.index_range()) {
494 std::stringstream ss;
495 const int value = values[i];
496 ss << " " << value;
497 const std::string value_str = ss.str();
498 ui::Button *but = uiDefIconTextBut(params.block,
500 ICON_NONE,
501 value_str,
502 params.xmin + i * segment_width,
503 params.ymin,
504 segment_width,
505 params.height,
506 nullptr,
507 std::nullopt);
508 button_func_tooltip_set(
509 but,
510 [](bContext * /*C*/, void *argN, const StringRef /*tip*/) {
511 return fmt::format("{}", *(static_cast<int *>(argN)));
512 },
513 MEM_new<int>(__func__, value),
515 /* Right-align Floats. */
516 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
517 button_drawflag_enable(but, ui::BUT_TEXT_RIGHT);
518 }
519 }
520
521 void draw_byte_color(const CellDrawParams &params, const ColorGeometry4b color) const
522 {
523 const ColorGeometry4f float_color = color::decode(color);
524 Span<float> values(&float_color.r, 4);
525 const float segment_width = float(params.width) / values.size();
526 for (const int i : values.index_range()) {
527 std::stringstream ss;
528 const float value = values[i];
529 ss << " " << std::fixed << std::setprecision(3) << value;
530 const std::string value_str = ss.str();
531 ui::Button *but = uiDefIconTextBut(params.block,
533 ICON_NONE,
534 value_str,
535 params.xmin + i * segment_width,
536 params.ymin,
537 segment_width,
538 params.height,
539 nullptr,
540 std::nullopt);
541 /* Right-align Floats. */
542 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
543 button_drawflag_enable(but, ui::BUT_TEXT_RIGHT);
544
545 /* Tooltip showing raw byte values. Encode values in pointer to avoid memory allocation. */
546 button_func_tooltip_set(
547 but,
548 [](bContext * /*C*/, void *argN, const StringRef /*tip*/) {
549 const uint32_t uint_color = POINTER_AS_UINT(argN);
550 ColorGeometry4b color = *reinterpret_cast<ColorGeometry4b *>(
551 const_cast<uint32_t *>(&uint_color));
552 return fmt::format(fmt::runtime(TIP_("Byte Color (sRGB encoded):\n{} {} {} {}")),
553 color.r,
554 color.g,
555 color.b,
556 color.a);
557 },
559 nullptr);
560 }
561 }
562
563 void draw_float4x4(const CellDrawParams &params, const float4x4 &value) const
564 {
565 ui::Button *but = this->draw_undrawable(params);
566 /* Center alignment. */
567 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
568 button_func_tooltip_custom_set(
569 but,
570 [](bContext & /*C*/, ui::TooltipData &tip, ui::Button * /*but*/, void *argN) {
571 const float4x4 matrix = *static_cast<const float4x4 *>(argN);
572 tooltip_text_field_add(
574 },
575 MEM_new<float4x4>(__func__, value),
577 }
578
579 template<typename T>
580 void draw_data_block(const CellDrawParams &params,
581 const T *id,
582 const std::optional<int> icon_override = std::nullopt) const
583 {
584 if (!id) {
585 uiDefIconTextBut(params.block,
587 icon_override.value_or(ui::icon_from_idcode(int(T::id_type))),
588 "",
589 params.xmin,
590 params.ymin,
591 params.width,
592 params.height,
593 nullptr,
594 std::nullopt);
595 return;
596 }
597 const int icon = icon_override.value_or(ED_outliner_icon_from_id(id_cast<const ID &>(*id)));
598 uiDefIconTextBut(params.block,
600 icon,
601 BKE_id_name(id_cast<const ID &>(*id)),
602 params.xmin,
603 params.ymin,
604 params.width,
605 params.height,
606 nullptr,
607 std::nullopt);
608 }
609
611 {
612 ui::Button *but = uiDefIconTextBut(params.block,
614 ICON_NONE,
615 "...",
616 params.xmin,
617 params.ymin,
618 params.width,
619 params.height,
620 nullptr,
621 std::nullopt);
622 /* Center alignment. */
623 button_drawflag_disable(but, ui::BUT_TEXT_LEFT);
624 return but;
625 }
626
627 int column_width(int column_index) const final
628 {
629 return spreadsheet_layout_.columns[column_index].width;
630 }
631};
632
633template<typename T>
634static float estimate_max_column_width(const float min_width,
635 const int fontid,
636 const std::optional<int64_t> max_sample_size,
637 const VArray<T> &data,
638 FunctionRef<std::string(const T &)> to_string)
639{
640 if (const std::optional<T> value = data.get_if_single()) {
641 const std::string str = to_string(*value);
642 return std::max(min_width, BLF_width(fontid, str.c_str(), str.size()));
643 }
644 const int sample_size = max_sample_size.value_or(data.size());
645 float width = min_width;
646 for (const int i : data.index_range().take_front(sample_size)) {
647 const std::string str = to_string(data[i]);
648 const float value_width = BLF_width(fontid, str.c_str(), str.size());
649 width = std::max(width, value_width);
650 }
651 return width;
652}
653
654float ColumnValues::fit_column_values_width_px(const std::optional<int64_t> &max_sample_size) const
655{
656 const int fontid = BLF_default();
658
659 auto get_min_width = [&](const float min_width) {
660 return max_sample_size.has_value() ? min_width : 0.0f;
661 };
662
663 const eSpreadsheetColumnValueType column_type = this->type();
664 switch (column_type) {
666 return 2.0f * SPREADSHEET_WIDTH_UNIT;
667 }
669 return 2.0f * SPREADSHEET_WIDTH_UNIT;
670 }
673 get_min_width(3 * SPREADSHEET_WIDTH_UNIT),
674 fontid,
675 max_sample_size,
676 data_.typed<int8_t>(),
677 [](const int value) { return fmt::format("{}", value); });
678 }
681 fontid,
682 max_sample_size,
683 data_.typed<int>(),
684 [](const int value) {
685 char dst[BLI_STR_FORMAT_INT32_GROUPED_SIZE];
686 BLI_str_format_int_grouped(dst, value);
687 return std::string(dst);
688 });
689 }
691 const ColumnValueDisplayHint display_hint = this->display_hint();
693 get_min_width(3 * SPREADSHEET_WIDTH_UNIT),
694 fontid,
695 max_sample_size,
697 [display_hint](const int64_t value) {
698 switch (display_hint) {
699 case ColumnValueDisplayHint::Bytes: {
700 char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE];
701 BLI_str_format_byte_unit(dst, value, true);
702 return std::string(dst);
703 }
704 default: {
705 char dst[BLI_STR_FORMAT_INT64_GROUPED_SIZE];
706 BLI_str_format_int64_grouped(dst, value);
707 return std::string(dst);
708 }
709 }
710 });
711 }
714 get_min_width(3 * SPREADSHEET_WIDTH_UNIT),
715 fontid,
716 max_sample_size,
717 data_.typed<float>(),
718 [](const float value) { return fmt::format("{:.3f}", value); });
719 }
721 if (data_.type().is<short2>()) {
723 get_min_width(6 * SPREADSHEET_WIDTH_UNIT),
724 fontid,
725 max_sample_size,
726 data_.typed<short2>(),
727 [](const short2 value) { return fmt::format("{} {}", value.x, value.y); });
728 }
730 get_min_width(6 * SPREADSHEET_WIDTH_UNIT),
731 fontid,
732 max_sample_size,
733 data_.typed<int2>(),
734 [](const int2 value) { return fmt::format("{} {}", value.x, value.y); });
735 }
738 get_min_width(9 * SPREADSHEET_WIDTH_UNIT),
739 fontid,
740 max_sample_size,
741 data_.typed<int3>(),
742 [](const int3 value) { return fmt::format("{} {} {}", value.x, value.y, value.z); });
743 }
746 get_min_width(6 * SPREADSHEET_WIDTH_UNIT),
747 fontid,
748 max_sample_size,
749 data_.typed<float2>(),
750 [](const float2 value) { return fmt::format("{:.3f} {:.3f}", value.x, value.y); });
751 }
754 get_min_width(9 * SPREADSHEET_WIDTH_UNIT),
755 fontid,
756 max_sample_size,
757 data_.typed<float3>(),
758 [](const float3 value) {
759 return fmt::format("{:.3f} {:.3f} {:.3f}", value.x, value.y, value.z);
760 });
761 }
764 get_min_width(12 * SPREADSHEET_WIDTH_UNIT),
765 fontid,
766 max_sample_size,
767 data_.typed<float4>(),
768 [](const float4 value) {
769 return fmt::format(
770 "{:.3f} {:.3f} {:.3f} {:.3f}", value.x, value.y, value.z, value.w);
771 });
772 }
775 get_min_width(12 * SPREADSHEET_WIDTH_UNIT),
776 fontid,
777 max_sample_size,
779 [](const ColorGeometry4f value) {
780 return fmt::format(
781 "{:.3f} {:.3f} {:.3f} {:.3f}", value.r, value.g, value.b, value.a);
782 });
783 }
786 get_min_width(12 * SPREADSHEET_WIDTH_UNIT),
787 fontid,
788 max_sample_size,
790 [](const ColorGeometry4b value) {
791 const ColorGeometry4f color = color::decode(value);
792 return fmt::format(
793 "{:.3f} {:.3f} {:.3f} {:.3f}", color.r, color.g, color.b, color.a);
794 });
795 }
798 get_min_width(12 * SPREADSHEET_WIDTH_UNIT),
799 fontid,
800 max_sample_size,
802 [](const math::Quaternion value) {
803 return fmt::format(
804 "{:.3f} {:.3f} {:.3f} {:.3f}", value.x, value.y, value.z, value.w);
805 });
806 }
808 return UI_ICON_SIZE + 0.5f * UI_UNIT_X +
810 get_min_width(8 * SPREADSHEET_WIDTH_UNIT),
811 fontid,
812 max_sample_size,
814 [](const bke::InstanceReference &value) {
815 const StringRef name = value.name().is_empty() ? IFACE_("(Geometry)") :
816 value.name();
817 return name;
818 });
819 }
821 if (data_.type().is<std::string>()) {
823 fontid,
824 max_sample_size,
825 data_.typed<std::string>(),
826 [](const StringRef value) { return value; });
827 }
828 if (data_.type().is<MStringProperty>()) {
830 get_min_width(SPREADSHEET_WIDTH_UNIT),
831 fontid,
832 max_sample_size,
834 [](const MStringProperty &value) { return StringRef(value.s, value.s_len); });
835 }
836 break;
837 }
839 return 12 * SPREADSHEET_WIDTH_UNIT;
840 }
842 break;
843 }
844 }
845 return 2.0f * SPREADSHEET_WIDTH_UNIT;
846}
847
848float ColumnValues::fit_column_width_px(const std::optional<int64_t> &max_sample_size) const
849{
850 const float padding_px = 0.5 * SPREADSHEET_WIDTH_UNIT;
851 const float min_width_px = SPREADSHEET_WIDTH_UNIT;
852
853 const float data_width_px = this->fit_column_values_width_px(max_sample_size);
854
855 const int fontid = BLF_default();
857 const float name_width_px = BLF_width(fontid, name_.data(), name_.size());
858
859 const float width_px = std::max(min_width_px,
860 padding_px + std::max(data_width_px, name_width_px));
861 return width_px;
862}
863
864std::unique_ptr<SpreadsheetDrawer> spreadsheet_drawer_from_layout(
865 const SpreadsheetLayout &spreadsheet_layout)
866{
867 return std::make_unique<SpreadsheetLayoutDrawer>(spreadsheet_layout);
868}
869
870} // namespace blender::ed::spreadsheet
#define BUFFER_FOR_CPP_TYPE_VALUE(type, variable_name)
#define BLI_STR_FORMAT_INT64_GROUPED_SIZE
Definition BLI_string.hh:25
#define BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE
Definition BLI_string.hh:31
#define TIP_(msgid)
#define IFACE_(msgid)
Object groups, one object can be in many groups at once.
#define SPREADSHEET_WIDTH_UNIT
#define UI_SCALE_FAC
#define UI_ICON_SIZE
#define UI_DEFAULT_TEXT_POINTS
#define UI_UNIT_X
#define POINTER_AS_UINT(i)
#define POINTER_FROM_UINT(i)
bool is() const
void destruct(void *ptr) const
ChannelStorageType r
const CPPType * type() const
GPointer()=default
const void * get() const
constexpr IndexRange()=default
constexpr Span()=default
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
constexpr bool is_empty() const
Definition BLI_span.hh:264
constexpr StringRef()
StringRefNull name() const
Definition instances.cc:140
constexpr bool is_empty() const
eSpreadsheetColumnValueType type() const
float fit_column_values_width_px(const std::optional< int64_t > &max_sample_size=std::nullopt) const
float fit_column_width_px(const std::optional< int64_t > &max_sample_size=std::nullopt) const
void draw_top_row_cell(int column_index, const CellDrawParams &params) const final
void draw_int(const CellDrawParams &params, const int64_t value, const ColumnValueDisplayHint display_hint) const
void draw_byte_color(const CellDrawParams &params, const ColorGeometry4b color) const
void draw_content_cell_value(const GPointer value_ptr, const CellDrawParams &params, const ColumnValues &column) const
void draw_data_block(const CellDrawParams &params, const T *id, const std::optional< int > icon_override=std::nullopt) const
void draw_left_column_cell(int row_index, const CellDrawParams &params) const final
void draw_int_vector(const CellDrawParams &params, const Span< int > values) const
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
void draw_float4x4(const CellDrawParams &params, const float4x4 &value) const
ui::Button * draw_undrawable(const CellDrawParams &params) const
SpreadsheetLayoutDrawer(const SpreadsheetLayout &spreadsheet_layout)
#define str(s)
float length(VecOp< float, D >) RET
#define T
BLI_INLINE ColorSceneLinear4f< Alpha > decode(const ColorSceneLinearByteEncoded4b< Alpha > &color)
Definition BLI_color.hh:77
static float estimate_max_column_width(const float min_width, const int fontid, const std::optional< int64_t > max_sample_size, const VArray< T > &data, FunctionRef< std::string(const T &)> to_string)
int get_instance_reference_icon(const bke::InstanceReference &reference)
std::unique_ptr< SpreadsheetDrawer > spreadsheet_drawer_from_layout(const SpreadsheetLayout &spreadsheet_layout)
static std::string format_matrix_to_grid(const float4x4 &matrix)
const char * to_string(ShaderStage stage)
Definition mtl_shader.mm:52
QuaternionBase< float > Quaternion
MatBase< T, NumCol, NumRow > transpose(const MatBase< T, NumRow, NumCol > &mat)
T abs(const T &a)
int icon_from_idcode(int idcode)
void BLI_str_format_byte_unit(char dst[BLI_STR_FORMAT_INT64_BYTE_UNIT_SIZE], long long int bytes, bool base_10) ATTR_NONNULL(1)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
float BLF_width(int fontid, const char *str, size_t str_len, ResultBLF *r_info=nullptr) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(2)
Definition blf.cc:835
size_t BLI_str_format_int64_grouped(char dst[BLI_STR_FORMAT_INT64_GROUPED_SIZE], int64_t num) ATTR_NONNULL(1)
MatBase< float, 4, 4 > float4x4
const char * BKE_id_name(const ID &id)
void MEM_delete_void(void *vmemh)
VecBase< float, 4 > float4
unsigned int uint32_t
Definition BKE_text.h:81
void BLF_size(int fontid, float size)
Definition blf.cc:445
VecBase< int32_t, 2 > int2
VecBase< float, 2 > float2
VecBase< int32_t, 3 > int3
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
BMesh const char void * data
int BLF_default()
int ED_outliner_icon_from_id(const ID &id)
T * DEG_get_original(T *id)
VecBase< int16_t, 2 > short2
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
@ SPREADSHEET_VALUE_TYPE_INT32_3D
@ SPREADSHEET_VALUE_TYPE_QUATERNION
@ SPREADSHEET_VALUE_TYPE_STRING
@ SPREADSHEET_VALUE_TYPE_INSTANCES
@ SPREADSHEET_VALUE_TYPE_FLOAT
@ SPREADSHEET_VALUE_TYPE_FLOAT4X4
@ SPREADSHEET_VALUE_TYPE_FLOAT2
@ SPREADSHEET_VALUE_TYPE_INT32_2D
@ SPREADSHEET_VALUE_TYPE_FLOAT4
@ SPREADSHEET_VALUE_TYPE_BYTE_COLOR
@ SPREADSHEET_VALUE_TYPE_INT8
@ SPREADSHEET_VALUE_TYPE_UNKNOWN
@ SPREADSHEET_VALUE_TYPE_BOOL
@ SPREADSHEET_VALUE_TYPE_INT64
@ SPREADSHEET_VALUE_TYPE_INT32
@ SPREADSHEET_VALUE_TYPE_BUNDLE_ITEM
@ SPREADSHEET_VALUE_TYPE_FLOAT3
@ SPREADSHEET_VALUE_TYPE_COLOR
signed char int8_t
Definition BKE_text.h:76
ColorSceneLinearByteEncoded4b< eAlpha::Premultiplied > ColorGeometry4b
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
#define CELL_PADDING_X
std::variant< BundleItemSocketValue, BundleItemInternalValue > value