MCPcopy Create free account
hub / github.com/KDE/labplot / sampleValues

Method sampleValues

src/frontend/spreadsheet/SampleValuesDialog.cpp:176–233  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

174};
175
176void 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}

Callers

nothing calls this directly

Calls 15

beginMacroMethod · 0.80
parentAspectMethod · 0.80
endMacroMethod · 0.80
currentIndexMethod · 0.45
valueMethod · 0.45
rowCountMethod · 0.45
nameMethod · 0.45
setColumnCountMethod · 0.45
countMethod · 0.45
setRowCountMethod · 0.45
setNameMethod · 0.45
setColumnModeMethod · 0.45

Tested by

no test coverage detected