Welcome to mirror list, hosted at ThFree Co, Russian Federation.

spreadsheet_column_layout.cc « space_spreadsheet « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 396767e96d9c317743d82867c113b9993d2f2251 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <iomanip>
#include <sstream>

#include "spreadsheet_column_layout.hh"

#include "DNA_userdef_types.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "BLF_api.h"

namespace blender::ed::spreadsheet {

class ColumnLayoutDrawer : public SpreadsheetDrawer {
 private:
  const SpreadsheetColumnLayout &column_layout_;
  Vector<int> column_widths_;

 public:
  ColumnLayoutDrawer(const SpreadsheetColumnLayout &column_layout) : column_layout_(column_layout)
  {
    tot_columns = column_layout.columns.size();
    tot_rows = column_layout.row_indices.size();

    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 &params) const final
  {
    const StringRefNull name = column_layout_.columns[column_index]->name();
    uiBut *but = uiDefIconTextBut(params.block,
                                  UI_BTYPE_LABEL,
                                  0,
                                  ICON_NONE,
                                  name.c_str(),
                                  params.xmin,
                                  params.ymin,
                                  params.width,
                                  params.height,
                                  nullptr,
                                  0,
                                  0,
                                  0,
                                  0,
                                  nullptr);
    /* Center-align column headers. */
    UI_but_drawflag_disable(but, UI_BUT_TEXT_LEFT);
    UI_but_drawflag_disable(but, UI_BUT_TEXT_RIGHT);
  }

  void draw_left_column_cell(int row_index, const CellDrawParams &params) const final
  {
    const int real_index = column_layout_.row_indices[row_index];
    std::string index_str = std::to_string(real_index);
    uiBut *but = uiDefIconTextBut(params.block,
                                  UI_BTYPE_LABEL,
                                  0,
                                  ICON_NONE,
                                  index_str.c_str(),
                                  params.xmin,
                                  params.ymin,
                                  params.width,
                                  params.height,
                                  nullptr,
                                  0,
                                  0,
                                  0,
                                  0,
                                  nullptr);
    /* Right-align indices. */
    UI_but_drawflag_enable(but, UI_BUT_TEXT_RIGHT);
    UI_but_drawflag_disable(but, UI_BUT_TEXT_LEFT);
  }

  void draw_content_cell(int row_index, int column_index, const CellDrawParams &params) const final
  {
    const int real_index = column_layout_.row_indices[row_index];
    const SpreadsheetColumn &column = *column_layout_.columns[column_index];
    CellValue cell_value;
    column.get_value(real_index, cell_value);

    if (cell_value.value_int.has_value()) {
      const int value = *cell_value.value_int;
      const std::string value_str = std::to_string(value);
      uiDefIconTextBut(params.block,
                       UI_BTYPE_LABEL,
                       0,
                       ICON_NONE,
                       value_str.c_str(),
                       params.xmin,
                       params.ymin,
                       params.width,
                       params.height,
                       nullptr,
                       0,
                       0,
                       0,
                       0,
                       nullptr);
    }
    else if (cell_value.value_float.has_value()) {
      const float value = *cell_value.value_float;
      std::stringstream ss;
      ss << std::fixed << std::setprecision(3) << value;
      const std::string value_str = ss.str();
      uiDefIconTextBut(params.block,
                       UI_BTYPE_LABEL,
                       0,
                       ICON_NONE,
                       value_str.c_str(),
                       params.xmin,
                       params.ymin,
                       params.width,
                       params.height,
                       nullptr,
                       0,
                       0,
                       0,
                       0,
                       nullptr);
    }
    else if (cell_value.value_bool.has_value()) {
      const bool value = *cell_value.value_bool;
      const int icon = value ? ICON_CHECKBOX_HLT : ICON_CHECKBOX_DEHLT;
      uiDefIconTextBut(params.block,
                       UI_BTYPE_LABEL,
                       0,
                       icon,
                       "",
                       params.xmin,
                       params.ymin,
                       params.width,
                       params.height,
                       nullptr,
                       0,
                       0,
                       0,
                       0,
                       nullptr);
    }
    else if (cell_value.value_object.has_value()) {
      const ObjectCellValue value = *cell_value.value_object;
      uiDefIconTextBut(params.block,
                       UI_BTYPE_LABEL,
                       0,
                       ICON_OBJECT_DATA,
                       reinterpret_cast<const ID *const>(value.object)->name + 2,
                       params.xmin,
                       params.ymin,
                       params.width,
                       params.height,
                       nullptr,
                       0,
                       0,
                       0,
                       0,
                       nullptr);
    }
    else if (cell_value.value_collection.has_value()) {
      const CollectionCellValue value = *cell_value.value_collection;
      uiDefIconTextBut(params.block,
                       UI_BTYPE_LABEL,
                       0,
                       ICON_OUTLINER_COLLECTION,
                       reinterpret_cast<const ID *const>(value.collection)->name + 2,
                       params.xmin,
                       params.ymin,
                       params.width,
                       params.height,
                       nullptr,
                       0,
                       0,
                       0,
                       0,
                       nullptr);
    }
  }

  int column_width(int column_index) const final
  {
    return column_widths_[column_index];
  }
};

std::unique_ptr<SpreadsheetDrawer> spreadsheet_drawer_from_column_layout(
    const SpreadsheetColumnLayout &column_layout)
{
  return std::make_unique<ColumnLayoutDrawer>(column_layout);
}

}  // namespace blender::ed::spreadsheet