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 <MovingAverageDialog.hxx>
16 #include <scresid.hxx>
17 #include <strings.hrc>
19 ScMovingAverageDialog::ScMovingAverageDialog(
20 SfxBindings
* pSfxBindings
, SfxChildWindow
* pChildWindow
,
21 weld::Window
* pParent
, ScViewData
& rViewData
)
22 : ScStatisticsInputOutputDialog(
23 pSfxBindings
, pChildWindow
, pParent
, rViewData
,
24 u
"modules/scalc/ui/movingaveragedialog.ui"_ustr
,
25 u
"MovingAverageDialog"_ustr
)
26 , mxTrimRangeCheck(m_xBuilder
->weld_check_button(u
"trimrange-check"_ustr
))
27 , mxIntervalSpin(m_xBuilder
->weld_spin_button(u
"interval-spin"_ustr
))
31 ScMovingAverageDialog::~ScMovingAverageDialog()
35 void ScMovingAverageDialog::Close()
37 DoClose( ScMovingAverageDialogWrapper::GetChildWindowId() );
40 TranslateId
ScMovingAverageDialog::GetUndoNameId()
42 return STR_MOVING_AVERAGE_UNDO_NAME
;
45 ScRange
ScMovingAverageDialog::ApplyOutput(ScDocShell
* pDocShell
)
47 AddressWalkerWriter
output(mOutputAddress
, pDocShell
, mDocument
,
48 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH
, mAddressDetails
.eConv
));
49 FormulaTemplate
aTemplate(&mDocument
);
51 if (mxTrimRangeCheck
->get_active())
52 mDocument
.GetDataAreaSubrange(mInputRange
);
54 std::unique_ptr
<DataRangeIterator
> pIterator
;
55 if (mGroupedBy
== BY_COLUMN
)
56 pIterator
.reset(new DataRangeByColumnIterator(mInputRange
));
58 pIterator
.reset(new DataRangeByRowIterator(mInputRange
));
60 sal_Int32 aIntervalSize
= mxIntervalSpin
->get_value();
61 const bool aCentral
= true; //to-do add support to change this to the dialog
63 for( ; pIterator
->hasNext(); pIterator
->next() )
68 if (mGroupedBy
== BY_COLUMN
)
69 aTemplate
.setTemplate(ScResId(STR_COLUMN_LABEL_TEMPLATE
));
71 aTemplate
.setTemplate(ScResId(STR_ROW_LABEL_TEMPLATE
));
73 aTemplate
.applyNumber(u
"%NUMBER%", pIterator
->index() + 1);
74 output
.writeBoldString(aTemplate
.getTemplate());
77 DataCellIterator aDataCellIterator
= pIterator
->iterateCells();
78 std::vector
<OUString
> aFormulas
;
80 for (; aDataCellIterator
.hasNext(); aDataCellIterator
.next())
82 ScAddress aIntervalStart
;
83 ScAddress aIntervalEnd
;
87 sal_Int32 aHalf
= aIntervalSize
/ 2;
88 sal_Int32 aHalfRemainder
= aIntervalSize
% 2;
89 aIntervalStart
= aDataCellIterator
.getRelative(-aHalf
);
90 aIntervalEnd
= aDataCellIterator
.getRelative(aHalf
- 1 + aHalfRemainder
);
94 aIntervalStart
= aDataCellIterator
.getRelative(-aIntervalSize
);
95 aIntervalEnd
= aDataCellIterator
.getRelative(0);
98 if(aIntervalStart
.IsValid() && aIntervalEnd
.IsValid())
100 aTemplate
.setTemplate("=AVERAGE(%RANGE%)");
101 aTemplate
.applyRange(u
"%RANGE%", ScRange(aIntervalStart
, aIntervalEnd
));
102 aFormulas
.push_back(aTemplate
.getTemplate());
106 aFormulas
.push_back(u
"=#N/A"_ustr
);
110 output
.writeFormulas(aFormulas
);
113 return ScRange(output
.mMinimumAddress
, output
.mMaximumAddress
);
116 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */