Coverage Report

Created: 2026-02-14 09:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/source/ui/miscdlgs/solvrdlg.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
 * 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 <scitems.hxx>
21
#include <sfx2/dispatch.hxx>
22
#include <svl/numformat.hxx>
23
#include <vcl/svapp.hxx>
24
#include <vcl/weld/Dialog.hxx>
25
#include <vcl/weld/MessageDialog.hxx>
26
#include <vcl/weld/weld.hxx>
27
28
#include <uiitems.hxx>
29
#include <reffact.hxx>
30
#include <document.hxx>
31
#include <globstr.hrc>
32
#include <scresid.hxx>
33
#include <sc.hrc>
34
#include <solvrdlg.hxx>
35
36
ScSolverDlg::ScSolverDlg( SfxBindings* pB, SfxChildWindow* pCW, weld::Window* pParent,
37
                          ScDocument* pDocument,
38
                          const ScAddress& aCursorPos )
39
40
0
    : ScAnyRefDlgController(pB, pCW, pParent, u"modules/scalc/ui/goalseekdlg.ui"_ustr, u"GoalSeekDialog"_ustr)
41
0
    , theFormulaCell(aCursorPos)
42
0
    , theVariableCell(aCursorPos)
43
0
    , pDoc(pDocument)
44
0
    , nCurTab(aCursorPos.Tab())
45
0
    , bDlgLostFocus(false)
46
0
    , errMsgInvalidVar(ScResId(STR_INVALIDVAR))
47
0
    , errMsgInvalidForm(ScResId(STR_INVALIDFORM))
48
0
    , errMsgNoFormula(ScResId(STR_NOFORMULA))
49
0
    , errMsgInvalidVal(ScResId(STR_INVALIDVAL))
50
0
    , m_pEdActive(nullptr)
51
0
    , m_xFtFormulaCell(m_xBuilder->weld_label(u"formulatext"_ustr))
52
0
    , m_xEdFormulaCell(new formula::RefEdit(m_xBuilder->weld_entry(u"formulaedit"_ustr)))
53
0
    , m_xRBFormulaCell(new formula::RefButton(m_xBuilder->weld_button(u"formulabutton"_ustr)))
54
0
    , m_xEdTargetVal(m_xBuilder->weld_entry(u"target"_ustr))
55
0
    , m_xFtVariableCell(m_xBuilder->weld_label(u"vartext"_ustr))
56
0
    , m_xEdVariableCell(new formula::RefEdit(m_xBuilder->weld_entry(u"varedit"_ustr)))
57
0
    , m_xRBVariableCell(new formula::RefButton(m_xBuilder->weld_button(u"varbutton"_ustr)))
58
0
    , m_xBtnOk(m_xBuilder->weld_button(u"ok"_ustr))
59
0
    , m_xBtnCancel(m_xBuilder->weld_button(u"cancel"_ustr))
