| 174 | }; |
| 175 | |
| 176 | void SampleValuesDialog::sampleValues() const { |
| 177 | WAIT_CURSOR; |
| 178 | const auto method = static_cast<Method>(ui.cbMethod->currentIndex()); |
| 179 | |
| 180 | QVector<int> rows; |
| 181 | switch (method) { |
| 182 | case Method::Periodic: { |
| 183 | int period = ui.sbValue->value(); |
| 184 | int count = std::floor(m_spreadsheet->rowCount() / period); |
| 185 | for (int i = 0; i < count; ++i) |
| 186 | rows << period * (i + 1) - 1; |
| 187 | break; |
| 188 | } |
| 189 | case Method::Random: { |
| 190 | // create a generator chosen by the environment variable GSL_RNG_TYPE |
| 191 | gsl_rng_env_setup(); |
| 192 | const gsl_rng_type* T = gsl_rng_default; |
| 193 | gsl_rng* r = gsl_rng_alloc(T); |
| 194 | gsl_rng_set(r, QDateTime::currentMSecsSinceEpoch()); |
| 195 | |
| 196 | int sampleSize = ui.sbValue->value(); |
| 197 | int a = 0; |
| 198 | int b = m_spreadsheet->rowCount() - 1; |
| 199 | for (int i = 0; i < sampleSize; ++i) |
| 200 | rows << (int)round(gsl_ran_flat(r, a, b)); |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | m_spreadsheet->beginMacro(i18n("%1: sample values", m_spreadsheet->name())); |
| 206 | |
| 207 | // create target spreadsheet |
| 208 | auto* targetSpreadsheet = new Spreadsheet(i18n("Sample of %1", m_spreadsheet->name())); |
| 209 | targetSpreadsheet->setColumnCount(m_columns.count()); |
| 210 | targetSpreadsheet->setRowCount(rows.count()); |
| 211 | |
| 212 | int index = 0; |
| 213 | const auto& targetColumns = targetSpreadsheet->children<Column>(); |
| 214 | for (auto* source : m_columns) { |
| 215 | auto* target = targetColumns.at(index); |
| 216 | target->setName(source->name()); |
| 217 | target->setColumnMode(source->columnMode()); |
| 218 | ++index; |
| 219 | } |
| 220 | |
| 221 | for (int i = 0; i < m_columns.count(); ++i) { |
| 222 | auto* task = new SampleValuesTask(m_columns.at(i), targetColumns.at(i), rows); |
| 223 | QThreadPool::globalInstance()->start(task); |
| 224 | } |
| 225 | |
| 226 | // wait until all columns were processed |
| 227 | QThreadPool::globalInstance()->waitForDone(); |
| 228 | |
| 229 | m_spreadsheet->parentAspect()->addChild(targetSpreadsheet); |
| 230 | |
| 231 | m_spreadsheet->endMacro(); |
| 232 | RESET_CURSOR; |
| 233 | } |
nothing calls this directly
no test coverage detected