LibreOffice Module reportdesign (master)  1
Formula.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  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <unotools/viewoptions.hxx>
21 #include <formula/formdata.hxx>
22 #include <formula/funcutl.hxx>
23 #include <formula/tokenarray.hxx>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 
28 #include <memory>
29 
30 #include <Formula.hxx>
31 #include <AddField.hxx>
32 #include <helpids.h>
33 
34 
35 namespace rptui
36 {
37  using namespace formula;
38  using namespace ::com::sun::star;
39 
40 
41 // initialization / shared functions for the dialog
42 
43 
45  , const uno::Reference<lang::XMultiServiceFactory>& _xServiceFactory
46  , const std::shared_ptr< IFunctionManager >& _pFunctionMgr
47  , const OUString& _sFormula
48  , const css::uno::Reference < css::beans::XPropertySet >& _xRowSet
49  , svl::SharedStringPool& rStrPool )
50  : FormulaModalDialog( pParent, _pFunctionMgr.get(),this)
51  ,m_aFunctionManager(_pFunctionMgr)
52  ,m_xFormulaData(new FormEditData())
53  ,m_xRowSet(_xRowSet)
54  ,m_pEdit(nullptr)
55  ,m_sFormula("=")
56  ,m_nStart(0)
57  ,m_nEnd(1)
58  ,mrStringPool(rStrPool)
59 {
60  if ( !_sFormula.isEmpty() )
61  {
62  if ( _sFormula[0] != '=' )
63  m_sFormula += _sFormula;
64  else
65  m_sFormula = _sFormula;
66  }
67  m_xParser.set(_xServiceFactory->createInstance("org.libreoffice.report.pentaho.SOFormulaParser"),uno::UNO_QUERY);
68  if ( m_xParser.is() )
69  m_xOpCodeMapper = m_xParser->getFormulaOpCodeMapper();
70  fill();
71 }
72 
74 {
75 }
76 
78 {
82  Update();
83 }
84 
86 {
87  if ( m_xAddField )
88  {
89  SvtViewOptions aDlgOpt( EViewType::Window, HID_RPT_FIELD_SEL_WIN );
90  aDlgOpt.SetWindowState(OStringToOUString(m_xAddField->getDialog()->get_window_state(WindowStateMask::X | WindowStateMask::Y | WindowStateMask::State | WindowStateMask::Minimized), RTL_TEXTENCODING_ASCII_US));
91 
92  if (m_xAddField->getDialog()->get_visible())
93  m_xAddField->response(RET_CANCEL);
94 
95  m_xAddField.reset();
96  }
97 
99  m_pEdit = nullptr;
100 }
101 
102 // functions for right side
103 
104 bool FormulaDialog::calculateValue( const OUString& rStrExp, OUString& rStrResult, bool /*bMatrixFormula*/ )
105 {
106  rStrResult = rStrExp;
107  return false;
108 }
109 
110 std::shared_ptr<formula::FormulaCompiler> FormulaDialog::getCompiler() const
111 {
112  return nullptr;
113 }
114 
115 std::unique_ptr<formula::FormulaCompiler> FormulaDialog::createCompiler( formula::FormulaTokenArray& rArray ) const
116 {
117  return std::unique_ptr<formula::FormulaCompiler>(new FormulaCompiler(rArray));
118 }
119 
120 void FormulaDialog::doClose(bool _bOk)
121 {
122  response(_bOk ? RET_OK : RET_CANCEL);
123 }
124 
126 {
127 }
128 void FormulaDialog::showReference(const OUString& /*_sFormula*/)
129 {
130 }
131 void FormulaDialog::dispatch(bool /*_bOK*/, bool /*_bMatrixChecked*/)
132 {
133 }
134 void FormulaDialog::setDispatcherLock( bool /*bLock*/ )
135 {
136 }
138 {
139 }
141 {
142 }
144 {
145 }
147 {
148  return m_xFormulaData.get();
149 }
150 void FormulaDialog::setCurrentFormula(const OUString& _sReplacement)
151 {
152  const sal_Int32 nOldLen = m_nEnd - m_nStart;
153  const sal_Int32 nNewLen = _sReplacement.getLength();
154  if (nOldLen)
155  m_sFormula = m_sFormula.replaceAt( m_nStart, nOldLen, "" );
156  if (nNewLen)
157  m_sFormula = m_sFormula.replaceAt( m_nStart, 0, _sReplacement );
158  m_nEnd = m_nStart + nNewLen;
159 }
160 void FormulaDialog::setSelection(sal_Int32 _nStart, sal_Int32 _nEnd)
161 {
162  if ( _nStart <= _nEnd )
163  {
164  m_nStart = _nStart;
165  m_nEnd = _nEnd;
166  }
167  else
168  {
169  m_nEnd = _nStart;
170  m_nStart = _nEnd;
171  }
172 }
173 void FormulaDialog::getSelection(sal_Int32& _nStart, sal_Int32& _nEnd) const
174 {
175  _nStart = m_nStart;
176  _nEnd = m_nEnd;
177 }
179 {
180  return m_sFormula;
181 }
183 {
184  return m_aFunctionManager.get();
185 }
186 
187 void FormulaDialog::ShowReference(const OUString& /*_sRef*/)
188 {
189 }
190 
191 void FormulaDialog::HideReference( bool /*bDoneRefMode*/)
192 {
193 }
194 
196 {
197 }
198 
200 {
201  ::std::pair<RefButton*,RefEdit*> aPair = RefInputStartBefore( _pEdit, _pButton );
202  m_pEdit = aPair.second;
203  if ( m_pEdit )
204  m_pEdit->GetWidget()->hide();
205  if ( aPair.first )
206  aPair.first->GetWidget()->hide();
207 
208  if (!m_xAddField)
209  {
210  m_xAddField = std::make_shared<OAddFieldWindow>(m_xDialog.get(), m_xRowSet);
211  m_xAddField->SetCreateHdl(LINK( this, FormulaDialog, OnClickHdl ) );
212  SvtViewOptions aDlgOpt( EViewType::Window, HID_RPT_FIELD_SEL_WIN );
213  if ( aDlgOpt.Exists() )
214  {
215  m_xAddField->getDialog()->set_window_state(OUStringToOString(aDlgOpt.GetWindowState(), RTL_TEXTENCODING_ASCII_US));
216 
217  }
218 
219  m_xAddField->Update();
220  }
222 
223  if (!m_xAddField->getDialog()->get_visible())
224  weld::DialogController::runAsync(m_xAddField, [this](sal_Int32 /*nResult*/) { m_xAddField.reset(); });
225 }
226 
227 IMPL_LINK( FormulaDialog, OnClickHdl, OAddFieldWindow& ,_rAddFieldDlg, void)
228 {
229  const uno::Sequence< beans::PropertyValue > aArgs = _rAddFieldDlg.getSelectedFieldDescriptors();
230  // we use this way to create undo actions
231  if ( m_pEdit && aArgs.getLength() == 1)
232  {
233  uno::Sequence< beans::PropertyValue > aValue;
234  aArgs[0].Value >>= aValue;
235  svx::ODataAccessDescriptor aDescriptor(aValue);
236  OUString sName;
237  aDescriptor[ svx::DataAccessDescriptorProperty::ColumnName ] >>= sName;
238  if ( !sName.isEmpty() )
239  {
240  sName = "[" + sName + "]";
241  m_pEdit->SetText(sName);
242  }
243  }
244  m_pEdit = nullptr;
245  if (_rAddFieldDlg.getDialog()->get_visible())
246  _rAddFieldDlg.response(RET_CANCEL);
247  RefInputDoneAfter();
248 }
249 
250 uno::Reference< sheet::XFormulaParser> FormulaDialog::getFormulaParser() const
251 {
252  return m_xParser;
253 }
254 
255 uno::Reference< sheet::XFormulaOpCodeMapper> FormulaDialog::getFormulaOpCodeMapper() const
256 {
257  return m_xOpCodeMapper;
258 }
259 
260 table::CellAddress FormulaDialog::getReferencePosition() const
261 {
262  return table::CellAddress();
263 }
264 
265 ::std::unique_ptr<formula::FormulaTokenArray> FormulaDialog::convertToTokenArray(const uno::Sequence< sheet::FormulaToken >& _aTokenList)
266 {
267  ::std::unique_ptr<formula::FormulaTokenArray> pArray(new FormulaTokenArray());
268  pArray->Fill(_aTokenList, mrStringPool, nullptr);
269  return pArray;
270 }
271 
272 } // rptui
273 
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::linguistic2::XProofreadingIterator > get(css::uno::Reference< css::uno::XComponentContext > const &context)
virtual void HideReference(bool bDoneRefMode=true) override
Definition: Formula.cxx:191
virtual void ReleaseFocus(formula::RefEdit *pEdit) override
Definition: Formula.cxx:195
::std::pair< RefButton *, RefEdit * > RefInputStartBefore(RefEdit *pEdit, RefButton *pButton)
virtual formula::FormEditData * getFormEditData() const override
Definition: Formula.cxx:146
std::shared_ptr< OAddFieldWindow > m_xAddField
Definition: Formula.hxx:50
virtual void hide()=0
virtual OUString getCurrentFormula() const override
Definition: Formula.cxx:178
virtual void getSelection(sal_Int32 &_nStart, sal_Int32 &_nEnd) const override
Definition: Formula.cxx:173
virtual void clear() override
Definition: Formula.cxx:140
virtual formula::IFunctionManager * getFunctionManager() override
Definition: Formula.cxx:182
virtual void setCurrentFormula(const OUString &_sReplacement) override
Definition: Formula.cxx:150
RET_CANCEL
virtual std::unique_ptr< formula::FormulaCompiler > createCompiler(formula::FormulaTokenArray &rArray) const override
Definition: Formula.cxx:115
formula::RefEdit * m_pEdit
Definition: Formula.hxx:54
svl::SharedStringPool & mrStringPool
Definition: Formula.hxx:59
static bool runAsync(const std::shared_ptr< DialogController > &rController, const std::function< void(sal_Int32)> &)
FormulaDialog(weld::Window *pParent, const css::uno::Reference< css::lang::XMultiServiceFactory > &_xServiceFactory, const std::shared_ptr< formula::IFunctionManager > &_pFunctionMgr, const OUString &_sFormula, const css::uno::Reference< css::beans::XPropertySet > &_xRowSet, svl::SharedStringPool &rStrPool)
Definition: Formula.cxx:44
virtual ::std::unique_ptr< formula::FormulaTokenArray > convertToTokenArray(const css::uno::Sequence< css::sheet::FormulaToken > &_aTokenList) override
Definition: Formula.cxx:265
void SetMeText(const OUString &_sText)
virtual void ShowReference(const OUString &_sRef) override
Definition: Formula.cxx:187
css::uno::Reference< css::sheet::XFormulaOpCodeMapper > m_xOpCodeMapper
Definition: Formula.hxx:53
virtual void fill() override
Definition: Formula.cxx:77
css::uno::Reference< css::beans::XPropertySet > m_xRowSet
Definition: Formula.hxx:51
void StoreFormEditData(FormEditData *pData)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
virtual css::uno::Reference< css::sheet::XFormulaOpCodeMapper > getFormulaOpCodeMapper() const override
Definition: Formula.cxx:255
virtual void setSelection(sal_Int32 _nStart, sal_Int32 _nEnd) override
Definition: Formula.cxx:160
const char * sName
virtual css::table::CellAddress getReferencePosition() const override
Definition: Formula.cxx:260
virtual void deleteFormData() override
Definition: Formula.cxx:137
OUString m_sFormula
Definition: Formula.hxx:55
css::uno::Reference< css::report::meta::XFormulaParser > m_xParser
Definition: Formula.hxx:52
virtual css::uno::Reference< css::sheet::XFormulaParser > getFormulaParser() const override
Definition: Formula.cxx:250
sal_Int32 m_nEnd
Definition: Formula.hxx:57
void SetWindowState(const OUString &sState)
virtual void switchBack() override
Definition: Formula.cxx:143
virtual void setDispatcherLock(bool bLock) override
Definition: Formula.cxx:134
std::unique_ptr< formula::FormEditData > m_xFormulaData
Definition: Formula.hxx:49
virtual void showReference(const OUString &_sFormula) override
Definition: Formula.cxx:128
OUString GetWindowState() const
weld::Entry * GetWidget() const
virtual std::shared_ptr< formula::FormulaCompiler > getCompiler() const override
Definition: Formula.cxx:110
virtual void doClose(bool _bOk) override
Definition: Formula.cxx:120
IMPL_LINK(OAddFieldWindow, DragBeginHdl, bool &, rUnsetDragIcon, bool)
Definition: AddField.cxx:49
virtual void ToggleCollapsed(formula::RefEdit *pEdit, formula::RefButton *pButton) override
Definition: Formula.cxx:199
#define HID_RPT_FIELD_SEL_WIN
Definition: helpids.h:36
RET_OK
virtual void notifyChange() override
Definition: Formula.cxx:73
bool Exists() const
virtual void insertEntryToLRUList(const formula::IFunctionDescription *pDesc) override
Definition: Formula.cxx:125
Reference< XExecutableDialog > m_xDialog
virtual ~FormulaDialog() override
Definition: Formula.cxx:85
sal_Int32 m_nStart
Definition: Formula.hxx:56
virtual void dispatch(bool _bOK, bool _bMatrixChecked) override
Definition: Formula.cxx:131
virtual bool calculateValue(const OUString &_sExpression, OUString &_rResult, bool bMatrixFormula) override
Definition: Formula.cxx:104
std::shared_ptr< formula::IFunctionManager > m_aFunctionManager
Definition: Formula.hxx:48
void CheckMatrix(OUString &aFormula)