CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
FWProxyBuilderBase.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWProxyBuilderBase
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author: Chris Jones, Matevz Tadel, Alja Mrak-Tadel
10 // Created: Thu Mar 18 14:12:00 CET 2010
11 // $Id: FWProxyBuilderBase.cc,v 1.32 2010/10/29 10:42:02 amraktad Exp $
12 //
13 
14 // system include files
15 #include <iostream>
16 #include <boost/bind.hpp>
17 
18 // user include files
19 #include "TEveElement.h"
20 #include "TEveCompound.h"
21 #include "TEveManager.h"
22 #include "TEveProjectionManager.h"
23 #include "TEveSelection.h"
24 
31 
32 //
33 // constants, enums and typedefs
34 //
35 
36 //
37 // static data member definitions
38 //
39 
40 //
41 // constructors and destructor
42 //
43 
44 
45 FWProxyBuilderBase::Product::Product(FWViewType::EType t, const FWViewContext* c) : m_viewType(t), m_viewContext(c), m_elements(0)
46 {
47  m_elements = new TEveElementList("ProxyProduct");
48  m_elements->IncDenyDestroy();
49 }
50 
51 
53 {
54  // remove product from projected scene (RhoPhi or RhoZ)
55  TEveProjectable* pable = dynamic_cast<TEveProjectable*>(m_elements);
56  // don't have to check cast, because TEveElementList is TEveProjectable
57  for (TEveProjectable::ProjList_i i = pable->BeginProjecteds(); i != pable->EndProjecteds(); ++i)
58  {
59  TEveElement* projected = (*i)->GetProjectedAsElement();
60  (*projected->BeginParents())->RemoveElement(projected);
61  }
62 
63  // remove from 3D scenes
64  while (m_elements->HasParents())
65  {
66  TEveElement* parent = *m_elements->BeginParents();
67  parent->RemoveElement(m_elements);
68  }
69 
70  m_elements->Annihilate();
71 }
72 
73 //______________________________________________________________________________
74 
77  m_item(0),
81  m_layer(0)
82 {
83 }
84 
86 {
87  m_products.clear();
88 }
89 
90 //
91 // member functions
92 //
93 
94 void
96 {
97  m_item = iItem;
98 }
99 
100 void
102 {
103  bool oldValue = m_haveWindow;
104  m_haveWindow=iFlag;
105 
106  if(iFlag && !oldValue) {
107  //this is our first view so may need to rerun our building
108  if(m_mustBuild) {
109  build();
110  }
111  }
112 }
113 
114 void
116 {
117  m_item=0;
118 
119  cleanLocal();
120 
121  for (Product_it i = m_products.begin(); i!= m_products.end(); i++)
122  {
123 
124  (*i)->m_scaleConnection.disconnect();
125  delete (*i);
126  }
127 
128  m_products.clear();
129 }
130 
131 void
133 {
134  if (m_item)
135  {
136  try
137  {
138  size_t itemSize = m_item->size(); //cashed
139 
140  clean();
141  for (Product_it i = m_products.begin(); i != m_products.end(); ++i)
142  {
143  // printf("build() %s \n", m_item->name().c_str());
144  TEveElementList* elms = (*i)->m_elements;
145  size_t oldSize = elms->NumChildren();
146 
147  if (haveSingleProduct())
148  {
149  build(m_item, elms, (*i)->m_viewContext);
150  }
151  else
152  {
153  buildViewType(m_item, elms, (*i)->m_viewType, (*i)->m_viewContext);
154  }
155 
156  // Project all children of current product.
157  // If product is not registered into any projection-manager,
158  // this does nothing.
159  TEveProjectable* pable = dynamic_cast<TEveProjectable*>(elms);
160  if (pable->HasProjecteds())
161  {
162  for (TEveProjectable::ProjList_i i = pable->BeginProjecteds(); i != pable->EndProjecteds(); ++i)
163  {
164  TEveProjectionManager *pmgr = (*i)->GetManager();
165  Float_t oldDepth = pmgr->GetCurrentDepth();
166  pmgr->SetCurrentDepth(item()->layer());
167  size_t cnt = 0;
168 
169  TEveElement* projectedAsElement = (*i)->GetProjectedAsElement();
170  TEveElement::List_i parentIt = projectedAsElement->BeginChildren();
171  for (TEveElement::List_i prodIt = elms->BeginChildren(); prodIt != elms->EndChildren(); ++prodIt, ++cnt)
172  {
173  if (cnt < oldSize)
174  {
175  // reused projected holder
176  pmgr->SubImportChildren(*prodIt, *parentIt);
177  ++parentIt;
178  }
179  else if (cnt < itemSize)
180  {
181  // new product holder
182  pmgr->SubImportElements(*prodIt, projectedAsElement);
183  }
184  else
185  {
186  break;
187  }
188  }
189  pmgr->SetCurrentDepth(oldDepth);
190  }
191  }
192 
193 
194  if (m_interactionList && itemSize > oldSize)
195  {
196  TEveElement::List_i elIt = elms->BeginChildren();
197  for (size_t cnt = 0; cnt < itemSize; ++cnt, ++elIt)
198  {
199  if (cnt >= oldSize )
200  m_interactionList->added(*elIt, cnt);
201  }
202  }
203  }
204  }
205  catch (const std::runtime_error& iException)
206  {
207  fwLog(fwlog::kError) << "Caught exception in build function for item " << m_item->name() << ":\n"
208  << iException.what() << std::endl;
209  exit(1);
210  }
211  }
212  m_mustBuild = false;
213 }
214 
215 //______________________________________________________________________________
216 void
218 {
219  TEveElementList* elms = p->m_elements;
220  assert(m_item && static_cast<int>(m_item->size()) <= elms->NumChildren() && "can not use default modelChanges implementation");
221 
222  TEveElement::List_i itElement = elms->BeginChildren();
223  int index = 0;
224  for (FWModelIds::const_iterator it = iIds.begin(), itEnd = iIds.end();
225  it != itEnd;
226  ++it,++itElement,++index)
227  {
228  assert(itElement != elms->EndChildren());
229  while (index < it->index())
230  {
231  ++itElement;
232  ++index;
233  assert(itElement != elms->EndChildren());
234  }
235  if (visibilityModelChanges(*it, *itElement, p->m_viewType, p->m_viewContext))
236  {
237  elms->ProjectChild(*itElement);
238  }
239  else
240  {
241  localModelChanges(*it, *itElement, p->m_viewType, p->m_viewContext);
242  }
243  }
244 }
245 
246 void
248 {
249  if(m_haveWindow) {
250  for (Product_it i = m_products.begin(); i!= m_products.end(); ++i)
251  {
252  modelChanges(iIds, *i);
253  }
254  m_modelsChanged=false;
255  } else {
256  m_modelsChanged=true;
257  }
258 }
259 
260 //______________________________________________________________________________
261 void
263 {
264  if (iItem->layer() != m_layer)
265  setProjectionLayer(iItem->layer());
266 
267  if(m_haveWindow) {
268  build();
269  } else {
270  m_mustBuild=true;
271  }
272  m_modelsChanged=false;
273 }
274 
275 //______________________________________________________________________________
276 bool
278 {
279  if (m_item)
280  return (item.purpose() == m_item->purpose());
281 
282  return false;
283 }
284 
285 //______________________________________________________________________________
286 
287 TEveElementList*
289 {
290  if ( havePerViewProduct(viewType) == false && m_products.empty() == false)
291  {
292  if (haveSingleProduct())
293  {
294  return m_products.back()->m_elements;
295  }
296  else
297  {
298  for (Product_it i = m_products.begin(); i!= m_products.end(); ++i)
299  {
300  if (viewType == (*i)->m_viewType)
301  return (*i)->m_elements;
302  }
303  }
304  }
305 
306  // printf("new product %s for item %s \n", FWViewType::idToName(viewType).c_str(), item()->name().c_str()); fflush(stdout);
307 
308  Product* product = new Product(viewType, viewContext);
309  m_products.push_back(product);
310  if (viewContext)
311  {
312  product->m_scaleConnection = viewContext->scaleChanged_.connect(boost::bind(&FWProxyBuilderBase::scaleChanged, this, _1));
313  }
314 
315  if (item())
316  {
317  // debug info in eve browser
318  product->m_elements->SetElementName(item()->name().c_str());
319  }
320  return product->m_elements;
321 }
322 
323 //______________________________________________________________________________
324 
325 void
327 {
328  for (Product_it i = m_products.begin(); i!= m_products.end(); ++i)
329  {
330  if (havePerViewProduct(type) && (*i)->m_viewContext == vc)
331  {
332  if ((*i)->m_elements)
333  (*i)->m_elements->DestroyElements();
334 
335  if ( (*i)->m_viewContext)
336  (*i)->m_scaleConnection.disconnect();
337 
338  delete (*i);
339  m_products.erase(i);
340  break;
341  }
342  }
343 }
344 
345 //------------------------------------------------------------------------------
346 
347 void
349 {
350  // Called if willHandleInteraction() returns false. Purpose ignored by default.
351 
353 }
354 
355 
356 bool
358 {
359  return false;
360 }
361 
362 void
364 {
365  // Nothing to be done in base class.
366  // Visibility, main color and main transparency are handled through FWInteractionList.
367 }
368 
369 
370 void
372 {
373  for (Product_it i = m_products.begin(); i!= m_products.end(); ++i)
374  {
375  if ( havePerViewProduct((*i)->m_viewType) && (*i)->m_viewContext == vc)
376  {
377  scaleProduct((*i)->m_elements, (*i)->m_viewType, (*i)->m_viewContext);
378  }
379  }
380  gEve->Redraw3D();
381 }
382 
383 void
385 {
386  // Cleans local common element list.
387  for (Product_it i = m_products.begin(); i != m_products.end(); ++i)
388  {
389  if ((*i)->m_elements)
390  (*i)->m_elements->DestroyElements();
391  }
392 
393  cleanLocal();
394 }
395 
396 void
398 {
399  // Cleans local common element list.
400 }
401 
402 
403 void
404 FWProxyBuilderBase::build(const FWEventItem*, TEveElementList*, const FWViewContext*)
405 {
406  assert("virtual build(const FWEventItem*, TEveElementList*, const FWViewContext*) not implemented by inherited class");
407 }
408 
409 void
411 {
412  assert("virtual buildViewType(const FWEventItem*, TEveElementList*, FWViewType::EType, const FWViewContext*) not implemented by inherited class");
413 }
414 
415 void
417 {
418  m_layer = layer;
419  for (Product_it pIt = m_products.begin(); pIt != m_products.end(); ++pIt)
420  {
421  TEveProjectable* pable = static_cast<TEveProjectable*>((*pIt)->m_elements);
422  for (TEveProjectable::ProjList_i i = pable->BeginProjecteds(); i != pable->EndProjecteds(); ++i)
423  (*i)->SetDepth(m_layer);
424  }
425 }
426 
427 //------------------------------------------------------------------------------
428 
429 void
430 FWProxyBuilderBase::setupAddElement(TEveElement* el, TEveElement* parent, bool color) const
431 {
432  setupElement(el, color);
433  parent->AddElement(el);
434 }
435 
439 void
440 FWProxyBuilderBase::setupElement(TEveElement* el, bool color) const
441 {
442  el->CSCTakeAnyParentAsMaster();
443  el->SetPickable(true);
444 
445  if (color)
446  {
447  el->CSCApplyMainColorToMatchingChildren();
448  el->CSCApplyMainTransparencyToMatchingChildren();
449  el->SetMainColor(m_item->defaultDisplayProperties().color());
450  assert((m_item->defaultDisplayProperties().transparency() >= 0)
451  && (m_item->defaultDisplayProperties().transparency() <= 100));
452  el->SetMainTransparency(m_item->defaultDisplayProperties().transparency());
453  }
454 }
455 
456 //------------------------------------------------------------------------------
457 
458 TEveCompound*
459 FWProxyBuilderBase::createCompound(bool set_color, bool propagate_color_to_all_children) const
460 {
461  TEveCompound* c = new TEveCompound();
462  c->CSCTakeAnyParentAsMaster();
463  c->CSCImplySelectAllChildren();
464  c->SetPickable(true);
465  if (set_color)
466  {
467  c->SetMainColor(m_item->defaultDisplayProperties().color());
468  c->SetMainTransparency(m_item->defaultDisplayProperties().transparency());
469  }
470  if (propagate_color_to_all_children)
471  {
472  c->CSCApplyMainColorToAllChildren();
473  c->CSCApplyMainTransparencyToAllChildren();
474  }
475  else
476  {
477  c->CSCApplyMainColorToMatchingChildren();
478  c->CSCApplyMainTransparencyToMatchingChildren();
479  }
480  return c;
481 }
482 
483 void
485  const std::string& name, Char_t transpOffset)
486 {
487  // Helper function to increse transparency of certain components.
488 
489  const FWDisplayProperties& dp = item()->modelInfo(index).displayProperties();
490  Char_t transp = TMath::Min(100, transpOffset + (100 - transpOffset) * dp.transparency() / 100);
491  TEveElement::List_t matches;
492  holder->FindChildren(matches, name.c_str());
493  for (TEveElement::List_i m = matches.begin(); m != matches.end(); ++m)
494  {
495  (*m)->SetMainTransparency(transp);
496  }
497 }
498 
499 //
500 // const member functions
501 //
502 
503 const fireworks::Context&
505 {
506  return m_item->context();
507 }
508 
509 int
511 {
512  return m_item->layer();
513 }
514 
515 //
516 // static member functions
517 //
518 
520 {
521  return std::string();
522 }
523 
525 {
526  return false;
527 }
type
Definition: HCALResponse.h:22
const fireworks::Context & context() const
virtual void buildViewType(const FWEventItem *iItem, TEveElementList *, FWViewType::EType, const FWViewContext *)
int i
Definition: DBlmapReader.cc:9
virtual void scaleProduct(TEveElementList *parent, FWViewType::EType, const FWViewContext *vc)
std::vector< Product * >::iterator Product_it
const FWDisplayProperties & defaultDisplayProperties() const
Definition: FWEventItem.cc:451
const FWDisplayProperties & displayProperties() const
Definition: FWEventItem.h:68
static std::string typeOfBuilder()
Used by the plugin system to determine how the proxy uses the data from FWEventItem.
std::vector< Product * > m_products
list parent
Definition: dbtoconf.py:74
virtual void itemBeingDestroyed(const FWEventItem *)
virtual bool havePerViewProduct(FWViewType::EType) const
const std::string & name() const
Definition: FWEventItem.cc:500
virtual bool visibilityModelChanges(const FWModelId &, TEveElement *, FWViewType::EType, const FWViewContext *)
void setupAddElement(TEveElement *el, TEveElement *parent, bool set_color=true) const
static bool representsSubPart()
std::set< FWModelId > FWModelIds
const FWViewContext * m_viewContext
virtual void cleanLocal()
void scaleChanged(const FWViewContext *)
const FWEventItem * m_item
void added(TEveElement *, unsigned int)
const FWEventItem * item() const
Color_t color() const
int layer() const
Definition: FWEventItem.cc:457
sigc::connection m_scaleConnection
virtual void localModelChanges(const FWModelId &iId, TEveElement *iCompound, FWViewType::EType viewType, const FWViewContext *vc)
Char_t transparency() const
sigc::signal< void, const FWViewContext * > scaleChanged_
Definition: FWViewContext.h:44
Product(FWViewType::EType t, const FWViewContext *c)
void set_color(TEveElement *element, Color_t color, float alpha, unsigned int levels)
Definition: eve_macros.cc:154
virtual void setItem(const FWEventItem *iItem)
const fireworks::Context & context() const
Definition: FWEventItem.h:127
void removePerViewProduct(FWViewType::EType, const FWViewContext *vc)
void setHaveWindow(bool iFlag)
virtual bool haveSingleProduct() const
size_t size() const
Definition: FWEventItem.cc:548
#define fwLog(_level_)
Definition: fwLog.h:51
virtual bool canHandle(const FWEventItem &)
FWInteractionList * m_interactionList
TEveCompound * createCompound(bool set_color=true, bool propagate_color_to_all_children=false) const
TEveElementList * createProduct(FWViewType::EType, const FWViewContext *)
ModelInfo modelInfo(int iIndex) const
Definition: FWEventItem.cc:535
const std::string & purpose() const
Definition: FWEventItem.cc:512
void setProjectionLayer(float)
void increaseComponentTransparency(unsigned int index, TEveElement *holder, const std::string &name, Char_t transpOffset)
void itemChanged(const FWEventItem *)
void modelChanges(const FWModelIds &)
virtual void setInteractionList(FWInteractionList *, const std::string &)
void setupElement(TEveElement *el, bool color=true) const