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 0 : ViewContact::ViewContact()
46 : : maViewObjectContactVector(),
47 0 : mxViewIndependentPrimitive2DSequence()
48 : {
49 0 : }
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 0 : void ViewContact::StartGettingViewed()
55 : {
56 0 : }
57 :
58 0 : void ViewContact::StopGettingViewed()
59 : {
60 0 : }
61 :
62 0 : ViewContact::~ViewContact()
63 : {
64 0 : deleteAllVOCs();
65 0 : }
66 :
67 0 : 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 0 : std::vector< ViewObjectContact* > aLocalVOCList(maViewObjectContactVector);
74 0 : maViewObjectContactVector.clear();
75 :
76 0 : 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 0 : DBG_ASSERT(maViewObjectContactVector.empty(), "Corrupted ViewObjectContactList in VC (!)");
90 0 : }
91 :
92 : // get a Object-specific ViewObjectContact for a specific
93 : // ObjectContact (->View). Always needs to return something.
94 0 : ViewObjectContact& ViewContact::GetViewObjectContact(ObjectContact& rObjectContact)
95 : {
96 0 : ViewObjectContact* pRetval = 0L;
97 0 : const sal_uInt32 nCount(maViewObjectContactVector.size());
98 :
99 : // first search if there exists a VOC for the given OC
100 0 : for(sal_uInt32 a(0); !pRetval && a < nCount; a++)
101 : {
102 0 : ViewObjectContact* pCandidate = maViewObjectContactVector[a];
103 : DBG_ASSERT(pCandidate, "Corrupted ViewObjectContactList (!)");
104 :
105 0 : if(&(pCandidate->GetObjectContact()) == &rObjectContact)
106 : {
107 0 : pRetval = pCandidate;
108 : }
109 : }
110 :
111 0 : if(!pRetval)
112 : {
113 : // create a new one. It's inserted to the local list from the
114 : // ViewObjectContact constructor via AddViewObjectContact()
115 0 : pRetval = &CreateObjectSpecificViewObjectContact(rObjectContact);
116 : }
117 :
118 0 : return *pRetval;
119 : }
120 :
121 : // A new ViewObjectContact was created and shall be remembered.
122 0 : void ViewContact::AddViewObjectContact(ViewObjectContact& rVOContact)
123 : {
124 0 : maViewObjectContactVector.push_back(&rVOContact);
125 :
126 0 : if(1L == maViewObjectContactVector.size())
127 : {
128 0 : StartGettingViewed();
129 : }
130 0 : }
131 :
132 : // A ViewObjectContact was deleted and shall be forgotten.
133 0 : void ViewContact::RemoveViewObjectContact(ViewObjectContact& rVOContact)
134 : {
135 0 : std::vector< ViewObjectContact* >::iterator aFindResult = std::find(maViewObjectContactVector.begin(), maViewObjectContactVector.end(), &rVOContact);
136 :
137 0 : if(aFindResult != maViewObjectContactVector.end())
138 : {
139 0 : maViewObjectContactVector.erase(aFindResult);
140 :
141 0 : 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 0 : StopGettingViewed();
146 : }
147 : }
148 0 : }
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 0 : bool ViewContact::HasViewObjectContacts(bool bExcludePreviews) const
153 : {
154 0 : const sal_uInt32 nCount(maViewObjectContactVector.size());
155 :
156 0 : if(bExcludePreviews)
157 : {
158 0 : 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 0 : 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 0 : sal_uInt32 ViewContact::GetObjectCount() const
195 : {
196 : // no sub-objects
197 0 : 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 0 : 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 its range
217 0 : const sal_uInt32 nCount(maViewObjectContactVector.size());
218 :
219 0 : 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 0 : }
229 :
230 : // React on changes of the object of this ViewContact
231 0 : void ViewContact::ActionChanged()
232 : {
233 : // propagate change to all existing VOCs. This will invalidate
234 : // all drawn visualisations in all known views
235 0 : const sal_uInt32 nCount(maViewObjectContactVector.size());
236 :
237 0 : for(sal_uInt32 a(0); a < nCount; a++)
238 : {
239 0 : ViewObjectContact* pCandidate = maViewObjectContactVector[a];
240 : DBG_ASSERT(pCandidate, "ViewContact::GetViewObjectContact() invalid ViewObjectContactList (!)");
241 :
242 0 : pCandidate->ActionChanged();
243 : }
244 0 : }
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 0 : drawinglayer::primitive2d::Primitive2DSequence ViewContact::getViewIndependentPrimitive2DSequence() const
278 : {
279 : // local up-to-date checks. Create new list and compare.
280 0 : drawinglayer::primitive2d::Primitive2DSequence xNew(createViewIndependentPrimitive2DSequence());
281 :
282 0 : if(xNew.hasElements())
283 : {
284 : // allow evtl. embedding in object-specific infos, e.g. Name, Title, Description
285 0 : xNew = embedToObjectSpecificInformation(xNew);
286 : }
287 :
288 0 : if(!drawinglayer::primitive2d::arePrimitive2DSequencesEqual(mxViewIndependentPrimitive2DSequence, xNew))
289 : {
290 : // has changed, copy content
291 0 : const_cast< ViewContact* >(this)->mxViewIndependentPrimitive2DSequence = xNew;
292 : }
293 :
294 : // return current Primitive2DSequence
295 0 : return mxViewIndependentPrimitive2DSequence;
296 : }
297 :
298 : // add Gluepoints (if available)
299 0 : drawinglayer::primitive2d::Primitive2DSequence ViewContact::createGluePointPrimitive2DSequence() const
300 : {
301 : // default returns empty reference
302 0 : return drawinglayer::primitive2d::Primitive2DSequence();
303 : }
304 :
305 0 : drawinglayer::primitive2d::Primitive2DSequence ViewContact::embedToObjectSpecificInformation(const drawinglayer::primitive2d::Primitive2DSequence& rSource) const
306 : {
307 : // nothing to do for default
308 0 : return rSource;
309 : }
310 :
311 0 : void ViewContact::flushViewObjectContacts(bool bWithHierarchy)
312 : {
313 0 : if(bWithHierarchy)
314 : {
315 : // flush DrawingLayer hierarchy
316 0 : const sal_uInt32 nCount(GetObjectCount());
317 :
318 0 : for(sal_uInt32 a(0); a < nCount; a++)
319 : {
320 0 : ViewContact& rChild = GetViewContact(a);
321 0 : rChild.flushViewObjectContacts(bWithHierarchy);
322 : }
323 : }
324 :
325 : // delete local VOCs
326 0 : deleteAllVOCs();
327 0 : }
328 : } // end of namespace contact
329 : } // end of namespace sdr
330 :
331 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|