Remove non-const Sequence::begin()/end() in internal code
[LibreOffice.git] / sc / source / ui / miscdlgs / solveroptions.cxx
blob770520785b39a32dd820c0c0cb170f79d79f3359
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 * This file incorporates work covered by the following license notice:
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 .
20 #include <memory>
21 #include <solveroptions.hxx>
22 #include <global.hxx>
23 #include <miscuno.hxx>
24 #include <solverutil.hxx>
26 #include <rtl/math.hxx>
27 #include <unotools/collatorwrapper.hxx>
28 #include <unotools/localedatawrapper.hxx>
29 #include <osl/diagnose.h>
31 #include <algorithm>
33 #include <com/sun/star/sheet/XSolver.hpp>
34 #include <com/sun/star/sheet/XSolverDescription.hpp>
35 #include <com/sun/star/beans/PropertyValue.hpp>
37 using namespace com::sun::star;
39 namespace {
41 /// Helper for sorting properties
42 struct ScSolverOptionsEntry
44 sal_Int32 nPosition;
45 OUString aDescription;
47 ScSolverOptionsEntry() : nPosition(0) {}
49 bool operator< (const ScSolverOptionsEntry& rOther) const
51 return (ScGlobal::GetCollator().compareString( aDescription, rOther.aDescription ) < 0);
57 ScSolverOptionsDialog::ScSolverOptionsDialog(weld::Window* pParent,
58 const uno::Sequence<OUString>& rImplNames,
59 const uno::Sequence<OUString>& rDescriptions,
60 const OUString& rEngine,
61 const uno::Sequence<beans::PropertyValue>& rProperties )
62 : GenericDialogController(pParent, "modules/scalc/ui/solveroptionsdialog.ui", "SolverOptionsDialog")
63 , maImplNames(rImplNames)
64 , maEngine(rEngine)
65 , maProperties(rProperties)
66 , m_xLbEngine(m_xBuilder->weld_combo_box("engine"))
67 , m_xLbSettings(m_xBuilder->weld_tree_view("settings"))
68 , m_xBtnEdit(m_xBuilder->weld_button("edit"))
70 m_xLbSettings->set_size_request(m_xLbSettings->get_approximate_digit_width() * 32,
71 m_xLbSettings->get_height_rows(6));
73 m_xLbSettings->enable_toggle_buttons(weld::ColumnToggleType::Check);
75 m_xLbEngine->connect_changed( LINK( this, ScSolverOptionsDialog, EngineSelectHdl ) );
77 m_xBtnEdit->connect_clicked( LINK( this, ScSolverOptionsDialog, ButtonHdl ) );
79 m_xLbSettings->connect_changed( LINK( this, ScSolverOptionsDialog, SettingsSelHdl ) );
80 m_xLbSettings->connect_row_activated( LINK( this, ScSolverOptionsDialog, SettingsDoubleClickHdl ) );
82 sal_Int32 nSelect = -1;
83 sal_Int32 nImplCount = maImplNames.getLength();
84 for (sal_Int32 nImpl=0; nImpl<nImplCount; ++nImpl)
86 OUString aImplName( maImplNames[nImpl] );
87 OUString aDescription( rDescriptions[nImpl] ); // user-visible descriptions in list box
88 m_xLbEngine->append_text(aDescription);
89 if ( aImplName == maEngine )
90 nSelect = nImpl;
92 if ( nSelect < 0 ) // no (valid) engine given
94 if ( nImplCount > 0 )
96 maEngine = maImplNames[0]; // use first implementation
97 nSelect = 0;
99 else
100 maEngine.clear();
101 maProperties.realloc(0); // don't use options from different engine
103 if ( nSelect >= 0 ) // select in list box
104 m_xLbEngine->set_active(nSelect);
106 if ( !maProperties.hasElements() )
107 ReadFromComponent(); // fill maProperties from component (using maEngine)
108 FillListBox(); // using maProperties
111 ScSolverOptionsDialog::~ScSolverOptionsDialog()
113 if (m_xIntDialog)
114 m_xIntDialog->response(RET_CANCEL);
115 assert(!m_xIntDialog);
116 if (m_xValDialog)
117 m_xValDialog->response(RET_CANCEL);
118 assert(!m_xValDialog);
121 const uno::Sequence<beans::PropertyValue>& ScSolverOptionsDialog::GetProperties()
123 // update maProperties from list box content
124 // order of entries in list box and maProperties is the same
125 sal_Int32 nEntryCount = maProperties.getLength();
126 if (nEntryCount == m_xLbSettings->n_children())
128 for (sal_Int32 nEntryPos=0; nEntryPos<nEntryCount; ++nEntryPos)
130 uno::Any& rValue = maProperties[nEntryPos].Value;
131 if (ScSolverOptionsString* pStringItem = reinterpret_cast<ScSolverOptionsString*>(m_xLbSettings->get_id(nEntryPos).toInt64()))
133 if (pStringItem->IsDouble())
134 rValue <<= pStringItem->GetDoubleValue();
135 else
136 rValue <<= pStringItem->GetIntValue();
138 else
139 rValue <<= m_xLbSettings->get_toggle(nEntryPos) == TRISTATE_TRUE;
142 else
144 OSL_FAIL( "wrong count" );
147 return maProperties;
150 void ScSolverOptionsDialog::FillListBox()
152 // get property descriptions, sort by them
154 uno::Reference<sheet::XSolverDescription> xDesc( ScSolverUtil::GetSolver( maEngine ), uno::UNO_QUERY );
155 sal_Int32 nCount = maProperties.getLength();
156 std::vector<ScSolverOptionsEntry> aDescriptions( nCount );
157 for (sal_Int32 nPos=0; nPos<nCount; nPos++)
159 OUString aPropName( maProperties[nPos].Name );
160 OUString aVisName;
161 if ( xDesc.is() )
162 aVisName = xDesc->getPropertyDescription( aPropName );
163 if ( aVisName.isEmpty() )
164 aVisName = aPropName;
165 aDescriptions[nPos].nPosition = nPos;
166 aDescriptions[nPos].aDescription = aVisName;
168 std::sort( aDescriptions.begin(), aDescriptions.end() );
170 // also update maProperties to the order of descriptions
172 uno::Sequence<beans::PropertyValue> aNewSeq;
173 aNewSeq.realloc( nCount );
174 std::transform(aDescriptions.begin(), aDescriptions.end(), aNewSeq.getArray(),
175 [this](const ScSolverOptionsEntry& rDescr) -> beans::PropertyValue { return maProperties[ rDescr.nPosition ]; });
176 maProperties = aNewSeq;
178 // fill the list box
180 m_xLbSettings->freeze();
181 m_xLbSettings->clear();
183 for (sal_Int32 nPos=0; nPos<nCount; nPos++)
185 OUString aVisName = aDescriptions[nPos].aDescription;
187 uno::Any aValue = maProperties[nPos].Value;
188 uno::TypeClass eClass = aValue.getValueTypeClass();
190 m_xLbSettings->append();
192 if ( eClass == uno::TypeClass_BOOLEAN )
194 // check box entry
195 m_xLbSettings->set_toggle(nPos, ScUnoHelpFunctions::GetBoolFromAny(aValue) ? TRISTATE_TRUE : TRISTATE_FALSE);
196 m_xLbSettings->set_text(nPos, aVisName, 0);
198 else
200 // value entry
201 m_xLbSettings->set_text(nPos, aVisName, 0);
202 m_aOptions.emplace_back(new ScSolverOptionsString(aVisName));
203 if (eClass == uno::TypeClass_DOUBLE)
205 double fDoubleValue = 0.0;
206 if (aValue >>= fDoubleValue)
207 m_aOptions.back()->SetDoubleValue(fDoubleValue);
209 OUString sTxt = aVisName + ": ";
210 sTxt += rtl::math::doubleToUString(fDoubleValue,
211 rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max,
212 ScGlobal::getLocaleData().getNumDecimalSep()[0], true );
214 m_xLbSettings->set_text(nPos, sTxt, 0);
216 else
218 sal_Int32 nIntValue = 0;
219 if (aValue >>= nIntValue)
220 m_aOptions.back()->SetIntValue(nIntValue);
222 OUString sTxt = aVisName + ": " + OUString::number(nIntValue);
224 m_xLbSettings->set_text(nPos, sTxt, 0);
226 m_xLbSettings->set_id(nPos, OUString::number(reinterpret_cast<sal_Int64>(m_aOptions.back().get())));
230 m_xLbSettings->thaw();
233 void ScSolverOptionsDialog::ReadFromComponent()
235 maProperties = ScSolverUtil::GetDefaults( maEngine );
238 void ScSolverOptionsDialog::EditOption()
240 int nEntry = m_xLbSettings->get_selected_index();
241 if (nEntry == -1)
242 return;
243 ScSolverOptionsString* pStringItem = reinterpret_cast<ScSolverOptionsString*>(m_xLbSettings->get_id(nEntry).toInt64());
244 if (!pStringItem)
245 return;
247 if (pStringItem->IsDouble())
249 m_xValDialog = std::make_shared<ScSolverValueDialog>(m_xDialog.get());
250 m_xValDialog->SetOptionName(pStringItem->GetText());
251 if (maProperties[nEntry].Name == "DECR")
252 m_xValDialog->SetMax(1.0);
253 else if (maProperties[nEntry].Name == "DEFactorMax")
254 m_xValDialog->SetMax(1.2);
255 else if (maProperties[nEntry].Name == "DEFactorMin")
256 m_xValDialog->SetMax(1.2);
257 else if (maProperties[nEntry].Name == "PSCL")
258 m_xValDialog->SetMax(0.005);
259 m_xValDialog->SetValue(pStringItem->GetDoubleValue());
260 weld::DialogController::runAsync(m_xValDialog, [nEntry, pStringItem, this](sal_Int32 nResult){
261 if (nResult == RET_OK)
263 pStringItem->SetDoubleValue(m_xValDialog->GetValue());
265 OUString sTxt(pStringItem->GetText() + ": ");
266 sTxt += rtl::math::doubleToUString(pStringItem->GetDoubleValue(),
267 rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max,
268 ScGlobal::getLocaleData().getNumDecimalSep()[0], true );
270 m_xLbSettings->set_text(nEntry, sTxt, 0);
272 m_xValDialog.reset();
275 else
277 m_xIntDialog = std::make_shared<ScSolverIntegerDialog>(m_xDialog.get());
278 m_xIntDialog->SetOptionName( pStringItem->GetText() );
279 if (maProperties[nEntry].Name == "EpsilonLevel")
280 m_xIntDialog->SetMax(3);
281 else if (maProperties[nEntry].Name == "Algorithm")
282 m_xIntDialog->SetMax(1);
283 m_xIntDialog->SetValue( pStringItem->GetIntValue() );
284 weld::DialogController::runAsync(m_xIntDialog, [nEntry, pStringItem, this](sal_Int32 nResult){
285 if (nResult == RET_OK)
287 pStringItem->SetIntValue(m_xIntDialog->GetValue());
289 OUString sTxt(
290 pStringItem->GetText() + ": " + OUString::number(pStringItem->GetIntValue()));
292 m_xLbSettings->set_text(nEntry, sTxt, 0);
294 m_xIntDialog.reset();
299 IMPL_LINK( ScSolverOptionsDialog, ButtonHdl, weld::Button&, rBtn, void )
301 if (&rBtn == m_xBtnEdit.get())
302 EditOption();
305 IMPL_LINK_NOARG(ScSolverOptionsDialog, SettingsDoubleClickHdl, weld::TreeView&, bool)
307 EditOption();
308 return true;
311 IMPL_LINK_NOARG(ScSolverOptionsDialog, EngineSelectHdl, weld::ComboBox&, void)
313 const sal_Int32 nSelectPos = m_xLbEngine->get_active();
314 if ( nSelectPos < maImplNames.getLength() )
316 OUString aNewEngine( maImplNames[nSelectPos] );
317 if ( aNewEngine != maEngine )
319 maEngine = aNewEngine;
320 ReadFromComponent(); // fill maProperties from component (using maEngine)
321 FillListBox(); // using maProperties
326 IMPL_LINK_NOARG(ScSolverOptionsDialog, SettingsSelHdl, weld::TreeView&, void)
328 bool bCheckbox = false;
330 int nEntry = m_xLbSettings->get_selected_index();
331 if (nEntry != -1)
333 ScSolverOptionsString* pStringItem = reinterpret_cast<ScSolverOptionsString*>(m_xLbSettings->get_id(nEntry).toInt64());
334 if (!pStringItem)
335 bCheckbox = true;
338 m_xBtnEdit->set_sensitive(!bCheckbox);
341 ScSolverIntegerDialog::ScSolverIntegerDialog(weld::Window * pParent)
342 : GenericDialogController(pParent, "modules/scalc/ui/integerdialog.ui", "IntegerDialog")
343 , m_xFrame(m_xBuilder->weld_frame("frame"))
344 , m_xNfValue(m_xBuilder->weld_spin_button("value"))
348 ScSolverIntegerDialog::~ScSolverIntegerDialog()
352 void ScSolverIntegerDialog::SetOptionName( const OUString& rName )
354 m_xFrame->set_label(rName);
357 void ScSolverIntegerDialog::SetValue( sal_Int32 nValue )
359 m_xNfValue->set_value( nValue );
362 void ScSolverIntegerDialog::SetMax( sal_Int32 nMax )
364 m_xNfValue->set_range(0, nMax);
367 sal_Int32 ScSolverIntegerDialog::GetValue() const
369 return m_xNfValue->get_value();
372 ScSolverValueDialog::ScSolverValueDialog(weld::Window* pParent)
373 : GenericDialogController(pParent, "modules/scalc/ui/doubledialog.ui", "DoubleDialog")
374 , m_xFrame(m_xBuilder->weld_frame("frame"))
375 , m_xEdValue(m_xBuilder->weld_entry("value"))
376 , m_fMaxValue(std::numeric_limits<double>::quiet_NaN())
380 ScSolverValueDialog::~ScSolverValueDialog()
384 void ScSolverValueDialog::SetOptionName( const OUString& rName )
386 m_xFrame->set_label(rName);
389 void ScSolverValueDialog::SetValue( double fValue )
391 m_xEdValue->set_text( rtl::math::doubleToUString( fValue,
392 rtl_math_StringFormat_Automatic, rtl_math_DecimalPlaces_Max,
393 ScGlobal::getLocaleData().getNumDecimalSep()[0], true ) );
396 void ScSolverValueDialog::SetMax(double fMax)
398 m_fMaxValue = fMax;
401 double ScSolverValueDialog::GetValue() const
403 OUString aInput = m_xEdValue->get_text();
405 rtl_math_ConversionStatus eStatus = rtl_math_ConversionStatus_Ok;
406 sal_Int32 nParseEnd = 0;
407 double fValue = ScGlobal::getLocaleData().stringToDouble( aInput, true, &eStatus, &nParseEnd);
408 /* TODO: shouldn't there be some error checking? */
409 if (!std::isnan(m_fMaxValue) && fValue > m_fMaxValue)
410 fValue = m_fMaxValue;
411 return fValue;
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */