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 <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 <svx/sdr/contact/objectcontactofpageview.hxx>
29 #include <tools/debug.hxx>
30 
31 namespace sdr { namespace contact {
32 
33 // Create an 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
CreateObjectSpecificViewObjectContact(ObjectContact & rObjectContact)36 ViewObjectContact& ViewContact::CreateObjectSpecificViewObjectContact(ObjectContact& rObjectContact)
37 {
38     return *(new ViewObjectContact(rObjectContact, *this));
39 }
40 
ViewContact()41 ViewContact::ViewContact()
42 :   maViewObjectContactVector(),
43     mxViewIndependentPrimitive2DSequence()
44 {
45 }
46 
~ViewContact()47 ViewContact::~ViewContact()
48 {
49     deleteAllVOCs();
50 }
51 
deleteAllVOCs()52 void ViewContact::deleteAllVOCs()
53 {
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;
59     aLocalVOCList.swap(maViewObjectContactVector);
60 
61     for (const auto & pCandidate : aLocalVOCList)
62         // ViewObjectContacts only make sense with View and Object contacts.
63         // When the contact to the SdrObject is deleted like in this case,
64         // all ViewObjectContacts can be deleted, too.
65         delete pCandidate;
66 
67     // assert when there were new entries added during deletion
68     DBG_ASSERT(maViewObjectContactVector.empty(), "Corrupted ViewObjectContactList in VC (!)");
69 }
70 
71 // get an Object-specific ViewObjectContact for a specific
72 // ObjectContact (->View). Always needs to return something.
GetViewObjectContact(ObjectContact & rObjectContact)73 ViewObjectContact& ViewContact::GetViewObjectContact(ObjectContact& rObjectContact)
74 {
75     ViewObjectContact* pRetval = nullptr;
76     const sal_uInt32 nCount(maViewObjectContactVector.size());
77 
78     // first search if there exists a VOC for the given OC
79     for(sal_uInt32 a(0); !pRetval && a < nCount; a++)
80     {
81         ViewObjectContact* pCandidate = maViewObjectContactVector[a];
82         DBG_ASSERT(pCandidate, "Corrupted ViewObjectContactList (!)");
83 
84         if(&(pCandidate->GetObjectContact()) == &rObjectContact)
85         {
86             pRetval = pCandidate;
87         }
88     }
89 
90     if(!pRetval)
91     {
92         // create a new one. It's inserted to the local list from the
93         // ViewObjectContact constructor via AddViewObjectContact()
94         pRetval = &CreateObjectSpecificViewObjectContact(rObjectContact);
95     }
96 
97     return *pRetval;
98 }
99 
100 // A new ViewObjectContact was created and shall be remembered.
AddViewObjectContact(ViewObjectContact & rVOContact)101 void ViewContact::AddViewObjectContact(ViewObjectContact& rVOContact)
102 {
103     maViewObjectContactVector.push_back(&rVOContact);
104 }
105 
106 // A ViewObjectContact was deleted and shall be forgotten.
RemoveViewObjectContact(ViewObjectContact & rVOContact)107 void ViewContact::RemoveViewObjectContact(ViewObjectContact& rVOContact)
108 {
109     std::vector< ViewObjectContact* >::iterator aFindResult = std::find(maViewObjectContactVector.begin(), maViewObjectContactVector.end(), &rVOContact);
110 
111     if(aFindResult != maViewObjectContactVector.end())
112     {
113         maViewObjectContactVector.erase(aFindResult);
114     }
115 }
116 
117 // Test if this ViewContact has ViewObjectContacts at all. This can
118 // be used to test if this ViewContact is visualized ATM or not
HasViewObjectContacts() const119 bool ViewContact::HasViewObjectContacts() const
120 {
121     const sal_uInt32 nCount(maViewObjectContactVector.size());
122 
123     for(sal_uInt32 a(0); a < nCount; a++)
124     {
125         if(!maViewObjectContactVector[a]->GetObjectContact().IsPreviewRenderer())
126         {
127             return true;
128         }
129     }
130     return false;
131 }
132 
133 // Test if this ViewContact has ViewObjectContacts at all. This can
134 // be used to test if this ViewContact is visualized ATM or not
isAnimatedInAnyViewObjectContact() const135 bool ViewContact::isAnimatedInAnyViewObjectContact() const
136 {
137     const sal_uInt32 nCount(maViewObjectContactVector.size());
138 
139     for(sal_uInt32 a(0); a < nCount; a++)
140     {
141         if(maViewObjectContactVector[a]->isAnimated())
142         {
143             return true;
144         }
145     }
146 
147     return false;
148 }
149 
150 // Access to possible sub-hierarchy and parent. GetObjectCount() default is 0L
151 // and GetViewContact default pops up an assert since it's an error if
152 // GetObjectCount has a result != 0 and it's not overridden.
GetObjectCount() const153 sal_uInt32 ViewContact::GetObjectCount() const
154 {
155     // no sub-objects
156     return 0;
157 }
158 
GetViewContact(sal_uInt32) const159 ViewContact& ViewContact::GetViewContact(sal_uInt32 /*nIndex*/) const
160 {
161     // This is the default implementation; call would be an error
162     OSL_FAIL("ViewContact::GetViewContact: This call needs to be overridden when GetObjectCount() can return results != 0 (!)");
163     return const_cast<ViewContact&>(*this);
164 }
165 
GetParentContact() const166 ViewContact* ViewContact::GetParentContact() const
167 {
168     // default has no parent
169     return nullptr;
170 }
171 
ActionChildInserted(ViewContact & rChild)172 void ViewContact::ActionChildInserted(ViewContact& rChild)
173 {
174     // propagate change to all existing visualisations which
175     // will force a VOC for the new child and invalidate its range
176     const sal_uInt32 nCount(maViewObjectContactVector.size());
177 
178     for(sal_uInt32 a(0); a < nCount; a++)
179     {
180         ViewObjectContact* pCandidate = maViewObjectContactVector[a];
181         DBG_ASSERT(pCandidate, "ViewContact::GetViewObjectContact() invalid ViewObjectContactList (!)");
182 
183         // take action at all VOCs. At the VOCs ObjectContact the initial
184         // rectangle will be invalidated at the associated OutputDevice.
185         pCandidate->ActionChildInserted(rChild);
186     }
187 }
188 
189 // React on changes of the object of this ViewContact
ActionChanged()190 void ViewContact::ActionChanged()
191 {
192     // propagate change to all existing VOCs. This will invalidate
193     // all drawn visualisations in all known views
194     const sal_uInt32 nCount(maViewObjectContactVector.size());
195 
196     for(sal_uInt32 a(0); a < nCount; a++)
197     {
198         ViewObjectContact* pCandidate = maViewObjectContactVector[a];
199         DBG_ASSERT(pCandidate, "ViewContact::GetViewObjectContact() invalid ViewObjectContactList (!)");
200 
201         pCandidate->ActionChanged();
202     }
203 }
204 
205 // access to SdrObject and/or SdrPage. May return 0L like the default
206 // implementations do. Override as needed.
TryToGetSdrObject() const207 SdrObject* ViewContact::TryToGetSdrObject() const
208 {
209     return nullptr;
210 }
211 
212 // primitive stuff
213 
createViewIndependentPrimitive2DSequence() const214 drawinglayer::primitive2d::Primitive2DContainer ViewContact::createViewIndependentPrimitive2DSequence() const
215 {
216     // This is the default implementation and should never be called (see header). If this is called,
217     // someone implemented a ViewContact (VC) visualisation object without defining the visualisation by
218     // providing a sequence of primitives -> which cannot be correct.
219     // Since we have no access to any known model data here, the default implementation creates a yellow placeholder
220     // hairline polygon with a default size of (1000, 1000, 5000, 3000)
221     OSL_FAIL("ViewContact::createViewIndependentPrimitive2DSequence(): Never call the fallback base implementation, this is always an error (!)");
222     const basegfx::B2DPolygon aOutline(basegfx::utils::createPolygonFromRect(basegfx::B2DRange(1000.0, 1000.0, 5000.0, 3000.0)));
223     const basegfx::BColor aYellow(1.0, 1.0, 0.0);
224     const drawinglayer::primitive2d::Primitive2DReference xReference(
225         new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(aOutline, aYellow));
226 
227     return drawinglayer::primitive2d::Primitive2DContainer { xReference };
228 }
229 
getViewIndependentPrimitive2DContainer() const230 drawinglayer::primitive2d::Primitive2DContainer const & ViewContact::getViewIndependentPrimitive2DContainer() const
231 {
232     // local up-to-date checks. Create new list and compare.
233     drawinglayer::primitive2d::Primitive2DContainer xNew(createViewIndependentPrimitive2DSequence());
234 
235     if(!xNew.empty())
236     {
237         // allow evtl. embedding in object-specific infos, e.g. Name, Title, Description
238         xNew = embedToObjectSpecificInformation(std::move(xNew));
239     }
240 
241     if(mxViewIndependentPrimitive2DSequence != xNew)
242     {
243         // has changed, copy content
244         const_cast< ViewContact* >(this)->mxViewIndependentPrimitive2DSequence = std::move(xNew);
245     }
246 
247     // return current Primitive2DContainer
248     return mxViewIndependentPrimitive2DSequence;
249 }
250 
251 // add Gluepoints (if available)
createGluePointPrimitive2DSequence() const252 drawinglayer::primitive2d::Primitive2DContainer ViewContact::createGluePointPrimitive2DSequence() const
253 {
254     // default returns empty reference
255     return drawinglayer::primitive2d::Primitive2DContainer();
256 }
257 
embedToObjectSpecificInformation(drawinglayer::primitive2d::Primitive2DContainer aSource) const258 drawinglayer::primitive2d::Primitive2DContainer ViewContact::embedToObjectSpecificInformation(drawinglayer::primitive2d::Primitive2DContainer aSource) const
259 {
260     // nothing to do for default
261     return aSource;
262 }
263 
getRange(const drawinglayer::geometry::ViewInformation2D &) const264 basegfx::B2DRange ViewContact::getRange( const drawinglayer::geometry::ViewInformation2D& /*rViewInfo2D*/ ) const
265 {
266     // Return empty range.
267     return basegfx::B2DRange();
268 }
269 
flushViewObjectContacts(bool bWithHierarchy)270 void ViewContact::flushViewObjectContacts(bool bWithHierarchy)
271 {
272     if(bWithHierarchy)
273     {
274         // flush DrawingLayer hierarchy
275         const sal_uInt32 nCount(GetObjectCount());
276 
277         for(sal_uInt32 a(0); a < nCount; a++)
278         {
279             ViewContact& rChild = GetViewContact(a);
280             rChild.flushViewObjectContacts(bWithHierarchy);
281         }
282     }
283 
284     // delete local VOCs
285     deleteAllVOCs();
286 }
287 
288 }}
289 
290 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
291