Files
loongoffice/chart2/source/view/main/DrawModelWrapper.cxx
Armin Le Grand (allotropia) ca3c6d468f ITEM: ItemPool Rework (I)
Driving forward the Item reworks I now take the
ItemPool in focus: It now does not hold any items
anymore and should be renamed to something like
ItemInfoProvider/ItemHelper, since it's main purpose
is to provide the Defaults for the Item functionality.

There is that SfxItemInfo, only a struct and bundling
SlotID and ItemFlags. There are also the DefaultItems,
just handled as ptrs in an array. It is/was always
error-prone to keep these in sync. Remember that it's
also necessary for the order to not only being sorted
but being increments of one with no gaps allowed in
the WhichIDs to which the Items are bound.

I now bundled that to a new class ItemInfo that joins
WhichID, SlotID, ItemFlags and the default Item. This
is a pure virtual base class, it comes in three
derivations:

(1) ItemInfoStatic:
This is supposed to be global static and hosts the
Item in a std::unique_ptr to ensure cleanup. It is
designed to be constructed once during runtime and
being shared globally. It allows the ItemPtr to be
nullptr to mark as non-static (if initial static is
not possible for some reason) but still offers the
needed data. Most cases (95%+) are of that case.
The contained Item is owned by that instance. The
flag isStaticDefault() is set at the Item.

(2) ItemInfoDynamic:
This is supposed to be used for cases where the Item
cannot be static: Mainly for SfxSetItem (that needs
a Pool itself in the contained SfxItemSet, so lifetime
is bound to that Pool), but other cases showed up in
the transition. These instances live while the Pool
lives and get destructed when the Pool goes down.
Also uses std::unique_ptr for the Item instance for
as much automated cleanup as possible, the contained
Item is owned by that instance, the instance by the
Pool. The flag isDynamicDefault() is set at the Item.

(3) ItemInfoUser:
This is used for UserDefaults that can be set for
every ItemInfo entry to 'overshadow' the default
from the 'outside'. It uses a regular Item and
the central access methods implCreateItemEntry/
implCleanupItemEntry to manage the Item instance,
thus works like a SfxPoolItemHolder. The Item
instance can be globally shared and re-used even
when the Pool goes down. Instances belong to the
Pool and are cleaned up when the Pool goes down.
This Item does not need any further flag to be
set.

The ItemInfos are organized using a class
called ItemInfoPackage:

This bundles groups of ItemInfoStatic to
functional instances. There are derivations/
implementations of this e.g. for Writer ItemPool
bundling all the needed defaults for Writer,
similar for draw/impress, Calc and other usages.

These ItemInfoPackage can be 'registered' at an
ItemPool using it's method registerItemInfoPackage.
This does all the needed stuff to setup that
group of ItemInfos at the Pool (It even sets
internal vars First/LastWhich, that info can just
be derived from the buildup ItemInfo Ptrs).

The ItemInfoPackage has methods 'size()' and
'getItemInfo(index) to allow looping over it
and deliver the infos the Pool needs. The
(forced, pure virtual) overloads of getItemInfo
in the specific implementations check for the
ItemPtr being nullptr and create a exclusive
incarnation of ItemInfoDynamic for the Pool if
needed, returning that. The Pool owns the
ItemInfoDynamic incarnations and uses the
ItemInfoStatic directly. On shutdown it cleans
up the ItemInfoDynamic as needed.

The ItemInfoUser is used by the Pool when a
UserDefault is set/used: for SetUserDefaultItem,
GetUserDefaultItem, ResetUserDefaultItem. It
is not held in a 2nd list, but directly in the
list of ItemInfo'ptrs: To keep track of this
an unordered_map is used that helds the original
ItemInfo associated with the WhichID. That way
no two lookups (as before) are needed to get the
current Pool's default for any WhichID.

The derivations of ItemInfoPackage are
encapsulated and just allow access to an
ItemInfoPackage& with a single method as
return value. All use a static local instance
of a std::array<ItemInfoStatic, FIXED_SIZE>
which constructs all ItemInfoStatic and the
static Item instances - if already possible.
Sometimes it is necessary to overload the
constructor to set some static instances
for Items later than the lib init. These are
also just marked with nullptr as Item instance.
Some need to overload getItemInfo to complete
instances of ItemInfoStatic, if needed, or
create and deliver instances of ItemInfoDynamic.

