Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/space_spreadsheet/spreadsheet_layout.cc
- This file was moved from source/blender/editors/space_spreadsheet/spreadsheet_column_layout.cc.
| Show All 11 Lines | |||||
| * You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
| * along with this program; if not, write to the Free Software Foundation, | * along with this program; if not, write to the Free Software Foundation, | ||||
| * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
| */ | */ | ||||
| #include <iomanip> | #include <iomanip> | ||||
| #include <sstream> | #include <sstream> | ||||
| #include "spreadsheet_column_layout.hh" | #include "spreadsheet_layout.hh" | ||||
| #include "DNA_userdef_types.h" | #include "DNA_userdef_types.h" | ||||
| #include "UI_interface.h" | #include "UI_interface.h" | ||||
| #include "UI_resources.h" | #include "UI_resources.h" | ||||
| #include "BLF_api.h" | #include "BLF_api.h" | ||||
| namespace blender::ed::spreadsheet { | namespace blender::ed::spreadsheet { | ||||
| class ColumnLayoutDrawer : public SpreadsheetDrawer { | class SpreadsheetLayoutDrawer : public SpreadsheetDrawer { | ||||
| private: | private: | ||||
| const SpreadsheetColumnLayout &column_layout_; | const SpreadsheetLayout &spreadsheet_layout_; | ||||
| Vector<int> column_widths_; | |||||
| public: | public: | ||||
| ColumnLayoutDrawer(const SpreadsheetColumnLayout &column_layout) : column_layout_(column_layout) | SpreadsheetLayoutDrawer(const SpreadsheetLayout &spreadsheet_layout) | ||||
| : spreadsheet_layout_(spreadsheet_layout) | |||||
| { | { | ||||
| tot_columns = column_layout.columns.size(); | tot_columns = spreadsheet_layout.columns.size(); | ||||
| tot_rows = column_layout.row_indices.size(); | tot_rows = spreadsheet_layout.row_indices.size(); | ||||
| left_column_width = spreadsheet_layout.index_column_width; | |||||
| const int fontid = UI_style_get()->widget.uifont_id; | |||||
| /* Use a consistent font size for the width calculation. */ | |||||
| BLF_size(fontid, UI_style_get_dpi()->widget.points * U.pixelsize, U.dpi); | |||||
| /* The width of the index column depends on the maximum row index. */ | |||||
| left_column_width = std::to_string(std::max(0, column_layout_.tot_rows - 1)).size() * | |||||
| BLF_width(fontid, "0", 1) + | |||||
| UI_UNIT_X * 0.75; | |||||
| /* The column widths depend on the column name widths. */ | |||||
| const int minimum_column_width = 3 * UI_UNIT_X; | |||||
| const int header_name_padding = UI_UNIT_X; | |||||
| for (const SpreadsheetColumn *column : column_layout_.columns) { | |||||
| if (column->default_width == 0.0f) { | |||||
| StringRefNull name = column->name(); | |||||
| const int name_width = BLF_width(fontid, name.data(), name.size()); | |||||
| const int width = std::max(name_width + header_name_padding, minimum_column_width); | |||||
| column_widths_.append(width); | |||||
| } | |||||
| else { | |||||
| column_widths_.append(column->default_width * UI_UNIT_X); | |||||
| } | |||||
| } | |||||
| } | } | ||||
| void draw_top_row_cell(int column_index, const CellDrawParams ¶ms) const final | void draw_top_row_cell(int column_index, const CellDrawParams ¶ms) const final | ||||
| { | { | ||||
| const StringRefNull name = column_layout_.columns[column_index]->name(); | const StringRefNull name = spreadsheet_layout_.columns[column_index].values->name(); | ||||
| uiBut *but = uiDefIconTextBut(params.block, | uiBut *but = uiDefIconTextBut(params.block, | ||||
| UI_BTYPE_LABEL, | UI_BTYPE_LABEL, | ||||
| 0, | 0, | ||||
| ICON_NONE, | ICON_NONE, | ||||
| name.c_str(), | name.c_str(), | ||||
| params.xmin, | params.xmin, | ||||
| params.ymin, | params.ymin, | ||||
| params.width, | params.width, | ||||
| params.height, | params.height, | ||||
| nullptr, | nullptr, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| nullptr); | nullptr); | ||||
| /* Center-align column headers. */ | /* Center-align column headers. */ | ||||
| UI_but_drawflag_disable(but, UI_BUT_TEXT_LEFT); | UI_but_drawflag_disable(but, UI_BUT_TEXT_LEFT); | ||||
| UI_but_drawflag_disable(but, UI_BUT_TEXT_RIGHT); | UI_but_drawflag_disable(but, UI_BUT_TEXT_RIGHT); | ||||
| } | } | ||||
| void draw_left_column_cell(int row_index, const CellDrawParams ¶ms) const final | void draw_left_column_cell(int row_index, const CellDrawParams ¶ms) const final | ||||
| { | { | ||||
| const int real_index = column_layout_.row_indices[row_index]; | const int real_index = spreadsheet_layout_.row_indices[row_index]; | ||||
| std::string index_str = std::to_string(real_index); | std::string index_str = std::to_string(real_index); | ||||
| uiBut *but = uiDefIconTextBut(params.block, | uiBut *but = uiDefIconTextBut(params.block, | ||||
| UI_BTYPE_LABEL, | UI_BTYPE_LABEL, | ||||
| 0, | 0, | ||||
| ICON_NONE, | ICON_NONE, | ||||
| index_str.c_str(), | index_str.c_str(), | ||||
| params.xmin, | params.xmin, | ||||
| params.ymin, | params.ymin, | ||||
| params.width, | params.width, | ||||
| params.height, | params.height, | ||||
| nullptr, | nullptr, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| nullptr); | nullptr); | ||||
| /* Right-align indices. */ | /* Right-align indices. */ | ||||
| UI_but_drawflag_enable(but, UI_BUT_TEXT_RIGHT); | UI_but_drawflag_enable(but, UI_BUT_TEXT_RIGHT); | ||||
| UI_but_drawflag_disable(but, UI_BUT_TEXT_LEFT); | UI_but_drawflag_disable(but, UI_BUT_TEXT_LEFT); | ||||
| } | } | ||||
| void draw_content_cell(int row_index, int column_index, const CellDrawParams ¶ms) const final | void draw_content_cell(int row_index, int column_index, const CellDrawParams ¶ms) const final | ||||
| { | { | ||||
| const int real_index = column_layout_.row_indices[row_index]; | const int real_index = spreadsheet_layout_.row_indices[row_index]; | ||||
| const SpreadsheetColumn &column = *column_layout_.columns[column_index]; | const ColumnValues &column = *spreadsheet_layout_.columns[column_index].values; | ||||
| CellValue cell_value; | CellValue cell_value; | ||||
| column.get_value(real_index, cell_value); | column.get_value(real_index, cell_value); | ||||
| if (cell_value.value_int.has_value()) { | if (cell_value.value_int.has_value()) { | ||||
| const int value = *cell_value.value_int; | const int value = *cell_value.value_int; | ||||
| const std::string value_str = std::to_string(value); | const std::string value_str = std::to_string(value); | ||||
| uiBut *but = uiDefIconTextBut(params.block, | uiBut *but = uiDefIconTextBut(params.block, | ||||
| UI_BTYPE_LABEL, | UI_BTYPE_LABEL, | ||||
| ▲ Show 20 Lines • Show All 93 Lines • ▼ Show 20 Lines | else if (cell_value.value_collection.has_value()) { | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| nullptr); | nullptr); | ||||
| } | } | ||||
| } | } | ||||
| int column_width(int column_index) const final | int column_width(int column_index) const final | ||||
| { | { | ||||
| return column_widths_[column_index]; | return spreadsheet_layout_.columns[column_index].width; | ||||
| } | } | ||||
| }; | }; | ||||
| std::unique_ptr<SpreadsheetDrawer> spreadsheet_drawer_from_column_layout( | std::unique_ptr<SpreadsheetDrawer> spreadsheet_drawer_from_layout( | ||||
| const SpreadsheetColumnLayout &column_layout) | const SpreadsheetLayout &spreadsheet_layout) | ||||
| { | { | ||||
| return std::make_unique<ColumnLayoutDrawer>(column_layout); | return std::make_unique<SpreadsheetLayoutDrawer>(spreadsheet_layout); | ||||
| } | } | ||||
| } // namespace blender::ed::spreadsheet | } // namespace blender::ed::spreadsheet | ||||