// @(#)root/eve:$Id$
// Author: Matevz Tadel 2007

/*************************************************************************
 * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "TEveSelection.h"
#include "TEveProjectionBases.h"
#include "TEveCompound.h"
#include "TEveManager.h"

#include "TClass.h"

//______________________________________________________________________________
//
// Make sure there is a SINGLE running TEveSelection for each
// selection type (select/highlight).

ClassImp(TEveSelection);

//______________________________________________________________________________
TEveSelection::TEveSelection(const char* n, const char* t) :
   TEveElementList(n, t),
   fPickToSelect  (kPS_Projectable),
   fActive        (kTRUE),
   fIsMaster      (kTRUE)
{
   // Constructor.

   fSelElement       = &TEveElement::SelectElement;
   fIncImpSelElement = &TEveElement::IncImpliedSelected;
   fDecImpSelElement = &TEveElement::DecImpliedSelected;
}

//______________________________________________________________________________
void TEveSelection::SetHighlightMode()
{
   // Set to 'highlight' mode.

   // Most importantly, this sets the pointers-to-function-members in
   // TEveElement that are used to mark elements as (un)selected and
   // implied-(un)selected.

   fPickToSelect = kPS_Projectable;
   fIsMaster     = kFALSE;

   fSelElement       = &TEveElement::HighlightElement;
   fIncImpSelElement = &TEveElement::IncImpliedHighlighted;
   fDecImpSelElement = &TEveElement::DecImpliedHighlighted;
}


/******************************************************************************/
// Protected helpers
/******************************************************************************/

//______________________________________________________________________________
void TEveSelection::DoElementSelect(TEveSelection::SelMap_i entry)
{
   // Select element indicated by the entry and fill its
   // implied-selected set.

   TEveElement *el  = entry->first;
   Set_t       &set = entry->second;

   (el->*fSelElement)(kTRUE);
   el->FillImpliedSelectedSet(set);
   for (Set_i i = set.begin(); i != set.end(); ++i)
      ((*i)->*fIncImpSelElement)();
}

//______________________________________________________________________________
void TEveSelection::DoElementUnselect(TEveSelection::SelMap_i entry)
{
   // Deselect element indicated by the entry and clear its
   // implied-selected set.

   TEveElement *el  = entry->first;
   Set_t       &set = entry->second;

   for (Set_i i = set.begin(); i != set.end(); ++i)
      ((*i)->*fDecImpSelElement)();
   set.clear();
   (el->*fSelElement)(kFALSE);
}


/******************************************************************************/
// Overrides of child-element-management virtuals from TEveElement
/******************************************************************************/

//______________________________________________________________________________
Bool_t TEveSelection::AcceptElement(TEveElement* el)
{
   // Pre-addition check. Deny addition if el is already selected.
   // Virtual from TEveElement.

   return el != this && fImpliedSelected.find(el) == fImpliedSelected.end() &&
          el->IsA()->InheritsFrom(TEveSelection::Class()) == kFALSE;
}

//______________________________________________________________________________
void TEveSelection::AddElement(TEveElement* el)
{
   // Add an element into selection, virtual from TEveElement.

   TEveElementList::AddElement(el);

   SelMap_i i = fImpliedSelected.insert(std::make_pair(el, Set_t())).first;
   if (fActive)
   {
      DoElementSelect(i);
   }
   SelectionAdded(el);
}

//______________________________________________________________________________
void TEveSelection::RemoveElement(TEveElement* el)
{
   // Add an element into selection, virtual from TEveElement.
   // Overriden here just so that a signal can be emitted.

   TEveElementList::RemoveElement(el);
   SelectionRemoved(el);
}

//______________________________________________________________________________
void TEveSelection::RemoveElementLocal(TEveElement* el)
{
   // Virtual from TEveElement.

   SelMap_i i = fImpliedSelected.find(el);

   if (i != fImpliedSelected.end())
   {
      if (fActive)
      {
         DoElementUnselect(i);
      }
      fImpliedSelected.erase(i);
   }
   else
   {
      Warning("TEveSelection::RemoveElementLocal", "element not found in map.");
   }
}

//______________________________________________________________________________
void TEveSelection::RemoveElements()
{
   // Add an element into selection, virtual from TEveElement.
   // Overriden here just so that a signal can be emitted.

   TEveElementList::RemoveElements();
   SelectionCleared();
}

//______________________________________________________________________________
void TEveSelection::RemoveElementsLocal()
{
   // Virtual from TEveElement.

   if (fActive)
   {
      for (SelMap_i i = fImpliedSelected.begin(); i != fImpliedSelected.end(); ++i)
         DoElementUnselect(i);
   }
   fImpliedSelected.clear();
}

//______________________________________________________________________________
void TEveSelection::RemoveImpliedSelected(TEveElement* el)
{
   // Remove element from all implied-selected sets.
   //
   // This is called as part of the element destruction from
   // TEveManager::PreDeleteElement() and should not be called
   // directly.

   for (SelMap_i i = fImpliedSelected.begin(); i != fImpliedSelected.end(); ++i)
   {
      Set_i j = i->second.find(el);
      if (j != i->second.end())
         i->second.erase(j);
   }
}

//______________________________________________________________________________
void TEveSelection::RecheckImpliedSet(SelMap_i smi)
{
   // Recalculate implied-selected state for given selection entry.
   // Add new elements to implied-selected set and increase their
   // implied-selected count.

   Set_t set;
   smi->first->FillImpliedSelectedSet(set);
   for (Set_i i = set.begin(); i != set.end(); ++i)
   {
      if (smi->second.find(*i) == smi->second.end())
      {
         smi->second.insert(*i);
         ((*i)->*fIncImpSelElement)();
      }
   }
}

//______________________________________________________________________________
void TEveSelection::RecheckImpliedSetForElement(TEveElement* el)
{
   // If given element is selected or implied-selected with this
   // selection and recheck implied-set for given selection entry.

   // Top-level selected.
   {
      SelMap_i i = fImpliedSelected.find(el);
      if (i != fImpliedSelected.end())
         RecheckImpliedSet(i);
   }

   // Implied selected, need to loop over all.
   {
      for (SelMap_i i = fImpliedSelected.begin(); i != fImpliedSelected.end(); ++ i)
      {
         if (i->second.find(el) != i->second.end())
            RecheckImpliedSet(i);
      }
   }
}


//******************************************************************************
// Signals
//******************************************************************************

//______________________________________________________________________________
void TEveSelection::SelectionAdded(TEveElement* el)
{
   // Emit SelectionAdded signal.

   Emit("SelectionAdded(TEveElement*)", (Long_t)el);
}

//______________________________________________________________________________
void TEveSelection::SelectionRemoved(TEveElement* el)
{
   // Emit SelectionRemoved signal.

   Emit("SelectionRemoved(TEveElement*)", (Long_t)el);
}

//______________________________________________________________________________
void TEveSelection::SelectionCleared()
{
   // Emit SelectionCleared signal.

   Emit("SelectionCleared()");
}

//______________________________________________________________________________
void TEveSelection::SelectionRepeated(TEveElement* el)
{
   // Called when secondary selection changed internally.

   Emit("SelectionRepeated(TEveElement*)", (Long_t)el);
}

/******************************************************************************/
// Activation / deactivation of selection
/******************************************************************************/

//______________________________________________________________________________
void TEveSelection::ActivateSelection()
{
   // Activate this selection.

   for (SelMap_i i = fImpliedSelected.begin(); i != fImpliedSelected.end(); ++i)
      DoElementSelect(i);
   fActive = kTRUE;
}

//______________________________________________________________________________
void TEveSelection::DeactivateSelection()
{
   // Deactivate this selection.

   fActive = kFALSE;
   for (SelMap_i i = fImpliedSelected.begin(); i != fImpliedSelected.end(); ++i)
      DoElementUnselect(i);
}


/******************************************************************************/
// User input processing
/******************************************************************************/

//______________________________________________________________________________
TEveElement* TEveSelection::MapPickedToSelected(TEveElement* el)
{
   // Given element el that was picked or clicked by the user, find
   // the parent/ancestor element that should actually become the main
   // selected element according to current selection mode.

   if (el == 0)
      return 0;

   if (el->ForwardSelection())
   {
      return el->ForwardSelection();
   }

   switch (fPickToSelect)
   {
      case kPS_Ignore:
      {
         return 0;
      }
      case kPS_Element:
      {
         return el;
      }
      case kPS_Projectable:
      {
         TEveProjected* pted = dynamic_cast<TEveProjected*>(el);
         if (pted)
            return dynamic_cast<TEveElement*>(pted->GetProjectable());
         return el;
      }
      case kPS_Compound:
      {
         TEveElement* cmpnd = el->GetCompound();
         if (cmpnd)
            return cmpnd;
         return el;
      }
      case kPS_PableCompound:
      {
         TEveProjected* pted = dynamic_cast<TEveProjected*>(el);
         if (pted)
            el = dynamic_cast<TEveElement*>(pted->GetProjectable());
         TEveElement* cmpnd = el->GetCompound();
         if (cmpnd)
            return cmpnd;
         return el;
      }
      case kPS_Master:
      {
         TEveElement* mstr = el->GetMaster();
         if (mstr)
            return mstr;
         return el;
      }
   }
   return el;
}

//______________________________________________________________________________
void TEveSelection::UserPickedElement(TEveElement* el, Bool_t multi)
{
   // Called when user picks/clicks on an element. If multi is true,
   // the user is requiring a multiple selection (usually this is
   // associated with control-key being pressed at the time of pick
   // event).

   TEveElement *edit_el = el ? el->ForwardEdit() : 0;

   el = MapPickedToSelected(el);

   if (el || HasChildren())
   {
      if (!multi)
         RemoveElements();
      if (el)
      {
         if (HasChild(el))
             RemoveElement(el);
         else
            AddElement(el);
      }
      if (fIsMaster)
         gEve->ElementSelect(edit_el ? edit_el : el);
      gEve->Redraw3D();
   }
}

//______________________________________________________________________________
void TEveSelection::UserRePickedElement(TEveElement* el)
{
   // Called when secondary selection becomes empty.

   el = MapPickedToSelected(el);
   if (el && HasChild(el))
   {
      SelectionRepeated(el);
      gEve->Redraw3D();
   }
}

