1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 #include <reffact.hxx>
14 #include <TableFillingAndNavigationTools.hxx>
15 #include <ExponentialSmoothingDialog.hxx>
16 #include <scresid.hxx>
17 #include <strings.hrc>
19 ScExponentialSmoothingDialog::ScExponentialSmoothingDialog(
20 SfxBindings
* pSfxBindings
, SfxChildWindow
* pChildWindow
,
21 weld::Window
* pParent
, ScViewData
& rViewData
)
22 : ScStatisticsInputOutputDialog(
23 pSfxBindings
, pChildWindow
, pParent
, rViewData
,
24 u
"modules/scalc/ui/exponentialsmoothingdialog.ui"_ustr
,
25 u
"ExponentialSmoothingDialog"_ustr
)
26 , mxSmoothingFactor(m_xBuilder
->weld_spin_button(u
"smoothing-factor-spin"_ustr
))
30 ScExponentialSmoothingDialog::~ScExponentialSmoothingDialog()
34 void ScExponentialSmoothingDialog::Close()
36 DoClose( ScExponentialSmoothingDialogWrapper::GetChildWindowId() );
39 TranslateId
ScExponentialSmoothingDialog::GetUndoNameId()
41 return STR_EXPONENTIAL_SMOOTHING_UNDO_NAME
;
44 ScRange
ScExponentialSmoothingDialog::ApplyOutput(ScDocShell
* pDocShell
)
46 AddressWalkerWriter
output(mOutputAddress
, pDocShell
, mDocument
,
47 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH
, mAddressDetails
.eConv
));
48 FormulaTemplate
aTemplate(&mDocument
);
51 double aSmoothingFactor
= mxSmoothingFactor
->get_value() / 100.0;
54 output
.writeBoldString(ScResId(STR_LABEL_ALPHA
));
58 ScAddress aSmoothingFactorAddress
= output
.current();
59 output
.writeValue(aSmoothingFactor
);
62 // Exponential Smoothing
65 std::unique_ptr
<DataRangeIterator
> pIterator
;
66 if (mGroupedBy
== BY_COLUMN
)
67 pIterator
.reset(new DataRangeByColumnIterator(mInputRange
));
69 pIterator
.reset(new DataRangeByRowIterator(mInputRange
));
71 for( ; pIterator
->hasNext(); pIterator
->next() )
75 ScRange aCurrentRange
= pIterator
->get();
78 if (mGroupedBy
== BY_COLUMN
)
79 aTemplate
.setTemplate(ScResId(STR_COLUMN_LABEL_TEMPLATE
));
81 aTemplate
.setTemplate(ScResId(STR_ROW_LABEL_TEMPLATE
));
82 aTemplate
.applyNumber(u
"%NUMBER%", pIterator
->index() + 1);
83 output
.writeBoldString(aTemplate
.getTemplate());
89 aTemplate
.setTemplate("=AVERAGE(%RANGE%)");
90 aTemplate
.applyRange(u
"%RANGE%", aCurrentRange
);
91 output
.writeFormula(aTemplate
.getTemplate());
95 aTemplate
.setTemplate("=%VAR%");
96 aTemplate
.applyAddress(u
"%VAR%", aCurrentRange
.aStart
);
97 output
.writeFormula(aTemplate
.getTemplate());
102 DataCellIterator aDataCellIterator
= pIterator
->iterateCells();
104 for (; aDataCellIterator
.hasNext(); aDataCellIterator
.next())
106 aTemplate
.setTemplate("=%VALUE% * %PREVIOUS_INPUT% + (1 - %VALUE%) * %PREVIOUS_OUTPUT%");
107 aTemplate
.applyAddress(u
"%PREVIOUS_INPUT%", aDataCellIterator
.get());
108 aTemplate
.applyAddress(u
"%PREVIOUS_OUTPUT%", output
.current(0, -1));
109 aTemplate
.applyAddress(u
"%VALUE%", aSmoothingFactorAddress
);
111 output
.writeFormula(aTemplate
.getTemplate());
117 return ScRange (output
.mMinimumAddress
, output
.mMaximumAddress
);
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */