Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:11:42

0001 // -*- C++ -*-
0002 //
0003 // Package:     Core
0004 // Class  :     FWSelectionManager
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Original Author:  Chris Jones
0010 //         Created:  Fri Jan 18 14:40:51 EST 2008
0011 //
0012 
0013 // system include files
0014 #include <functional>
0015 #include <iostream>
0016 #include <cassert>
0017 
0018 // user include files
0019 #include "Fireworks/Core/interface/FWSelectionManager.h"
0020 #include "Fireworks/Core/interface/FWModelChangeManager.h"
0021 #include "Fireworks/Core/interface/FWEventItem.h"
0022 
0023 //
0024 // constants, enums and typedefs
0025 //
0026 
0027 //
0028 // static data member definitions
0029 //
0030 
0031 //
0032 // constructors and destructor
0033 //
0034 FWSelectionManager::FWSelectionManager(FWModelChangeManager* iCM) : m_changeManager(iCM), m_wasChanged(false) {
0035   assert(nullptr != m_changeManager);
0036   m_changeManager->changeSignalsAreDone_.connect(std::bind(&FWSelectionManager::finishedAllSelections, this));
0037 }
0038 
0039 // FWSelectionManager::FWSelectionManager(const FWSelectionManager& rhs)
0040 // {
0041 //    // do actual copying here;
0042 // }
0043 
0044 /*FWSelectionManager::~FWSelectionManager()
0045    {
0046    }*/
0047 
0048 //
0049 // assignment operators
0050 //
0051 // const FWSelectionManager& FWSelectionManager::operator=(const FWSelectionManager& rhs)
0052 // {
0053 //   //An exception safe implementation is
0054 //   FWSelectionManager temp(rhs);
0055 //   swap(rhs);
0056 //
0057 //   return *this;
0058 // }
0059 
0060 //
0061 // member functions
0062 //
0063 void FWSelectionManager::clearSelection() {
0064   FWChangeSentry sentry(*m_changeManager);
0065   for (std::set<FWModelId>::iterator it = m_selection.begin(), itEnd = m_selection.end(); it != itEnd; ++it) {
0066     //NOTE: this will cause
0067     it->unselect();
0068   }
0069   clearItemSelection();
0070 }
0071 
0072 void FWSelectionManager::clearItemSelection() {
0073   //may need this in the future
0074   //FWChangeSentry sentry(*m_changeManager);
0075   std::set<FWEventItem*> items;
0076   items.swap(m_itemSelection);
0077   for (std::set<FWEventItem*>::iterator it = items.begin(), itEnd = items.end(); it != itEnd; ++it) {
0078     //NOTE: this will cause
0079     (*it)->unselectItem();
0080   }
0081 }
0082 
0083 void FWSelectionManager::clearModelSelectionLeaveItem() {
0084   FWChangeSentry sentry(*m_changeManager);
0085   for (std::set<FWModelId>::iterator it = m_selection.begin(), itEnd = m_selection.end(); it != itEnd; ++it) {
0086     //NOTE: this will cause
0087     it->unselect();
0088   }
0089 }
0090 
0091 void FWSelectionManager::finishedAllSelections() {
0092   if (m_wasChanged) {
0093     m_selection = m_newSelection;
0094     selectionChanged_(*this);
0095     m_wasChanged = false;
0096   }
0097 }
0098 
0099 void FWSelectionManager::select(const FWModelId& iId) {
0100   bool changed = m_newSelection.insert(iId).second;
0101   m_wasChanged |= changed;
0102   if (changed) {
0103     //if this is new, we need to connect to the 'item' just incase it changes
0104     if (m_itemConnectionCount.size() <= iId.item()->id()) {
0105       m_itemConnectionCount.resize(iId.item()->id() + 1);
0106     }
0107     if (1 == ++(m_itemConnectionCount[iId.item()->id()].first)) {
0108       //want to know early about item change so we can send the 'selectionChanged' message
0109       // as part of the itemChange message from the ChangeManager
0110       // This way if more than one Item has changed, we still only send one 'selectionChanged' message
0111       m_itemConnectionCount[iId.item()->id()].second =
0112           iId.item()->preItemChanged_.connect(std::bind(&FWSelectionManager::itemChanged, this, std::placeholders::_1));
0113     }
0114   }
0115 }
0116 
0117 void FWSelectionManager::unselect(const FWModelId& iId) {
0118   bool changed = (0 != m_newSelection.erase(iId));
0119   m_wasChanged |= changed;
0120   if (changed) {
0121     assert(m_itemConnectionCount.size() > iId.item()->id());
0122     //was this the last model selected for this item?
0123     if (0 == --(m_itemConnectionCount[iId.item()->id()].first)) {
0124       m_itemConnectionCount[iId.item()->id()].second.disconnect();
0125     }
0126   }
0127 }
0128 
0129 void FWSelectionManager::itemChanged(const FWEventItem* iItem) {
0130   assert(nullptr != iItem);
0131   assert(m_itemConnectionCount.size() > iItem->id());
0132   //if this appears in any of our models we need to remove them
0133   FWModelId low(iItem, 0);
0134   FWModelId high(iItem, 0x7FFFFFFF);  //largest signed 32 bit number
0135   bool someoneChanged = false;
0136   {
0137     std::set<FWModelId>::iterator itL = m_newSelection.lower_bound(low), itH = m_newSelection.upper_bound(high);
0138     if (itL != itH) {
0139       m_wasChanged = true;
0140       someoneChanged = true;
0141       m_newSelection.erase(itL, itH);
0142     }
0143   }
0144   {
0145     std::set<FWModelId>::iterator itL = m_selection.lower_bound(low), itH = m_selection.upper_bound(high);
0146     if (itL != itH) {
0147       m_wasChanged = true;
0148       someoneChanged = true;
0149       //Don't need to erase here since will happen in 'finishedAllSelection'
0150     }
0151   }
0152   assert(someoneChanged);
0153   m_itemConnectionCount[iItem->id()].second.disconnect();
0154   m_itemConnectionCount[iItem->id()].first = 0;
0155 }
0156 
0157 void FWSelectionManager::selectItem(FWEventItem* iItem) {
0158   m_itemSelection.insert(iItem);
0159   itemSelectionChanged_(*this);
0160 }
0161 void FWSelectionManager::unselectItem(FWEventItem* iItem) {
0162   m_itemSelection.erase(iItem);
0163   itemSelectionChanged_(*this);
0164 }
0165 //
0166 // const member functions
0167 //
0168 const std::set<FWModelId>& FWSelectionManager::selected() const { return m_selection; }
0169 
0170 const std::set<FWEventItem*>& FWSelectionManager::selectedItems() const { return m_itemSelection; }
0171 
0172 //
0173 // static member functions
0174 //