60
0
{
61
0
    m_xEdFormulaCell->SetReferences(this, m_xFtFormulaCell.get());
62
0
    m_xRBFormulaCell->SetReferences(this, m_xEdFormulaCell.get());
63
0
    m_xEdVariableCell->SetReferences(this, m_xFtVariableCell.get());
64
0
    m_xRBVariableCell->SetReferences(this, m_xEdVariableCell.get());
65
0
    Init();
66
0
}
67
68
ScSolverDlg::~ScSolverDlg()
69
0
{
70
0
    if (m_xMessageBox)
71
0
        m_xMessageBox->response(RET_CANCEL);
72
0
    assert(!m_xMessageBox);
73
0
}
74
75
void ScSolverDlg::Init()
76
0
{
77
0
    m_xBtnOk->connect_clicked( LINK( this, ScSolverDlg, BtnHdl ) );
78
0
    m_xBtnCancel->connect_clicked( LINK( this, ScSolverDlg, BtnHdl ) );
79
80
0
    Link<formula::RefEdit&,void> aEditLink = LINK( this, ScSolverDlg, GetEditFocusHdl );
81
0
    m_xEdFormulaCell->SetGetFocusHdl( aEditLink );
82
0
    m_xEdVariableCell->SetGetFocusHdl( aEditLink );
83
84
0
    Link<formula::RefButton&,void> aButtonLink = LINK( this, ScSolverDlg, GetButtonFocusHdl );
85
0
    m_xRBFormulaCell->SetGetFocusHdl( aButtonLink );
86
0
    m_xRBVariableCell->SetGetFocusHdl( aButtonLink );
87
88
0
    m_xEdTargetVal->connect_focus_in(LINK(this, ScSolverDlg, GetFocusHdl));
89
90
0
    aEditLink = LINK( this, ScSolverDlg, LoseEditFocusHdl );
91
0
    m_xEdFormulaCell->SetLoseFocusHdl ( aEditLink );
92
0
    m_xEdVariableCell->SetLoseFocusHdl ( aEditLink );
93
94
0
    aButtonLink = LINK( this, ScSolverDlg, LoseButtonFocusHdl );
95
0
    m_xRBFormulaCell->SetLoseFocusHdl ( aButtonLink );
96
0
    m_xRBVariableCell->SetLoseFocusHdl ( aButtonLink );
97
98
0
    OUString aStr(theFormulaCell.Format(ScRefFlags::ADDR_ABS, nullptr, pDoc->GetAddressConvention()));
99
100
    // If Goal Seek settings are stored in the document, restore them
101
0
    const ScGoalSeekSettings& rSettings = pDoc->GetGoalSeekSettings();
102
0
    if (rSettings.bDefined)
103
0
    {
104
0
        OUString sFormulaString(rSettings.aFormulaCell.Format(ScRefFlags::ADDR_ABS, nullptr, pDoc->GetAddressConvention()));
105
0
        OUString sVariableString(rSettings.aVariableCell.Format(ScRefFlags::ADDR_ABS, nullptr, pDoc->GetAddressConvention()));
106
0
        m_xEdFormulaCell->SetText(sFormulaString);
107
0
        m_xEdVariableCell->SetText(sVariableString);
108
0
        m_xEdTargetVal->set_text(rSettings.sTargetValue);
109
0
    }
110
0
    else
111
0
    {
112
0
        m_xEdFormulaCell->SetText( aStr );
113
0
    }
114
115
0
    m_xEdFormulaCell->GrabFocus();
116
0
    m_pEdActive = m_xEdFormulaCell.get();
117
0
}
118
119
void ScSolverDlg::Close()
120
0
{
121
0
    DoClose( ScSolverDlgWrapper::GetChildWindowId() );
122
0
}
123
124
void ScSolverDlg::SetActive()
125
0
{
126
0
    if ( bDlgLostFocus )
127
0
    {
128
0
        bDlgLostFocus = false;
129
0
        if( m_pEdActive )
130
0
            m_pEdActive->GrabFocus();
131
0
    }
132
0
    else
133
0
    {
134
0
        m_xDialog->grab_focus();
135
0
    }
136
0
    RefInputDone();
137
0
}
138
139
void ScSolverDlg::SetReference( const ScRange& rRef, ScDocument& rDocP )
140
0
{
141
0
    if( !m_pEdActive )
142
0
        return;
143
144
0
    if ( rRef.aStart != rRef.aEnd )
145
0
        RefInputStart(m_pEdActive);
146
147
0
    ScAddress   aAdr = rRef.aStart;
148
0
    ScRefFlags      nFmt = ( aAdr.Tab() == nCurTab )
149
0
                            ? ScRefFlags::ADDR_ABS
150
0
                            : ScRefFlags::ADDR_ABS_3D;
151
152
0
    OUString aStr(aAdr.Format(nFmt, &rDocP, rDocP.GetAddressConvention()));
153
0
    m_pEdActive->SetRefString( aStr );
154
155
0
    if (m_pEdActive == m_xEdFormulaCell.get())
156
0
        theFormulaCell = aAdr;
157
0
    else if (m_pEdActive == m_xEdVariableCell.get())
158
0
        theVariableCell = aAdr;
159
0
}
160
161
void ScSolverDlg::RaiseError( ScSolverErr eError )
162
0
{
163
0
    OUString sMessage;
164
165
0
    switch (eError)
166
0
    {
167
0
        case SOLVERR_NOFORMULA:
168
0
            sMessage = errMsgNoFormula;
169
0
            break;
170
0
        case SOLVERR_INVALID_FORMULA:
171
0
            sMessage = errMsgInvalidForm;
172
0
            break;
173
0
        case SOLVERR_INVALID_VARIABLE:
174
0
            sMessage = errMsgInvalidVar;
175
0
            break;
176
0
        case SOLVERR_INVALID_TARGETVALUE:
177
0
            sMessage = errMsgInvalidVal;
178
0
            break;
179
0
    }
180
181
0
    m_xMessageBox.reset(Application::CreateMessageDialog(m_xDialog.get(),
182
0
                                                         VclMessageType::Warning, VclButtonsType::Ok,
183
0
                                                         sMessage));
184
0
    m_xMessageBox->runAsync(m_xMessageBox, [this](sal_Int32 /*nResult*/) {
185
0
        m_xEdTargetVal->grab_focus();
186
0
        m_xMessageBox.reset();
187
0
    });
188
0
}
189
190
bool ScSolverDlg::IsRefInputMode() const
191
0
{
192
0
    return m_pEdActive != nullptr;
193
0
}
194
195
bool ScSolverDlg::CheckTargetValue( const OUString& rStrVal )
196
0
{
197
0
    sal_uInt32 n1 = 0;
198
0
    double n2;
199
200
0
    return pDoc->GetFormatTable()->IsNumberFormat( rStrVal, n1, n2 );
201
0
}
202
203
// Handler:
204
205
IMPL_LINK(ScSolverDlg, BtnHdl, weld::Button&, rBtn, void)
206
0
{
207
0
    if (&rBtn == m_xBtnOk.get())
208
0
    {
209
0
        theTargetValStr = m_xEdTargetVal->get_text();
210
211
        // The following code checks:
212
        // 1. do the strings contain correct references / defined names?
213
        // 2. does the formula coordinate refer to a cell containing a formula?
214
        // 3. has a valid target value been entered?
215
216
0
        const formula::FormulaGrammar::AddressConvention eConv = pDoc->GetAddressConvention();
217
0
        ScRefFlags  nRes1 = theFormulaCell .Parse( m_xEdFormulaCell->GetText(),  *pDoc, eConv );
218
0
        ScRefFlags  nRes2 = theVariableCell.Parse( m_xEdVariableCell->GetText(), *pDoc, eConv );
219
220
        // Remember Goal Seek settings for the next time the dialog opens
221
0
        ScGoalSeekSettings aSettings;
222
0
        aSettings.bDefined = true;
223
0
        aSettings.aFormulaCell = theFormulaCell;
224
0
        aSettings.aVariableCell = theVariableCell;
225
0
        aSettings.sTargetValue = theTargetValStr;
226
0
        pDoc->SetGoalSeekSettings(aSettings);
227
228
0
        if ( (nRes1 & ScRefFlags::VALID) == ScRefFlags::VALID )
229
0
        {
230
0
            if ( (nRes2 & ScRefFlags::VALID) == ScRefFlags::VALID )
231
0
            {
232
0
                if ( CheckTargetValue( theTargetValStr ) )
233
0
                {
234
0
                    CellType eType = pDoc->GetCellType( theFormulaCell.Col(),
235
0
                                                        theFormulaCell.Row(),
236
0
                                                        theFormulaCell.Tab());
237
238
0
                    if ( CELLTYPE_FORMULA  == eType )
239
0
                    {
240
0
                        ScSolveParam aOutParam( theFormulaCell,
241
0
                                                theVariableCell,
242
0
                                                theTargetValStr );
243
0
                        ScSolveItem  aOutItem( SCITEM_SOLVEDATA, &aOutParam );
244
245
0
                        SetDispatcherLock( false );
246
247
0
                        SwitchToDocument();
248
0
                        GetBindings().GetDispatcher()->ExecuteList(SID_SOLVE,
249
0
                                                  SfxCallMode::SLOT | SfxCallMode::RECORD,
250
0
                                                  { &aOutItem });
251
0
                        response(RET_OK);
252
0
                    }
253
0
                    else RaiseError( SOLVERR_NOFORMULA );
254
0
                }
255
0
                else RaiseError( SOLVERR_INVALID_TARGETVALUE );
256
0
            }
257
0
            else RaiseError( SOLVERR_INVALID_VARIABLE );
258
0
        }
259
0
        else RaiseError( SOLVERR_INVALID_FORMULA );
260
0
    }
261
0
    else if (&rBtn == m_xBtnCancel.get())
262
0
    {
263
0
        response(RET_CANCEL);
264
0
    }
265
0
}
266
267
IMPL_LINK(ScSolverDlg, GetEditFocusHdl, formula::RefEdit&, rCtrl, void)
268
0
{
269
0
    if (&rCtrl == m_xEdFormulaCell.get())
270
0
        m_pEdActive = m_xEdFormulaCell.get();
271
0
    else if (&rCtrl == m_xEdVariableCell.get())
272
0
        m_pEdActive = m_xEdVariableCell.get();
273
274
0
    if (m_pEdActive)
275
0
        m_pEdActive->SelectAll();
276
0
}
277
278
IMPL_LINK_NOARG(ScSolverDlg, GetFocusHdl, weld::Widget&, void)
279
0
{
280
0
    m_pEdActive = nullptr;
281
0
    m_xEdTargetVal->select_region(0, -1);
282
0
}
283
284
IMPL_LINK(ScSolverDlg, GetButtonFocusHdl, formula::RefButton&, rCtrl, void)
285
0
{
286
0
    if (&rCtrl == m_xRBFormulaCell.get())
287
0
        m_pEdActive = m_xEdFormulaCell.get();
288
0
    else if (&rCtrl == m_xRBVariableCell.get())
289
0
        m_pEdActive = m_xEdVariableCell.get();
290
291
0
    if (m_pEdActive)
292
0
        m_pEdActive->SelectAll();
293
0
}
294
295
IMPL_LINK_NOARG(ScSolverDlg, LoseEditFocusHdl, formula::RefEdit&, void)
296
0
{
297
0
    bDlgLostFocus = !m_xDialog->has_toplevel_focus();
298
0
}
299
300
IMPL_LINK_NOARG(ScSolverDlg, LoseButtonFocusHdl, formula::RefButton&, void)
301
0
{
302
0
    bDlgLostFocus = !m_xDialog->has_toplevel_focus();
303
0
}
304
305
306
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */