15#include <document.hxx>
25 "modules/scalc/ui/samplingdialog.ui",
"SamplingDialog")
26 , mpActiveEdit(nullptr)
27 , mViewData(rViewData)
28 , mDocument(rViewData.GetDocument())
29 , mInputRange(
ScAddress::INITIALIZE_INVALID)
30 , mAddressDetails(mDocument.GetAddressConvention(), 0, 0)
31 , mOutputAddress(
ScAddress::INITIALIZE_INVALID)
32 , mCurrentAddress(rViewData.GetCurX(), rViewData.GetCurY(), rViewData.GetTabNo())
33 , mnLastSampleSizeValue(1)
34 , mnLastPeriodValue(1)
35 , mDialogLostFocus(false)
36 , mxInputRangeLabel(m_xBuilder->weld_label(
"input-range-label"))
37 , mxInputRangeEdit(new
formula::RefEdit(m_xBuilder->weld_entry(
"input-range-edit")))
38 , mxInputRangeButton(new
formula::RefButton(m_xBuilder->weld_button(
"input-range-button")))
39 , mxOutputRangeLabel(m_xBuilder->weld_label(
"output-range-label"))
40 , mxOutputRangeEdit(new
formula::RefEdit(m_xBuilder->weld_entry(
"output-range-edit")))
41 , mxOutputRangeButton(new
formula::RefButton(m_xBuilder->weld_button(
"output-range-button")))
42 , mxSampleSize(m_xBuilder->weld_spin_button(
"sample-size-spin"))
43 , mxPeriod(m_xBuilder->weld_spin_button(
"period-spin"))
44 , mxRandomMethodRadio(m_xBuilder->weld_radio_button(
"random-method-radio"))
45 , mxWithReplacement(m_xBuilder->weld_check_button(
"with-replacement"))
46 , mxKeepOrder(m_xBuilder->weld_check_button(
"keep-order"))
47 , mxPeriodicMethodRadio(m_xBuilder->weld_radio_button(
"periodic-method-radio"))
48 , mxButtonOk(m_xBuilder->weld_button(
"ok"))
49 , mxButtonCancel(m_xBuilder->weld_button(
"cancel"))
137 if ( rReferenceRange.
aStart != rReferenceRange.
aEnd )
140 OUString aReferenceString;
161 sal_Int64 aSelectedSampleSize = rReferenceRange.
aEnd.
Row() - rReferenceRange.
aStart.
Row() + 1;
162 if (aSelectedSampleSize > 1)
181 sal_Int64 aPeriod =
mxPeriod->get_value();
183 for (
SCROW inTab = aStart.
Tab(); inTab <= aEnd.
Tab(); inTab++)
186 for (
SCCOL inCol = aStart.
Col(); inCol <= aEnd.
Col(); inCol++)
190 for (
SCROW inRow = aStart.
Row(); inRow <= aEnd.
Row(); inRow++)
192 assert(aPeriod &&
"div-by-zero");
193 if (
i % aPeriod == aPeriod - 1 )
217 const sal_Int64 nSampleSize =
mxSampleSize->get_value();
221 const sal_Int64 nPopulationSize = aEnd.
Row() - aStart.
Row() + 1;
227 assert( bWithReplacement || nSampleSize <= nPopulationSize);
228 if (!bWithReplacement && nSampleSize > nPopulationSize)
232 for (
SCROW inTab = aStart.
Tab(); inTab <= aEnd.
Tab(); inTab++)
235 for (
SCCOL inCol = aStart.
Col(); inCol <= aEnd.
Col(); inCol++)
238 std::vector<bool> vUsed( nPopulationSize,
false);
245 if (!bWithReplacement)
247 nRandom -= aStart.
Row();
254 SCROW nBack = nRandom;
255 SCROW nForw = nRandom;
258 if (nForw < nPopulationSize - 1 && !vUsed[++nForw])
263 if (nBack > 0 && !vUsed[--nBack])
271 vUsed[nRandom] =
true;
272 nRandom += aStart.
Row();
299 for (
SCROW inTab = aStart.
Tab(); inTab <= aEnd.
Tab(); inTab++)
302 for (
SCCOL inCol = aStart.
Col(); inCol <= aEnd.
Col(); inCol++)
304 SCROW aPopulationSize = (aEnd.
Row() - aStart.
Row()) + 1;
307 inRow = aStart.
Row();
313 if ( (aPopulationSize - (inRow - aStart.
Row())) * aRandomValue >= aSampleSize - (outRow -
mOutputAddress.
Row()) )
335 OUString aUndo(
ScResId(STR_SAMPLING_UNDO_NAME));
370 if (nPopulationSize <= mnLastSampleSizeValue && !mxWithReplacement->get_active())
373 mxPeriod->set_value( nPopulationSize);
378 if (!mxWithReplacement->get_active())
381 const sal_Int64 nPopulationSize = GetPopulationSize();
382 if (mxSampleSize->get_value() > nPopulationSize)
383 mxSampleSize->set_value(nPopulationSize);
385 mnLastSampleSizeValue = mxSampleSize->get_value();
391 const sal_Int64 nPopulationSize = GetPopulationSize();
392 if (mxPeriod->get_value() > nPopulationSize)
393 mxPeriod->set_value(nPopulationSize);
394 mnLastPeriodValue = mxPeriod->get_value();
399 if (&rCtrl == mxInputRangeEdit.get())
400 mpActiveEdit = mxInputRangeEdit.get();
401 else if (&rCtrl == mxOutputRangeEdit.get())
402 mpActiveEdit = mxOutputRangeEdit.get();
404 mpActiveEdit =
nullptr;
407 mpActiveEdit->SelectAll();
412 if (&rCtrl == mxInputRangeButton.get())
413 mpActiveEdit = mxInputRangeEdit.get();
414 else if (&rCtrl == mxOutputRangeButton.get())
415 mpActiveEdit = mxOutputRangeEdit.get();
417 mpActiveEdit =
nullptr;
420 mpActiveEdit->SelectAll();
426 if (&rButton == mxButtonOk.get())
437 mDialogLostFocus = !
m_xDialog->has_toplevel_focus();
442 mDialogLostFocus = !
m_xDialog->has_toplevel_focus();
447 ToggleSamplingMethod();
478 if (&rBtn == mxWithReplacement.get())
480 if (mxWithReplacement->get_active())
483 mxKeepOrder->set_active(
false);
488 SamplingSizeValueModified(*mxSampleSize);
491 else if (&rBtn == mxKeepOrder.get())
493 if (mxKeepOrder->get_active())
496 mxWithReplacement->set_active(
false);
497 SamplingSizeValueModified(*mxSampleSize);
506 if ( mpActiveEdit == mxInputRangeEdit.get() )
509 bool bValid = ParseWithNames( aRangeList, mxInputRangeEdit->GetText(), mDocument);
510 const ScRange* pRange = (bValid && aRangeList.
size() == 1) ? &aRangeList[0] :
nullptr;
513 mInputRange = *pRange;
515 mxInputRangeEdit->StartUpdateData();
517 LimitSampleSizeAndPeriod();
524 else if ( mpActiveEdit == mxOutputRangeEdit.get() )
527 bool bValid = ParseWithNames( aRangeList, mxOutputRangeEdit->GetText(), mDocument);
528 const ScRange* pRange = (bValid && aRangeList.
size() == 1) ? &aRangeList[0] :
nullptr;
531 mOutputAddress = pRange->
aStart;
536 ScRefFlags nFormat = ( mOutputAddress.Tab() == mCurrentAddress.Tab() ) ?
539 OUString aReferenceString = mOutputAddress.Format(nFormat, &mDocument, mDocument.GetAddressConvention());
540 mxOutputRangeEdit->SetRefString( aReferenceString );
544 sal_Int64 aSelectedSampleSize = pRange->
aEnd.
Row() - pRange->
aStart.
Row() + 1;
545 if (aSelectedSampleSize > 1)
546 mxSampleSize->set_value(aSelectedSampleSize);
547 SamplingSizeValueModified(*mxSampleSize);
550 mxOutputRangeEdit->StartUpdateData();
560 mxButtonOk->set_sensitive(mInputRange.IsValid() && mOutputAddress.IsValid());
IMPL_LINK_NOARG(ScSamplingDialog, SamplingSizeValueModified, weld::SpinButton &, void)
IMPL_LINK(ScSamplingDialog, GetEditFocusHandler, formula::RefEdit &, rCtrl, void)
Reference< XExecutableDialog > m_xDialog
static sal_uInt16 GetChildWindowId()
SC_DLLPUBLIC void Format(OStringBuffer &r, ScRefFlags nFlags, const ScDocument *pDocument=nullptr, const Details &rDetails=detailsOOOa1) const
bool SetValueCell(const ScAddress &rPos, double fVal, bool bInteraction)
void PostPaint(SCCOL nStartCol, SCROW nStartRow, SCTAB nStartTab, SCCOL nEndCol, SCROW nEndRow, SCTAB nEndTab, PaintPartFlags nPart, sal_uInt16 nExtFlags=0)
virtual SfxUndoManager * GetUndoManager() override
SC_DLLPUBLIC formula::FormulaGrammar::AddressConvention GetAddressConvention() const
SC_DLLPUBLIC double GetValue(const ScAddress &rPos) const
OUString Format(const ScDocument &rDocument, ScRefFlags nFlags=ScRefFlags::ZERO, const ScAddress::Details &rDetails=ScAddress::detailsOOOa1, bool bFullAddressNotation=false) const
Returns string with formatted cell range from aStart to aEnd, according to provided address conventio...
virtual void RefInputStart(formula::RefEdit *pEdit, formula::RefButton *pButton=nullptr) override
virtual void RefInputDone(bool bForced=false) override
bool DoClose(sal_uInt16 nId)
sal_Int64 GetPopulationSize() const
ScAddress mCurrentAddress
formula::RefEdit * mpActiveEdit
std::unique_ptr< weld::Button > mxButtonCancel
std::unique_ptr< weld::RadioButton > mxPeriodicMethodRadio
virtual void SetActive() override
sal_Int64 mnLastPeriodValue
std::unique_ptr< formula::RefEdit > mxInputRangeEdit
std::unique_ptr< weld::SpinButton > mxSampleSize
virtual void Close() override
const ScDocument & mDocument
void GetRangeFromSelection()
std::unique_ptr< weld::SpinButton > mxPeriod
void ToggleSamplingMethod()
virtual void SetReference(const ScRange &rRef, ScDocument &rDoc) override
ScSamplingDialog(SfxBindings *pB, SfxChildWindow *pCW, weld::Window *pParent, ScViewData &rViewData)
virtual ~ScSamplingDialog() override
std::unique_ptr< weld::RadioButton > mxRandomMethodRadio
ScAddress::Details mAddressDetails
std::unique_ptr< weld::CheckButton > mxWithReplacement
ScRange PerformRandomSampling(ScDocShell *pDocShell)
std::unique_ptr< weld::Button > mxButtonOk
std::unique_ptr< formula::RefButton > mxOutputRangeButton
void LimitSampleSizeAndPeriod()
std::unique_ptr< weld::CheckButton > mxKeepOrder
ScRange PerformPeriodicSampling(ScDocShell *pDocShell)
std::unique_ptr< weld::Label > mxInputRangeLabel
ScRange PerformRandomSamplingKeepOrder(ScDocShell *pDocShell)
std::unique_ptr< formula::RefEdit > mxOutputRangeEdit
std::unique_ptr< weld::Label > mxOutputRangeLabel
std::unique_ptr< formula::RefButton > mxInputRangeButton
ScDocShell * GetDocShell() const
ScTabViewShell * GetViewShell() const
ScMarkType GetSimpleArea(SCCOL &rStartCol, SCROW &rStartRow, SCTAB &rStartTab, SCCOL &rEndCol, SCROW &rEndRow, SCTAB &rEndTab) const
virtual void EnterListAction(const OUString &rComment, const OUString &rRepeatComment, sal_uInt16 nId, ViewShellId nViewShellId)
ViewShellId GetViewShellId() const override
#define LINK(Instance, Class, Member)
double uniform_real_distribution(double a=0.0, double b=1.0)
int uniform_int_distribution(int a, int b)
OUString ScResId(TranslateId aId)