CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/Fireworks/Core/src/FWSelectionManager.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWSelectionManager
00005 //
00006 // Implementation:
00007 //     <Notes on implementation>
00008 //
00009 // Original Author:  Chris Jones
00010 //         Created:  Fri Jan 18 14:40:51 EST 2008
00011 // $Id: FWSelectionManager.cc,v 1.12 2012/09/21 09:26:26 eulisse Exp $
00012 //
00013 
00014 // system include files
00015 #include <boost/bind.hpp>
00016 #include <iostream>
00017 #include <cassert>
00018 
00019 // user include files
00020 #include "Fireworks/Core/interface/FWSelectionManager.h"
00021 #include "Fireworks/Core/interface/FWModelChangeManager.h"
00022 #include "Fireworks/Core/interface/FWEventItem.h"
00023 
00024 
00025 //
00026 // constants, enums and typedefs
00027 //
00028 
00029 //
00030 // static data member definitions
00031 //
00032 
00033 //
00034 // constructors and destructor
00035 //
00036 FWSelectionManager::FWSelectionManager(FWModelChangeManager* iCM) :
00037    m_changeManager(iCM),
00038    m_wasChanged(false)
00039 {
00040    assert(0!=m_changeManager);
00041    m_changeManager->changeSignalsAreDone_.connect(boost::bind(&FWSelectionManager::finishedAllSelections,this));
00042 }
00043 
00044 // FWSelectionManager::FWSelectionManager(const FWSelectionManager& rhs)
00045 // {
00046 //    // do actual copying here;
00047 // }
00048 
00049 /*FWSelectionManager::~FWSelectionManager()
00050    {
00051    }*/
00052 
00053 //
00054 // assignment operators
00055 //
00056 // const FWSelectionManager& FWSelectionManager::operator=(const FWSelectionManager& rhs)
00057 // {
00058 //   //An exception safe implementation is
00059 //   FWSelectionManager temp(rhs);
00060 //   swap(rhs);
00061 //
00062 //   return *this;
00063 // }
00064 
00065 //
00066 // member functions
00067 //
00068 void
00069 FWSelectionManager::clearSelection()
00070 {
00071    FWChangeSentry sentry(*m_changeManager);
00072    for(std::set<FWModelId>::iterator it = m_selection.begin(), itEnd = m_selection.end();
00073        it != itEnd;
00074        ++it) {
00075       //NOTE: this will cause
00076       it->unselect();
00077    }
00078    clearItemSelection();
00079 }
00080 
00081 void
00082 FWSelectionManager::clearItemSelection()
00083 {
00084    //may need this in the future 
00085    //FWChangeSentry sentry(*m_changeManager);
00086    std::set<FWEventItem*> items;
00087    items.swap(m_itemSelection);
00088    for(std::set<FWEventItem*>::iterator it = items.begin(), itEnd = items.end();
00089        it != itEnd;
00090        ++it) {
00091       //NOTE: this will cause
00092       (*it)->unselectItem();
00093    }
00094 }
00095 
00096 void 
00097 FWSelectionManager::clearModelSelectionLeaveItem()
00098 {
00099    FWChangeSentry sentry(*m_changeManager);
00100    for(std::set<FWModelId>::iterator it = m_selection.begin(), itEnd = m_selection.end();
00101        it != itEnd;
00102        ++it) {
00103       //NOTE: this will cause
00104       it->unselect();
00105    }
00106 }
00107 
00108 void
00109 FWSelectionManager::finishedAllSelections()
00110 {
00111    if(m_wasChanged) {
00112       m_selection=m_newSelection;
00113       selectionChanged_(*this);
00114       m_wasChanged = false;
00115    }
00116 }
00117 
00118 void
00119 FWSelectionManager::select(const FWModelId& iId)
00120 {
00121    bool changed = m_newSelection.insert(iId).second;
00122    m_wasChanged |=changed;
00123    if(changed) {
00124       //if this is new, we need to connect to the 'item' just incase it changes
00125       if(m_itemConnectionCount.size()<= iId.item()->id()) {
00126          m_itemConnectionCount.resize(iId.item()->id()+1);
00127       }
00128       if(1 ==++(m_itemConnectionCount[iId.item()->id()].first) ) {
00129          //want to know early about item change so we can send the 'selectionChanged' message
00130          // as part of the itemChange message from the ChangeManager
00131          // This way if more than one Item has changed, we still only send one 'selectionChanged' message
00132          m_itemConnectionCount[iId.item()->id()].second =
00133             iId.item()->preItemChanged_.connect(boost::bind(&FWSelectionManager::itemChanged,this,_1));
00134       }
00135    }
00136 }
00137 
00138 void
00139 FWSelectionManager::unselect(const FWModelId& iId)
00140 {
00141    bool changed = (0 != m_newSelection.erase(iId));
00142    m_wasChanged |=changed;
00143    if(changed) {
00144       assert(m_itemConnectionCount.size() > iId.item()->id());
00145       //was this the last model selected for this item?
00146       if(0 ==--(m_itemConnectionCount[iId.item()->id()].first)) {
00147          m_itemConnectionCount[iId.item()->id()].second.disconnect();
00148       }
00149    }
00150 }
00151 
00152 void
00153 FWSelectionManager::itemChanged(const FWEventItem* iItem)
00154 {
00155    assert(0!=iItem);
00156    assert(m_itemConnectionCount.size() > iItem->id());
00157    //if this appears in any of our models we need to remove them
00158    FWModelId low(iItem,0);
00159    FWModelId high(iItem,0x7FFFFFFF); //largest signed 32 bit number
00160    bool someoneChanged = false;
00161    {
00162       std::set<FWModelId>::iterator itL=m_newSelection.lower_bound(low),
00163                                     itH=m_newSelection.upper_bound(high);
00164       if(itL!=itH) {
00165          m_wasChanged =true;
00166          someoneChanged=true;
00167          m_newSelection.erase(itL,itH);
00168       }
00169    }
00170    {
00171       std::set<FWModelId>::iterator itL=m_selection.lower_bound(low),
00172                                     itH=m_selection.upper_bound(high);
00173       if(itL!=itH) {
00174          m_wasChanged =true;
00175          someoneChanged = true;
00176          //Don't need to erase here since will happen in 'finishedAllSelection'
00177       }
00178    }
00179    assert(someoneChanged);
00180    m_itemConnectionCount[iItem->id()].second.disconnect();
00181    m_itemConnectionCount[iItem->id()].first = 0;
00182 }
00183 
00184 void 
00185 FWSelectionManager::selectItem(FWEventItem* iItem)
00186 {
00187    m_itemSelection.insert(iItem);
00188    itemSelectionChanged_(*this);
00189 }
00190 void 
00191 FWSelectionManager::unselectItem(FWEventItem* iItem)
00192 {
00193    m_itemSelection.erase(iItem);
00194    itemSelectionChanged_(*this);
00195 }
00196 //
00197 // const member functions
00198 //
00199 const std::set<FWModelId>&
00200 FWSelectionManager::selected() const
00201 {
00202    return m_selection;
00203 }
00204 
00205 const std::set<FWEventItem*>&
00206 FWSelectionManager::selectedItems() const
00207 {
00208    return m_itemSelection;
00209 }
00210 
00211 //
00212 // static member functions
00213 //