Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / StatisticsDialogs / MovingAverageDialog.cxx
blob85466deb5d84037d22aa96ba279ef024d87d154e
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 "MovingAverageDialog.hxx"
30 ScMovingAverageDialog::ScMovingAverageDialog(
31 SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
32 Window* pParent, ScViewData* pViewData ) :
33 ScStatisticsInputOutputDialog(
34 pSfxBindings, pChildWindow, pParent, pViewData,
35 "MovingAverageDialog", "modules/scalc/ui/movingaveragedialog.ui" )
37 get(mpIntervalSpin, "interval-spin");
40 ScMovingAverageDialog::~ScMovingAverageDialog()
43 sal_Bool ScMovingAverageDialog::Close()
45 return DoClose( ScMovingAverageDialogWrapper::GetChildWindowId() );
48 sal_Int16 ScMovingAverageDialog::GetUndoNameId()
50 return STR_MOVING_AVERAGE_UNDO_NAME;
53 ScRange ScMovingAverageDialog::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 boost::scoped_ptr<DataRangeIterator> pIterator;
60 if (mGroupedBy == BY_COLUMN)
61 pIterator.reset(new DataRangeByColumnIterator(mInputRange));
62 else
63 pIterator.reset(new DataRangeByRowIterator(mInputRange));
65 sal_Int32 aIntervalSize = mpIntervalSpin->GetValue();
66 bool aCentral = true;
68 for( ; pIterator->hasNext(); pIterator->next() )
70 output.resetRow();
72 // Write label
73 if (mGroupedBy == BY_COLUMN)
74 aTemplate.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS, STR_COLUMN_LABEL_TEMPLATE));
75 else
76 aTemplate.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS, STR_ROW_LABEL_TEMPLATE));
78 aTemplate.applyNumber("%NUMBER%", pIterator->index() + 1);
79 output.writeBoldString(aTemplate.getTemplate());
80 output.nextRow();
82 DataCellIterator aDataCellIterator = pIterator->iterateCells();
84 for (; aDataCellIterator.hasNext(); aDataCellIterator.next())
86 ScAddress aIntervalStart;
87 ScAddress aIntervalEnd;
89 if (aCentral)
91 sal_Int32 aHalf = aIntervalSize / 2;
92 sal_Int32 aHalfRemainder = aIntervalSize % 2;
93 aIntervalStart = aDataCellIterator.getRelative(-aHalf);
94 aIntervalEnd = aDataCellIterator.getRelative(aHalf - 1 + aHalfRemainder);
96 else
98 aIntervalStart = aDataCellIterator.getRelative(-aIntervalSize);
99 aIntervalEnd = aDataCellIterator.getRelative(0);
102 if(aIntervalStart.IsValid() && aIntervalEnd.IsValid())
104 aTemplate.setTemplate("=AVERAGE(%RANGE%)");
105 aTemplate.applyRange("%RANGE%", ScRange(aIntervalStart, aIntervalEnd));
106 output.writeFormula(aTemplate.getTemplate());
108 else
110 output.writeFormula("=#N/A");
112 output.nextRow();
114 output.nextColumn();
116 return ScRange(output.mMinimumAddress, output.mMaximumAddress);
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */