| 256 | } |
| 257 | |
| 258 | bool SpreadsheetModel::setData(const QModelIndex& index, const QVariant& value, int role) { |
| 259 | if (!index.isValid()) |
| 260 | return false; |
| 261 | |
| 262 | int row = index.row(); |
| 263 | auto* column = m_spreadsheet->column(index.column()); |
| 264 | |
| 265 | // DEBUG("SpreadsheetModel::setData() value = " << STDSTRING(value.toString())) |
| 266 | |
| 267 | // don't do anything if no new value was provided |
| 268 | if (column->columnMode() == AbstractColumn::ColumnMode::Double) { |
| 269 | bool ok; |
| 270 | double new_value = QLocale().toDouble(value.toString(), &ok); |
| 271 | if (ok) { |
| 272 | if (column->valueAt(row) == new_value) |
| 273 | return false; |
| 274 | } else { |
| 275 | // an empty (non-numeric value) was provided |
| 276 | if (std::isnan(column->valueAt(row))) |
| 277 | return false; |
| 278 | } |
| 279 | } else { |
| 280 | if (column->asStringColumn()->textAt(row) == value.toString()) |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | switch (role) { |
| 285 | case Qt::EditRole: |
| 286 | // remark: the validity of the cell is determined by the input filter |
| 287 | if (m_formula_mode) |
| 288 | column->setFormula(row, value.toString()); |
| 289 | else |
| 290 | column->asStringColumn()->setTextAt(row, value.toString()); |
| 291 | return true; |
| 292 | case static_cast<int>(CustomDataRole::MaskingRole): |
| 293 | m_spreadsheet->column(index.column())->setMasked(row, value.toBool()); |
| 294 | return true; |
| 295 | case static_cast<int>(CustomDataRole::FormulaRole): |
| 296 | m_spreadsheet->column(index.column())->setFormula(row, value.toString()); |
| 297 | return true; |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
| 303 | QModelIndex SpreadsheetModel::index(int row, int column, const QModelIndex& /*parent*/) const { |
| 304 | return createIndex(row, column); |
no test coverage detected