* @brief Exports a table's registers to a CSV file chosen by the user, with the * register permission written as read-write/read-only to match the editor UI. */
| 5692 | * register permission written as read-write/read-only to match the editor UI. |
| 5693 | */ |
| 5694 | void DataModel::ProjectModel::exportTableToCsv(const QString& tableName) |
| 5695 | { |
| 5696 | const int idx = findTableIndexByPath(tableName); |
| 5697 | if (idx < 0) |
| 5698 | return; |
| 5699 | |
| 5700 | const auto it = m_tables.begin() + idx; |
| 5701 | |
| 5702 | const auto path = QFileDialog::getSaveFileName( |
| 5703 | nullptr, |
| 5704 | tr("Export Table"), |
| 5705 | QStringLiteral("%1/%2.csv").arg(Misc::WorkspaceManager::instance().path("CSV"), it->name), |
| 5706 | tr("CSV files (*.csv)")); |
| 5707 | |
| 5708 | if (path.isEmpty()) |
| 5709 | return; |
| 5710 | |
| 5711 | QFile file(path); |
| 5712 | if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) |
| 5713 | return; |
| 5714 | |
| 5715 | QTextStream out(&file); |
| 5716 | out << "name,permissions,value\n"; |
| 5717 | for (const auto& reg : it->registers) { |
| 5718 | const auto permissions = (reg.type == DataModel::RegisterType::Computed) |
| 5719 | ? QStringLiteral("read-write") |
| 5720 | : QStringLiteral("read-only"); |
| 5721 | |
| 5722 | auto val = reg.defaultValue.toString(); |
| 5723 | if (val.contains(',') || val.contains('"') || val.contains('\n')) |
| 5724 | val = QStringLiteral("\"%1\"").arg(val.replace('"', "\"\"")); |
| 5725 | |
| 5726 | out << reg.name << ',' << permissions << ',' << val << '\n'; |
| 5727 | } |
| 5728 | |
| 5729 | file.close(); |
| 5730 | } |
| 5731 | |
| 5732 | /** |
| 5733 | * @brief Imports registers from a CSV file into an existing table, accepting both the |