Coverage Report

Created: 2025-12-31 10:39

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