/src/libreoffice/sc/source/ui/dialogs/SparklineDataRangeDialog.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 | | |
10 | | #include <SparklineDataRangeDialog.hxx> |
11 | | #include <Sparkline.hxx> |
12 | | #include <reffact.hxx> |
13 | | #include <docfunc.hxx> |
14 | | |
15 | | #include <vcl/weld/Dialog.hxx> |
16 | | |
17 | | namespace sc |
18 | | { |
19 | | SparklineDataRangeDialog::SparklineDataRangeDialog(SfxBindings* pBindings, |
20 | | SfxChildWindow* pChildWindow, |
21 | | weld::Window* pWindow, ScViewData& rViewData) |
22 | 0 | : ScAnyRefDlgController(pBindings, pChildWindow, pWindow, |
23 | 0 | u"modules/scalc/ui/sparklinedatarangedialog.ui"_ustr, |
24 | 0 | u"SparklineDataRangeDialog"_ustr) |
25 | 0 | , mrViewData(rViewData) |
26 | 0 | , mrDocument(rViewData.GetDocument()) |
27 | 0 | , mpActiveEdit(nullptr) |
28 | 0 | , mbDialogLostFocus(false) |
29 | 0 | , mxButtonOk(m_xBuilder->weld_button(u"ok"_ustr)) |
30 | 0 | , mxButtonCancel(m_xBuilder->weld_button(u"cancel"_ustr)) |
31 | 0 | , mxDataRangeLabel(m_xBuilder->weld_label(u"cell-range-label"_ustr)) |
32 | 0 | , mxDataRangeEdit(new formula::RefEdit(m_xBuilder->weld_entry(u"cell-range-edit"_ustr))) |
33 | 0 | , mxDataRangeButton(new formula::RefButton(m_xBuilder->weld_button(u"cell-range-button"_ustr))) |
34 | | |
35 | 0 | { |
36 | 0 | mxDataRangeEdit->SetReferences(this, mxDataRangeLabel.get()); |
37 | 0 | mxDataRangeButton->SetReferences(this, mxDataRangeEdit.get()); |
38 | |
|
39 | 0 | mxButtonCancel->connect_clicked(LINK(this, SparklineDataRangeDialog, ButtonClicked)); |
40 | 0 | mxButtonOk->connect_clicked(LINK(this, SparklineDataRangeDialog, ButtonClicked)); |
41 | |
|
42 | 0 | mxButtonOk->set_sensitive(false); |
43 | |
|
44 | 0 | Link<formula::RefEdit&, void> aEditLink |
45 | 0 | = LINK(this, SparklineDataRangeDialog, EditFocusHandler); |
46 | 0 | mxDataRangeEdit->SetGetFocusHdl(aEditLink); |
47 | 0 | aEditLink = LINK(this, SparklineDataRangeDialog, LoseEditFocusHandler); |
48 | 0 | mxDataRangeEdit->SetLoseFocusHdl(aEditLink); |
49 | |
|
50 | 0 | Link<formula::RefButton&, void> aButtonLink |
51 | 0 | = LINK(this, SparklineDataRangeDialog, ButtonFocusHandler); |
52 | 0 | mxDataRangeButton->SetGetFocusHdl(aButtonLink); |
53 | 0 | aButtonLink = LINK(this, SparklineDataRangeDialog, LoseButtonFocusHandler); |
54 | 0 | mxDataRangeButton->SetLoseFocusHdl(aButtonLink); |
55 | |
|
56 | 0 | Link<formula::RefEdit&, void> aModifyLink |
57 | 0 | = LINK(this, SparklineDataRangeDialog, RefInputModifyHandler); |
58 | 0 | mxDataRangeEdit->SetModifyHdl(aModifyLink); |
59 | |
|
60 | 0 | setupValues(); |
61 | |
|
62 | 0 | mxDataRangeEdit->GrabFocus(); |
63 | 0 | } |
64 | | |
65 | 0 | SparklineDataRangeDialog::~SparklineDataRangeDialog() = default; |
66 | | |
67 | | void SparklineDataRangeDialog::setupValues() |
68 | 0 | { |
69 | 0 | ScAddress aCurrentAddress = mrViewData.GetCurPos(); |
70 | 0 | mpSparkline = mrDocument.GetSparkline(aCurrentAddress); |
71 | |
|
72 | 0 | if (mpSparkline) |
73 | 0 | { |
74 | 0 | ScRangeList aRangeList(mpSparkline->getInputRange()); |
75 | 0 | if (!aRangeList.empty()) |
76 | 0 | { |
77 | 0 | maDataRange = aRangeList[0]; |
78 | 0 | OUString aString |
79 | 0 | = maDataRange.Format(mrDocument, ScRefFlags::VALID | ScRefFlags::TAB_3D, |
80 | 0 | mrDocument.GetAddressConvention()); |
81 | 0 | mxDataRangeEdit->SetRefString(aString); |
82 | 0 | mxButtonOk->set_sensitive(true); |
83 | 0 | } |
84 | 0 | } |
85 | 0 | } |
86 | | |
87 | | void SparklineDataRangeDialog::Close() |
88 | 0 | { |
89 | 0 | DoClose(sc::SparklineDataRangeDialogWrapper::GetChildWindowId()); |
90 | 0 | } |
91 | | |
92 | | void SparklineDataRangeDialog::SetActive() |
93 | 0 | { |
94 | 0 | if (mbDialogLostFocus) |
95 | 0 | { |
96 | 0 | mbDialogLostFocus = false; |
97 | 0 | if (mpActiveEdit) |
98 | 0 | mpActiveEdit->GrabFocus(); |
99 | 0 | } |
100 | 0 | else |
101 | 0 | { |
102 | 0 | m_xDialog->grab_focus(); |
103 | 0 | } |
104 | 0 | RefInputDone(); |
105 | 0 | } |
106 | | |
107 | | void SparklineDataRangeDialog::SetReference(const ScRange& rReferenceRange, ScDocument& rDocument) |
108 | 0 | { |
109 | 0 | if (mpActiveEdit) |
110 | 0 | { |
111 | 0 | if (rReferenceRange.aStart != rReferenceRange.aEnd) |
112 | 0 | RefInputStart(mpActiveEdit); |
113 | |
|
114 | 0 | OUString aString; |
115 | 0 | const ScRefFlags eFlags = ScRefFlags::VALID | ScRefFlags::TAB_3D; |
116 | 0 | auto eAddressConvention = rDocument.GetAddressConvention(); |
117 | |
|
118 | 0 | if (mpActiveEdit == mxDataRangeEdit.get()) |
119 | 0 | { |
120 | 0 | maDataRange = rReferenceRange; |
121 | 0 | aString = maDataRange.Format(rDocument, eFlags, eAddressConvention); |
122 | 0 | mxDataRangeEdit->SetRefString(aString); |
123 | 0 | } |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | IMPL_LINK(SparklineDataRangeDialog, EditFocusHandler, formula::RefEdit&, rEdit, void) |
128 | 0 | { |
129 | 0 | if (mxDataRangeEdit.get() == &rEdit) |
130 | 0 | mpActiveEdit = mxDataRangeEdit.get(); |
131 | 0 | else |
132 | 0 | mpActiveEdit = nullptr; |
133 | |
|
134 | 0 | if (mpActiveEdit) |
135 | 0 | mpActiveEdit->SelectAll(); |
136 | 0 | } |
137 | | |
138 | | IMPL_LINK(SparklineDataRangeDialog, ButtonFocusHandler, formula::RefButton&, rButton, void) |
139 | 0 | { |
140 | 0 | if (mxDataRangeButton.get() == &rButton) |
141 | 0 | mpActiveEdit = mxDataRangeEdit.get(); |
142 | 0 | else |
143 | 0 | mpActiveEdit = nullptr; |
144 | |
|
145 | 0 | if (mpActiveEdit) |
146 | 0 | mpActiveEdit->SelectAll(); |
147 | 0 | } |
148 | | |
149 | | IMPL_LINK_NOARG(SparklineDataRangeDialog, LoseEditFocusHandler, formula::RefEdit&, void) |
150 | 0 | { |
151 | 0 | mbDialogLostFocus = !m_xDialog->has_toplevel_focus(); |
152 | 0 | } |
153 | | |
154 | | IMPL_LINK_NOARG(SparklineDataRangeDialog, LoseButtonFocusHandler, formula::RefButton&, void) |
155 | 0 | { |
156 | 0 | mbDialogLostFocus = !m_xDialog->has_toplevel_focus(); |
157 | 0 | } |
158 | | |
159 | | IMPL_LINK_NOARG(SparklineDataRangeDialog, RefInputModifyHandler, formula::RefEdit&, void) |
160 | 0 | { |
161 | 0 | if (mpActiveEdit) |
162 | 0 | { |
163 | 0 | if (mpActiveEdit == mxDataRangeEdit.get()) |
164 | 0 | { |
165 | 0 | ScRangeList aRangeList; |
166 | 0 | bool bValid = ParseWithNames(aRangeList, mxDataRangeEdit->GetText(), mrDocument); |
167 | 0 | const ScRange* pRange = (bValid && aRangeList.size() == 1) ? &aRangeList[0] : nullptr; |
168 | 0 | if (pRange) |
169 | 0 | { |
170 | 0 | maDataRange = *pRange; |
171 | 0 | mxDataRangeEdit->StartUpdateData(); |
172 | 0 | } |
173 | 0 | else |
174 | 0 | { |
175 | 0 | maDataRange = ScRange(ScAddress::INITIALIZE_INVALID); |
176 | 0 | } |
177 | 0 | } |
178 | 0 | } |
179 | 0 | } |
180 | | |
181 | | IMPL_LINK(SparklineDataRangeDialog, ButtonClicked, weld::Button&, rButton, void) |
182 | 0 | { |
183 | 0 | if (mxButtonOk.get() == &rButton) |
184 | 0 | { |
185 | 0 | perform(); |
186 | 0 | response(RET_OK); |
187 | 0 | } |
188 | 0 | else |
189 | 0 | { |
190 | 0 | response(RET_CANCEL); |
191 | 0 | } |
192 | 0 | } |
193 | | |
194 | | void SparklineDataRangeDialog::perform() |
195 | 0 | { |
196 | 0 | ScRangeList aList{ maDataRange }; |
197 | |
|
198 | 0 | auto& rDocFunc = mrViewData.GetDocShell()->GetDocFunc(); |
199 | 0 | rDocFunc.ChangeSparkline(mpSparkline, mrViewData.CurrentTabForData(), aList); |
200 | 0 | } |
201 | | |
202 | | } // end sc |
203 | | |
204 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |