Line data Source code
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/app.hxx>
22 : #include <sfx2/bindings.hxx>
23 : #include <vcl/msgbox.hxx>
24 :
25 : #include <com/sun/star/sdbc/XResultSet.hpp>
26 :
27 : #include "dbfunc.hxx"
28 : #include "docsh.hxx"
29 : #include "attrib.hxx"
30 : #include "sc.hrc"
31 : #include "undodat.hxx"
32 : #include "dbdata.hxx"
33 : #include "globstr.hrc"
34 : #include "global.hxx"
35 : #include "dbdocfun.hxx"
36 : #include "editable.hxx"
37 : #include "queryentry.hxx"
38 : #include "markdata.hxx"
39 :
40 : //==================================================================
41 :
42 203 : ScDBFunc::ScDBFunc( Window* pParent, ScDocShell& rDocSh, ScTabViewShell* pViewShell ) :
43 203 : ScViewFunc( pParent, rDocSh, pViewShell )
44 : {
45 203 : }
46 :
47 201 : ScDBFunc::~ScDBFunc()
48 : {
49 201 : }
50 :
51 : //
52 : // auxiliary functions
53 : //
54 :
55 0 : void ScDBFunc::GotoDBArea( const OUString& rDBName )
56 : {
57 0 : ScDocument* pDoc = GetViewData()->GetDocument();
58 0 : ScDBCollection* pDBCol = pDoc->GetDBCollection();
59 0 : ScDBData* pData = pDBCol->getNamedDBs().findByUpperName(ScGlobal::pCharClass->uppercase(rDBName));
60 0 : if (pData)
61 : {
62 0 : SCTAB nTab = 0;
63 0 : SCCOL nStartCol = 0;
64 0 : SCROW nStartRow = 0;
65 0 : SCCOL nEndCol = 0;
66 0 : SCROW nEndRow = 0;
67 :
68 0 : pData->GetArea( nTab, nStartCol, nStartRow, nEndCol, nEndRow );
69 0 : SetTabNo( nTab );
70 :
71 : MoveCursorAbs( nStartCol, nStartRow, ScFollowMode( SC_FOLLOW_JUMP ),
72 0 : false, false ); // bShift,bControl
73 0 : DoneBlockMode();
74 0 : InitBlockMode( nStartCol, nStartRow, nTab );
75 0 : MarkCursor( nEndCol, nEndRow, nTab );
76 0 : SelectionChanged();
77 : }
78 0 : }
79 :
80 : // search current datarange for sort / filter
81 :
82 0 : ScDBData* ScDBFunc::GetDBData( bool bMark, ScGetDBMode eMode, ScGetDBSelection eSel )
83 : {
84 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
85 0 : ScDBData* pData = NULL;
86 0 : ScRange aRange;
87 0 : ScMarkType eMarkType = GetViewData()->GetSimpleArea(aRange);
88 0 : if ( eMarkType == SC_MARK_SIMPLE || eMarkType == SC_MARK_SIMPLE_FILTERED )
89 : {
90 0 : bool bShrinkColumnsOnly = false;
91 0 : if (eSel == SC_DBSEL_ROW_DOWN)
92 : {
93 : // Don't alter row range, additional rows may have been selected on
94 : // purpose to append data, or to have a fake header row.
95 0 : bShrinkColumnsOnly = true;
96 : // Select further rows only if only one row or a portion thereof is
97 : // selected.
98 0 : if (aRange.aStart.Row() != aRange.aEnd.Row())
99 : {
100 : // If an area is selected shrink that to the actual used
101 : // columns, don't draw filter buttons for empty columns.
102 0 : eSel = SC_DBSEL_SHRINK_TO_USED_DATA;
103 : }
104 0 : else if (aRange.aStart.Col() == aRange.aEnd.Col())
105 : {
106 : // One cell only, if it is not marked obtain entire used data
107 : // area.
108 0 : const ScMarkData& rMarkData = GetViewData()->GetMarkData();
109 0 : if (!(rMarkData.IsMarked() || rMarkData.IsMultiMarked()))
110 0 : eSel = SC_DBSEL_KEEP;
111 : }
112 : }
113 0 : switch (eSel)
114 : {
115 : case SC_DBSEL_SHRINK_TO_SHEET_DATA:
116 : {
117 : // Shrink the selection to sheet data area.
118 0 : ScDocument* pDoc = pDocSh->GetDocument();
119 0 : SCCOL nCol1 = aRange.aStart.Col(), nCol2 = aRange.aEnd.Col();
120 0 : SCROW nRow1 = aRange.aStart.Row(), nRow2 = aRange.aEnd.Row();
121 0 : if (pDoc->ShrinkToDataArea( aRange.aStart.Tab(), nCol1, nRow1, nCol2, nRow2))
122 : {
123 0 : aRange.aStart.SetCol(nCol1);
124 0 : aRange.aEnd.SetCol(nCol2);
125 0 : aRange.aStart.SetRow(nRow1);
126 0 : aRange.aEnd.SetRow(nRow2);
127 : }
128 : }
129 0 : break;
130 : case SC_DBSEL_SHRINK_TO_USED_DATA:
131 : case SC_DBSEL_ROW_DOWN:
132 : {
133 : // Shrink the selection to actual used area.
134 0 : ScDocument* pDoc = pDocSh->GetDocument();
135 0 : SCCOL nCol1 = aRange.aStart.Col(), nCol2 = aRange.aEnd.Col();
136 0 : SCROW nRow1 = aRange.aStart.Row(), nRow2 = aRange.aEnd.Row();
137 : bool bShrunk;
138 0 : pDoc->ShrinkToUsedDataArea( bShrunk, aRange.aStart.Tab(),
139 0 : nCol1, nRow1, nCol2, nRow2, bShrinkColumnsOnly);
140 0 : if (bShrunk)
141 : {
142 0 : aRange.aStart.SetCol(nCol1);
143 0 : aRange.aEnd.SetCol(nCol2);
144 0 : aRange.aStart.SetRow(nRow1);
145 0 : aRange.aEnd.SetRow(nRow2);
146 : }
147 : }
148 0 : break;
149 : default:
150 : ; // nothing
151 : }
152 0 : pData = pDocSh->GetDBData( aRange, eMode, eSel );
153 : }
154 0 : else if ( eMode != SC_DB_OLD )
155 : pData = pDocSh->GetDBData(
156 0 : ScRange( GetViewData()->GetCurX(), GetViewData()->GetCurY(),
157 0 : GetViewData()->GetTabNo() ),
158 0 : eMode, SC_DBSEL_KEEP );
159 :
160 0 : if (!pData)
161 0 : return NULL;
162 :
163 0 : if (bMark)
164 : {
165 0 : ScRange aFound;
166 0 : pData->GetArea(aFound);
167 0 : MarkRange( aFound, false );
168 : }
169 0 : return pData;
170 : }
171 :
172 0 : ScDBData* ScDBFunc::GetAnonymousDBData()
173 : {
174 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
175 0 : ScRange aRange;
176 0 : ScMarkType eMarkType = GetViewData()->GetSimpleArea(aRange);
177 0 : if (eMarkType != SC_MARK_SIMPLE && eMarkType != SC_MARK_SIMPLE_FILTERED)
178 0 : return NULL;
179 :
180 : // Expand to used data area if not explicitly marked.
181 0 : const ScMarkData& rMarkData = GetViewData()->GetMarkData();
182 0 : if (!rMarkData.IsMarked() && !rMarkData.IsMultiMarked())
183 : {
184 0 : SCCOL nCol1 = aRange.aStart.Col();
185 0 : SCCOL nCol2 = aRange.aEnd.Col();
186 0 : SCROW nRow1 = aRange.aStart.Row();
187 0 : SCROW nRow2 = aRange.aEnd.Row();
188 0 : pDocSh->GetDocument()->GetDataArea(aRange.aStart.Tab(), nCol1, nRow1, nCol2, nRow2, false, false);
189 0 : aRange.aStart.SetCol(nCol1);
190 0 : aRange.aStart.SetRow(nRow1);
191 0 : aRange.aEnd.SetCol(nCol2);
192 0 : aRange.aEnd.SetRow(nRow2);
193 : }
194 :
195 0 : return pDocSh->GetAnonymousDBData(aRange);
196 : }
197 :
198 : // change database range (dialog)
199 :
200 0 : void ScDBFunc::NotifyCloseDbNameDlg( const ScDBCollection& rNewColl, const std::vector<ScRange> &rDelAreaList )
201 : {
202 :
203 0 : ScDocShell* pDocShell = GetViewData()->GetDocShell();
204 0 : ScDocShellModificator aModificator( *pDocShell );
205 0 : ScDocument* pDoc = pDocShell->GetDocument();
206 0 : ScDBCollection* pOldColl = pDoc->GetDBCollection();
207 0 : ScDBCollection* pUndoColl = NULL;
208 0 : const sal_Bool bRecord (pDoc->IsUndoEnabled());
209 :
210 0 : std::vector<ScRange>::const_iterator iter;
211 0 : for (iter = rDelAreaList.begin(); iter != rDelAreaList.end(); ++iter)
212 : {
213 : // unregistering target in SBA no longer necessary
214 0 : const ScAddress& rStart = iter->aStart;
215 0 : const ScAddress& rEnd = iter->aEnd;
216 0 : pDocShell->DBAreaDeleted( rStart.Tab(),
217 0 : rStart.Col(), rStart.Row(),
218 0 : rEnd.Col(), rEnd.Row() );
219 :
220 : }
221 :
222 0 : if (bRecord)
223 0 : pUndoColl = new ScDBCollection( *pOldColl );
224 :
225 : // register target in SBA no longer necessary
226 :
227 0 : pDoc->CompileDBFormula( sal_True ); // CreateFormulaString
228 0 : pDoc->SetDBCollection( new ScDBCollection( rNewColl ) );
229 0 : pDoc->CompileDBFormula( false ); // CompileFormulaString
230 0 : pOldColl = NULL;
231 0 : pDocShell->PostPaint(ScRange(0, 0, 0, MAXCOL, MAXROW, MAXTAB), PAINT_GRID);
232 0 : aModificator.SetDocumentModified();
233 0 : SFX_APP()->Broadcast( SfxSimpleHint( SC_HINT_DBAREAS_CHANGED ) );
234 :
235 0 : if (bRecord)
236 : {
237 0 : ScDBCollection* pRedoColl = new ScDBCollection( rNewColl );
238 0 : pDocShell->GetUndoManager()->AddUndoAction(
239 0 : new ScUndoDBData( pDocShell, pUndoColl, pRedoColl ) );
240 0 : }
241 0 : }
242 :
243 : //
244 : // main functions
245 : //
246 :
247 : // Sort
248 :
249 0 : void ScDBFunc::UISort( const ScSortParam& rSortParam, sal_Bool bRecord )
250 : {
251 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
252 0 : ScDocument* pDoc = pDocSh->GetDocument();
253 0 : SCTAB nTab = GetViewData()->GetTabNo();
254 : ScDBData* pDBData = pDoc->GetDBAtArea( nTab, rSortParam.nCol1, rSortParam.nRow1,
255 0 : rSortParam.nCol2, rSortParam.nRow2 );
256 0 : if (!pDBData)
257 : {
258 : OSL_FAIL( "Sort: no DBData" );
259 0 : return;
260 : }
261 :
262 0 : ScSubTotalParam aSubTotalParam;
263 0 : pDBData->GetSubTotalParam( aSubTotalParam );
264 0 : if (aSubTotalParam.bGroupActive[0] && !aSubTotalParam.bRemoveOnly)
265 : {
266 : // repeat subtotals, with new sortorder
267 :
268 0 : DoSubTotals( aSubTotalParam, bRecord, &rSortParam );
269 : }
270 : else
271 : {
272 0 : Sort( rSortParam, bRecord ); // just sort
273 : }
274 : }
275 :
276 0 : void ScDBFunc::Sort( const ScSortParam& rSortParam, sal_Bool bRecord, sal_Bool bPaint )
277 : {
278 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
279 0 : SCTAB nTab = GetViewData()->GetTabNo();
280 0 : ScDBDocFunc aDBDocFunc( *pDocSh );
281 0 : sal_Bool bSuccess = aDBDocFunc.Sort( nTab, rSortParam, bRecord, bPaint, false );
282 0 : if ( bSuccess && !rSortParam.bInplace )
283 : {
284 : // mark target
285 : ScRange aDestRange( rSortParam.nDestCol, rSortParam.nDestRow, rSortParam.nDestTab,
286 : rSortParam.nDestCol + rSortParam.nCol2 - rSortParam.nCol1,
287 0 : rSortParam.nDestRow + rSortParam.nRow2 - rSortParam.nRow1,
288 0 : rSortParam.nDestTab );
289 0 : MarkRange( aDestRange );
290 0 : }
291 0 : }
292 :
293 : // filters
294 :
295 3 : void ScDBFunc::Query( const ScQueryParam& rQueryParam, const ScRange* pAdvSource, sal_Bool bRecord )
296 : {
297 3 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
298 3 : SCTAB nTab = GetViewData()->GetTabNo();
299 3 : ScDBDocFunc aDBDocFunc( *pDocSh );
300 3 : sal_Bool bSuccess = aDBDocFunc.Query( nTab, rQueryParam, pAdvSource, bRecord, false );
301 :
302 3 : if (bSuccess)
303 : {
304 3 : sal_Bool bCopy = !rQueryParam.bInplace;
305 3 : if (bCopy)
306 : {
307 : // mark target range (data base range has been set up if applicable)
308 0 : ScDocument* pDoc = pDocSh->GetDocument();
309 : ScDBData* pDestData = pDoc->GetDBAtCursor(
310 : rQueryParam.nDestCol, rQueryParam.nDestRow,
311 0 : rQueryParam.nDestTab, sal_True );
312 0 : if (pDestData)
313 : {
314 0 : ScRange aDestRange;
315 0 : pDestData->GetArea(aDestRange);
316 0 : MarkRange( aDestRange );
317 : }
318 : }
319 :
320 3 : if (!bCopy)
321 : {
322 3 : UpdateScrollBars();
323 3 : SelectionChanged(); // for attribute states (filtered rows are ignored)
324 : }
325 :
326 3 : GetViewData()->GetBindings().Invalidate( SID_UNFILTER );
327 3 : }
328 3 : }
329 :
330 : // autofilter-buttons show / hide
331 :
332 0 : void ScDBFunc::ToggleAutoFilter()
333 : {
334 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
335 0 : ScDocShellModificator aModificator( *pDocSh );
336 :
337 0 : ScQueryParam aParam;
338 0 : ScDocument* pDoc = GetViewData()->GetDocument();
339 0 : ScDBData* pDBData = GetDBData(false, SC_DB_MAKE, SC_DBSEL_ROW_DOWN);
340 :
341 0 : pDBData->SetByRow( sal_True ); //! undo, retrieve beforehand ??
342 0 : pDBData->GetQueryParam( aParam );
343 :
344 :
345 : SCCOL nCol;
346 0 : SCROW nRow = aParam.nRow1;
347 0 : SCTAB nTab = GetViewData()->GetTabNo();
348 : sal_Int16 nFlag;
349 0 : sal_Bool bHasAuto = sal_True;
350 0 : sal_Bool bHeader = pDBData->HasHeader();
351 0 : sal_Bool bPaint = false;
352 :
353 : //! instead retrieve from DB-range?
354 :
355 0 : for (nCol=aParam.nCol1; nCol<=aParam.nCol2 && bHasAuto; nCol++)
356 : {
357 : nFlag = ((ScMergeFlagAttr*) pDoc->
358 0 : GetAttr( nCol, nRow, nTab, ATTR_MERGE_FLAG ))->GetValue();
359 :
360 0 : if ( (nFlag & SC_MF_AUTO) == 0 )
361 0 : bHasAuto = false;
362 : }
363 :
364 0 : if (bHasAuto) // remove
365 : {
366 : // hide filter buttons
367 :
368 0 : for (nCol=aParam.nCol1; nCol<=aParam.nCol2; nCol++)
369 : {
370 : nFlag = ((ScMergeFlagAttr*) pDoc->
371 0 : GetAttr( nCol, nRow, nTab, ATTR_MERGE_FLAG ))->GetValue();
372 0 : pDoc->ApplyAttr( nCol, nRow, nTab, ScMergeFlagAttr( nFlag & ~SC_MF_AUTO ) );
373 : }
374 :
375 : // use a list action for the AutoFilter buttons (ScUndoAutoFilter) and the filter operation
376 :
377 0 : String aUndo = ScGlobal::GetRscString( STR_UNDO_QUERY );
378 0 : pDocSh->GetUndoManager()->EnterListAction( aUndo, aUndo );
379 :
380 0 : ScRange aRange;
381 0 : pDBData->GetArea( aRange );
382 0 : pDocSh->GetUndoManager()->AddUndoAction(
383 0 : new ScUndoAutoFilter( pDocSh, aRange, pDBData->GetName(), false ) );
384 :
385 0 : pDBData->SetAutoFilter(false);
386 :
387 : // remove filter (incl. Paint / Undo)
388 :
389 0 : SCSIZE nEC = aParam.GetEntryCount();
390 0 : for (SCSIZE i=0; i<nEC; i++)
391 0 : aParam.GetEntry(i).bDoQuery = false;
392 0 : aParam.bDuplicate = sal_True;
393 0 : Query( aParam, NULL, sal_True );
394 :
395 0 : pDocSh->GetUndoManager()->LeaveListAction();
396 :
397 0 : bPaint = sal_True;
398 : }
399 : else // show filter buttons
400 : {
401 0 : if ( !pDoc->IsBlockEmpty( nTab,
402 : aParam.nCol1, aParam.nRow1,
403 0 : aParam.nCol2, aParam.nRow2 ) )
404 : {
405 0 : if (!bHeader)
406 : {
407 0 : if ( MessBox( GetViewData()->GetDialogParent(), WinBits(WB_YES_NO | WB_DEF_YES),
408 0 : ScGlobal::GetRscString( STR_MSSG_DOSUBTOTALS_0 ), // "StarCalc"
409 0 : ScGlobal::GetRscString( STR_MSSG_MAKEAUTOFILTER_0 ) // header from first row?
410 0 : ).Execute() == RET_YES )
411 : {
412 0 : pDBData->SetHeader( sal_True ); //! Undo ??
413 0 : bHeader = sal_True;
414 : }
415 : }
416 :
417 0 : ScRange aRange;
418 0 : pDBData->GetArea( aRange );
419 0 : pDocSh->GetUndoManager()->AddUndoAction(
420 0 : new ScUndoAutoFilter( pDocSh, aRange, pDBData->GetName(), sal_True ) );
421 :
422 0 : pDBData->SetAutoFilter(sal_True);
423 :
424 0 : for (nCol=aParam.nCol1; nCol<=aParam.nCol2; nCol++)
425 : {
426 : nFlag = ((ScMergeFlagAttr*) pDoc->
427 0 : GetAttr( nCol, nRow, nTab, ATTR_MERGE_FLAG ))->GetValue();
428 0 : pDoc->ApplyAttr( nCol, nRow, nTab, ScMergeFlagAttr( nFlag | SC_MF_AUTO ) );
429 : }
430 : pDocSh->PostPaint(ScRange(aParam.nCol1, nRow, nTab, aParam.nCol2, nRow, nTab),
431 0 : PAINT_GRID);
432 0 : bPaint = sal_True;
433 : }
434 : else
435 : {
436 : ErrorBox aErrorBox( GetViewData()->GetDialogParent(), WinBits( WB_OK | WB_DEF_OK ),
437 0 : ScGlobal::GetRscString( STR_ERR_AUTOFILTER ) );
438 0 : aErrorBox.Execute();
439 : }
440 : }
441 :
442 0 : if ( bPaint )
443 : {
444 0 : aModificator.SetDocumentModified();
445 :
446 0 : SfxBindings& rBindings = GetViewData()->GetBindings();
447 0 : rBindings.Invalidate( SID_AUTO_FILTER );
448 0 : rBindings.Invalidate( SID_AUTOFILTER_HIDE );
449 0 : }
450 0 : }
451 :
452 : // just hide, no data change
453 :
454 0 : void ScDBFunc::HideAutoFilter()
455 : {
456 0 : ScDocShell* pDocSh = GetViewData()->GetDocShell();
457 0 : ScDocShellModificator aModificator( *pDocSh );
458 :
459 0 : ScDocument* pDoc = pDocSh->GetDocument();
460 :
461 0 : ScQueryParam aParam;
462 0 : ScDBData* pDBData = GetDBData( false );
463 :
464 : SCTAB nTab;
465 : SCCOL nCol1, nCol2;
466 : SCROW nRow1, nRow2;
467 0 : pDBData->GetArea(nTab, nCol1, nRow1, nCol2, nRow2);
468 :
469 0 : for (SCCOL nCol=nCol1; nCol<=nCol2; nCol++)
470 : {
471 : sal_Int16 nFlag = ((ScMergeFlagAttr*) pDoc->
472 0 : GetAttr( nCol, nRow1, nTab, ATTR_MERGE_FLAG ))->GetValue();
473 0 : pDoc->ApplyAttr( nCol, nRow1, nTab, ScMergeFlagAttr( nFlag & ~SC_MF_AUTO ) );
474 : }
475 :
476 0 : ScRange aRange;
477 0 : pDBData->GetArea( aRange );
478 0 : pDocSh->GetUndoManager()->AddUndoAction(
479 0 : new ScUndoAutoFilter( pDocSh, aRange, pDBData->GetName(), false ) );
480 :
481 0 : pDBData->SetAutoFilter(false);
482 :
483 0 : pDocSh->PostPaint(ScRange(nCol1, nRow1, nTab, nCol2, nRow1, nTab), PAINT_GRID );
484 0 : aModificator.SetDocumentModified();
485 :
486 0 : SfxBindings& rBindings = GetViewData()->GetBindings();
487 0 : rBindings.Invalidate( SID_AUTO_FILTER );
488 0 : rBindings.Invalidate( SID_AUTOFILTER_HIDE );
489 0 : }
490 :
491 : // Re-Import
492 :
493 0 : sal_Bool ScDBFunc::ImportData( const ScImportParam& rParam, sal_Bool bRecord )
494 : {
495 0 : ScDocument* pDoc = GetViewData()->GetDocument();
496 0 : ScEditableTester aTester( pDoc, GetViewData()->GetTabNo(), rParam.nCol1,rParam.nRow1,
497 0 : rParam.nCol2,rParam.nRow2 );
498 0 : if ( !aTester.IsEditable() )
499 : {
500 0 : ErrorMessage(aTester.GetMessageId());
501 0 : return false;
502 : }
503 :
504 0 : ScDBDocFunc aDBDocFunc( *GetViewData()->GetDocShell() );
505 0 : return aDBDocFunc.DoImport( GetViewData()->GetTabNo(), rParam, NULL, bRecord );
506 93 : }
507 :
508 :
509 :
510 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|