Bump version to 4.3-4
[LibreOffice.git] / sc / source / ui / StatisticsDialogs / MovingAverageDialog.cxx
blob2786e53b662627948b54b732b225dcb04873e4b7
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 "docfunc.hxx"
24 #include "StatisticsDialogs.hrc"
25 #include "TableFillingAndNavigationTools.hxx"
27 #include "MovingAverageDialog.hxx"
29 ScMovingAverageDialog::ScMovingAverageDialog(
30 SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
31 Window* pParent, ScViewData* pViewData ) :
32 ScStatisticsInputOutputDialog(
33 pSfxBindings, pChildWindow, pParent, pViewData,
34 "MovingAverageDialog", "modules/scalc/ui/movingaveragedialog.ui" )
36 get(mpIntervalSpin, "interval-spin");
39 ScMovingAverageDialog::~ScMovingAverageDialog()
42 bool ScMovingAverageDialog::Close()
44 return DoClose( ScMovingAverageDialogWrapper::GetChildWindowId() );
47 sal_Int16 ScMovingAverageDialog::GetUndoNameId()
49 return STR_MOVING_AVERAGE_UNDO_NAME;
52 ScRange ScMovingAverageDialog::ApplyOutput(ScDocShell* pDocShell)
54 AddressWalkerWriter output(mOutputAddress, pDocShell, mDocument,
55 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv));
56 FormulaTemplate aTemplate(mDocument);
58 boost::scoped_ptr<DataRangeIterator> pIterator;
59 if (mGroupedBy == BY_COLUMN)
60 pIterator.reset(new DataRangeByColumnIterator(mInputRange));
61 else
62 pIterator.reset(new DataRangeByRowIterator(mInputRange));
64 sal_Int32 aIntervalSize = mpIntervalSpin->GetValue();
65 bool aCentral = true;
67 for( ; pIterator->hasNext(); pIterator->next() )
69 output.resetRow();
71 // Write label
72 if (mGroupedBy == BY_COLUMN)
73 aTemplate.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS, STR_COLUMN_LABEL_TEMPLATE));
74 else
75 aTemplate.setTemplate(SC_STRLOAD(RID_STATISTICS_DLGS, STR_ROW_LABEL_TEMPLATE));
77 aTemplate.applyNumber("%NUMBER%", pIterator->index() + 1);
78 output.writeBoldString(aTemplate.getTemplate());
79 output.nextRow();
81 DataCellIterator aDataCellIterator = pIterator->iterateCells();
83 for (; aDataCellIterator.hasNext(); aDataCellIterator.next())
85 ScAddress aIntervalStart;
86 ScAddress aIntervalEnd;
88 if (aCentral)
90 sal_Int32 aHalf = aIntervalSize / 2;
91 sal_Int32 aHalfRemainder = aIntervalSize % 2;
92 aIntervalStart = aDataCellIterator.getRelative(-aHalf);
93 aIntervalEnd = aDataCellIterator.getRelative(aHalf - 1 + aHalfRemainder);
95 else
97 aIntervalStart = aDataCellIterator.getRelative(-aIntervalSize);
98 aIntervalEnd = aDataCellIterator.getRelative(0);
101 if(aIntervalStart.IsValid() && aIntervalEnd.IsValid())
103 aTemplate.setTemplate("=AVERAGE(%RANGE%)");
104 aTemplate.applyRange("%RANGE%", ScRange(aIntervalStart, aIntervalEnd));
105 output.writeFormula(aTemplate.getTemplate());
107 else
109 output.writeFormula("=#N/A");
111 output.nextRow();
113 output.nextColumn();
115 return ScRange(output.mMinimumAddress, output.mMaximumAddress);
118 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */