1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <svx/sdr/contact/viewcontact.hxx>
21 #include <svx/sdr/contact/viewobjectcontact.hxx>
22 #include <svx/sdr/contact/objectcontact.hxx>
23 #include <basegfx/polygon/b2dpolygon.hxx>
24 #include <basegfx/polygon/b2dpolygontools.hxx>
25 #include <basegfx/color/bcolor.hxx>
26 #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
27 #include <basegfx/matrix/b2dhommatrix.hxx>
28 #include <sdr/contact/objectcontactofpageview.hxx>
29 #include <tools/debug.hxx>
31 namespace sdr
{ namespace contact
{
33 // Create a Object-Specific ViewObjectContact, set ViewContact and
34 // ObjectContact. Always needs to return something. Default is to create
35 // a standard ViewObjectContact containing the given ObjectContact and *this
36 ViewObjectContact
& ViewContact::CreateObjectSpecificViewObjectContact(ObjectContact
& rObjectContact
)
38 return *(new ViewObjectContact(rObjectContact
, *this));
41 ViewContact::ViewContact()
42 : maViewObjectContactVector(),
43 mxViewIndependentPrimitive2DSequence()
47 ViewContact::~ViewContact()
52 void ViewContact::deleteAllVOCs()
54 // get rid of all VOCs
55 // #i84257# To avoid that each 'delete pCandidate' again uses
56 // the local RemoveViewObjectContact with a search and removal in the
57 // vector, simply copy and clear local vector.
58 std::vector
< ViewObjectContact
* > aLocalVOCList(maViewObjectContactVector
);
59 maViewObjectContactVector
.clear();
61 while(!aLocalVOCList
.empty())
63 ViewObjectContact
* pCandidate
= aLocalVOCList
.back();
64 aLocalVOCList
.pop_back();
65 DBG_ASSERT(pCandidate
, "Corrupted ViewObjectContactList in VC (!)");
67 // ViewObjectContacts only make sense with View and Object contacts.
68 // When the contact to the SdrObject is deleted like in this case,
69 // all ViewObjectContacts can be deleted, too.
73 // assert when there were new entries added during deletion
74 DBG_ASSERT(maViewObjectContactVector
.empty(), "Corrupted ViewObjectContactList in VC (!)");
77 // get a Object-specific ViewObjectContact for a specific
78 // ObjectContact (->View). Always needs to return something.
79 ViewObjectContact
& ViewContact::GetViewObjectContact(ObjectContact
& rObjectContact
)
81 ViewObjectContact
* pRetval
= nullptr;
82 const sal_uInt32
nCount(maViewObjectContactVector
.size());
84 // first search if there exists a VOC for the given OC
85 for(sal_uInt32
a(0); !pRetval
&& a
< nCount
; a
++)
87 ViewObjectContact
* pCandidate
= maViewObjectContactVector
[a
];
88 DBG_ASSERT(pCandidate
, "Corrupted ViewObjectContactList (!)");
90 if(&(pCandidate
->GetObjectContact()) == &rObjectContact
)
98 // create a new one. It's inserted to the local list from the
99 // ViewObjectContact constructor via AddViewObjectContact()
100 pRetval
= &CreateObjectSpecificViewObjectContact(rObjectContact
);
106 // A new ViewObjectContact was created and shall be remembered.
107 void ViewContact::AddViewObjectContact(ViewObjectContact
& rVOContact
)
109 maViewObjectContactVector
.push_back(&rVOContact
);
112 // A ViewObjectContact was deleted and shall be forgotten.
113 void ViewContact::RemoveViewObjectContact(ViewObjectContact
& rVOContact
)
115 std::vector
< ViewObjectContact
* >::iterator aFindResult
= std::find(maViewObjectContactVector
.begin(), maViewObjectContactVector
.end(), &rVOContact
);
117 if(aFindResult
!= maViewObjectContactVector
.end())
119 maViewObjectContactVector
.erase(aFindResult
);
123 // Test if this ViewContact has ViewObjectContacts at all. This can
124 // be used to test if this ViewContact is visualized ATM or not
125 bool ViewContact::HasViewObjectContacts() const
127 const sal_uInt32
nCount(maViewObjectContactVector
.size());
129 for(sal_uInt32
a(0); a
< nCount
; a
++)
131 if(!maViewObjectContactVector
[a
]->GetObjectContact().IsPreviewRenderer())
139 // Test if this ViewContact has ViewObjectContacts at all. This can
140 // be used to test if this ViewContact is visualized ATM or not
141 bool ViewContact::isAnimatedInAnyViewObjectContact() const
143 const sal_uInt32
nCount(maViewObjectContactVector
.size());
145 for(sal_uInt32
a(0); a
< nCount
; a
++)
147 if(maViewObjectContactVector
[a
]->isAnimated())
156 // Access to possible sub-hierarchy and parent. GetObjectCount() default is 0L
157 // and GetViewContact default pops up an assert since it's an error if
158 // GetObjectCount has a result != 0 and it's not overridden.
159 sal_uInt32
ViewContact::GetObjectCount() const
165 ViewContact
& ViewContact::GetViewContact(sal_uInt32
/*nIndex*/) const
167 // This is the default implementation; call would be an error
168 OSL_FAIL("ViewContact::GetViewContact: This call needs to be overridden when GetObjectCount() can return results != 0 (!)");
169 return (ViewContact
&)(*this);
172 ViewContact
* ViewContact::GetParentContact() const
174 // default has no parent
178 void ViewContact::ActionChildInserted(ViewContact
& rChild
)
180 // propagate change to all exsisting visualisations which
181 // will force a VOC for the new child and invalidate its range
182 const sal_uInt32
nCount(maViewObjectContactVector
.size());
184 for(sal_uInt32
a(0); a
< nCount
; a
++)
186 ViewObjectContact
* pCandidate
= maViewObjectContactVector
[a
];
187 DBG_ASSERT(pCandidate
, "ViewContact::GetViewObjectContact() invalid ViewObjectContactList (!)");
189 // take action at all VOCs. At the VOCs ObjectContact the initial
190 // rectangle will be invalidated at the associated OutputDevice.
191 pCandidate
->ActionChildInserted(rChild
);
195 // React on changes of the object of this ViewContact
196 void ViewContact::ActionChanged()
198 // propagate change to all existing VOCs. This will invalidate
199 // all drawn visualisations in all known views
200 const sal_uInt32
nCount(maViewObjectContactVector
.size());
202 for(sal_uInt32
a(0); a
< nCount
; a
++)
204 ViewObjectContact
* pCandidate
= maViewObjectContactVector
[a
];
205 DBG_ASSERT(pCandidate
, "ViewContact::GetViewObjectContact() invalid ViewObjectContactList (!)");
207 pCandidate
->ActionChanged();
211 // access to SdrObject and/or SdrPage. May return 0L like the default
212 // implementations do. Override as needed.
213 SdrObject
* ViewContact::TryToGetSdrObject() const
220 drawinglayer::primitive2d::Primitive2DContainer
ViewContact::createViewIndependentPrimitive2DSequence() const
222 // This is the default implementation and should never be called (see header). If this is called,
223 // someone implemented a ViewContact (VC) visualisation object without defining the visualisation by
224 // providing a sequence of primitives -> which cannot be correct.
225 // Since we have no access to any known model data here, the default implementation creates a yellow placeholder
226 // hairline polygon with a default size of (1000, 1000, 5000, 3000)
227 OSL_FAIL("ViewContact::createViewIndependentPrimitive2DSequence(): Never call the fallback base implementation, this is always an error (!)");
228 const basegfx::B2DPolygon
aOutline(basegfx::tools::createPolygonFromRect(basegfx::B2DRange(1000.0, 1000.0, 5000.0, 3000.0)));
229 const basegfx::BColor
aYellow(1.0, 1.0, 0.0);
230 const drawinglayer::primitive2d::Primitive2DReference
xReference(
231 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(aOutline
, aYellow
));
233 return drawinglayer::primitive2d::Primitive2DContainer
{ xReference
};
236 drawinglayer::primitive2d::Primitive2DContainer
const & ViewContact::getViewIndependentPrimitive2DContainer() const
238 // local up-to-date checks. Create new list and compare.
239 drawinglayer::primitive2d::Primitive2DContainer
xNew(createViewIndependentPrimitive2DSequence());
243 // allow evtl. embedding in object-specific infos, e.g. Name, Title, Description
244 xNew
= embedToObjectSpecificInformation(xNew
);
247 if(mxViewIndependentPrimitive2DSequence
!= xNew
)
249 // has changed, copy content
250 const_cast< ViewContact
* >(this)->mxViewIndependentPrimitive2DSequence
= xNew
;
253 // return current Primitive2DContainer
254 return mxViewIndependentPrimitive2DSequence
;
257 // add Gluepoints (if available)
258 drawinglayer::primitive2d::Primitive2DContainer
ViewContact::createGluePointPrimitive2DSequence() const
260 // default returns empty reference
261 return drawinglayer::primitive2d::Primitive2DContainer();
264 drawinglayer::primitive2d::Primitive2DContainer
ViewContact::embedToObjectSpecificInformation(const drawinglayer::primitive2d::Primitive2DContainer
& rSource
) const
266 // nothing to do for default
270 basegfx::B2DRange
ViewContact::getRange( const drawinglayer::geometry::ViewInformation2D
& /*rViewInfo2D*/ ) const
272 // Return empty range.
273 return basegfx::B2DRange();
276 void ViewContact::flushViewObjectContacts(bool bWithHierarchy
)
280 // flush DrawingLayer hierarchy
281 const sal_uInt32
nCount(GetObjectCount());
283 for(sal_uInt32
a(0); a
< nCount
; a
++)
285 ViewContact
& rChild
= GetViewContact(a
);
286 rChild
.flushViewObjectContacts(bWithHierarchy
);
296 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */