MeVisLabToolboxReference
MeVisLab/Standard/Sources/ML/MLCSO/CSOBase/CSOList.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //----------------------------------------------------------------------------------
00006 
00011 //----------------------------------------------------------------------------------
00012 
00013 #ifndef __CSOList_H
00014 #define __CSOList_H
00015 
00016 
00017 #include "MLCSOIncludes.h"
00018 
00019 #include "CSO.h"
00020 #include "CSOScopeEvents.h"
00021 #include "CSOEvent.h"
00022 #include "CSORules/CSOListRules.h"
00023 
00024 ML_START_NAMESPACE
00025 
00027 
00029 typedef void csoNotificationCB(void* data, int notificationFlag);
00030 
00032 
00034 class CSOUndoRedoManager;
00035 class CSOManager;
00036 class CSOGroup;
00037 
00038 // Define that can be used to detect if the CSOList is ref counted:
00039 #define ML_CSOLIST_REFCOUNTED 1
00040 
00041 // Declare CSOListPtr smart pointer
00042 ML_REFCOUNTED_PTR(CSOList)
00043 
00044 
00045 
00054 class MLCSO_EXPORT CSOList : public RefCountedBase
00055 {
00056 
00057 public:
00058 
00060   CSOList();
00062   CSOList(const CSOList& csoList);
00063 
00064 protected:
00070   virtual ~CSOList();
00071 
00072 public:
00073 
00074   /* DATA STRUCTURES */
00075 
00078   struct CSONotificationCBData {
00079     CSONotificationCBData()
00080     {
00081       notificationCB = NULL;
00082       notificationCBData = NULL;
00083       notificationFlag = NOTIFICATION_NONE;
00084     }
00085     csoNotificationCB* notificationCB; 
00086     void* notificationCBData;          
00087     int notificationFlag;              
00088   };
00089 
00090   typedef std::map<unsigned int, unsigned int> IdToIdMap;
00091 
00092   /* METHODS */
00093 
00095 
00097   void clone(const CSOList& csoList, bool useUndoRedo=false);
00099   void merge(const CSOList& csoList, bool mergeGroupWithSameLabel, IdToIdMap& origToThisGroupIdMap, IdToIdMap& origToThisCSOIdMap, bool useUndoRedo=false);
00100 
00102 
00105   bool saveTo(std::string& filename, bool binary) const;
00108   bool saveTo(std::ostream& outStream, bool binary) const;
00111   bool loadFrom(std::string& filename, std::string& message);
00113   bool loadFromVersionBefore5(std::istream& inStream, bool binary, short version, bool swapBytes,  std::string& message);
00116   bool loadFrom(std::istream& inStream, bool binary, std::string& message);
00117 
00119 
00121   void addNotificationObserver(csoNotificationCB* callback, void* userData);
00123   void removeNotificationObserver(csoNotificationCB* callback, void* userData);
00125   void notifyObservers(int notificationFlag);
00127   static void removeNotificationObserverFromAllCSOLists(csoNotificationCB* callback, void* userData);
00128 
00130 
00132   void updateDisplays();
00133 
00135 
00137   inline CSOUndoRedoManager* getUndoRedoManager() { return _csoUndoRedoManager; }
00139   inline CSOManager* getManager()                 { return _csoManager; }
00140 
00142   inline void setUndoRedoManager(CSOUndoRedoManager* undoRedoManager) { _csoUndoRedoManager = undoRedoManager; }
00144   void setManager(CSOManager* manager);
00145 
00147 
00150   unsigned int getCurrentCSOId();
00153   unsigned int getCurrentGroupId();
00155   inline void setCurrentCSOId(unsigned int id)   { _currentLastCSOId = id;   }
00157   inline void setCurrentGroupId(unsigned int id) { _currentLastGroupId = id; }
00158 
00160 
00162   bool isSelected(const CSO* cso) const;
00164   bool isSelected(const CSOGroup* group) const;
00165 
00167   void addSelected(CSO* cso);
00169   void addSelected(CSOGroup* group);
00170 
00173   bool setSelected(CSO* cso);
00176   bool setSelected(CSOGroup* group);
00177 
00179   inline void clearSelectedCSOs()  { 
00180     _selectedCSOs.clear(); 
00181   }
00183   inline void clearSelectedGroups() { 
00184     _selectedGroups.clear();
00185   }
00187   void clearSelectedSeedPoints();
00188 
00190   void removeFromSelection(CSO* cso);
00192   void removeFromSelection(CSOGroup* group);
00193 
00195   inline unsigned int numSelectedCSOs()   const { return static_cast<unsigned int>(_selectedCSOs.size()); }
00197   inline unsigned int numSelectedGroups() const { return static_cast<unsigned int>(_selectedGroups.size()); }
00198 
00200   inline CSO* getSelectedCSOAt(unsigned int index) const
00201   {
00202     if (_selectedCSOs.size() > 0) {
00203       return _selectedCSOs[index];
00204     } else { 
00205       return NULL;} 
00206   }
00208   inline CSOGroup* getSelectedGroupAt(unsigned int index) const
00209   {
00210     if (_selectedGroups.size() > 0) {        
00211       return _selectedGroups[index]; 
00212     } else {
00213       return NULL;
00214     }
00215   }
00218   Vector3 getCenterOfSelectedCSOs(bool& resultIsValid)   const;
00221   Vector3 getCenterOfSelectedGroups(bool& resultIsValid) const;
00222 
00224   inline CSO* getCSOCurrentlyUnderMouse() const { return _currentlyUnderMouseCSO; }
00227   void setCSOCurrentlyUnderMouse(CSO* csoUnderMouse);
00228 
00231   Vector3 getCenterOfSelectedCSOs()   const { 
00232     CSO_DEBUG("Using the deprecated method 'CSOList::getCenterOfSelectedCSOs()'; use 'CSOList::getCenterOfSelectedCSOs(bool& resultIsValid)' instead!");
00233     bool dummy; 
00234     return getCenterOfSelectedCSOs(dummy);
00235   }
00238   Vector3 getCenterOfSelectedGroups() const {
00239     CSO_DEBUG("Using the deprecated method 'CSOList::getCenterOfSelectedGroups()'; use 'CSOList::getCenterOfSelectedGroups(bool& resultIsValid)' instead!");
00240     bool dummy;
00241     return getCenterOfSelectedGroups(dummy);
00242   }
00243 
00245 
00247   CSO* addCSO(bool useUndoRedo=true);  
00250   CSO* addCSO(const std::vector<Vector3>& seedPositions, bool closed, bool useUndoRedo);
00253   CSO* addCSO(unsigned int numSeedPoints, bool closed, bool useUndoRedo);
00255   CSO* addCSOCopy(CSO* cso, bool useUndoRedo=true, int oldId=-1);
00257   CSOGroup* addGroup(bool useUndoRedo=true);
00260   CSOGroup* addGroupCopy(CSOGroup* group, bool useUndoRedo=true, int oldId=-1);
00261 
00264   bool removeCSO(CSO* cso, bool useUndoRedo=true);
00267   bool removeCSO(unsigned int id, bool useUndoRedo=true);
00270   bool removeAllCSO(bool useUndoRedo=true);
00273   bool removeGroup(CSOGroup* group, bool useUndoRedo=true);
00276   bool removeGroup(unsigned int id, bool useUndoRedo=true);
00279   bool removeAllGroups(bool useUndoRedo);
00280 
00283   bool removeAll(bool useUndoRedo=true);
00284 
00286 
00288   CSO* getCSOAt(unsigned int index) const;
00291   CSO* getCSOById(unsigned int id) const;
00294   CSO* getCSOByLabel(const std::string& label) const;
00296   unsigned int getCSOIndex(CSO* cso) const;
00298   unsigned int getCSOIndex(unsigned int id) const;
00300   bool hasCSO(CSO* cso) const;
00301 
00303 
00305   CSOGroup* getGroupById(unsigned int id) const;
00307   CSOGroup* getGroupByLabel(const std::string& label) const;
00309   CSOGroup* getGroupAt(unsigned int index) const;
00311   unsigned int getGroupIndex(CSOGroup* group) const;
00313   unsigned int getGroupIndex(unsigned int id) const;
00315   bool hasGroup(CSOGroup* group) const;
00316 
00318 
00322   std::string getUniqueCSOLabel(std::string label, std::string format, bool addIdAlways);
00323 
00327   std::string getUniqueGroupLabel(std::string label, std::string format, bool addIdAlways);
00328 
00333   inline void resetUniqueCSOLabelIdMap() { _csoLabelUniqueIdMap.clear(); }
00334 
00339   inline void resetUniqueGroupLabelIdMap() { _groupLabelUniqueIdMap.clear(); }
00340 
00342 
00344   void combineCSOandGroup(CSO* cso, CSOGroup* group, bool useUndoRedo=true);
00346   void breakCSOandGroup(CSO* cso, CSOGroup* group, bool useUndoRedo=true);
00348   void breakCSOfromAllGroups(CSO* cso, bool useUndoRedo=true);
00350   void breakAllCSOsFromGroup(CSOGroup* group, bool useUndoRedo=true);
00351 
00353 
00355   unsigned int numCSOs() const;
00357   unsigned int numGroups() const;
00358 
00360 
00362   void undo();
00364   void redo();
00365 
00367 
00369   inline void enableUndoRedoManager()      { _csoUndoRedoManagerEnabled = true; }
00371   void disableUndoRedoManager();
00372 
00374 
00376   void sendOpenEventGroup();
00378   void sendCloseEventGroup();
00379 
00381   void sendModuleEventGroup(CSOEvent* event) { sendEvent(event); }
00382 
00384   void sendPreEvent(CSOEvent::CSOEventType eventType);
00386   void sendPreEvent(CSOEvent::CSOEventType eventType, int csoId);
00388   void sendPreEvent(CSOEvent::CSOEventType eventType, const std::vector<int>& csoIds);
00390   void sendPreEvent(CSOEvent::CSOEventType eventType, const std::vector<int>& csoIds, const std::vector<int>& groupIds);
00391 
00392 
00394   void sendPostEvent(CSOEvent::CSOEventType eventType);
00396   void sendPostEvent(CSOEvent::CSOEventType eventType, int csoId);
00399   void sendPostEvent(CSOEvent::CSOEventType eventType, int csoId, int groupId);
00401   void sendPostEvent(CSOEvent::CSOEventType eventType, const std::vector<int>& csoIds);
00404   void sendPostEvent(CSOEvent::CSOEventType eventType, int csoId, const std::vector<int>& groupIds);
00406   void sendPostEvent(CSOEvent::CSOEventType eventType, const std::vector<int>& csoIds, const std::vector<int>& groupIds);
00407 
00409 
00411   inline void enableRepaintObserver()      { _enableRepaintObserver     = true;  }
00413   inline void disableRepaintObserver()     { _enableRepaintObserver     = false; }
00414 
00416   inline void enableFinishedObserver()     { _enableFinishedObserver    = true;  }
00418   inline void disableFinishedObserver()    { _enableFinishedObserver    = false; }
00419 
00421   inline void enableInteractionObserver()  { _enableInteractionObserver = true;  }
00423   inline void disableInteractionObserver() { _enableInteractionObserver = false; }
00424 
00426   inline void enableSelectionObserver()    { _enableSelectionObserver   = true;  }
00428   inline void disableSelectionObserver()   { _enableSelectionObserver   = false; }
00429 
00431   inline void enableStartNewCSOObserver()  { _enableStartNewCSOObserver = true;  }
00433   inline void disableStartNewCSOObserver() { _enableStartNewCSOObserver = false; }
00434 
00436   inline void enableCSOMovedObserver()     { _enableCSOMovedObserver    = true;  }
00438   inline void disableCSOMovedObserver()    { _enableCSOMovedObserver    = false; }
00439 
00441   inline void enableCSOCurrentlyUnderMouseObserver() { _enableCSOCurrentlyUnderMouseObserver = true; }
00443   inline void disableCSOCurrentlyUnderMouseObserver() { _enableCSOCurrentlyUnderMouseObserver = false; }
00444 
00446 
00448   inline bool isUndoRedoManagerEnabled()             const { return _csoUndoRedoManagerEnabled; }
00450   void openUndoRedoGroup();
00452   void closeUndoRedoGroup();
00453 
00455 
00457   inline bool isRepaintNotificationEnabled()         const { return _enableRepaintObserver;     }
00459   inline bool isFinishedNotificationEnabled()        const { return _enableFinishedObserver;    }
00461   inline bool isInteractionInitNotificationEnabled() const { return _enableInteractionObserver; }
00463   inline bool isSelectionNotificationEnabled()       const { return _enableSelectionObserver;   }
00465   inline bool isStartNewCSONotificationEnabled()     const { return _enableStartNewCSOObserver; }
00467   inline bool isCSOMovedNotificationEnabled()        const { return _enableCSOMovedObserver;    }
00469   inline bool isCSOCurrentlyUnderMouseNotificationEnabled() const { return _enableCSOCurrentlyUnderMouseObserver; }
00470 
00472 
00474   static void alignSeedPathLinks(CSO* cso);
00475 
00477 
00479   static const char* sVoxelWriteModeStrings[4];
00481   static const char* sLineStyleModeStrings [5];
00483   static const char* sSeedPointStyleStrings    [3];
00484 
00485   enum NotificationType {
00487     NOTIFICATION_NONE = 0,
00489     NOTIFICATION_CSO_SELECTION = 1, 
00491     NOTIFICATION_GROUP_SELECTION = 2, 
00493     NOTIFICATION_CSO_FINISHED = 4,
00495     NOTIFICATION_GROUP_FINISHED = 8,
00497     NOTIFICATION_REPAINT = 16,
00499     NOTIFICATION_INTERACTION_INIT = 32,
00501     NOTIFICATION_START_NEW_CSO = 64,
00503     NOTIFICATION_CSO_MOVED = 128,
00505     NOTIFICATION_CSO_UNDER_MOUSE_CHANGED = 256
00506   };
00507 
00509 
00511   inline CSOListRules& getRules()                              { return _rules;                          }
00512 
00514   virtual void addStateToTree(TreeNode* /*parent*/) const;
00515 
00517   virtual void readStateFromTree(TreeNode* /*parent*/);
00518 
00519   // set current version number
00520   ML_SET_ADDSTATE_VERSION(1);
00521 
00523   virtual CSOList* deepCopy() const { return new CSOList(*this); };
00524 
00525 #ifdef ML_DEPRECATED
00526 
00527 
00528   inline ML_DEPRECATED unsigned int numCSO() const { return numCSOs(); }
00529 #endif
00530   
00531 protected:
00532 
00533   // nothing so far
00534 
00535 private:    
00536 
00537   /* DATA STRUCTURES */
00538 
00540   struct IndexPair {
00541     IndexPair() {
00542       index0 = index1 = -1;
00543     }
00544     int index0, index1;
00545   };
00546 
00547 
00548   /* MEMBER VARIABLES */
00549 
00551   CSOListRules _rules;
00552 
00554   std::vector<CSO*>_csoList;
00556   std::vector<CSOGroup*>_groupList;
00557 
00559   std::vector<CSO*>_selectedCSOs;
00561   std::vector<CSOGroup*>_selectedGroups;
00562 
00564   CSO* _currentlyUnderMouseCSO;
00565 
00567   bool _csoUndoRedoManagerEnabled;
00569   CSOUndoRedoManager* _csoUndoRedoManager;
00571   CSOManager* _csoManager;
00572 
00573 
00576   unsigned int _currentLastCSOId;
00579   unsigned int _currentLastGroupId;
00580 
00581 
00583   static std::vector<CSOList*> _registeredCSOLists;
00584 
00586   std::vector<CSONotificationCBData>_notificationObservers;
00587 
00589   std::map<std::string, int>_csoLabelUniqueIdMap;
00590   
00592   std::map<std::string, int>_groupLabelUniqueIdMap;
00593 
00595   bool _enableRepaintObserver;
00597   bool _enableFinishedObserver;
00599   bool _enableInteractionObserver;
00601   bool _enableSelectionObserver;
00603   bool _enableStartNewCSOObserver;
00605   bool _enableCSOMovedObserver;
00607   bool _enableCSOCurrentlyUnderMouseObserver;
00608 
00609 
00610   /* METHODS */
00611 
00613   inline bool _canUseUndoRedo(bool shouldUseUndoRedo) const { return shouldUseUndoRedo && _csoUndoRedoManagerEnabled && _csoUndoRedoManager; }
00615   void _setDefaultValues(CSO* cso);
00617   void _setDefaultValues(CSOGroup* group);
00619   void _setDefaultRules(CSOGroup* group);
00620 
00623   bool _fromStreamV1(std::istream& inStream, std::string& message);
00626   bool _fromStreamV3(std::istream& inStream, std::string& message);
00629   bool _fromStreamV4(std::istream& inStream, std::string& message);
00632   bool _fromStreamV5(std::istream& inStream, bool asBinary, std::string& message);
00633   
00634 
00637   bool _checkLine(const std::string& line, const std::string& name, std::string& errorMessage);
00638 
00640   inline std::string spaces(unsigned int numSpaces) const 
00641   {
00642     std::stringstream result;
00643     for (unsigned int i = 0; i < numSpaces; ++i) 
00644     {
00645       result << " ";
00646     }
00647     return result.str();
00648   }
00649   
00651   std::string buildLabel(std::string label, std::string format, int id)
00652   {
00653     //Example: buildLabel("cso", " (%i)", 9) returns "cso (0)"
00654     size_t p = format.find("%i");
00655     if (p == std::string::npos) 
00656     {
00657       format.append("%i");
00658       p = format.find("%i");
00659     }
00660     std::stringstream ss;
00661     ss << id;
00662     std::string tmp = format.replace(p, 2, ss.str()); 
00663     return label.append(tmp);
00664   }
00665 
00667   static void registerCSOList(CSOList* csoList);
00669   static void deregisterCSOList(CSOList* csoList);
00670 
00671   ML_CLASS_HEADER(CSOList)
00672 };
00673 
00675 
00676 
00677 ML_END_NAMESPACE
00678 
00679 
00680 #endif // __CSOLIST_H
00681