MeVisLab Toolbox Reference
mlListContainer.h
Go to the documentation of this file.
1 /*************************************************************************************
2 **
3 ** Copyright 2007, MeVis Medical Solutions AG
4 **
5 ** The user may use this file in accordance with the license agreement provided with
6 ** the Software or, alternatively, in accordance with the terms contained in a
7 ** written agreement between the user and MeVis Medical Solutions AG.
8 **
9 ** For further information use the contact form at https://www.mevislab.de/contact
10 **
11 **************************************************************************************/
12 
13 #ifndef ML_LIST_CONTAINER_H
14 #define ML_LIST_CONTAINER_H
15 
16 
18 
19 // Defines the following classes:
20 // - ListContainerBase: An abstract ML module class implementing basic functionality
21 // for a list container module.
22 // - ListContainerTemplate: Class template for container modules for a specific list
23 // class. Can also be used as a base class for viewer or editor modules.
24 
25 // ML-includes
26 #include "mlModuleIncludes.h"
27 #include "mlBaseInit.h"
28 
29 
30 
31 #include "mlListBase.h"
32 #include "mlRangeCasts.h"
33 
34 
35 ML_START_NAMESPACE
36 
37 
40 typedef bool ListContainerHandleNotificationCB(void *usrData, Field* field);
41 
42 // ------------------------------------------------------------------
44 // ------------------------------------------------------------------
45 
71 {
72 
73 public:
74 
77 
82  ListContainerBase (ListBase *listBasePtr, int inputNum, int outputNum);
83 
87  void handleNotification (Field *field) override;
88 
90  void activateAttachments () override;
91 
93  void beginSaveFields () override;
94 
96  void endSaveFields () override;
97 
99 
100 
103  void *userData = nullptr);
104 
105 
116  virtual void doDeleteAll ();
117 
119  virtual void doDeleteItem (MLssize_t index);
120 
124  virtual void doInsertItem (MLssize_t index, bool fromCurrentItem);
125 
130  virtual void doModifyItem (MLssize_t index, Field *field, bool fromCurrentItem);
131 
133  virtual void doCopyItemToTemplate (MLssize_t index);
134 
136  virtual void doClearItem (MLssize_t index);
137 
139  virtual void doSelectItem (MLssize_t index);
140 
142  virtual void doUpdate ();
143 
145 
147  BaseField* getOutputField() { return _fldOutputList; }
148 
149  // Returns the maximum number of items.
150  MLint maxNumItems() const { return _fldMaxSize->getIntValue(); }
151 
154  {
155  OvwNone = 0,
160  OvwNumModes
161  };
162 
164  int overflowMode() const { return _fldOverflowMode->getEnumValue(); }
165 
166 
167 protected:
168 
174  virtual ListBase *getInternalList () = 0;
175 
181  virtual bool setActiveList (Base *basePtr) = 0;
182 
184  virtual bool ownsList ()
185  { return !_fldInputList || (_listBasePtr != _fldInputList->getBaseValue()); }
186 
188  virtual MLssize_t getId (MLssize_t index) = 0;
189 
191 
192 
198  virtual bool isPropertyField (Field *field);
199 
201  virtual void resetPropertyFields ();
202 
204  virtual void updatePropertyFields () = 0;
205 
207 
208 
216  virtual void deleteItems (MLssize_t index, MLssize_t num) = 0;
217 
221  virtual void insertItem (MLssize_t index, bool fromCurrentItem) = 0;
222 
228  virtual bool modifyItem (MLssize_t index, Field *field, bool fromCurrentItem) = 0;
229 
231  virtual void copyItemToTemplate (MLssize_t index) = 0;
232 
234  virtual void copyTemplateToCurrent () = 0;
235 
238  virtual void initItem (MLssize_t index) = 0;
239 
241 
242 
247  virtual void updateListString (bool forceEnable = false);
248 
250  virtual void updateDisplay ();
251 
253  virtual void updateActionFields ();
254 
262  virtual void touchList (ListBase::ActionClass actionClass, MLssize_t id, MLssize_t index);
263 
265 
266 
267 
270 
273 
277 
282 
286 
291 
294 
300 
305 
307 
309 
310 
312 
313 
314 private:
315 
320  ListBase *_listBasePtr;
321 
323  std::string _listStringBackup;
324 
326  ListContainerHandleNotificationCB *_handleNotificationCB;
327 
329  void* _handleNotificationUserData;
330 
332 
333 };
334 
335 
336 
337 
338 
339 
340 // ------------------------------------------------------------------
342 // ------------------------------------------------------------------
343 
372  template <class T>
374  {
375 public:
376 
380  ListContainerTemplate (int inputNum, int outputNum);
381 
383  ~ListContainerTemplate () override {}
384 
385 
390  virtual T *getList () { return _listPtr; }
391 
392 
396  typename T::itemType _currentItem;
397 
398 
399 protected:
400 
403 
407  bool setActiveList (Base *basePtr) override;
408 
410  ListBase *getInternalList () override { return &_list; }
411 
413  MLssize_t getId (MLssize_t index) override;
414 
416 
417 
421  void updatePropertyFields () override;
422 
424 
425 
426 
430  void deleteItems (MLssize_t index, MLssize_t num) override;
431 
434  void insertItem (MLssize_t index, bool fromCurrentItem) override;
435 
440  bool modifyItem (MLssize_t index, Field *field, bool fromCurrentItem) override;
441 
443  void copyItemToTemplate (MLssize_t index) override;
444 
446  void copyTemplateToCurrent () override;
447 
451  void initItem (MLssize_t index) override;
452 
454 
455 
461 
462 private:
463 
464  //------------------------------------------------------------------------------------
467  //------------------------------------------------------------------------------------
469  {
470  ML_PRINT_FATAL_ERROR("ListContainerTemplate::ListContainerTemplate(const ListContainerTemplate &)",
472  "Usage of copy constructor of ListContainerTemplate is not supported.");
473  }
474 
475  //------------------------------------------------------------------------------------
478  //------------------------------------------------------------------------------------
479  ListContainerTemplate& operator=(const ListContainerTemplate &)
480  {
481  ML_PRINT_FATAL_ERROR("ListContainerTemplate& ListContainerTemplate::operator=(const ListContainerTemplate &)",
483  "Usage of assignment operator of ListContainerTemplate is not supported.");
484  return *this;
485  }
486 
487 
488 
490  T _list;
491 
492 };
493 
494 
496 template <class T>
498 : ListContainerBase(&_list, inputNum, outputNum),
499 _listPtr(&_list)
500 {
501 
502  // Suppress handleNotification()
503  _lockNotification = 1;
504 
505  // Get runtime type and name of this class and check for valid registration.
506  const RuntimeType *rt = T::getClassTypeId();
508  std::string rtName = rt->getName();
509 
510  // Add output list field
511 
512  std::string listFieldName = std::string("out")+rtName;
513 
514  _fldOutputList = getFieldContainer()->addBase(listFieldName.c_str());
516 
517  // Add input list field
518  listFieldName = std::string("in")+rtName;
519  _fldInputList = getFieldContainer()->addBase(listFieldName.c_str());
521 
522  // Allow handleNotification()
523  _lockNotification = 0;
524 }
525 
526 
530 template <class T>
532 {
533 
534  bool listValid = false;
535 
536  if (basePtr && ML_BASE_IS_A(basePtr, T))
537  {
538  _listPtr = static_cast<T*>(basePtr);
539  listValid = true;
540  } else {
541  _listPtr = nullptr;
542  }
543 
544  return listValid;
545 }
546 
547 
549 template <class T>
551 {
552 
553  return (_listPtr && (index >= 0) && (index < static_cast<MLssize_t>(_listPtr->size())) )
554  ? (*_listPtr)[mlrange_cast<size_t>(index)].getId() : -1;
555 }
556 
557 
559 template <class T>
561 {
562 
563  const MLssize_t index = _fldIndex->getIntValue();
564 
565  if (_listPtr && (index >= 0) && (index < static_cast<MLssize_t>(_listPtr->size())) )
566  {
567  _lockNotification++;
568 
569  const BaseItem *item = &(*_listPtr)[mlrange_cast<size_t>(index)];
570 
571  _fldId->setIntValue(item->getId());
572  _fldName->setStringValue(item->name() ? item->name() : "");
573 
574  _lockNotification--;
575  }
576 }
577 
578 
580 template <class T>
582 {
583  typename T::iterator it;
584  const MLssize_t listSize = static_cast<MLssize_t>(_listPtr->size());
585  if (_listPtr && (index >= 0) && (index < listSize) && (num > 0))
586  {
587  if (index+num > listSize){
588  num = listSize-index;
589  }
590  it = _listPtr->begin()+index;
591  _listPtr->erase(it, it+num);
592  }
593 }
594 
595 
598 template <class T>
599 void ListContainerTemplate<T>::insertItem (MLssize_t index, bool fromCurrentItem)
600 {
601  if (_listPtr && (index >= 0) && (index <= static_cast<MLssize_t>(_listPtr->size())) )
602  {
603  if (fromCurrentItem)
604  {
605  // Generate new id and store in _currentItem
606  _currentItem.setId(_listPtr->newId());
607  // Insert copy of _currentItem
608  _listPtr->insert(_listPtr->begin()+index, _currentItem);
609 
610  } else {
611 
612  typename T::itemType tmpItem;
613  // Insert new item and initialize
614  _listPtr->insert(_listPtr->begin()+index, tmpItem);
615 
616  // explicitly set the name to an empty string,
617  // because the initial name is a NULL pointer using an item's standard constructor
618  (*_listPtr)[mlrange_cast<size_t>(index)].setName("");
619 
620  initItem(index);
621  }
622  }
623 }
624 
625 
630 template <class T>
631 bool ListContainerTemplate<T>::modifyItem (MLssize_t index, Field *field, bool fromCurrentItem)
632 {
633  bool modified = true;
634 
635  if (_listPtr && (index >= 0) && (index < static_cast<MLssize_t>(_listPtr->size())) )
636  {
637  if (fromCurrentItem){
638 
639  // Copy _currentItem to list item
640  const MLssize_t oldId = (*_listPtr)[mlrange_cast<size_t>(index)].getId();
641  (*_listPtr)[mlrange_cast<size_t>(index)] = _currentItem;
642  (*_listPtr)[mlrange_cast<size_t>(index)].setId(oldId);
643  }
644  else if (field == _fldId)
645  {
646  // Reject change to item id
647  _fldId->setIntValue((*_listPtr)[mlrange_cast<size_t>(index)].getId());
648  modified = false;
649  }else if (field == _fldName) {
650  // Change item name
651  (*_listPtr)[mlrange_cast<size_t>(index)].setName(_fldName->getStringValue().c_str());
652  } else {
653  // Not a property field
654  modified = false;
655  }
656  } else {
657  // Invalid item or list
658  modified = false;
659  }
660 
661  return modified;
662 }
663 
665 template <class T>
667 {
668 
669  typename T::itemType *item= &(*_listPtr)[mlrange_cast<size_t>(index)];
670 
671  _fldNewName->setStringValue( item->name() ? item->name() : "" );
672 
673 }
674 
676 template <class T>
678 {
679 
680  _currentItem.setName( _fldNewName->getStringValue().c_str() );
681 
682 }
683 
687 template <class T>
689 {
690 
691  typename T::itemType *item= &(*_listPtr)[mlrange_cast<size_t>(index)];
692 
693  if (item->getId() == 0) {
694  // only assign a new id if the id is 0,
695  // because we do not want to assign a new id if the clear-field is triggered
696  // (0 is the initial id of an item, the first id returned by newId is 1)
697  item->setId(_listPtr->newId());
698  }
699 
700  item->setName("");
701 
702 }
703 
704 
705 ML_END_NAMESPACE
706 
707 #endif // __mlListContainer_H
@ T
Definition: SoKeyGrabber.h:71
Field to encapsulate a pointer to an ML base object.
Definition: mlFields.h:1187
void setBaseValueAndAddAllowedType(T *value)
convenience routine for setting the base value and its type at the same time.
Definition: mlFields.h:1238
General Base object class for list items that have an id and a name.
Definition: mlBaseItem.h:38
const char * name() const
Get name.
Definition: mlBaseItem.h:70
MLssize_t getId() const
Sets the id of the item.
Definition: mlBaseItem.h:59
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition: mlBase.h:62
Field to encapsulate a boolean value.
Definition: mlFields.h:62
Field to encapsulate an enumerated value.
Definition: mlFields.h:363
BaseField * addBase(const char *name)
Creates a Base field with name and adds it to the container. Default value is NULL.
Base class for all fields used in the ML.
Definition: mlField.h:73
Field to encapsulate an integer value.
Definition: mlFields.h:161
Base object class ListBase managing a number of BaseItem objects.
Definition: mlListBase.h:63
ActionClass
Constants to describe the type of action most recently performed.
Definition: mlListBase.h:188
Abstract module class ListContainerBase implementing basic functionality for a list container module.
IntField * _fldId
List item id.
virtual void insertItem(MLssize_t index, bool fromCurrentItem)=0
Insert an item at position index.
virtual void doModifyItem(MLssize_t index, Field *field, bool fromCurrentItem)
Modify item at position index.
virtual void copyTemplateToCurrent()=0
Copy the values of the templates fields to _currentItem.
StringField * _fldListString
String representation of list.
NotifyField * _fldCopyItemToTemplate
Copy values from current item to template fields.
virtual void updateActionFields()
Update last action fields.
void beginSaveFields() override
Prepare for persistence.
virtual void updatePropertyFields()=0
Update property fields from the current list item.
StringField * _fldActionClass
Action class of last action on list.
IntField * _fldCurrentIndex
Index of currently selected item.
virtual bool isPropertyField(Field *field)
virtual void updateDisplay()
Update display of list size, current item index and item properties.
OverflowModes
Mode constants for cases of list size overflow.
@ OvwRemoveFirst
Overflow ignored.
@ OvwRemoveAll
Last item(s) deleted.
@ OvwRemoveLast
First item(s) deleted.
@ OvwRemoveNew
All item(s) deleted.
void endSaveFields() override
Clean up after persistence.
StringField * _fldName
List item name.
virtual void updateListString(bool forceEnable=false)
virtual void doDeleteAll()
int _lockNotification
Suppress handleNotification() if non-zero.
NotifyField * _fldAdd
Add (= Append) button.
NotifyField * _fldDelete
Delete button.
virtual void doCopyItemToTemplate(MLssize_t index)
Copy values from item at position index to the template fields.
StringField * _fldNewName
List item name.
EnumField * _fldOverflowMode
Overflow mode, specifies which item(s) to delete on overflow.
BoolField * _fldUpToDate
Indicates that the property values are up to date.
IntField * _fldMaxSize
Maximum list size.
NotifyField * _fldCopyTemplateToItem
Copy values from template fields to current item.
virtual void doUpdate()
Update all fields and touch output list field.
virtual bool modifyItem(MLssize_t index, Field *field, bool fromCurrentItem)=0
Modify item at position index.
BaseField * getOutputField()
Returns a pointer to the output field.
BoolField * _fldListStringEnable
Enable list string field.
virtual bool ownsList()
Return true if the active list is the internal list.
MLint maxNumItems() const
NotifyField * _fldInsert
Insert button.
IntField * _fldActionIndex
Index of last action on list.
BoolField * _fldApplySelect
Perform a Select-action when index is changed.
virtual void doSelectItem(MLssize_t index)
Select item at position index, or deselect if item == -1.
BaseField * _fldInputList
Input list field, initialized by derived class.
ListContainerBase()
Constructor.
virtual void touchList(ListBase::ActionClass actionClass, MLssize_t id, MLssize_t index)
Set last list action and touch output list field.
void setHandleNotificationCB(ListContainerHandleNotificationCB *cb=nullptr, void *userData=nullptr)
Set the callback for handleNotification.
void handleNotification(Field *field) override
Called when any field data in the field container of this module is modified.
virtual void doDeleteItem(MLssize_t index)
Delete single item at position index.
BoolField * _fldUseInsertTemplate
If true the container should use the template fields for initializing the inserted item.
NotifyField * _fldUpdate
Update button.
BaseField * _fldOutputList
Output list field, initialized by derived class.
virtual void doInsertItem(MLssize_t index, bool fromCurrentItem)
Insert an item at position index.
IntField * _fldNumItems
List size.
void activateAttachments() override
Update fields after an initialization without handleNotification() called.
virtual void resetPropertyFields()
Reset all property fields.
virtual void deleteItems(MLssize_t index, MLssize_t num)=0
virtual void copyItemToTemplate(MLssize_t index)=0
Copy values from item at position index to the template fields.
ListContainerBase(ListBase *listBasePtr, int inputNum, int outputNum)
Constructor In listBasePtr pass a pointer to the list object, which has to be a member of the derived...
int overflowMode() const
Returns the overflow mode.
virtual void initItem(MLssize_t index)=0
Initialize the list item at position index.
NotifyField * _fldDeleteAll
Delete All button.
virtual bool setActiveList(Base *basePtr)=0
Set the active list (i.e.
virtual void doClearItem(MLssize_t index)
Init item at position index.
BoolField * _fldOwnsList
Reflect owner state (true if internal list is active)
NotifyField * _fldClearItem
Clear the current item (i.e. init it again).
virtual ListBase * getInternalList()=0
virtual MLssize_t getId(MLssize_t index)=0
Return the item id of the item index.
BoolField * _fldPersistent
Activate internal list persistence.
IntField * _fldIndex
Current list index.
IntField * _fldActionId
Item id of last action on list.
Template module class ListContainerTemplate for a specific list class.
void copyTemplateToCurrent() override
Copy the values of the templates fields to _currentItem.
T::itemType _currentItem
List item object used by insertItem() and modifyItem().
void insertItem(MLssize_t index, bool fromCurrentItem) override
Insert an item at position index.
ListContainerTemplate(int inputNum, int outputNum)
Constructor The values inputNum and outputNum specify the number of input and output image fields,...
MLssize_t getId(MLssize_t index) override
Return the item id of the item index.
void copyItemToTemplate(MLssize_t index) override
Copy values from item at position index to the template fields.
T * _listPtr
Pointer to the active list object Use this pointer for list access after testing that it is !...
bool setActiveList(Base *basePtr) override
Set the active list pointer _listPtr to basePtr, provided that it is of the correct type.
void deleteItems(MLssize_t index, MLssize_t num) override
Remove num items, starting at item index.
virtual T * getList()
Get pointer to the active list object Use this pointer for list access after testing that it is !...
~ListContainerTemplate() override
Destructor.
void updatePropertyFields() override
Update property fields from the current list item.
void initItem(MLssize_t index) override
Initialize the list item at position index.
bool modifyItem(MLssize_t index, Field *field, bool fromCurrentItem) override
Modify item at position index.
ListBase * getInternalList() override
Return address of internal list object.
Base class for an image processing module of the ML.
Definition: mlModule.h:156
FieldContainer * getFieldContainer()
Returns a pointer to the container of all the module's fields.
Definition: mlModule.h:777
Field without value for notifications.
Definition: mlFields.h:1049
RuntimeType contains type and inheritance information of a class and a static dictionary with informa...
Definition: mlRuntimeType.h:53
const char * getName() const
Returns the null terminated string name of the class. Returns "BadType" on error.
Definition: mlRuntimeType.h:84
Field to encapsulate a string value.
Definition: mlFields.h:1000
#define ML_ABSTRACT_MODULE_CLASS_HEADER(className)
Like ML_ABSTRACT_CLASS_HEADER for the usage of derived classes from Module.
#define ML_BASE_IS_A(base, type)
This file defines macros, which are inserted in classes to declare and implement additional class mem...
#define ML_PROGRAMMING_ERROR
A case occurred which should not appear and here are a variety of reasons, typically it is a programm...
Definition: mlTypeDefs.h:890
#define ML_PRINT_FATAL_ERROR(FUNC_NAME, REASON, HANDLING)
Like ML_PRINT_FATAL_ERROR_DUMP(FUNC_NAME, REASON, HANDLING, RT_OBJ) without a runtime object to be du...
#define MLBASEEXPORT
defined Header file mlBaseInit.h
Definition: mlBaseInit.h:22
#define ML_CHECK_RUNTIME_TYPE(x)
MLint64 MLint
A signed ML integer type with at least 64 bits used for index calculations on very large images even ...
Definition: mlTypeDefs.h:578
SSIZE_T MLssize_t
The signed ML size type which is a signed 32 bit size_t on 32 bit platforms and 64 bit one on 64 bit ...
Definition: mlTypeDefs.h:654
bool ListContainerHandleNotificationCB(void *usrData, Field *field)
Callback for handleNotification forwarding (return false if notification should not be propagated to ...