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
35ML_START_NAMESPACE
36
37
40typedef bool ListContainerHandleNotificationCB(void *usrData, Field* field);
41
42// ------------------------------------------------------------------
44// ------------------------------------------------------------------
45
71{
72
73public:
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
102 void setHandleNotificationCB (ListContainerHandleNotificationCB *cb = nullptr,
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 void notify();
148
150 BaseField* getOutputField() { return _fldOutputList; }
151
152 // Returns the maximum number of items.
153 MLint maxNumItems() const { return _fldMaxSize->getIntValue(); }
154
165
167 int overflowMode() const { return _fldOverflowMode->getEnumValue(); }
168
169
170protected:
171
177 virtual ListBase *getInternalList () = 0;
178
184 virtual bool setActiveList (Base *basePtr) = 0;
185
187 virtual bool ownsList ()
188 { return !_fldInputList || (_listBasePtr != _fldInputList->getBaseValue()); }
189
191 virtual MLssize_t getId (MLssize_t index) = 0;
192
194
195
201 virtual bool isPropertyField (Field *field);
202
204 virtual void resetPropertyFields ();
205
207 virtual void updatePropertyFields () = 0;
208
210
211
219 virtual void deleteItems (MLssize_t index, MLssize_t num) = 0;
220
224 virtual void insertItem (MLssize_t index, bool fromCurrentItem) = 0;
225
231 virtual bool modifyItem (MLssize_t index, Field *field, bool fromCurrentItem) = 0;
232
234 virtual void copyItemToTemplate (MLssize_t index) = 0;
235
237 virtual void copyTemplateToCurrent () = 0;
238
241 virtual void initItem (MLssize_t index) = 0;
242
244
245
250 virtual void updateListString (bool forceEnable = false);
251
253 virtual void updateDisplay ();
254
256 virtual void updateActionFields ();
257
265 virtual void touchList (ListBase::ActionClass actionClass, MLssize_t id, MLssize_t index);
266
268
269
270
273
276
280
285
289
294
297
303
308
310
314
315
317
318
319private:
320
325 ListBase *_listBasePtr;
326
328 std::string _listStringBackup;
329
331 ListContainerHandleNotificationCB *_handleNotificationCB;
332
334 void* _handleNotificationUserData;
335
336 void _copyAndTakeOwnership();
337
339
340};
341
342
343
344
345
346
347// ------------------------------------------------------------------
349// ------------------------------------------------------------------
350
379 template <class T>
381 {
382public:
383
387 ListContainerTemplate (int inputNum, int outputNum);
388
391
392
397 virtual T *getList () { return _listPtr; }
398
399
403 typename T::itemType _currentItem;
404
405
406protected:
407
410
414 bool setActiveList (Base *basePtr) override;
415
417 ListBase *getInternalList () override { return &_list; }
418
420 MLssize_t getId (MLssize_t index) override;
421
423
424
428 void updatePropertyFields () override;
429
431
432
433
437 void deleteItems (MLssize_t index, MLssize_t num) override;
438
441 void insertItem (MLssize_t index, bool fromCurrentItem) override;
442
447 bool modifyItem (MLssize_t index, Field *field, bool fromCurrentItem) override;
448
450 void copyItemToTemplate (MLssize_t index) override;
451
453 void copyTemplateToCurrent () override;
454
458 void initItem (MLssize_t index) override;
459
461
462
468
469private:
470
471 //------------------------------------------------------------------------------------
474 //------------------------------------------------------------------------------------
476 {
477 ML_PRINT_FATAL_ERROR("ListContainerTemplate::ListContainerTemplate(const ListContainerTemplate &)",
479 "Usage of copy constructor of ListContainerTemplate is not supported.");
480 }
481
482 //------------------------------------------------------------------------------------
485 //------------------------------------------------------------------------------------
487 {
488 ML_PRINT_FATAL_ERROR("ListContainerTemplate& ListContainerTemplate::operator=(const ListContainerTemplate &)",
490 "Usage of assignment operator of ListContainerTemplate is not supported.");
491 return *this;
492 }
493
494
495
497 T _list;
498
499};
500
501
503template <class T>
505: ListContainerBase(&_list, inputNum, outputNum),
506_listPtr(&_list)
507{
508
509 // Suppress handleNotification()
511
512 // Get runtime type and name of this class and check for valid registration.
513 const RuntimeType *rt = T::getClassTypeId();
515 std::string rtName = rt->getName();
516
517 // Add output list field
518
519 std::string listFieldName = std::string("out")+rtName;
520
521 _fldOutputList = addBase(listFieldName.c_str());
523
524 // Add input list field
525 listFieldName = std::string("in")+rtName;
526 _fldInputList = addBase(listFieldName.c_str());
528
529 // Allow handleNotification()
531}
532
533
537template <class T>
539{
540
541 bool listValid = false;
542
543 if (basePtr && ML_BASE_IS_A(basePtr, T))
544 {
545 _listPtr = static_cast<T*>(basePtr);
546 listValid = true;
547 } else {
548 _listPtr = nullptr;
549 }
550
551 return listValid;
552}
553
554
556template <class T>
558{
559
560 return (_listPtr && (index >= 0) && (index < static_cast<MLssize_t>(_listPtr->size())) )
561 ? (*_listPtr)[mlrange_cast<size_t>(index)].getId() : -1;
562}
563
564
566template <class T>
568{
569
570 const MLssize_t index = _fldIndex->getIntValue();
571
572 if (_listPtr && (index >= 0) && (index < static_cast<MLssize_t>(_listPtr->size())) )
573 {
574 _lockNotification++;
575
576 const BaseItem *item = &(*_listPtr)[mlrange_cast<size_t>(index)];
577
578 _fldId->setIntValue(item->getId());
579 _fldName->setStringValue(item->name() ? item->name() : "");
580
581 _lockNotification--;
582 }
583}
584
585
587template <class T>
589{
590 typename T::iterator it;
591 const MLssize_t listSize = static_cast<MLssize_t>(_listPtr->size());
592 if (_listPtr && (index >= 0) && (index < listSize) && (num > 0))
593 {
594 if (index+num > listSize){
595 num = listSize-index;
596 }
597 it = _listPtr->begin()+index;
598 _listPtr->erase(it, it+num);
599 }
600}
601
602
605template <class T>
606void ListContainerTemplate<T>::insertItem (MLssize_t index, bool fromCurrentItem)
607{
608 if (_listPtr && (index >= 0) && (index <= static_cast<MLssize_t>(_listPtr->size())) )
609 {
610 if (fromCurrentItem)
611 {
612 // Generate new id and store in _currentItem
613 _currentItem.setId(_listPtr->newId());
614 // Insert copy of _currentItem
615 _listPtr->insert(_listPtr->begin()+index, _currentItem);
616
617 } else {
618
619 typename T::itemType tmpItem;
620 // Insert new item and initialize
621 _listPtr->insert(_listPtr->begin()+index, tmpItem);
622
623 // explicitly set the name to an empty string,
624 // because the initial name is a NULL pointer using an item's standard constructor
625 (*_listPtr)[mlrange_cast<size_t>(index)].setName("");
626
627 initItem(index);
628 }
629 }
630}
631
632
637template <class T>
638bool ListContainerTemplate<T>::modifyItem (MLssize_t index, Field *field, bool fromCurrentItem)
639{
640 bool modified = true;
641
642 if (_listPtr && (index >= 0) && (index < static_cast<MLssize_t>(_listPtr->size())) )
643 {
644 if (fromCurrentItem){
645
646 // Copy _currentItem to list item
647 const MLssize_t oldId = (*_listPtr)[mlrange_cast<size_t>(index)].getId();
648 (*_listPtr)[mlrange_cast<size_t>(index)] = _currentItem;
649 (*_listPtr)[mlrange_cast<size_t>(index)].setId(oldId);
650 }
651 else if (field == _fldId)
652 {
653 // Reject change to item id
654 _fldId->setIntValue((*_listPtr)[mlrange_cast<size_t>(index)].getId());
655 modified = false;
656 }else if (field == _fldName) {
657 // Change item name
658 (*_listPtr)[mlrange_cast<size_t>(index)].setName(_fldName->getStringValue().c_str());
659 } else {
660 // Not a property field
661 modified = false;
662 }
663 } else {
664 // Invalid item or list
665 modified = false;
666 }
667
668 return modified;
669}
670
672template <class T>
674{
675
676 typename T::itemType *item= &(*_listPtr)[mlrange_cast<size_t>(index)];
677
678 _fldNewName->setStringValue( item->name() ? item->name() : "" );
679
680}
681
683template <class T>
685{
686
687 _currentItem.setName( _fldNewName->getStringValue().c_str() );
688
689}
690
694template <class T>
696{
697
698 typename T::itemType *item= &(*_listPtr)[mlrange_cast<size_t>(index)];
699
700 if (item->getId() == 0) {
701 // only assign a new id if the id is 0,
702 // because we do not want to assign a new id if the clear-field is triggered
703 // (0 is the initial id of an item, the first id returned by newId is 1)
704 item->setId(_listPtr->newId());
705 }
706
707 item->setName("");
708
709}
710
711
712ML_END_NAMESPACE
713
714#endif // __mlListContainer_H
@ T
Field to encapsulate a pointer to an ML base object.
Definition mlFields.h:729
void setBaseValueAndAddAllowedType(T *value)
Convenience routine for setting the base value and its type at the same time.
Definition mlFields.h:780
General Base object class for list items that have an id and a name.
Definition mlBaseItem.h:38
void setId(MLssize_t id)
Sets the id of the item.
Definition mlBaseItem.h:62
MLssize_t getId() const
Sets the id of the item.
Definition mlBaseItem.h:65
const char * name() const
Get name.
Definition mlBaseItem.h:76
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition mlBase.h:59
Field to encapsulate a boolean value.
Definition mlFields.h:56
Field to encapsulate an enumerated value.
Definition mlFields.h:173
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:117
Base object class ListBase managing a number of BaseItem objects.
Definition mlListBase.h:64
ActionClass
Constants to describe the type of action most recently performed.
Definition mlListBase.h:202
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.
BoolField * _fldAutoCopyAndTakeOwnership
If true, the ownership is taken automatically on connecting the input field.
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.
virtual ListBase * getInternalList()=0
NotifyField * _fldCopyAndTakeOwnership
Takes the ownership of an XMarkerList.
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.
BoolField * _fldListStringEnable
Enable list string field.
virtual bool ownsList()
Return true if the active list is the internal list.
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.
void notify()
Notifies this container and attached modules of a change.
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).
BaseField * getOutputField()
Returns a pointer to the output field.
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 !...
virtual T * getList()
Get pointer to the active list object Use this pointer for list access after testing that it is !...
ListBase * getInternalList() override
Return address of internal list object.
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.
~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.
Base class for an image processing module of the ML.
Definition mlModule.h:151
Field without value for notifications.
Definition mlFields.h:598
RuntimeType contains type and inheritance information of a class and a static dictionary with informa...
const char * getName() const
Returns the NULL-terminated string name of the class. Returns 'BadType' on error.
Field to encapsulate a string value.
Definition mlFields.h:553
#define ML_ABSTRACT_MODULE_CLASS_HEADER(className)
Similar to ML_ABSTRACT_CLASS_HEADER for the usage of derived classes from Module.
#define ML_BASE_IS_A(base, type)
This file defines macros that are inserted in classes to declare and implement additional class membe...
#define ML_PROGRAMMING_ERROR
A case occurred that should not appear and there are a variety of reasons, typically it is a programm...
Definition mlTypeDefs.h:787
#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:489
SSIZE_T MLssize_t
The signed ML size type that is a signed 32-bit size_t on 32-bit platforms and 64-bit one on 64-bit p...
Definition mlTypeDefs.h:565
bool ListContainerHandleNotificationCB(void *usrData, Field *field)
Callback for handleNotification forwarding (return false if notification should not be propagated to ...