/src/libreoffice/sc/source/ui/StatisticsDialogs/MovingAverageDialog.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | */ |
10 | | |
11 | | #include <memory> |
12 | | |
13 | | #include <reffact.hxx> |
14 | | #include <TableFillingAndNavigationTools.hxx> |
15 | | #include <MovingAverageDialog.hxx> |
16 | | #include <scresid.hxx> |
17 | | #include <strings.hrc> |
18 | | |
19 | | ScMovingAverageDialog::ScMovingAverageDialog( |
20 | | SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow, |
21 | | weld::Window* pParent, ScViewData& rViewData ) |
22 | 0 | : ScStatisticsInputOutputDialog( |
23 | 0 | pSfxBindings, pChildWindow, pParent, rViewData, |
24 | 0 | u"modules/scalc/ui/movingaveragedialog.ui"_ustr, |
25 | 0 | u"MovingAverageDialog"_ustr) |
26 | 0 | , mxTrimRangeCheck(m_xBuilder->weld_check_button(u"trimrange-check"_ustr)) |
27 | 0 | , mxIntervalSpin(m_xBuilder->weld_spin_button(u"interval-spin"_ustr)) |
28 | 0 | { |
29 | 0 | } |
30 | | |
31 | | ScMovingAverageDialog::~ScMovingAverageDialog() |
32 | 0 | { |
33 | 0 | } |
34 | | |
35 | | void ScMovingAverageDialog::Close() |
36 | 0 | { |
37 | 0 | DoClose( ScMovingAverageDialogWrapper::GetChildWindowId() ); |
38 | 0 | } |
39 | | |
40 | | TranslateId ScMovingAverageDialog::GetUndoNameId() |
41 | 0 | { |
42 | 0 | return STR_MOVING_AVERAGE_UNDO_NAME; |
43 | 0 | } |
44 | | |
45 | | ScRange ScMovingAverageDialog::ApplyOutput(ScDocShell& rDocShell) |
46 | 0 | { |
47 | 0 | AddressWalkerWriter output(mOutputAddress, rDocShell, mDocument, |
48 | 0 | formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH, mAddressDetails.eConv)); |
49 | 0 | FormulaTemplate aTemplate(&mDocument); |
50 | |
|
51 | 0 | if (mxTrimRangeCheck->get_active()) |
52 | 0 | mDocument.GetDataAreaSubrange(mInputRange); |
53 | |
|
54 | 0 | std::unique_ptr<DataRangeIterator> pIterator; |
55 | 0 | if (mGroupedBy == BY_COLUMN) |
56 | 0 | pIterator.reset(new DataRangeByColumnIterator(mInputRange)); |
57 | 0 | else |
58 | 0 | pIterator.reset(new DataRangeByRowIterator(mInputRange)); |
59 | |
|
60 | 0 | sal_Int32 aIntervalSize = mxIntervalSpin->get_value(); |
61 | 0 | const bool aCentral = true; //to-do add support to change this to the dialog |
62 | |
|
63 | 0 | for( ; pIterator->hasNext(); pIterator->next() ) |
64 | 0 | { |
65 | 0 | output.resetRow(); |
66 | | |
67 | | // Write label |
68 | 0 | if (mGroupedBy == BY_COLUMN) |
69 | 0 | aTemplate.setTemplate(ScResId(STR_COLUMN_LABEL_TEMPLATE)); |
70 | 0 | else |
71 | 0 | aTemplate.setTemplate(ScResId(STR_ROW_LABEL_TEMPLATE)); |
72 | |
|
73 | 0 | aTemplate.applyNumber(u"%NUMBER%", pIterator->index() + 1); |
74 | 0 | output.writeBoldString(aTemplate.getTemplate()); |
75 | 0 | output.nextRow(); |
76 | |
|
77 | 0 | DataCellIterator aDataCellIterator = pIterator->iterateCells(); |
78 | 0 | std::vector<OUString> aFormulas; |
79 | |
|
80 | 0 | for (; aDataCellIterator.hasNext(); aDataCellIterator.next()) |
81 | 0 | { |
82 | 0 | ScAddress aIntervalStart; |
83 | 0 | ScAddress aIntervalEnd; |
84 | |
|
85 | 0 | if (aCentral) |
86 | 0 | { |
87 | 0 | sal_Int32 aHalf = aIntervalSize / 2; |
88 | 0 | sal_Int32 aHalfRemainder = aIntervalSize % 2; |
89 | 0 | aIntervalStart = aDataCellIterator.getRelative(-aHalf); |
90 | 0 | aIntervalEnd = aDataCellIterator.getRelative(aHalf - 1 + aHalfRemainder); |
91 | 0 | } |
92 | 0 | else |
93 | 0 | { |
94 | 0 | aIntervalStart = aDataCellIterator.getRelative(-aIntervalSize); |
95 | 0 | aIntervalEnd = aDataCellIterator.getRelative(0); |
96 | 0 | } |
97 | |
|
98 | 0 | if(aIntervalStart.IsValid() && aIntervalEnd.IsValid()) |
99 | 0 | { |
100 | 0 | aTemplate.setTemplate("=AVERAGE(%RANGE%)"); |
101 | 0 | aTemplate.applyRange(u"%RANGE%", ScRange(aIntervalStart, aIntervalEnd)); |
102 | 0 | aFormulas.push_back(aTemplate.getTemplate()); |
103 | 0 | } |
104 | 0 | else |
105 | 0 | { |
106 | 0 | aFormulas.push_back(u"=#N/A"_ustr); |
107 | 0 | } |
108 | 0 | } |
109 | |
|
110 | 0 | output.writeFormulas(aFormulas); |
111 | 0 | output.nextColumn(); |
112 | 0 | } |
113 | 0 | return ScRange(output.mMinimumAddress, output.mMaximumAddress); |
114 | 0 | } |
115 | | |
116 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |