LibreOffice Module sc (master)  1
datafdlg.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 
10 #undef SC_DLLIMPLEMENTATION
11 
12 #include <datafdlg.hxx>
13 #include <viewdata.hxx>
14 #include <docsh.hxx>
15 #include <tabvwsh.hxx>
16 
17 #include <vcl/svapp.hxx>
18 #include <osl/diagnose.h>
19 
21  : GenericDialogController(pParent, "modules/scalc/ui/dataform.ui", "DataFormDialog")
22  , pTabViewShell(pTabViewShellOri)
23  , aColLength(0)
24  , nCurrentRow(0)
25  , nStartCol(0)
26  , nEndCol(0)
27  , nStartRow(0)
28  , nEndRow(0)
29  , nTab(0)
30  , m_xBtnNew(m_xBuilder->weld_button("new"))
31  , m_xBtnDelete(m_xBuilder->weld_button("delete"))
32  , m_xBtnRestore(m_xBuilder->weld_button("restore"))
33  , m_xBtnPrev(m_xBuilder->weld_button("prev"))
34  , m_xBtnNext(m_xBuilder->weld_button("next"))
35  , m_xBtnClose(m_xBuilder->weld_button("close"))
36  , m_xSlider(m_xBuilder->weld_scrolled_window("scrollbar", true))
37  , m_xGrid(m_xBuilder->weld_container("grid"))
38  , m_xFixedText(m_xBuilder->weld_label("label"))
39 {
40  sNewRecord = m_xFixedText->get_label();
41 
42  //read header from current document, and add new controls
43  OSL_ENSURE( pTabViewShell, "pTabViewShell is NULL! :-/" );
44  ScViewData& rViewData = pTabViewShell->GetViewData();
45 
46  pDoc = &rViewData.GetDocument();
47 
48  {
49  ScRange aRange;
50  rViewData.GetSimpleArea( aRange );
51  ScAddress aStart = aRange.aStart;
52  ScAddress aEnd = aRange.aEnd;
53 
54  nStartCol = aStart.Col();
55  nEndCol = aEnd.Col();
56  nStartRow = aStart.Row();
57  nEndRow = aEnd.Row();
58 
59  nTab = rViewData.GetTabNo();
60  bool bNoSelection(false);
61  //if there is no selection
62  if ((nStartCol == nEndCol) && (nStartRow == nEndRow))
63  bNoSelection = true;
64 
65  if (bNoSelection)
66  {
67  //find last not blank cell in row
68  for (int i=1;i<=MAX_DATAFORM_COLS;i++)
69  {
70  nEndCol++;
71  OUString aColName = pDoc->GetString(nEndCol, nStartRow, nTab);
72  int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
73  if (aColName.isEmpty() && nColWidth)
74  {
75  nEndCol--;
76  break;
77  }
78  }
79 
80  //find first not blank cell in row
81  for (int i=1;i<=MAX_DATAFORM_COLS;i++)
82  {
83  if (nStartCol <= 0)
84  break;
85  nStartCol--;
86 
87  OUString aColName = pDoc->GetString(nStartCol, nStartRow, nTab);
88  int nColWidth = pDoc->GetColWidth( nEndCol, nTab );
89  if (aColName.isEmpty() && nColWidth)
90  {
91  nStartCol++;
92  break;
93  }
94  }
95 
96  //skip leading hide column
97  for (int i=1;i<=MAX_DATAFORM_COLS;i++)
98  {
99  int nColWidth = pDoc->GetColWidth( nStartCol, nTab );
100  if (nColWidth)
101  break;
102  nStartCol++;
103  }
104 
105  if (nEndCol < nStartCol)
106  nEndCol = nStartCol;
107 
108  //find last not blank cell in row
109  for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
110  {
111  nEndRow++;
112  OUString aColName = pDoc->GetString(nStartCol, nEndRow, nTab);
113  if (aColName.isEmpty())
114  {
115  nEndRow--;
116  break;
117  }
118  }
119 
120  //find first not blank cell in row
121  for (int i=1;i<=MAX_DATAFORM_ROWS;i++)
122  {
123  if (nStartRow <= 0)
124  break;
125  nStartRow--;
126 
127  OUString aColName = pDoc->GetString(nStartCol, nStartRow, nTab);
128  if (aColName.isEmpty())
129  {
130  nStartRow++;
131  break;
132  }
133  }
134 
135  if (nEndRow < nStartRow)
136  nEndRow = nStartRow;
137  }
138 
139  nCurrentRow = nStartRow + 1;
140 
141  aColLength = nEndCol - nStartCol + 1;
142 
143  //new the controls
144  m_aEntries.reserve(aColLength);
145 
146  sal_Int32 nGridRow = 0;
147  for(sal_uInt16 nIndex = 0; nIndex < aColLength; ++nIndex)
148  {
149  OUString aFieldName = pDoc->GetString(nIndex + nStartCol, nStartRow, nTab);
150  int nColWidth = pDoc->GetColWidth( nIndex + nStartCol, nTab );
151  if (nColWidth)
152  {
153  m_aEntries.emplace_back(new ScDataFormFragment(m_xGrid.get(), nGridRow));
154 
155  ++nGridRow;
156 
157  m_aEntries[nIndex]->m_xLabel->set_label(aFieldName);
158  m_aEntries[nIndex]->m_xLabel->show();
159  m_aEntries[nIndex]->m_xEdit->show();
160  }
161  else
162  {
163  m_aEntries.emplace_back(nullptr );
164  }
165  if (m_aEntries[nIndex] != nullptr)
166  {
167  m_aEntries[nIndex]->m_xEdit->connect_changed(LINK( this, ScDataFormDlg, Impl_DataModifyHdl));
168  m_aEntries[nIndex]->m_xEdit->save_value();
169  }
170  }
171  }
172 
173  FillCtrls();
174 
175  m_xSlider->vadjustment_configure(0, 0, nEndRow - nStartRow + 1, 1, 10, 1);
176 
177  m_xBtnNew->connect_clicked(LINK( this, ScDataFormDlg, Impl_NewHdl));
178  m_xBtnPrev->connect_clicked(LINK( this, ScDataFormDlg, Impl_PrevHdl));
179  m_xBtnNext->connect_clicked(LINK( this, ScDataFormDlg, Impl_NextHdl));
180 
181  m_xBtnRestore->connect_clicked(LINK( this, ScDataFormDlg, Impl_RestoreHdl));
182  m_xBtnDelete->connect_clicked(LINK( this, ScDataFormDlg, Impl_DeleteHdl));
183  m_xBtnClose->connect_clicked(LINK( this, ScDataFormDlg, Impl_CloseHdl));
184 
185  m_xSlider->connect_vadjustment_changed(LINK( this, ScDataFormDlg, Impl_ScrollHdl));
186 
187  SetButtonState();
188 }
189 
191 {
192 }
193 
195 {
196  for (sal_uInt16 i = 0; i < aColLength; ++i)
197  {
198  if (m_aEntries[i])
199  {
200  if (nCurrentRow<=nEndRow && pDoc)
201  {
202  OUString aFieldName(pDoc->GetString(i + nStartCol, nCurrentRow, nTab));
203  m_aEntries[i]->m_xEdit->set_text(aFieldName);
204  }
205  else
206  m_aEntries[i]->m_xEdit->set_text(OUString());
207  }
208  }
209 
210  if (nCurrentRow <= nEndRow)
211  {
212  OUString sLabel =
213  OUString::number(static_cast<sal_Int32>(nCurrentRow - nStartRow)) +
214  " / " +
215  OUString::number(static_cast<sal_Int32>(nEndRow - nStartRow));
216  m_xFixedText->set_label(sLabel);
217  }
218  else
219  m_xFixedText->set_label(sNewRecord);
220 
221  m_xSlider->vadjustment_set_value(nCurrentRow-nStartRow-1);
222 }
223 
224 IMPL_LINK( ScDataFormDlg, Impl_DataModifyHdl, weld::Entry&, rEdit, void)
225 {
227  m_xBtnRestore->set_sensitive(true);
228 }
229 
231 {
232  ScViewData& rViewData = pTabViewShell->GetViewData();
233  ScDocShell* pDocSh = rViewData.GetDocShell();
234  if ( !pDoc )
235  return;
236 
237  bool bHasData = std::any_of(m_aEntries.begin(), m_aEntries.end(),
238  [](const std::unique_ptr<ScDataFormFragment>& rElem) { return (rElem != nullptr) && (!rElem->m_xEdit->get_text().isEmpty()); });
239 
240  if ( !bHasData )
241  return;
242 
243  pTabViewShell->DataFormPutData(nCurrentRow, nStartRow, nStartCol, nEndRow, nEndCol, m_aEntries, aColLength);
244  nCurrentRow++;
245  if (nCurrentRow >= nEndRow + 2)
246  {
247  nEndRow++;
248  m_xSlider->vadjustment_set_upper(nEndRow - nStartRow + 1);
249  }
250  SetButtonState();
251  FillCtrls();
252  pDocSh->SetDocumentModified();
253  pDocSh->PostPaintGridAll();
254 }
255 
257 {
258  if (pDoc)
259  {
260  if ( nCurrentRow > nStartRow +1 )
261  nCurrentRow--;
262 
263  SetButtonState();
264  FillCtrls();
265  }
266 }
267 
269 {
270  if (pDoc)
271  {
272  if ( nCurrentRow <= nEndRow)
273  nCurrentRow++;
274 
275  SetButtonState();
276  FillCtrls();
277  }
278 }
279 
280 IMPL_LINK_NOARG(ScDataFormDlg, Impl_RestoreHdl, weld::Button&, void)
281 {
282  if (pDoc)
283  {
284  FillCtrls();
285  }
286 }
287 
289 {
290  ScViewData& rViewData = pTabViewShell->GetViewData();
291  ScDocShell* pDocSh = rViewData.GetDocShell();
292  if (!pDoc)
293  return;
294 
295  ScRange aRange(nStartCol, nCurrentRow, nTab, nEndCol, nCurrentRow, nTab);
296  pDoc->DeleteRow(aRange);
297  nEndRow--;
298 
299  SetButtonState();
300  pDocSh->GetUndoManager()->Clear();
301 
302  FillCtrls();
303  pDocSh->SetDocumentModified();
304  pDocSh->PostPaintGridAll();
305 }
306 
308 {
309  m_xDialog->response(RET_CANCEL);
310 }
311 
313 {
314  auto nOffset = m_xSlider->vadjustment_get_value();
315  nCurrentRow = nStartRow + nOffset + 1;
316  SetButtonState();
317  FillCtrls();
318 }
319 
321 {
322  if (nCurrentRow > nEndRow)
323  {
324  m_xBtnDelete->set_sensitive( false );
325  m_xBtnNext->set_sensitive( false );
326  }
327  else
328  {
329  m_xBtnDelete->set_sensitive(true);
330  m_xBtnNext->set_sensitive(true);
331  }
332 
333  if (nCurrentRow == nStartRow + 1)
334  m_xBtnPrev->set_sensitive( false );
335  else
336  m_xBtnPrev->set_sensitive(true);
337 
338  m_xBtnRestore->set_sensitive( false );
339  if (!m_aEntries.empty() && m_aEntries[0] != nullptr)
340  m_aEntries[0]->m_xEdit->grab_focus();
341 }
342 
344  : m_xBuilder(Application::CreateBuilder(pGrid, "modules/scalc/ui/dataformfragment.ui"))
345  , m_xLabel(m_xBuilder->weld_label("label"))
346  , m_xEdit(m_xBuilder->weld_entry("entry"))
347 {
348  m_xLabel->set_grid_left_attach(0);
349  m_xLabel->set_grid_top_attach(nLine);
350 
351  m_xEdit->set_grid_left_attach(1);
352  m_xEdit->set_grid_top_attach(nLine);
353 }
354 
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Label > m_xLabel
Definition: viewfunc.hxx:64
SCROW nEndRow
Definition: datafdlg.hxx:34
sal_Int32 nIndex
ScDocShell * GetDocShell() const
Definition: viewdata.hxx:353
ScAddress aStart
Definition: address.hxx:499
std::unique_ptr< weld::Label > m_xFixedText
Definition: datafdlg.hxx:45
SCROW Row() const
Definition: address.hxx:261
void PostPaintGridAll()
Definition: docsh3.cxx:182
SCCOL nStartCol
Definition: datafdlg.hxx:31
std::unique_ptr< weld::Entry > m_xEdit
Definition: viewfunc.hxx:65
void SetDocumentModified()
Definition: docsh.cxx:2820
SCTAB GetTabNo() const
Definition: viewdata.hxx:394
ScAddress aEnd
Definition: address.hxx:500
std::unique_ptr< weld::ScrolledWindow > m_xSlider
Definition: datafdlg.hxx:43
SCROW nStartRow
Definition: datafdlg.hxx:33
ScDocument & GetDocument() const
Definition: viewdata.hxx:379
RET_CANCEL
std::unique_ptr< weld::Label > m_xLabel
std::unique_ptr< weld::Button > m_xBtnDelete
Definition: datafdlg.hxx:38
virtual SfxUndoManager * GetUndoManager() override
Definition: docsh.cxx:2806
std::unique_ptr< weld::Button > m_xBtnRestore
Definition: datafdlg.hxx:39
#define MAX_DATAFORM_ROWS
Definition: datafdlg.hxx:20
IMPL_LINK(ScDataFormDlg, Impl_DataModifyHdl, weld::Entry &, rEdit, void)
Definition: datafdlg.cxx:224
SC_DLLPUBLIC OUString GetString(SCCOL nCol, SCROW nRow, SCTAB nTab, const ScInterpreterContext *pContext=nullptr) const
Definition: document.cxx:3478
void SetButtonState()
Definition: datafdlg.cxx:320
ScViewData & GetViewData()
Definition: tabview.hxx:333
OUString sNewRecord
Definition: datafdlg.hxx:25
int i
void FillCtrls()
Definition: datafdlg.cxx:194
SCROW nCurrentRow
Definition: datafdlg.hxx:30
virtual ~ScDataFormDlg() override
Definition: datafdlg.cxx:190
std::unique_ptr< weld::Button > m_xBtnNew
Definition: datafdlg.hxx:37
virtual void Clear()
SCCOL nEndCol
Definition: datafdlg.hxx:32
ScMarkType GetSimpleArea(SCCOL &rStartCol, SCROW &rStartRow, SCTAB &rStartTab, SCCOL &rEndCol, SCROW &rEndRow, SCTAB &rEndTab) const
Definition: viewdata.cxx:1171
#define MAX_DATAFORM_COLS
Definition: datafdlg.hxx:19
std::unique_ptr< weld::Button > m_xBtnPrev
Definition: datafdlg.hxx:40
sal_uInt16 aColLength
Definition: datafdlg.hxx:29
SCCOL Col() const
Definition: address.hxx:266
ScTabViewShell * pTabViewShell
Definition: datafdlg.hxx:27
std::unique_ptr< weld::Button > m_xBtnNext
Definition: datafdlg.hxx:41
ScDocument * pDoc
Definition: datafdlg.hxx:28
std::unique_ptr< weld::Button > m_xBtnClose
Definition: datafdlg.hxx:42
weld::Entry & rEdit
std::vector< std::unique_ptr< ScDataFormFragment > > m_aEntries
Definition: datafdlg.hxx:46
ScDataFormDlg(weld::Window *pParent, ScTabViewShell *pTabViewShell)
Definition: datafdlg.cxx:20
bool get_value_changed_from_saved() const
Reference< XExecutableDialog > m_xDialog
IMPL_LINK_NOARG(ScDataFormDlg, Impl_NewHdl, weld::Button &, void)
Definition: datafdlg.cxx:230
std::unique_ptr< weld::Container > m_xGrid
Definition: datafdlg.hxx:44
SC_DLLPUBLIC sal_uInt16 GetColWidth(SCCOL nCol, SCTAB nTab, bool bHiddenAsZero=true) const
Definition: document.cxx:4112
ScDataFormFragment(weld::Container *pGrid, int nLine)
Definition: datafdlg.cxx:343