Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / StatisticsDialogs / ExponentialSmoothingDialog.cxx
blob6b20afd2ccc51a3a140a28b41b10c51ec24abd65
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 */
11 #include <sfx2/dispatch.hxx>
12 #include <svl/zforlist.hxx>
13 #include <svl/undo.hxx>
15 #include "formulacell.hxx"
16 #include "rangelst.hxx"
17 #include "scitems.hxx"
18 #include "docsh.hxx"
19 #include "document.hxx"
20 #include "uiitems.hxx"
21 #include "reffact.hxx"
22 #include "strload.hxx"
23 #include "random.hxx"
24 #include "docfunc.hxx"
25 #include "StatisticsDialogs.hrc"
26 #include "TableFillingAndNavigationTools.hxx"
28 #include "ExponentialSmoothingDialog.hxx"
30 ScExponentialSmoothingDialog::ScExponentialSmoothingDialog(
31 SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
32 Window* pParent, ScViewData* pViewData ) :
33 ScStatisticsInputOutputDialog(
34 pSfxBindings, pChildWindow, pParent, pViewData,
35 "ExponentialSmoothingDialog", "modules/scalc/ui/exponentialsmoothingdialog.ui" )
37 get(mpSmoothingFactor, "smoothing-factor-spin");
40 ScExponentialSmoothingDialog::~ScExponentialSmoothingDialog()
43 sal_Bool ScExponentialSmoothingDialog::Close()
45 return DoClose( ScExponentialSmoothingDialogWrapper::GetChildWindowId() );
48 sal_Int16 ScExponentialSmoothingDialog::GetUndoNameId()
50 return STR_EXPONENTIAL_SMOOTHING_UNDO_NAME;
53 ScRange ScExponentialSmoothingDialog::ApplyOutput(ScDocShell* pDocShell)
55 AddressWalkerWriter output(mOutputAddress, pDocShell, mDocument,
56 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
57 FormulaTemplate aTemplate(mDocument, mAddressDetails);
59 // Smoothing factor
60 double aSmoothingFactor = mpSmoothingFactor->GetValue() / 100.0;
62 // Alpha
63 output.writeBoldString(SC_STRLOAD(RID_STATISTICS_DLGS, STR_LABEL_ALPHA));
64 output.nextRow();
66 // Alpha Value
67 ScAddress aSmoothingFactorAddress = output.current();
68 output.writeValue(aSmoothingFactor);
69 output.nextRow();
71 // Exponential Smoothing
72 output.push();
74 boost::scoped_ptr<DataRangeIterator> pIterator;
75 if (mGroupedBy == BY_COLUMN)
76 pIterator.reset(new DataRangeByColumnIterator(mInputRange));
77 else
78 pIterator.reset(new DataRangeByRowIterator(mInputRange));
80 for( ; pIterator->hasNext(); pIterator->next() )
82 output.resetRow();
84 ScRange aCurrentRange = pIterator->get();
86 // Write column label
87 if (mGroupedBy == BY_COLUMN)
88 aTemplate.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS, STR_COLUMN_LABEL_TEMPLATE));
89 else
90 aTemplate.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS, STR_ROW_LABEL_TEMPLATE));
91 aTemplate.applyNumber("%NUMBER%", pIterator->index() + 1);
92 output.writeBoldString(aTemplate.getTemplate());
93 output.nextRow();
95 // Initial value
96 if (false)
98 aTemplate.setTemplate("=AVERAGE(%RANGE%)");
99 aTemplate.applyRange("%RANGE%", aCurrentRange);
100 output.writeFormula(aTemplate.getTemplate());
102 else
104 aTemplate.setTemplate("=%VAR%");
105 aTemplate.applyAddress("%VAR%", aCurrentRange.aStart);
106 output.writeFormula(aTemplate.getTemplate());
109 output.nextRow();
111 DataCellIterator aDataCellIterator = pIterator->iterateCells();
113 for (; aDataCellIterator.hasNext(); aDataCellIterator.next())
115 aTemplate.setTemplate("=%VALUE% * %PREVIOUS_INPUT% + (1 - %VALUE%) * %PREVIOUS_OUTPUT%");
116 aTemplate.applyAddress("%PREVIOUS_INPUT%", aDataCellIterator.get());
117 aTemplate.applyAddress("%PREVIOUS_OUTPUT%", output.current(0, -1));
118 aTemplate.applyAddress("%VALUE%", aSmoothingFactorAddress);
120 output.writeFormula(aTemplate.getTemplate());
121 output.nextRow();
123 output.nextColumn();
126 return ScRange (output.mMinimumAddress, output.mMaximumAddress);
129 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */