| 2878 | } |
| 2879 | |
| 2880 | void SpreadsheetView::normalizeSelectedColumns(QAction* action) { |
| 2881 | auto columns = selectedColumns(); |
| 2882 | if (columns.isEmpty()) |
| 2883 | return; |
| 2884 | |
| 2885 | auto method = static_cast<NormalizationMethod>(action->data().toInt()); |
| 2886 | |
| 2887 | double rescaleIntervalMin = 0.0; |
| 2888 | double rescaleIntervalMax = 0.0; |
| 2889 | if (method == Rescale) { |
| 2890 | auto* dlg = new RescaleDialog(this); |
| 2891 | dlg->setColumns(columns); |
| 2892 | int rc = dlg->exec(); |
| 2893 | if (rc != QDialog::Accepted) |
| 2894 | return; |
| 2895 | |
| 2896 | rescaleIntervalMin = dlg->min(); |
| 2897 | rescaleIntervalMax = dlg->max(); |
| 2898 | delete dlg; |
| 2899 | } |
| 2900 | |
| 2901 | WAIT_CURSOR; |
| 2902 | QStringList messages; |
| 2903 | auto* message = "Normalization of the column <i>%1</i> was not possible because of %2."; |
| 2904 | m_spreadsheet->beginMacro(i18n("%1: normalize columns", m_spreadsheet->name())); |
| 2905 | |
| 2906 | for (auto* col : columns) { |
| 2907 | if (col->columnMode() != AbstractColumn::ColumnMode::Double && col->columnMode() != AbstractColumn::ColumnMode::Integer |
| 2908 | && col->columnMode() != AbstractColumn::ColumnMode::BigInt) |
| 2909 | continue; |
| 2910 | |
| 2911 | if (col->columnMode() == AbstractColumn::ColumnMode::Integer || col->columnMode() == AbstractColumn::ColumnMode::BigInt) |
| 2912 | col->setColumnMode(AbstractColumn::ColumnMode::Double); |
| 2913 | |
| 2914 | auto* data = static_cast<QVector<double>*>(col->data()); |
| 2915 | QVector<double> new_data(col->rowCount()); |
| 2916 | |
| 2917 | switch (method) { |
| 2918 | case DivideBySum: { |
| 2919 | double sum = std::accumulate(data->begin(), data->end(), 0); |
| 2920 | if (sum != 0.0) { |
| 2921 | for (int i = 0; i < col->rowCount(); ++i) |
| 2922 | new_data[i] = data->operator[](i) / sum; |
| 2923 | } else { |
| 2924 | messages << i18n(message, col->name(), i18n("Sum = 0")); |
| 2925 | continue; |
| 2926 | } |
| 2927 | break; |
| 2928 | } |
| 2929 | case DivideByMin: { |
| 2930 | double min = col->minimum(); |
| 2931 | if (min != 0.0) { |
| 2932 | for (int i = 0; i < col->rowCount(); ++i) |
| 2933 | new_data[i] = data->operator[](i) / min; |
| 2934 | } else { |
| 2935 | messages << i18n(message, col->name(), i18n("Min = 0")); |
| 2936 | continue; |
| 2937 | } |
nothing calls this directly
no test coverage detected