//______________________________________________________________________________
void TEveSelection::UserUnPickedElement(TEveElement* el)
{
   // Called when secondary selection becomes empty.

   el = MapPickedToSelected(el);
   if (el)
   {
      RemoveElement(el);
      gEve->Redraw3D();
   }
}
 TEveSelection.cxx:1
 TEveSelection.cxx:2
 TEveSelection.cxx:3
 TEveSelection.cxx:4
 TEveSelection.cxx:5
 TEveSelection.cxx:6
 TEveSelection.cxx:7
 TEveSelection.cxx:8
 TEveSelection.cxx:9
 TEveSelection.cxx:10
 TEveSelection.cxx:11
 TEveSelection.cxx:12
 TEveSelection.cxx:13
 TEveSelection.cxx:14
 TEveSelection.cxx:15
 TEveSelection.cxx:16
 TEveSelection.cxx:17
 TEveSelection.cxx:18
 TEveSelection.cxx:19
 TEveSelection.cxx:20
 TEveSelection.cxx:21
 TEveSelection.cxx:22
 TEveSelection.cxx:23
 TEveSelection.cxx:24
 TEveSelection.cxx:25
 TEveSelection.cxx:26
 TEveSelection.cxx:27
 TEveSelection.cxx:28
 TEveSelection.cxx:29
 TEveSelection.cxx:30
 TEveSelection.cxx:31
 TEveSelection.cxx:32
 TEveSelection.cxx:33
 TEveSelection.cxx:34
 TEveSelection.cxx:35
 TEveSelection.cxx:36
 TEveSelection.cxx:37
 TEveSelection.cxx:38
 TEveSelection.cxx:39
 TEveSelection.cxx:40
 TEveSelection.cxx:41
 TEveSelection.cxx:42
 TEveSelection.cxx:43
 TEveSelection.cxx:44
 TEveSelection.cxx:45
 TEveSelection.cxx:46
 TEveSelection.cxx:47
 TEveSelection.cxx:48
 TEveSelection.cxx:49
 TEveSelection.cxx:50
 TEveSelection.cxx:51
 TEveSelection.cxx:52
 TEveSelection.cxx:53
 TEveSelection.cxx:54
 TEveSelection.cxx:55
 TEveSelection.cxx:56
 TEveSelection.cxx:57
 TEveSelection.cxx:58
 TEveSelection.cxx:59
 TEveSelection.cxx:60
 TEveSelection.cxx:61
 TEveSelection.cxx:62
 TEveSelection.cxx:63
 TEveSelection.cxx:64
 TEveSelection.cxx:65
 TEveSelection.cxx:66
 TEveSelection.cxx:67
 TEveSelection.cxx:68
 TEveSelection.cxx:69
 TEveSelection.cxx:70
 TEveSelection.cxx:71
 TEveSelection.cxx:72
 TEveSelection.cxx:73
 TEveSelection.cxx:74
 TEveSelection.cxx:75
 TEveSelection.cxx:76
 TEveSelection.cxx:77
 TEveSelection.cxx:78
 TEveSelection.cxx:79
 TEveSelection.cxx:80
 TEveSelection.cxx:81
 TEveSelection.cxx:82
 TEveSelection.cxx:83
 TEveSelection.cxx:84
 TEveSelection.cxx:85
 TEveSelection.cxx:86
 TEveSelection.cxx:87
 TEveSelection.cxx:88
 TEveSelection.cxx:89
 TEveSelection.cxx:90
 TEveSelection.cxx:91
 TEveSelection.cxx:92
 TEveSelection.cxx:93
 TEveSelection.cxx:94
 TEveSelection.cxx:95
 TEveSelection.cxx:96
 TEveSelection.cxx:97
 TEveSelection.cxx:98
 TEveSelection.cxx:99
 TEveSelection.cxx:100
 TEveSelection.cxx:101
 TEveSelection.cxx:102
 TEveSelection.cxx:103
 TEveSelection.cxx:104
 TEveSelection.cxx:105
 TEveSelection.cxx:106
 TEveSelection.cxx:107
 TEveSelection.cxx:108
 TEveSelection.cxx:109
 TEveSelection.cxx:110
 TEveSelection.cxx:111
 TEveSelection.cxx:112
 TEveSelection.cxx:113
 TEveSelection.cxx:114
 TEveSelection.cxx:115
 TEveSelection.cxx:116
 TEveSelection.cxx:117
 TEveSelection.cxx:118
 TEveSelection.cxx:119
 TEveSelection.cxx:120
 TEveSelection.cxx:121
 TEveSelection.cxx:122
 TEveSelection.cxx:123
 TEveSelection.cxx:124
 TEveSelection.cxx:125
 TEveSelection.cxx:126
 TEveSelection.cxx:127
 TEveSelection.cxx:128
 TEveSelection.cxx:129
 TEveSelection.cxx:130
 TEveSelection.cxx:131
 TEveSelection.cxx:132
 TEveSelection.cxx:133
 TEveSelection.cxx:134
 TEveSelection.cxx:135
 TEveSelection.cxx:136
 TEveSelection.cxx:137
 TEveSelection.cxx:138
 TEveSelection.cxx:139
 TEveSelection.cxx:140
 TEveSelection.cxx:141
 TEveSelection.cxx:142
 TEveSelection.cxx:143
 TEveSelection.cxx:144
 TEveSelection.cxx:145
 TEveSelection.cxx:146
 TEveSelection.cxx:147
 TEveSelection.cxx:148
 TEveSelection.cxx:149
 TEveSelection.cxx:150
 TEveSelection.cxx:151
 TEveSelection.cxx:152
 TEveSelection.cxx:153
 TEveSelection.cxx:154
 TEveSelection.cxx:155
 TEveSelection.cxx:156
 TEveSelection.cxx:157
 TEveSelection.cxx:158
 TEveSelection.cxx:159
 TEveSelection.cxx:160
 TEveSelection.cxx:161
 TEveSelection.cxx:162
 TEveSelection.cxx:163
 TEveSelection.cxx:164
 TEveSelection.cxx:165
 TEveSelection.cxx:166
 TEveSelection.cxx:167
 TEveSelection.cxx:168
 TEveSelection.cxx:169
 TEveSelection.cxx:170
 TEveSelection.cxx:171
 TEveSelection.cxx:172
 TEveSelection.cxx:173
 TEveSelection.cxx:174
 TEveSelection.cxx:175
 TEveSelection.cxx:176
 TEveSelection.cxx:177
 TEveSelection.cxx:178
 TEveSelection.cxx:179
 TEveSelection.cxx:180
 TEveSelection.cxx:181
 TEveSelection.cxx:182
 TEveSelection.cxx:183
 TEveSelection.cxx:184
 TEveSelection.cxx:185
 TEveSelection.cxx:186
 TEveSelection.cxx:187
 TEveSelection.cxx:188
 TEveSelection.cxx:189
 TEveSelection.cxx:190
 TEveSelection.cxx:191
 TEveSelection.cxx:192
 TEveSelection.cxx:193
 TEveSelection.cxx:194
 TEveSelection.cxx:195
 TEveSelection.cxx:196
 TEveSelection.cxx:197
 TEveSelection.cxx:198
 TEveSelection.cxx:199
 TEveSelection.cxx:200
 TEveSelection.cxx:201
 TEveSelection.cxx:202
 TEveSelection.cxx:203
 TEveSelection.cxx:204
 TEveSelection.cxx:205
 TEveSelection.cxx:206
 TEveSelection.cxx:207
 TEveSelection.cxx:208
 TEveSelection.cxx:209
 TEveSelection.cxx:210
 TEveSelection.cxx:211
 TEveSelection.cxx:212
 TEveSelection.cxx:213
 TEveSelection.cxx:214
 TEveSelection.cxx:215
 TEveSelection.cxx:216
 TEveSelection.cxx:217
 TEveSelection.cxx:218
 TEveSelection.cxx:219
 TEveSelection.cxx:220
 TEveSelection.cxx:221
 TEveSelection.cxx:222
 TEveSelection.cxx:223
 TEveSelection.cxx:224
 TEveSelection.cxx:225
 TEveSelection.cxx:226
 TEveSelection.cxx:227
 TEveSelection.cxx:228
 TEveSelection.cxx:229
 TEveSelection.cxx:230
 TEveSelection.cxx:231
 TEveSelection.cxx:232
 TEveSelection.cxx:233
 TEveSelection.cxx:234
 TEveSelection.cxx:235
 TEveSelection.cxx:236
 TEveSelection.cxx:237
 TEveSelection.cxx:238
 TEveSelection.cxx:239
 TEveSelection.cxx:240
 TEveSelection.cxx:241
 TEveSelection.cxx:242
 TEveSelection.cxx:243
 TEveSelection.cxx:244
 TEveSelection.cxx:245
 TEveSelection.cxx:246
 TEveSelection.cxx:247
 TEveSelection.cxx:248
 TEveSelection.cxx:249
 TEveSelection.cxx:250
 TEveSelection.cxx:251
 TEveSelection.cxx:252
 TEveSelection.cxx:253
 TEveSelection.cxx:254
 TEveSelection.cxx:255
 TEveSelection.cxx:256
 TEveSelection.cxx:257
 TEveSelection.cxx:258
 TEveSelection.cxx:259
 TEveSelection.cxx:260
 TEveSelection.cxx:261
 TEveSelection.cxx:262
 TEveSelection.cxx:263
 TEveSelection.cxx:264
 TEveSelection.cxx:265
 TEveSelection.cxx:266
 TEveSelection.cxx:267
 TEveSelection.cxx:268
 TEveSelection.cxx:269
 TEveSelection.cxx:270
 TEveSelection.cxx:271
 TEveSelection.cxx:272
 TEveSelection.cxx:273
 TEveSelection.cxx:274
 TEveSelection.cxx:275
 TEveSelection.cxx:276
 TEveSelection.cxx:277
 TEveSelection.cxx:278
 TEveSelection.cxx:279
 TEveSelection.cxx:280
 TEveSelection.cxx:281
 TEveSelection.cxx:282
 TEveSelection.cxx:283
 TEveSelection.cxx:284
 TEveSelection.cxx:285
 TEveSelection.cxx:286
 TEveSelection.cxx:287
 TEveSelection.cxx:288
 TEveSelection.cxx:289
 TEveSelection.cxx:290
 TEveSelection.cxx:291
 TEveSelection.cxx:292
 TEveSelection.cxx:293
 TEveSelection.cxx:294
 TEveSelection.cxx:295
 TEveSelection.cxx:296
 TEveSelection.cxx:297
 TEveSelection.cxx:298
 TEveSelection.cxx:299
 TEveSelection.cxx:300
 TEveSelection.cxx:301
 TEveSelection.cxx:302
 TEveSelection.cxx:303
 TEveSelection.cxx:304
 TEveSelection.cxx:305
 TEveSelection.cxx:306
 TEveSelection.cxx:307
 TEveSelection.cxx:308
 TEveSelection.cxx:309
 TEveSelection.cxx:310
 TEveSelection.cxx:311
 TEveSelection.cxx:312
 TEveSelection.cxx:313
 TEveSelection.cxx:314
 TEveSelection.cxx:315
 TEveSelection.cxx:316
 TEveSelection.cxx:317
 TEveSelection.cxx:318
 TEveSelection.cxx:319
 TEveSelection.cxx:320
 TEveSelection.cxx:321
 TEveSelection.cxx:322
 TEveSelection.cxx:323
 TEveSelection.cxx:324
 TEveSelection.cxx:325
 TEveSelection.cxx:326
 TEveSelection.cxx:327
 TEveSelection.cxx:328
 TEveSelection.cxx:329
 TEveSelection.cxx:330
 TEveSelection.cxx:331
 TEveSelection.cxx:332
 TEveSelection.cxx:333
 TEveSelection.cxx:334
 TEveSelection.cxx:335
 TEveSelection.cxx:336
 TEveSelection.cxx:337
 TEveSelection.cxx:338
 TEveSelection.cxx:339
 TEveSelection.cxx:340
 TEveSelection.cxx:341
 TEveSelection.cxx:342
 TEveSelection.cxx:343
 TEveSelection.cxx:344
 TEveSelection.cxx:345
 TEveSelection.cxx:346
 TEveSelection.cxx:347
 TEveSelection.cxx:348
 TEveSelection.cxx:349
 TEveSelection.cxx:350
 TEveSelection.cxx:351
 TEveSelection.cxx:352
 TEveSelection.cxx:353
 TEveSelection.cxx:354
 TEveSelection.cxx:355
 TEveSelection.cxx:356
 TEveSelection.cxx:357
 TEveSelection.cxx:358
 TEveSelection.cxx:359
 TEveSelection.cxx:360
 TEveSelection.cxx:361
 TEveSelection.cxx:362
 TEveSelection.cxx:363
 TEveSelection.cxx:364
 TEveSelection.cxx:365
 TEveSelection.cxx:366
 TEveSelection.cxx:367
 TEveSelection.cxx:368
 TEveSelection.cxx:369
 TEveSelection.cxx:370
 TEveSelection.cxx:371
 TEveSelection.cxx:372
 TEveSelection.cxx:373
 TEveSelection.cxx:374
 TEveSelection.cxx:375
 TEveSelection.cxx:376
 TEveSelection.cxx:377
 TEveSelection.cxx:378
 TEveSelection.cxx:379
 TEveSelection.cxx:380
 TEveSelection.cxx:381
 TEveSelection.cxx:382
 TEveSelection.cxx:383
 TEveSelection.cxx:384
 TEveSelection.cxx:385
 TEveSelection.cxx:386
 TEveSelection.cxx:387
 TEveSelection.cxx:388
 TEveSelection.cxx:389
 TEveSelection.cxx:390
 TEveSelection.cxx:391
 TEveSelection.cxx:392
 TEveSelection.cxx:393
 TEveSelection.cxx:394
 TEveSelection.cxx:395
 TEveSelection.cxx:396
 TEveSelection.cxx:397
 TEveSelection.cxx:398
 TEveSelection.cxx:399
 TEveSelection.cxx:400
 TEveSelection.cxx:401
 TEveSelection.cxx:402
 TEveSelection.cxx:403
 TEveSelection.cxx:404
 TEveSelection.cxx:405
 TEveSelection.cxx:406
 TEveSelection.cxx:407
 TEveSelection.cxx:408
 TEveSelection.cxx:409
 TEveSelection.cxx:410
 TEveSelection.cxx:411
 TEveSelection.cxx:412
 TEveSelection.cxx:413
 TEveSelection.cxx:414