The registerItemInfoPackage also offers a
optional lambda callback: there were two cases
where local data from the Pool was needed to
incarnate the item - just add that to the
call to registerItemInfoPackage if needed,
see examples in the adapted code.

For the re-use of Items this means that now
in SfxItemSet/SfxPoolItemHolder *true* static
Items can and will be used without RefCount
directly and globally. This is also the case
for dynamic Items, with the exception of
differing Pools for SfxSetItems which cannot
be done.

Future:
That design is already prepared to allow
solving that Pool-chaining problem: currently
there are master/sub-pools and all accesses
have to traverse that structure before even
doing anything.
For the future the idea is more to 'compose'
a Pool by registering ItemInfoPackages, e.g.
for Writer pool you may start with SfxItemPool,
register the writer-specific ItemInfoPackage,
then the one for DrawingLayer (if needed) and
the one for EditEngine.
It should also be possible to get to smaller
granularities of that packages. Ideas for
new ones will emerge. We might also think
about composing Pools which can e.g. run Writer
and Chart, so allowing to use Chart *without*
OLE stuff in Writer - just ideas...

More changes:
- Adapted all stuff, cleaned up old stuff/
  definitions
- Removed FreezeIdRanges, that can be done
  once per Pool on-demand (and cannot be
  forgotten to be called)
- Merged XOutdevItemPool with SdrItemPool
  and offered a ItemInfoPackage which joins
  both needed sets of Items
- All the cleanup hassle with Pools and
  defaults cleaned up
- Adapted all access methods of the pool
  to use that new stuff. Pool chaining
  currently stays, but I use a central
  method 'getTargetPool' instead of
  recursive calling to get the correct
  Pool for the action

Change-Id: I2b8d3d4c3cc80b1d0d0b3c0f4bd90d7656b4bab7
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/163157
Tested-by: Jenkins
Reviewed-by: Armin Le Grand <Armin.Le.Grand@me.com>
2024-02-12 10:35:33 +01:00

