Line data Source code
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 : //////////////////////////////////////////////////////////////////////////////
32 :
33 : namespace sdr
34 : {
35 : namespace contact
36 : {
37 : // Create a Object-Specific ViewObjectContact, set ViewContact and
38 : // ObjectContact. Always needs to return something. Default is to create
39 : // a standard ViewObjectContact containing the given ObjectContact and *this
40 0 : ViewObjectContact& ViewContact::CreateObjectSpecificViewObjectContact(ObjectContact& rObjectContact)
41 : {
42 0 : return *(new ViewObjectContact(rObjectContact, *this));
43 : }
44 :
45 12425 : ViewContact::ViewContact()
46 : : maViewObjectContactVector(),
47 12425 : mxViewIndependentPrimitive2DSequence()
48 : {
49 12425 : }
50 :
51 : // Methods to react on start getting viewed or stop getting
52 : // viewed. This info is derived from the count of members of
53 : // registered ViewObjectContacts. Default does nothing.
54 1740 : void ViewContact::StartGettingViewed()
55 : {
56 1740 : }
57 :
58 118 : void ViewContact::StopGettingViewed()
59 : {
60 118 : }
61 :
62 19676 : ViewContact::~ViewContact()
63 : {
64 9838 : deleteAllVOCs();
65 9838 : }
66 :
67 30549 : void ViewContact::deleteAllVOCs()
68 : {
69 : // get rid of all VOCs
70 : // #i84257# To avoid that each 'delete pCandidate' again uses
71 : // the local RemoveViewObjectContact with a search and removal in the
72 : // vector, simply copy and clear local vector.
73 30549 : std::vector< ViewObjectContact* > aLocalVOCList(maViewObjectContactVector);
74 30549 : maViewObjectContactVector.clear();
75 :
76 61098 : while(!aLocalVOCList.empty())
77 : {
78 0 : ViewObjectContact* pCandidate = aLocalVOCList.back();
79 0 : aLocalVOCList.pop_back();
80 : DBG_ASSERT(pCandidate, "Corrupted ViewObjectContactList in VC (!)");
81 :
82 : // ViewObjectContacts only make sense with View and Object contacts.
83 : // When the contact to the SdrObject is deleted like in this case,
84 : // all ViewObjectContacts can be deleted, too.
85 0 : delete pCandidate;
86 : }
87 :
88 : // assert when there were new entries added during deletion
89 30549 : DBG_ASSERT(maViewObjectContactVector.empty(), "Corrupted ViewObjectContactList in VC (!)");
90 30549 : }
91 :
92 : // get a Object-specific ViewObjectContact for a specific
93 : // ObjectContact (->View). Always needs to return something.
94 10561 : ViewObjectContact& ViewContact::GetViewObjectContact(ObjectContact& rObjectContact)
95 : {
96 10561 : ViewObjectContact* pRetval = 0L;
97 10561 : const sal_uInt32 nCount(maViewObjectContactVector.size());
98 :
99 : // first search if there exists a VOC for the given OC
100 19382 : for(sal_uInt32 a(0); !pRetval && a < nCount; a++)
101 : {
102 8821 : ViewObjectContact* pCandidate = maViewObjectContactVector[a];
103 : DBG_ASSERT(pCandidate, "Corrupted ViewObjectContactList (!)");
104 :
105 8821 : if(&(pCandidate->GetObjectContact()) == &rObjectContact)
106 : {
107 8821 : pRetval = pCandidate;
108 : }
109 : }
110 :
111 10561 : if(!pRetval)
112 : {
113 : // create a new one. It's inserted to the local list from the
114 : // ViewObjectContact constructor via AddViewObjectContact()
115 1740 : pRetval = &CreateObjectSpecificViewObjectContact(rObjectContact);
116 : }
117 :
118 10561 : return *pRetval;
119 : }
120 :
121 : // A new ViewObjectContact was created and shall be remembered.
122 1740 : void ViewContact::AddViewObjectContact(ViewObjectContact& rVOContact)
123 : {
124 1740 : maViewObjectContactVector.push_back(&rVOContact);
125 :
126 1740 : if(1L == maViewObjectContactVector.size())
127 : {
128 1740 : StartGettingViewed();
129 : }
130 1740 : }
131 :
132 : // A ViewObjectContact was deleted and shall be forgotten.
133 118 : void ViewContact::RemoveViewObjectContact(ViewObjectContact& rVOContact)
134 : {
135 118 : std::vector< ViewObjectContact* >::iterator aFindResult = std::find(maViewObjectContactVector.begin(), maViewObjectContactVector.end(), &rVOContact);
136 :
137 118 : if(aFindResult != maViewObjectContactVector.end())
138 : {
139 118 : maViewObjectContactVector.erase(aFindResult);
140 :
141 118 : if(maViewObjectContactVector.empty())
142 : {
143 : // This may need to get asynchron later since it eventually triggers
144 : // deletes of OCs where the VOC is still added.
145 118 : StopGettingViewed();
146 : }
147 : }
148 118 : }
149 :
150 : // Test if this ViewContact has ViewObjectContacts at all. This can
151 : // be used to test if this ViewContact is visualized ATM or not
152 2 : bool ViewContact::HasViewObjectContacts(bool bExcludePreviews) const
153 : {
154 2 : const sal_uInt32 nCount(maViewObjectContactVector.size());
155 :
156 2 : if(bExcludePreviews)
157 : {
158 2 : for(sal_uInt32 a(0); a < nCount; a++)
159 : {
160 0 : if(!maViewObjectContactVector[a]->GetObjectContact().IsPreviewRenderer())
161 : {
162 0 : return true;
163 : }
164 : }
165 :
166 2 : return false;
167 : }
168 : else
169 : {
170 0 : return (0L != nCount);
171 : }
172 : }
173 :
174 : // Test if this ViewContact has ViewObjectContacts at all. This can
175 : // be used to test if this ViewContact is visualized ATM or not
176 0 : bool ViewContact::isAnimatedInAnyViewObjectContact() const
177 : {
178 0 : const sal_uInt32 nCount(maViewObjectContactVector.size());
179 :
180 0 : for(sal_uInt32 a(0); a < nCount; a++)
181 : {
182 0 : if(maViewObjectContactVector[a]->isAnimated())
183 : {
184 0 : return true;
185 : }
186 : }
187 :
188 0 : return false;
189 : }
190 :
191 : // Access to possible sub-hierarchy and parent. GetObjectCount() default is 0L
192 : // and GetViewContact default pops up an assert since it's an error if
193 : // GetObjectCount has a result != 0 and it's not overloaded.
194 1210 : sal_uInt32 ViewContact::GetObjectCount() const
195 : {
196 : // no sub-objects
197 1210 : return 0;
198 : }
199 :
200 0 : ViewContact& ViewContact::GetViewContact(sal_uInt32 /*nIndex*/) const
201 : {
202 : // This is the default implementation; call would be an error
203 : OSL_FAIL("ViewContact::GetViewContact: This call needs to be overloaded when GetObjectCount() can return results != 0 (!)");
204 0 : return (ViewContact&)(*this);
205 : }
206 :
207 0 : ViewContact* ViewContact::GetParentContact() const
208 : {
209 : // default has no parent
210 0 : return 0;
211 : }
212 :
213 5535 : void ViewContact::ActionChildInserted(ViewContact& rChild)
214 : {
215 : // propagate change to all exsisting visualisations which
216 : // will force a VOC for the new child and invalidate it's range
217 5535 : const sal_uInt32 nCount(maViewObjectContactVector.size());
218 :
219 5535 : for(sal_uInt32 a(0); a < nCount; a++)
220 : {
221 0 : ViewObjectContact* pCandidate = maViewObjectContactVector[a];
222 : DBG_ASSERT(pCandidate, "ViewContact::GetViewObjectContact() invalid ViewObjectContactList (!)");
223 :
224 : // take action at all VOCs. At the VOCs ObjectContact the initial
225 : // rectangle will be invalidated at the associated OutputDevice.
226 0 : pCandidate->ActionChildInserted(rChild);
227 : }
228 5535 : }
229 :
230 : // React on changes of the object of this ViewContact
231 74988 : void ViewContact::ActionChanged()
232 : {
233 : // propagate change to all existing VOCs. This will invalidate
234 : // all drawn visualisations in all known views
235 74988 : const sal_uInt32 nCount(maViewObjectContactVector.size());
236 :
237 75000 : for(sal_uInt32 a(0); a < nCount; a++)
238 : {
239 12 : ViewObjectContact* pCandidate = maViewObjectContactVector[a];
240 : DBG_ASSERT(pCandidate, "ViewContact::GetViewObjectContact() invalid ViewObjectContactList (!)");
241 :
242 12 : pCandidate->ActionChanged();
243 : }
244 74988 : }
245 :
246 : // access to SdrObject and/or SdrPage. May return 0L like the default
247 : // implementations do. Needs to be overloaded as needed.
248 0 : SdrObject* ViewContact::TryToGetSdrObject() const
249 : {
250 0 : return 0L;
251 : }
252 :
253 0 : SdrPage* ViewContact::TryToGetSdrPage() const
254 : {
255 0 : return 0L;
256 : }
257 :
258 : //////////////////////////////////////////////////////////////////////////////
259 : // primitive stuff
260 :
261 0 : drawinglayer::primitive2d::Primitive2DSequence ViewContact::createViewIndependentPrimitive2DSequence() const
262 : {
263 : // This is the default implementation and should never be called (see header). If this is called,
264 : // someone implemented a ViewContact (VC) visualisation object without defining the visualisation by
265 : // providing a seqence of primitives -> which cannot be correct.
266 : // Since we have no access to any known model data here, the default implementation creates a yellow placeholder
267 : // hairline polygon with a default size of (1000, 1000, 5000, 3000)
268 : OSL_FAIL("ViewContact::createViewIndependentPrimitive2DSequence(): Never call the fallback base implementation, this is always an error (!)");
269 0 : const basegfx::B2DPolygon aOutline(basegfx::tools::createPolygonFromRect(basegfx::B2DRange(1000.0, 1000.0, 5000.0, 3000.0)));
270 0 : const basegfx::BColor aYellow(1.0, 1.0, 0.0);
271 : const drawinglayer::primitive2d::Primitive2DReference xReference(
272 0 : new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(aOutline, aYellow));
273 :
274 0 : return drawinglayer::primitive2d::Primitive2DSequence(&xReference, 1);
275 : }
276 :
277 862 : drawinglayer::primitive2d::Primitive2DSequence ViewContact::getViewIndependentPrimitive2DSequence() const
278 : {
279 : // local up-to-date checks. Create new list and compare.
280 862 : const drawinglayer::primitive2d::Primitive2DSequence xNew(createViewIndependentPrimitive2DSequence());
281 :
282 862 : if(!drawinglayer::primitive2d::arePrimitive2DSequencesEqual(mxViewIndependentPrimitive2DSequence, xNew))
283 : {
284 : // has changed, copy content
285 436 : const_cast< ViewContact* >(this)->mxViewIndependentPrimitive2DSequence = xNew;
286 : }
287 :
288 : // return current Primitive2DSequence
289 862 : return mxViewIndependentPrimitive2DSequence;
290 : }
291 :
292 : // add Gluepoints (if available)
293 0 : drawinglayer::primitive2d::Primitive2DSequence ViewContact::createGluePointPrimitive2DSequence() const
294 : {
295 : // default returns empty reference
296 0 : return drawinglayer::primitive2d::Primitive2DSequence();
297 : }
298 :
299 20711 : void ViewContact::flushViewObjectContacts(bool bWithHierarchy)
300 : {
301 20711 : if(bWithHierarchy)
302 : {
303 : // flush DrawingLayer hierarchy
304 20711 : const sal_uInt32 nCount(GetObjectCount());
305 :
306 35970 : for(sal_uInt32 a(0); a < nCount; a++)
307 : {
308 15259 : ViewContact& rChild = GetViewContact(a);
309 15259 : rChild.flushViewObjectContacts(bWithHierarchy);
310 : }
311 : }
312 :
313 : // delete local VOCs
314 20711 : deleteAllVOCs();
315 20711 : }
316 : } // end of namespace contact
317 : } // end of namespace sdr
318 :
319 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|