summaryrefslogtreecommitdiff
path: root/sc
diff options
context:
space:
mode:
authorKohei Yoshida <kohei.yoshida@gmail.com>2013-01-11 23:22:22 -0500
committerKohei Yoshida <kohei.yoshida@gmail.com>2013-01-11 23:37:13 -0500
commitfbc389bf17b7a2259380ade43fe15fd952c13eef (patch)
treee1fe250d8e5360a5da1745158a178aac8c68add4 /sc
parent7eca12e396766dcf6ad7bf76ee3c0dfa49ac99c4 (diff)
Remove more duplicated code blocks.
Change-Id: I4b392a10b324ba082764df92a8640edb3e225666
Diffstat (limited to 'sc')
-rw-r--r--sc/source/ui/docshell/dbdocfun.cxx135
1 files changed, 50 insertions, 85 deletions
diff --git a/sc/source/ui/docshell/dbdocfun.cxx b/sc/source/ui/docshell/dbdocfun.cxx
index 1a20f6ae2513..e25dda036c1e 100644
--- a/sc/source/ui/docshell/dbdocfun.cxx
+++ b/sc/source/ui/docshell/dbdocfun.cxx
@@ -1231,6 +1231,49 @@ SAL_WNODEPRECATED_DECLARATIONS_POP
pDoc->CopyToDocument(rRange, IDF_ALL, false, pUndoDoc.get());
}
+bool checkNewOutputRange(ScDPObject& rDPObj, ScDocShell& rDocShell, ScRange& rNewOut, bool bApi)
+{
+ ScDocument* pDoc = rDocShell.GetDocument();
+
+ bool bOverflow = false;
+ rNewOut = rDPObj.GetNewOutputRange(bOverflow);
+
+ // Test for overlap with source data range.
+ // TODO: Check with other pivot tables as well.
+ const ScSheetSourceDesc* pSheetDesc = rDPObj.GetSheetDesc();
+ if (pSheetDesc && pSheetDesc->GetSourceRange().Intersects(rNewOut))
+ {
+ // New output range intersepts with the source data. Move it up to
+ // where the old range is and see if that works.
+ ScRange aOldRange = rDPObj.GetOutRange();
+ SCsROW nDiff = aOldRange.aStart.Row() - rNewOut.aStart.Row();
+ rNewOut.aStart.SetRow(aOldRange.aStart.Row());
+ rNewOut.aEnd.IncRow(nDiff);
+ if (!ValidRow(rNewOut.aStart.Row()) || !ValidRow(rNewOut.aEnd.Row()))
+ bOverflow = true;
+ }
+
+ if (bOverflow)
+ {
+ if (!bApi)
+ rDocShell.ErrorMessage(STR_PIVOT_ERROR);
+
+ return false;
+ }
+
+ ScEditableTester aTester(pDoc, rNewOut);
+ if (!aTester.IsEditable())
+ {
+ // destination area isn't editable
+ if (!bApi)
+ rDocShell.ErrorMessage(aTester.GetMessageId());
+
+ return false;
+ }
+
+ return true;
+}
+
}
bool ScDBDocFunc::DataPilotUpdate( ScDPObject* pOldObj, const ScDPObject* pNewObj,
@@ -1294,44 +1337,13 @@ bool ScDBDocFunc::DataPilotUpdate( ScDPObject* pOldObj, const ScDPObject* pNewOb
if (pOldObj->GetName().isEmpty())
pOldObj->SetName( pDoc->GetDPCollection()->CreateNewName() );
- bool bOverflow = false;
- ScRange aNewOut = pOldObj->GetNewOutputRange(bOverflow);
-
- //! test for overlap with other data pilot tables
- const ScSheetSourceDesc* pSheetDesc = pOldObj->GetSheetDesc();
- if (pSheetDesc && pSheetDesc->GetSourceRange().Intersects(aNewOut))
- {
- ScRange aOldRange = pOldObj->GetOutRange();
- SCsROW nDiff = aOldRange.aStart.Row() - aNewOut.aStart.Row();
- aNewOut.aStart.SetRow(aOldRange.aStart.Row());
- aNewOut.aEnd.SetRow(aNewOut.aEnd.Row() + nDiff);
- if (!ValidRow(aNewOut.aStart.Row()) || !ValidRow(aNewOut.aEnd.Row()))
- bOverflow = true;
- }
-
- if (bOverflow)
+ ScRange aNewOut;
+ if (!checkNewOutputRange(*pOldObj, rDocShell, aNewOut, bApi))
{
*pOldObj = aUndoDPObj;
-
- if (!bApi)
- rDocShell.ErrorMessage(STR_PIVOT_ERROR);
-
return false;
}
- {
- ScEditableTester aTester(pDoc, aNewOut);
- if (!aTester.IsEditable())
- {
- // destination area isn't editable
- //! reverse everything done so far, don't proceed
- if (!bApi)
- rDocShell.ErrorMessage(aTester.GetMessageId());
-
- return false;
- }
- }
-
// test if new output area is empty except for old area
if (!bApi)
{
@@ -1350,12 +1362,7 @@ bool ScDBDocFunc::DataPilotUpdate( ScDPObject* pOldObj, const ScDPObject* pNewOb
}
if (bRecord)
- {
- SCTAB nTab = aNewOut.aStart.Tab();
- pNewUndoDoc.reset(new ScDocument(SCDOCMODE_UNDO));
- pNewUndoDoc->InitUndo(pDoc, nTab, nTab);
- pDoc->CopyToDocument(aNewOut, IDF_ALL, false, pNewUndoDoc.get());
- }
+ createUndoDoc(pNewUndoDoc, pDoc, aNewOut);
pOldObj->Output(aNewOut.aStart);
rDocShell.PostPaintGridAll(); //! only necessary parts
@@ -1514,12 +1521,7 @@ bool ScDBDocFunc::CreatePivotTable(const ScDPObject& rDPObj, bool bRecord, bool
}
if (bRecord)
- {
- SCTAB nTab = aNewOut.aStart.Tab();
- pNewUndoDoc.reset(new ScDocument(SCDOCMODE_UNDO));
- pNewUndoDoc->InitUndo( pDoc, nTab, nTab );
- pDoc->CopyToDocument(aNewOut, IDF_ALL, false, pNewUndoDoc.get());
- }
+ createUndoDoc(pNewUndoDoc, pDoc, aNewOut);
rDestObj.Output(aNewOut.aStart);
rDocShell.PostPaintGridAll(); //! only necessary parts
@@ -1570,45 +1572,13 @@ bool ScDBDocFunc::UpdatePivotTable(ScDPObject& rDPObj, bool bRecord, bool bApi)
if (rDPObj.GetName().isEmpty())
rDPObj.SetName( pDoc->GetDPCollection()->CreateNewName() );
- bool bOverflow = false;
- ScRange aNewOut = rDPObj.GetNewOutputRange(bOverflow);
-
- //! test for overlap with other data pilot tables
- const ScSheetSourceDesc* pSheetDesc = rDPObj.GetSheetDesc();
- if (pSheetDesc && pSheetDesc->GetSourceRange().Intersects(aNewOut))
- {
- ScRange aOldRange = rDPObj.GetOutRange();
- SCsROW nDiff = aOldRange.aStart.Row()-aNewOut.aStart.Row();
- aNewOut.aStart.SetRow( aOldRange.aStart.Row() );
- aNewOut.aEnd.SetRow( aNewOut.aEnd.Row()+nDiff );
- if (!ValidRow(aNewOut.aStart.Row()) || !ValidRow(aNewOut.aEnd.Row()))
- bOverflow = true;
- }
-
- if (bOverflow)
+ ScRange aNewOut;
+ if (!checkNewOutputRange(rDPObj, rDocShell, aNewOut, bApi))
{
- if (!bApi)
- rDocShell.ErrorMessage(STR_PIVOT_ERROR);
-
rDPObj = aUndoDPObj;
return false;
}
- {
- ScEditableTester aTester(pDoc, aNewOut);
- if (!aTester.IsEditable())
- {
- // destination area isn't editable
- //! reverse everything done so far, don't proceed
-
- if (!bApi)
- rDocShell.ErrorMessage(aTester.GetMessageId());
-
- rDPObj = aUndoDPObj;
- return false;
- }
- }
-
// test if new output area is empty except for old area
if (!bApi)
{
@@ -1625,12 +1595,7 @@ bool ScDBDocFunc::UpdatePivotTable(ScDPObject& rDPObj, bool bRecord, bool bApi)
}
if (bRecord)
- {
- SCTAB nTab = aNewOut.aStart.Tab();
- pNewUndoDoc.reset(new ScDocument(SCDOCMODE_UNDO));
- pNewUndoDoc->InitUndo( pDoc, nTab, nTab );
- pDoc->CopyToDocument(aNewOut, IDF_ALL, false, pNewUndoDoc.get());
- }
+ createUndoDoc(pNewUndoDoc, pDoc, aNewOut);
rDPObj.Output(aNewOut.aStart);
rDocShell.PostPaintGridAll(); //! only necessary parts