330 lines
10 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <chartview/DrawModelWrapper.hxx>
#include <ShapeFactory.hxx>
#include "ChartItemPool.hxx"
#include <ObjectIdentifier.hxx>
#include <svx/unomodel.hxx>
#include <svl/itempool.hxx>
#include <svx/objfac3d.hxx>
#include <svx/svdpage.hxx>
#include <svx/svx3ditems.hxx>
#include <svx/xtable.hxx>
#include <svx/svdoutl.hxx>
#include <editeng/unolingu.hxx>
#include <vcl/svapp.hxx>
#include <vcl/virdev.hxx>
#include <libxml/xmlwriter.h>
#include <osl/diagnose.h>
namespace com::sun::star::linguistic2 { class XHyphenator; }
namespace com::sun::star::linguistic2 { class XSpellChecker1; }
using namespace ::com::sun::star;
namespace chart
{
DrawModelWrapper::DrawModelWrapper()
: SdrModel()
{
m_xChartItemPool = ChartItemPool::CreateChartItemPool();
SetDefaultFontHeight(423); // 12pt
SfxItemPool* pMasterPool = &GetItemPool();
pMasterPool->SetDefaultMetric(MapUnit::Map100thMM);
pMasterPool->SetUserDefaultItem(SfxBoolItem(EE_PARA_HYPHENATE, true) );
pMasterPool->SetUserDefaultItem(makeSvx3DPercentDiagonalItem (5));
// append chart pool to end of pool chain
pMasterPool->GetLastPoolInChain()->SetSecondaryPool(m_xChartItemPool.get());
SetTextDefaults();
//this factory needs to be created before first use of 3D scenes once upon an office runtime
//@todo in future this should be done by drawing engine itself on demand
static bool b3dFactoryInitialized = false;
if(!b3dFactoryInitialized)
{
E3dObjFactory aObjFactory;
b3dFactoryInitialized = true;
}
//Hyphenation and spellchecking
SdrOutliner& rOutliner = GetDrawOutliner();
try
{
uno::Reference< linguistic2::XHyphenator > xHyphenator( LinguMgr::GetHyphenator() );
if( xHyphenator.is() )
rOutliner.SetHyphenator( xHyphenator );
uno::Reference< linguistic2::XSpellChecker1 > xSpellChecker( LinguMgr::GetSpellChecker() );
if ( xSpellChecker.is() )
rOutliner.SetSpeller( xSpellChecker );
}
catch(...)
{
OSL_FAIL("Can't get Hyphenator or SpellChecker for chart");
}
//ref device for font rendering
OutputDevice* pDefaultDevice = rOutliner.GetRefDevice();
if( !pDefaultDevice )
pDefaultDevice = Application::GetDefaultDevice();
m_pRefDevice.disposeAndClear();
m_pRefDevice = VclPtr<VirtualDevice>::Create(*pDefaultDevice);
MapMode aMapMode = m_pRefDevice->GetMapMode();
aMapMode.SetMapUnit(MapUnit::Map100thMM);
m_pRefDevice->SetMapMode(aMapMode);
SetRefDevice(m_pRefDevice.get());
rOutliner.SetRefDevice(m_pRefDevice.get());
}
DrawModelWrapper::~DrawModelWrapper()
{
// normally call from ~SdrModel, but do it here explicitly before we clear m_xChartItemPool
implDtorClearModel();
//remove m_pChartItemPool from pool chain
if (m_xChartItemPool)
{
SfxItemPool* pPool = &GetItemPool();
for (;;)
{
SfxItemPool* pSecondary = pPool->GetSecondaryPool();
if(pSecondary == m_xChartItemPool.get())
{
pPool->SetSecondaryPool (nullptr);
break;
}
pPool = pSecondary;
}
m_xChartItemPool.clear();
}
m_pRefDevice.disposeAndClear();
}
uno::Reference< frame::XModel > DrawModelWrapper::createUnoModel()
{
return new SvxUnoDrawingModel( this ); //tell Andreas Schluens if SvxUnoDrawingModel is not needed anymore -> remove export from svx to avoid link problems in writer
}
uno::Reference< frame::XModel > DrawModelWrapper::getUnoModel()
{
return SdrModel::getUnoModel();
}
SdrModel& DrawModelWrapper::getSdrModel()
{
return *this;
}
uno::Reference< lang::XMultiServiceFactory > DrawModelWrapper::getShapeFactory()
{
uno::Reference< lang::XMultiServiceFactory > xShapeFactory( getUnoModel(), uno::UNO_QUERY );
return xShapeFactory;
}
const rtl::Reference<SvxDrawPage> & DrawModelWrapper::getMainDrawPage()
{
if (m_xMainDrawPage.is())
return m_xMainDrawPage;
// Create draw page.
uno::Reference<drawing::XDrawPagesSupplier> xDrawPagesSuplier(getUnoModel(), uno::UNO_QUERY);
if (!xDrawPagesSuplier.is())
return m_xMainDrawPage;
uno::Reference<drawing::XDrawPages> xDrawPages = xDrawPagesSuplier->getDrawPages();
if (xDrawPages->getCount() > 1)
{
// Take the first page in case of multiple pages.
uno::Any aPage = xDrawPages->getByIndex(0);
uno::Reference<drawing::XDrawPage> xTmp;
aPage >>= xTmp;
m_xMainDrawPage = dynamic_cast<SvxDrawPage*>(xTmp.get());
assert(m_xMainDrawPage);
}
if (!m_xMainDrawPage.is())
{
m_xMainDrawPage = dynamic_cast<SvxDrawPage*>(xDrawPages->insertNewByIndex(0).get());
assert(m_xMainDrawPage);
}
//ensure that additional shapes are in front of the chart objects so create the chart root before
// let us disable this call for now
// TODO:moggi
// ShapeFactory::getOrCreateShapeFactory(getShapeFactory())->getOrCreateChartRootShape( m_xMainDrawPage );
return m_xMainDrawPage;
}
const rtl::Reference<SvxDrawPage> & DrawModelWrapper::getHiddenDrawPage()
{
if( !m_xHiddenDrawPage.is() )
{
uno::Reference< drawing::XDrawPagesSupplier > xDrawPagesSuplier( getUnoModel(), uno::UNO_QUERY );
if( xDrawPagesSuplier.is() )
{
uno::Reference< drawing::XDrawPages > xDrawPages( xDrawPagesSuplier->getDrawPages () );
if( xDrawPages->getCount()>1 )
{
uno::Any aPage = xDrawPages->getByIndex( 1 ) ;
uno::Reference<drawing::XDrawPage> xTmp;
aPage >>= xTmp;
m_xHiddenDrawPage = dynamic_cast<SvxDrawPage*>(xTmp.get());
assert(m_xHiddenDrawPage);
}
if(!m_xHiddenDrawPage.is())
{
if( xDrawPages->getCount()==0 )
{
m_xMainDrawPage = dynamic_cast<SvxDrawPage*>(xDrawPages->insertNewByIndex( 0 ).get());
assert(m_xMainDrawPage);
}
m_xHiddenDrawPage = dynamic_cast<SvxDrawPage*>(xDrawPages->insertNewByIndex( 1 ).get());
assert(m_xHiddenDrawPage);
}
}
}
return m_xHiddenDrawPage;
}
void DrawModelWrapper::clearMainDrawPage()
{
//uno::Reference<drawing::XShapes> xChartRoot( m_xMainDrawPage, uno::UNO_QUERY );
rtl::Reference<SvxShapeGroupAnyD> xChartRoot( ShapeFactory::getChartRootShape( m_xMainDrawPage ) );
if( xChartRoot.is() )
{
sal_Int32 nSubCount = xChartRoot->getCount();
uno::Reference< drawing::XShape > xShape;
for( sal_Int32 nS = nSubCount; nS--; )
{
if( xChartRoot->getByIndex( nS ) >>= xShape )
xChartRoot->remove( xShape );
}
}
}
rtl::Reference<SvxShapeGroupAnyD> DrawModelWrapper::getChartRootShape( const rtl::Reference<SvxDrawPage>& xDrawPage )
{
return ShapeFactory::getChartRootShape( xDrawPage );
}
void DrawModelWrapper::lockControllers()
{
uno::Reference< frame::XModel > xDrawModel( getUnoModel() );
if( xDrawModel.is())
xDrawModel->lockControllers();
}
void DrawModelWrapper::unlockControllers()
{
uno::Reference< frame::XModel > xDrawModel( getUnoModel() );
if( xDrawModel.is())
xDrawModel->unlockControllers();
}
OutputDevice* DrawModelWrapper::getReferenceDevice() const
{
return SdrModel::GetRefDevice();
}
SfxItemPool& DrawModelWrapper::GetItemPool()
{
return SdrModel::GetItemPool();
}
XColorListRef DrawModelWrapper::GetColorList() const
{
return SdrModel::GetColorList();
}
XDashListRef DrawModelWrapper::GetDashList() const
{
return SdrModel::GetDashList();
}
XLineEndListRef DrawModelWrapper::GetLineEndList() const
{
return SdrModel::GetLineEndList();
}
XGradientListRef DrawModelWrapper::GetGradientList() const
{
return SdrModel::GetGradientList();
}
XHatchListRef DrawModelWrapper::GetHatchList() const
{
return SdrModel::GetHatchList();
}
XBitmapListRef DrawModelWrapper::GetBitmapList() const
{
return SdrModel::GetBitmapList();
}
XPatternListRef DrawModelWrapper::GetPatternList() const
{
return SdrModel::GetPatternList();
}
SdrObject* DrawModelWrapper::getNamedSdrObject( const OUString& rName )
{
if( rName.isEmpty() )
return nullptr;
return getNamedSdrObject( rName, GetPage(0) );
}
SdrObject* DrawModelWrapper::getNamedSdrObject( const OUString& rObjectCID, SdrObjList const * pSearchList )
{
if(!pSearchList || rObjectCID.isEmpty())
return nullptr;
for (const rtl::Reference<SdrObject>& pObj : *pSearchList)
{
if( ObjectIdentifier::areIdenticalObjects( rObjectCID, pObj->GetName() ) )
return pObj.get();
SdrObject* pNamedObj = DrawModelWrapper::getNamedSdrObject( rObjectCID, pObj->GetSubList() );
if(pNamedObj)
return pNamedObj;
}
return nullptr;
}
bool DrawModelWrapper::removeShape( const rtl::Reference<SvxShape>& xShape )
{
uno::Reference<drawing::XShapes> xShapes( xShape->getParent(), uno::UNO_QUERY );
if( xShapes.is() )
{
xShapes->remove(xShape);
return true;
}
return false;
}
void DrawModelWrapper::dumpAsXml(xmlTextWriterPtr pWriter) const
{
(void)xmlTextWriterStartElement(pWriter, BAD_CAST("DrawModelWrapper"));
(void)xmlTextWriterWriteFormatAttribute(pWriter, BAD_CAST("ptr"), "%p", this);
SdrModel::dumpAsXml(pWriter);
(void)xmlTextWriterEndElement(pWriter);
}
} //namespace chart
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */