LibreOffice Module sc (master)  1
MovingAverageDialog.cxx
Go to the documentation of this file.
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>
15 #include <MovingAverageDialog.hxx>
16 #include <scresid.hxx>
17 #include <strings.hrc>
18 
20  SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
21  weld::Window* pParent, ScViewData* pViewData )
23  pSfxBindings, pChildWindow, pParent, pViewData,
24  "modules/scalc/ui/movingaveragedialog.ui",
25  "MovingAverageDialog")
26  , mxTrimRangeCheck(m_xBuilder->weld_check_button("trimrange-check"))
27  , mxIntervalSpin(m_xBuilder->weld_spin_button("interval-spin"))
28 {
29 }
30 
32 {
33 }
34 
36 {
38 }
39 
41 {
42  return STR_MOVING_AVERAGE_UNDO_NAME;
43 }
44 
46 {
47  AddressWalkerWriter output(mOutputAddress, pDocShell, &mDocument,
49  FormulaTemplate aTemplate(&mDocument);
50 
51  if (mxTrimRangeCheck->get_active())
53 
54  std::unique_ptr<DataRangeIterator> pIterator;
55  if (mGroupedBy == BY_COLUMN)
56  pIterator.reset(new DataRangeByColumnIterator(mInputRange));
57  else
58  pIterator.reset(new DataRangeByRowIterator(mInputRange));
59 
60  sal_Int32 aIntervalSize = mxIntervalSpin->get_value();
61  const bool aCentral = true; //to-do add support to change this to the dialog
62 
63  for( ; pIterator->hasNext(); pIterator->next() )
64  {
65  output.resetRow();
66 
67  // Write label
68  if (mGroupedBy == BY_COLUMN)
69  aTemplate.setTemplate(ScResId(STR_COLUMN_LABEL_TEMPLATE));
70  else
71  aTemplate.setTemplate(ScResId(STR_ROW_LABEL_TEMPLATE));
72 
73  aTemplate.applyNumber("%NUMBER%", pIterator->index() + 1);
74  output.writeBoldString(aTemplate.getTemplate());
75  output.nextRow();
76 
77  DataCellIterator aDataCellIterator = pIterator->iterateCells();
78  std::vector<OUString> aFormulas;
79 
80  for (; aDataCellIterator.hasNext(); aDataCellIterator.next())
81  {
82  ScAddress aIntervalStart;
83  ScAddress aIntervalEnd;
84 
85  if (aCentral)
86  {
87  sal_Int32 aHalf = aIntervalSize / 2;
88  sal_Int32 aHalfRemainder = aIntervalSize % 2;
89  aIntervalStart = aDataCellIterator.getRelative(-aHalf);
90  aIntervalEnd = aDataCellIterator.getRelative(aHalf - 1 + aHalfRemainder);
91  }
92  else
93  {
94  aIntervalStart = aDataCellIterator.getRelative(-aIntervalSize);
95  aIntervalEnd = aDataCellIterator.getRelative(0);
96  }
97 
98  if(aIntervalStart.IsValid() && aIntervalEnd.IsValid())
99  {
100  aTemplate.setTemplate("=AVERAGE(%RANGE%)");
101  aTemplate.applyRange("%RANGE%", ScRange(aIntervalStart, aIntervalEnd));
102  aFormulas.push_back(aTemplate.getTemplate());
103  }
104  else
105  {
106  aFormulas.push_back("=#N/A");
107  }
108  }
109 
110  output.writeFormulas(aFormulas);
111  output.nextColumn();
112  }
113  return ScRange(output.mMinimumAddress, output.mMaximumAddress);
114 }
115 
116 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void applyNumber(const OUString &aVariable, sal_Int32 aValue)
std::unique_ptr< weld::CheckButton > mxTrimRangeCheck
ScAddress getRelative(int aDelta)
virtual ScRange ApplyOutput(ScDocShell *pDocShell) override
std::unique_ptr< weld::SpinButton > mxIntervalSpin
formula::FormulaGrammar::AddressConvention eConv
Definition: address.hxx:213
void writeBoldString(const OUString &aString)
virtual void Close() override
ScMovingAverageDialog(SfxBindings *pB, SfxChildWindow *pCW, weld::Window *pParent, ScViewData *pViewData)
static Grammar mergeToGrammar(const Grammar eGrammar, const AddressConvention eConv)
bool IsValid() const
Definition: address.hxx:293
OUString ScResId(const char *pId)
Definition: scdll.cxx:95
bool GetDataAreaSubrange(ScRange &rRange) const
Returns true if there is a non-empty subrange in the range given as input.
Definition: document.cxx:1102
void applyRange(const OUString &aVariable, const ScRange &aRange, bool b3D=true)
virtual const char * GetUndoNameId() override
void setTemplate(const OUString &aTemplate)
bool DoClose(sal_uInt16 nId)
Definition: anyrefdg.cxx:694
virtual ~ScMovingAverageDialog() override
void writeFormulas(const std::vector< OUString > &rFormulas)