]>
Commit | Line | Data |
---|---|---|
5a5a1232 | 1 | // $Header$ |
2 | ||
3 | #include "RenderElement.h" | |
4 | #include "RGTopFrame.h" | |
5 | ||
6 | #include <TColor.h> | |
7 | #include <TCanvas.h> | |
8 | #include <TGListTree.h> | |
9 | #include <THashList.h> | |
10 | ||
5a5a1232 | 11 | using namespace Reve; |
12 | ||
13 | //______________________________________________________________________ | |
dfb9c3e0 | 14 | // RenderElement |
15 | // | |
5a5a1232 | 16 | // |
17 | ||
18 | ClassImp(RenderElement) | |
19 | ||
dfb9c3e0 | 20 | RenderElement::RenderElement() : |
21 | fRnrElement (kTRUE), | |
22 | fMainColorPtr (0), | |
23 | fDestroyOnZeroRefCnt (kTRUE), | |
24 | fDenyDestroy (kFALSE) | |
25 | {} | |
5a5a1232 | 26 | |
dfb9c3e0 | 27 | RenderElement::RenderElement(Color_t& main_color) : |
28 | fRnrElement (kTRUE), | |
29 | fMainColorPtr (&main_color), | |
30 | fDestroyOnZeroRefCnt (kTRUE), | |
31 | fDenyDestroy (kFALSE) | |
32 | {} | |
a87df773 | 33 | |
34 | RenderElement::~RenderElement() | |
35 | { | |
dfb9c3e0 | 36 | static const Exc_t _eh("RenderElement::RenderElement "); |
37 | ||
38 | for(lpRE_i p=fParents.begin(); p!=fParents.end(); ++p) { | |
39 | RenderElementListBase* l = dynamic_cast<RenderElementListBase*>(*p); | |
40 | if(l) | |
41 | l->RemoveElementLocal(this); | |
42 | } | |
43 | fParents.clear(); | |
44 | ||
a87df773 | 45 | for(sLTI_i i=fItems.begin(); i!=fItems.end(); ++i) { |
46 | i->fTree->DeleteItem(i->fItem); | |
47 | gClient->NeedRedraw(i->fTree); | |
48 | } | |
5a5a1232 | 49 | } |
50 | ||
dfb9c3e0 | 51 | void RenderElement::Destroy() |
52 | { | |
53 | static const Exc_t eH("RenderElement::Destroy "); | |
54 | ||
55 | if(fDenyDestroy) | |
56 | throw(eH + "this object is protected against destruction."); | |
57 | ||
58 | delete this; | |
59 | gReve->Redraw3D(); | |
60 | } | |
61 | ||
62 | /**************************************************************************/ | |
63 | ||
64 | void RenderElement::AddParent(RenderElement* re) | |
65 | { | |
66 | fParents.push_back(re); | |
67 | } | |
68 | ||
69 | void RenderElement::RemoveParent(RenderElement* re) | |
70 | { | |
71 | static const Exc_t eH("RenderElement::RemoveParent "); | |
72 | ||
73 | fParents.remove(re); | |
74 | if(fParents.empty() && fDestroyOnZeroRefCnt) { | |
75 | // TObject* tobj = GetObject(eH); | |
76 | // Warning(eH.Data(), Form("auto-destructing '%s' on zero reference count.", tobj->GetName())); | |
77 | delete this; | |
78 | } | |
79 | } | |
80 | ||
5a5a1232 | 81 | /**************************************************************************/ |
82 | ||
83 | TObject* RenderElement::GetObject(Exc_t eh) | |
84 | { | |
85 | TObject* obj = dynamic_cast<TObject*>(this); | |
86 | if(obj == 0) | |
87 | throw(eh + "not a TObject."); | |
88 | return obj; | |
89 | } | |
90 | ||
91 | /**************************************************************************/ | |
92 | ||
93 | TGListTreeItem* RenderElement::AddIntoListTree(TGListTree* ltree, | |
dfb9c3e0 | 94 | TGListTreeItem* parent_lti) |
5a5a1232 | 95 | { |
96 | static const Exc_t eH("RenderElement::AddIntoListTree "); | |
97 | ||
98 | TObject* tobj = GetObject(eH); | |
dfb9c3e0 | 99 | TGListTreeItem* item = ltree->AddItem(parent_lti, tobj->GetName(), this, |
a87df773 | 100 | 0, 0, kTRUE); |
dfb9c3e0 | 101 | gClient->NeedRedraw(ltree); |
5a5a1232 | 102 | item->CheckItem(GetRnrElement()); |
dfb9c3e0 | 103 | |
a87df773 | 104 | if(fMainColorPtr != 0) item->SetColor(GetMainColor()); |
5a5a1232 | 105 | item->SetTipText(tobj->GetTitle()); |
106 | ||
107 | fItems.insert(ListTreeInfo(ltree, item)); | |
108 | ||
109 | return item; | |
110 | } | |
111 | ||
dfb9c3e0 | 112 | TGListTreeItem* RenderElement::AddIntoListTree(TGListTree* ltree, |
113 | RenderElement* parent) | |
114 | { | |
115 | TGListTreeItem* parent_lti = parent ? parent->FindListTreeItem(ltree) : 0; | |
116 | return AddIntoListTree(ltree, parent_lti); | |
117 | } | |
118 | ||
119 | Bool_t RenderElement::RemoveFromListTree(TGListTree* ltree) | |
120 | { | |
121 | sLTI_i i = FindItem(ltree); | |
122 | if(i != fItems.end()) { | |
123 | ltree->DeleteItem(i->fItem); | |
124 | fItems.erase(i); | |
125 | gClient->NeedRedraw(ltree); | |
126 | return kTRUE; | |
127 | } else { | |
128 | return kFALSE; | |
129 | } | |
130 | } | |
131 | ||
132 | Bool_t RenderElement::RemoveFromListTree(TGListTree* ltree, | |
133 | TGListTreeItem* parent_lti) | |
134 | { | |
135 | sLTI_i i = FindItem(ltree, parent_lti); | |
136 | if(i != fItems.end()) { | |
137 | ltree->DeleteItem(i->fItem); | |
138 | fItems.erase(i); | |
139 | gClient->NeedRedraw(ltree); | |
140 | return kTRUE; | |
141 | } else { | |
142 | return kFALSE; | |
143 | } | |
144 | } | |
145 | ||
146 | RenderElement::sLTI_i RenderElement::FindItem(TGListTree* ltree) | |
147 | { | |
148 | for(sLTI_i i = fItems.begin(); i != fItems.end(); ++i) | |
149 | if(i->fTree == ltree) | |
150 | return i; | |
151 | return fItems.end(); | |
152 | } | |
153 | ||
154 | RenderElement::sLTI_i RenderElement::FindItem(TGListTree* ltree, | |
155 | TGListTreeItem* parent_lti) | |
156 | { | |
157 | for(sLTI_i i = fItems.begin(); i != fItems.end(); ++i) | |
158 | if(i->fTree == ltree && i->fItem->GetParent() == parent_lti) | |
159 | return i; | |
160 | return fItems.end(); | |
161 | } | |
162 | ||
163 | TGListTreeItem* RenderElement::FindListTreeItem(TGListTree* ltree) | |
164 | { | |
165 | for(sLTI_i i = fItems.begin(); i != fItems.end(); ++i) | |
166 | if(i->fTree == ltree) | |
167 | return i->fItem; | |
168 | return 0; | |
169 | } | |
170 | ||
171 | TGListTreeItem* RenderElement::FindListTreeItem(TGListTree* ltree, | |
172 | TGListTreeItem* parent_lti) | |
173 | { | |
174 | for(sLTI_i i = fItems.begin(); i != fItems.end(); ++i) | |
175 | if(i->fTree == ltree && i->fItem->GetParent() == parent_lti) | |
176 | return i->fItem; | |
177 | return 0; | |
178 | } | |
179 | ||
5a5a1232 | 180 | /**************************************************************************/ |
181 | ||
dfb9c3e0 | 182 | void RenderElement::UpdateItems() |
5a5a1232 | 183 | { |
dfb9c3e0 | 184 | static const Exc_t eH("RenderElement::UpdateItems "); |
185 | ||
186 | TObject* tobj = GetObject(eH); | |
5a5a1232 | 187 | for(sLTI_i i=fItems.begin(); i!=fItems.end(); ++i) { |
dfb9c3e0 | 188 | i->fItem->Rename(tobj->GetName()); |
189 | i->fItem->SetTipText(tobj->GetTitle()); | |
5a5a1232 | 190 | i->fItem->CheckItem(fRnrElement); |
a87df773 | 191 | if(fMainColorPtr != 0) i->fItem->SetColor(GetMainColor()); |
dfb9c3e0 | 192 | gClient->NeedRedraw(i->fTree); |
5a5a1232 | 193 | } |
dfb9c3e0 | 194 | gReve->Redraw3D(); // This will go away once editor can notify ALL changes. |
5a5a1232 | 195 | } |
196 | ||
197 | /**************************************************************************/ | |
198 | ||
199 | void RenderElement::SpawnEditor() | |
200 | { | |
5a5a1232 | 201 | gReve->EditRenderElement(this); |
202 | } | |
203 | ||
204 | void RenderElement::ExportToCINT(Text_t* var_name) | |
205 | { | |
206 | static const Exc_t eH("RenderElement::ExportToCINT "); | |
207 | ||
208 | TObject* obj = GetObject(eH); | |
209 | const char* cname = obj->IsA()->GetName(); | |
210 | gROOT->ProcessLine(Form("%s* %s = (%s*) %p;", cname, var_name, cname, obj)); | |
211 | } | |
212 | ||
213 | /**************************************************************************/ | |
214 | /* | |
215 | void RenderElement::DumpSourceObject() | |
216 | { | |
217 | TObject* o = GetSourceObject(); | |
218 | if(o == 0) { | |
219 | printf("Source object not set.\n"); | |
220 | } else { | |
221 | o->Dump(); | |
222 | } | |
223 | } | |
224 | ||
225 | void RenderElement::InspectSourceObject() | |
226 | { | |
227 | TObject* o = GetSourceObject(); | |
228 | if(o == 0) { | |
229 | printf("Source object not set.\n"); | |
230 | } else { | |
231 | o->Inspect(); | |
232 | } | |
233 | } | |
234 | */ | |
235 | /**************************************************************************/ | |
236 | ||
237 | void RenderElement::SetRnrElement(Bool_t rnr) | |
238 | { | |
239 | if(rnr != fRnrElement) { | |
240 | fRnrElement = rnr; | |
dfb9c3e0 | 241 | UpdateItems(); |
5a5a1232 | 242 | } |
243 | } | |
092578a7 | 244 | |
5a5a1232 | 245 | /**************************************************************************/ |
246 | ||
247 | void RenderElement::SetMainColor(Color_t color) | |
248 | { | |
249 | if (fMainColorPtr) { | |
250 | *fMainColorPtr = color; | |
dfb9c3e0 | 251 | UpdateItems(); |
5a5a1232 | 252 | } |
253 | } | |
254 | ||
08a6202f | 255 | void RenderElement::SetMainColor(Pixel_t pixel) |
5a5a1232 | 256 | { |
257 | SetMainColor(Color_t(TColor::GetColor(pixel))); | |
258 | } | |
259 | ||
260 | /**************************************************************************/ | |
261 | /**************************************************************************/ | |
262 | ||
092578a7 | 263 | ClassImp(RenderElementObjPtr) |
264 | ||
dfb9c3e0 | 265 | RenderElementObjPtr::RenderElementObjPtr(TObject* obj, Bool_t own) : |
092578a7 | 266 | RenderElement(), |
dfb9c3e0 | 267 | fObject(obj), |
268 | fOwnObject(own) | |
092578a7 | 269 | {} |
270 | ||
dfb9c3e0 | 271 | RenderElementObjPtr::RenderElementObjPtr(TObject* obj, Color_t& mainColor, Bool_t own) : |
092578a7 | 272 | RenderElement(mainColor), |
dfb9c3e0 | 273 | fObject(obj), |
274 | fOwnObject(own) | |
092578a7 | 275 | {} |
276 | ||
277 | RenderElementObjPtr::~RenderElementObjPtr() | |
278 | { | |
dfb9c3e0 | 279 | if(fOwnObject) |
280 | delete fObject; | |
092578a7 | 281 | } |
282 | ||
283 | /**************************************************************************/ | |
284 | ||
285 | TObject* RenderElementObjPtr::GetObject(Reve::Exc_t eh) | |
286 | { | |
287 | if(fObject == 0) | |
288 | throw(eh + "fObject not set."); | |
289 | return fObject; | |
290 | } | |
291 | ||
dfb9c3e0 | 292 | /**************************************************************************/ |
293 | /**************************************************************************/ | |
294 | ||
295 | ClassImp(RenderElementListBase) | |
296 | ||
297 | RenderElementListBase::~RenderElementListBase() | |
092578a7 | 298 | { |
dfb9c3e0 | 299 | // Collapse all sub-trees |
300 | for(sLTI_i i=fItems.begin(); i!=fItems.end(); ++i) { | |
301 | DestroyListSubTree(i->fTree, i->fItem); | |
302 | // My own items removed in ~RenderElement | |
092578a7 | 303 | } |
dfb9c3e0 | 304 | RemoveElements(); |
092578a7 | 305 | } |
306 | ||
092578a7 | 307 | /**************************************************************************/ |
308 | ||
dfb9c3e0 | 309 | void RenderElementListBase::AddElement(RenderElement* el) |
310 | { | |
311 | fChildren.push_back(el); | |
312 | el->AddParent(this); | |
313 | } | |
314 | ||
315 | void RenderElementListBase::RemoveElement(RenderElement* el) | |
316 | { | |
317 | el->RemoveParent(this); | |
318 | RemoveElementLocal(el); | |
319 | } | |
320 | ||
321 | void RenderElementListBase::RemoveElementLocal(RenderElement* el) | |
322 | { | |
323 | fChildren.remove(el); | |
324 | } | |
325 | ||
326 | void RenderElementListBase::RemoveElements() | |
327 | { | |
328 | for(lpRE_i i=fChildren.begin(); i!=fChildren.end(); ++i) { | |
329 | (*i)->RemoveParent(this); | |
330 | } | |
331 | fChildren.clear(); | |
332 | } | |
333 | ||
334 | /**************************************************************************/ | |
5a5a1232 | 335 | |
a11f38da | 336 | void RenderElementListBase::DestroyElements() |
337 | { | |
338 | static const Exc_t eH("RenderElementListBase::DestroyElements "); | |
339 | ||
340 | while( ! fChildren.empty()) { | |
341 | RenderElement* c = fChildren.front(); | |
342 | try { | |
343 | c->Destroy(); | |
344 | } | |
345 | catch(Exc_t exc) { | |
346 | Warning(eH.Data(), Form("element destruction failed: '%s'.", exc.Data())); | |
347 | RemoveElement(c); | |
348 | } | |
349 | } | |
350 | } | |
351 | ||
352 | /**************************************************************************/ | |
353 | ||
5a5a1232 | 354 | Int_t RenderElementListBase::ExpandIntoListTree(TGListTree* ltree, |
355 | TGListTreeItem* parent) | |
356 | { | |
357 | // Populates parent with elements. | |
358 | // parent must be an already existing representation of *this*. | |
359 | // Returns number of inserted elements. | |
360 | // If parent already has children, it does nothing. | |
361 | // | |
362 | // RnrEl can be inserted in a list-tree several times, thus we can not | |
363 | // search through fItems to get parent here. | |
364 | // Anyhow, it is probably known as it must have been selected by the user. | |
365 | ||
366 | if(parent->GetFirstChild() != 0) | |
367 | return 0; | |
368 | Int_t n = 0; | |
dfb9c3e0 | 369 | for(lpRE_i i=fChildren.begin(); i!=fChildren.end(); ++i) { |
5a5a1232 | 370 | (*i)->AddIntoListTree(ltree, parent); |
371 | ++n; | |
372 | } | |
373 | return n; | |
374 | } | |
375 | ||
dfb9c3e0 | 376 | Int_t RenderElementListBase::DestroyListSubTree(TGListTree* ltree, |
377 | TGListTreeItem* parent) | |
378 | { | |
379 | Int_t n = 0; | |
380 | TGListTreeItem* i = parent->GetFirstChild(); | |
381 | while(i != 0) { | |
382 | n += DestroyListSubTree(ltree, i); | |
383 | RenderElement* re = (RenderElement*) i->GetUserData(); | |
384 | i = i->GetNextSibling(); | |
385 | re->RemoveFromListTree(ltree, parent); | |
386 | } | |
387 | return n; | |
388 | } | |
389 | ||
5a5a1232 | 390 | /**************************************************************************/ |
391 | ||
392 | void RenderElementListBase::EnableListElements() | |
393 | { | |
dfb9c3e0 | 394 | for(lpRE_i i=fChildren.begin(); i!=fChildren.end(); ++i) |
a87df773 | 395 | (*i)->SetRnrElement(kTRUE); |
5a5a1232 | 396 | } |
397 | ||
398 | void RenderElementListBase::DisableListElements() | |
399 | { | |
dfb9c3e0 | 400 | for(lpRE_i i=fChildren.begin(); i!=fChildren.end(); ++i) |
a87df773 | 401 | (*i)->SetRnrElement(kFALSE); |
5a5a1232 | 402 | } |
403 | ||
404 | /**************************************************************************/ | |
405 | ||
406 | void RenderElementListBase::SetMainColor(Color_t col) | |
407 | { | |
408 | Color_t oldcol = GetMainColor(); | |
dfb9c3e0 | 409 | for(lpRE_i i=fChildren.begin(); i!=fChildren.end(); ++i) { |
5a5a1232 | 410 | if((*i)->GetMainColor() == oldcol) (*i)->SetMainColor(col); |
411 | } | |
412 | RenderElement::SetMainColor(col); | |
413 | } | |
414 | ||
415 | void RenderElementListBase::SetMainColor(Pixel_t pixel) | |
416 | { | |
417 | // This one needed for proper calling via CINT (signals). | |
418 | ||
419 | SetMainColor(Color_t(TColor::GetColor(pixel))); | |
420 | } | |
421 | ||
422 | /**************************************************************************/ | |
423 | ||
424 | void RenderElementListBase::PaintElements(Option_t* option) | |
425 | { | |
426 | if(fRnrElement) { | |
dfb9c3e0 | 427 | for(lpRE_i i=fChildren.begin(); i!=fChildren.end(); ++i) { |
5a5a1232 | 428 | if((*i)->GetRnrElement()) |
429 | (*i)->GetObject()->Paint(option); | |
430 | } | |
431 | } | |
432 | } | |
433 | ||
434 | /**************************************************************************/ | |
435 | /**************************************************************************/ | |
436 | ||
437 | ClassImp(RenderElementList) | |
dfb9c3e0 | 438 | |
439 | RenderElementList::RenderElementList(const Text_t* n, const Text_t* t, Bool_t doColor) : | |
440 | RenderElementListBase(), | |
441 | TNamed(n, t), | |
442 | fColor(0), | |
443 | fDoColor(doColor) | |
444 | { | |
445 | if(fDoColor) { | |
446 | SetMainColorPtr(&fColor); | |
447 | } | |
448 | } |