Kaydet (Commit) af1c00c6 authored tarafından Noel Grandin's avatar Noel Grandin

move sorting code closer together

to make it easier to understand.
Also switch iteration to range-based for loops.

Change-Id: I39a3f565f40458fd5598bb2e97fe71a0fec1c09c
üst bc7fba24
...@@ -778,18 +778,18 @@ public: ...@@ -778,18 +778,18 @@ public:
vector<ZOrderHint> maZOrderList; vector<ZOrderHint> maZOrderList;
vector<ZOrderHint> maUnsortedList; vector<ZOrderHint> maUnsortedList;
sal_Int32 mnCurrentZ; sal_Int32 mnCurrentZ;
ShapeSortContext* mpParentContext; ShapeSortContext* mpParentContext;
const OUString msZOrder;
ShapeSortContext( uno::Reference< drawing::XShapes >& rShapes, ShapeSortContext* pParentContext = NULL ); ShapeSortContext( uno::Reference< drawing::XShapes >& rShapes, ShapeSortContext* pParentContext = NULL );
void popGroupAndSort();
private:
void moveShape( sal_Int32 nSourcePos, sal_Int32 nDestPos ); void moveShape( sal_Int32 nSourcePos, sal_Int32 nDestPos );
}; };
ShapeSortContext::ShapeSortContext( uno::Reference< drawing::XShapes >& rShapes, ShapeSortContext* pParentContext ) ShapeSortContext::ShapeSortContext( uno::Reference< drawing::XShapes >& rShapes, ShapeSortContext* pParentContext )
: mxShapes( rShapes ), mnCurrentZ( 0 ), mpParentContext( pParentContext ), : mxShapes( rShapes ), mnCurrentZ( 0 ), mpParentContext( pParentContext )
msZOrder("ZOrder")
{ {
} }
...@@ -799,127 +799,116 @@ void ShapeSortContext::moveShape( sal_Int32 nSourcePos, sal_Int32 nDestPos ) ...@@ -799,127 +799,116 @@ void ShapeSortContext::moveShape( sal_Int32 nSourcePos, sal_Int32 nDestPos )
uno::Reference< beans::XPropertySet > xPropSet; uno::Reference< beans::XPropertySet > xPropSet;
aAny >>= xPropSet; aAny >>= xPropSet;
if( xPropSet.is() && xPropSet->getPropertySetInfo()->hasPropertyByName( msZOrder ) ) if( xPropSet.is() && xPropSet->getPropertySetInfo()->hasPropertyByName( OUString("ZOrder") ) )
{ {
aAny <<= nDestPos; aAny <<= nDestPos;
xPropSet->setPropertyValue( msZOrder, aAny ); xPropSet->setPropertyValue( OUString("ZOrder"), aAny );
vector<ZOrderHint>::iterator aIter = maZOrderList.begin(); for( ZOrderHint& rHint : maZOrderList )
vector<ZOrderHint>::iterator aEnd = maZOrderList.end();
while( aIter != aEnd )
{ {
if( (*aIter).nIs < nSourcePos ) if( rHint.nIs < nSourcePos )
{ {
DBG_ASSERT( (*aIter).nIs >= nDestPos, "Shape sorting failed" ); DBG_ASSERT(rHint.nIs >= nDestPos, "Shape sorting failed" );
(*aIter).nIs++; rHint.nIs++;
} }
++aIter;
} }
aIter = maUnsortedList.begin(); for( ZOrderHint& rHint : maUnsortedList )
aEnd = maUnsortedList.end();
while( aIter != aEnd )
{ {
if( (*aIter).nIs < nSourcePos ) if( rHint.nIs < nSourcePos )
{ {
DBG_ASSERT( (*aIter).nIs >= nDestPos, "shape sorting failed" ); DBG_ASSERT( rHint.nIs >= nDestPos, "shape sorting failed" );
(*aIter).nIs++; rHint.nIs++;
} }
++aIter;
} }
} }
} }
void XMLShapeImportHelper::pushGroupForSorting( uno::Reference< drawing::XShapes >& rShapes ) // sort shapes
void ShapeSortContext::popGroupAndSort()
{ {
mpImpl->mpSortContext = new ShapeSortContext( rShapes, mpImpl->mpSortContext ); // only do something if we have shapes to sort
} if( maZOrderList.empty() )
void XMLShapeImportHelper::popGroupAndSort()
{
DBG_ASSERT( mpImpl->mpSortContext, "No context to sort!" );
if( mpImpl->mpSortContext == NULL )
return; return;
try // check if there are more shapes than inserted with ::shapeWithZIndexAdded()
{ // This can happen if there where already shapes on the page before import
vector<ZOrderHint>& rZList = mpImpl->mpSortContext->maZOrderList; // Since the writer may delete some of this shapes during import, we need
vector<ZOrderHint>& rUnsortedList = mpImpl->mpSortContext->maUnsortedList; // to do this here and not in our c'tor anymore
// sort shapes
if( !rZList.empty() )
{
// only do something if we have shapes to sort
// check if there are more shapes than inserted with ::shapeWithZIndexAdded() // check if we have more shapes than we know of
// This can happen if there where already shapes on the page before import sal_Int32 nCount = mxShapes->getCount();
// Since the writer may delete some of this shapes during import, we need
// to do this here and not in our c'tor anymore
// check if we have more shapes than we know of nCount -= maZOrderList.size();
sal_Int32 nCount = mpImpl->mpSortContext->mxShapes->getCount(); nCount -= maUnsortedList.size();
nCount -= rZList.size(); if( nCount > 0 )
nCount -= rUnsortedList.size(); {
// first update offsets of added shapes
for (ZOrderHint& rHint : maZOrderList)
rHint.nIs += nCount;
for (ZOrderHint& rHint : maUnsortedList)
rHint.nIs += nCount;
if( nCount > 0 ) // second add the already existing shapes in the unsorted list
{ ZOrderHint aNewHint;
// first update offsets of added shapes do
vector<ZOrderHint>::iterator aIter( rZList.begin() ); {
while( aIter != rZList.end() ) nCount--;
(*aIter++).nIs += nCount;
aIter = rUnsortedList.begin(); aNewHint.nIs = nCount;
while( aIter != rUnsortedList.end() ) aNewHint.nShould = -1;
(*aIter++).nIs += nCount;
// second add the already existing shapes in the unsorted list maUnsortedList.insert(maUnsortedList.begin(), aNewHint);
ZOrderHint aNewHint; }
while( nCount );
}
do // sort z-ordered shapes by nShould field
{ std::sort(maZOrderList.begin(), maZOrderList.end());
nCount--;
aNewHint.nIs = nCount; // this is the current index, all shapes before that
aNewHint.nShould = -1; // index are finished
sal_Int32 nIndex = 0;
for (ZOrderHint& rHint : maZOrderList)
{
while( nIndex < rHint.nShould && !maUnsortedList.empty() )
{
ZOrderHint aGapHint( *maUnsortedList.begin() );
maUnsortedList.erase(maUnsortedList.begin());
rUnsortedList.insert(rUnsortedList.begin(), aNewHint); moveShape( aGapHint.nIs, nIndex++ );
} }
while( nCount );
}
// sort z ordered shapes if(rHint.nIs != nIndex )
std::sort(rZList.begin(), rZList.end()); moveShape( rHint.nIs, nIndex );
// this is the current index, all shapes before that nIndex++;
// index are finished }
sal_Int32 nIndex = 0; maZOrderList.clear();
while( !rZList.empty() ) }
{
while( nIndex < (*rZList.begin()).nShould && !rUnsortedList.empty() )
{
ZOrderHint aGapHint( *rUnsortedList.begin() );
rUnsortedList.erase(rUnsortedList.begin());
mpImpl->mpSortContext->moveShape( aGapHint.nIs, nIndex++ ); void XMLShapeImportHelper::pushGroupForSorting( uno::Reference< drawing::XShapes >& rShapes )
} {
mpImpl->mpSortContext = new ShapeSortContext( rShapes, mpImpl->mpSortContext );
}
if( (*rZList.begin()).nIs != nIndex ) void XMLShapeImportHelper::popGroupAndSort()
mpImpl->mpSortContext->moveShape( (*rZList.begin()).nIs, nIndex ); {
DBG_ASSERT( mpImpl->mpSortContext, "No context to sort!" );
if( mpImpl->mpSortContext == NULL )
return;
rZList.erase(rZList.begin()); try
nIndex++; {
} mpImpl->mpSortContext->popGroupAndSort();
}
} }
catch( uno::Exception& ) catch( uno::Exception& )
{ {
OSL_FAIL("exception while sorting shapes, sorting failed!"); OSL_FAIL("exception while sorting shapes, sorting failed!");
} }
// put parent on top and delete current context, were done // put parent on top and delete current context, we are done
ShapeSortContext* pContext = mpImpl->mpSortContext; ShapeSortContext* pContext = mpImpl->mpSortContext;
mpImpl->mpSortContext = pContext->mpParentContext; mpImpl->mpSortContext = pContext->mpParentContext;
delete pContext; delete pContext;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment