MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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#include "mlListBase.h"
31#include "mlRangeCasts.h"
32
33
35
36
40
41// ------------------------------------------------------------------
43// ------------------------------------------------------------------
44
70{
71
72public:
75 : Module(0, 0) {};
76
82
87
89 void activateAttachments() override;
90
92 void beginSaveFields() override;
93
95 void endSaveFields() override;
96
98
99
101 void setHandleNotificationCB(ListContainerHandleNotificationCB* cb = nullptr, void* userData = nullptr);
102
103
114 virtual void doDeleteAll();
115
117 virtual void doDeleteItem(MLssize_t index);
118
122 virtual void doInsertItem(MLssize_t index, bool fromCurrentItem);
123
128 virtual void doModifyItem(MLssize_t index, Field* field, bool fromCurrentItem);
129
131 virtual void doCopyItemToTemplate(MLssize_t index);
132
134 virtual void doClearItem(MLssize_t index);
135
137 virtual void doSelectItem(MLssize_t index);
138
140 virtual void doUpdate();
141
143
145 void notify();
146
149 {
150 return _fldOutputList;
151 }
152
153 // Returns the maximum number of items.
155 {
156 return _fldMaxSize->getIntValue();
157 }
158
168
170 int overflowMode() const
171 {
172 return _fldOverflowMode->getEnumValue();
173 }
174
175
176protected:
182 virtual ListBase* getInternalList() = 0;
183
189 virtual bool setActiveList(Base* basePtr) = 0;
190
192 virtual bool ownsList()
193 {
194 return !_fldInputList || (_listBasePtr != _fldInputList->getBaseValue());
195 }
196
198 virtual MLssize_t getId(MLssize_t index) = 0;
199
201
202
209
211 virtual void resetPropertyFields();
212
214 virtual void updatePropertyFields() = 0;
215
217
218
226 virtual void deleteItems(MLssize_t index, MLssize_t num) = 0;
227
231 virtual void insertItem(MLssize_t index, bool fromCurrentItem) = 0;
232
238 virtual bool modifyItem(MLssize_t index, Field* field, bool fromCurrentItem) = 0;
239
241 virtual void copyItemToTemplate(MLssize_t index) = 0;
242
244 virtual void copyTemplateToCurrent() = 0;
245
248 virtual void initItem(MLssize_t index) = 0;
249
251
252
257 virtual void updateListString(bool forceEnable = false);
258
260 virtual void updateDisplay();
261
263 virtual void updateActionFields();
264
273
275
276
279
282
286
291
295
300
303
309
315
317
318 BoolField*
322
323
325
326
327private:
332 ListBase* _listBasePtr;
333
335 std::string _listStringBackup;
336
338 ListContainerHandleNotificationCB* _handleNotificationCB;
339
341 void* _handleNotificationUserData;
342
343 void _copyAndTakeOwnership();
344
346};
347
348
349// ------------------------------------------------------------------
351// ------------------------------------------------------------------
352
381template <class T> class ListContainerTemplate : public ListContainerBase
382{
383public:
388
391 {}
392
393
398 virtual T* getList()
399 {
400 return _listPtr;
401 }
402
403
407 typename T::itemType _currentItem;
408
409
410protected:
413
417 bool setActiveList(Base* basePtr) override;
418
421 {
422 return &_list;
423 }
424
426 MLssize_t getId(MLssize_t index) override;
427
429
430
434 void updatePropertyFields() override;
435
437
438
442 void deleteItems(MLssize_t index, MLssize_t num) override;
443
446 void insertItem(MLssize_t index, bool fromCurrentItem) override;
447
452 bool modifyItem(MLssize_t index, Field* field, bool fromCurrentItem) override;
453
455 void copyItemToTemplate(MLssize_t index) override;
456
458 void copyTemplateToCurrent() override;
459
463 void initItem(MLssize_t index) override;
464
466
467
473
474private:
475 //------------------------------------------------------------------------------------
478 //------------------------------------------------------------------------------------
480 {
481 ML_PRINT_FATAL_ERROR("ListContainerTemplate::ListContainerTemplate(const ListContainerTemplate &)",
482 ML_PROGRAMMING_ERROR, "Usage of copy constructor of ListContainerTemplate is not supported.");
483 }
484
485 //------------------------------------------------------------------------------------
488 //------------------------------------------------------------------------------------
490 {
491 ML_PRINT_FATAL_ERROR("ListContainerTemplate& ListContainerTemplate::operator=(const ListContainerTemplate &)",
493 "Usage of assignment operator of ListContainerTemplate is not supported.");
494 return *this;
495 }
496
497
499 T _list;
500};
501
502
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
523
524 // Add input list field
525 listFieldName = std::string("in") + rtName;
528
529 // Allow handleNotification()
531}
532
533
538{
539
540 bool listValid = false;
541
542 if (basePtr && ML_BASE_IS_A(basePtr, T)) {
543 _listPtr = static_cast<T*>(basePtr);
544 listValid = true;
545 }
546 else {
547 _listPtr = nullptr;
548 }
549
550 return listValid;
551}
552
553
556{
557
558 return (_listPtr && (index >= 0) && (index < static_cast<MLssize_t>(_listPtr->size())))
559 ? (*_listPtr)[mlrange_cast<size_t>(index)].getId()
560 : -1;
561}
562
563
566{
567
568 const MLssize_t index = _fldIndex->getIntValue();
569
570 if (_listPtr && (index >= 0) && (index < static_cast<MLssize_t>(_listPtr->size()))) {
571 _lockNotification++;
572
573 const BaseItem* item = &(*_listPtr)[mlrange_cast<size_t>(index)];
574
575 _fldId->setIntValue(item->getId());
576 _fldName->setStringValue(item->name() ? item->name() : "");
577
578 _lockNotification--;
579 }
580}
581
582
585{
586 typename T::iterator it;
587 const MLssize_t listSize = static_cast<MLssize_t>(_listPtr->size());
588 if (_listPtr && (index >= 0) && (index < listSize) && (num > 0)) {
589 if (index + num > listSize) {
590 num = listSize - index;
591 }
592 it = _listPtr->begin() + index;
593 _listPtr->erase(it, it + num);
594 }
595}
596
597
601{
602 if (_listPtr && (index >= 0) && (index <= static_cast<MLssize_t>(_listPtr->size()))) {
603 if (fromCurrentItem) {
604 // Generate new id and store in _currentItem
605 _currentItem.setId(_listPtr->newId());
606 // Insert copy of _currentItem
607 _listPtr->insert(_listPtr->begin() + index, _currentItem);
608 }
609 else {
610
611 typename T::itemType tmpItem;
612 // Insert new item and initialize
613 _listPtr->insert(_listPtr->begin() + index, tmpItem);
614
615 // explicitly set the name to an empty string,
616 // because the initial name is a NULL pointer using an item's standard constructor
617 (*_listPtr)[mlrange_cast<size_t>(index)].setName("");
618
619 initItem(index);
620 }
621 }
622}
623
624
630{
631 bool modified = true;
632
633 if (_listPtr && (index >= 0) && (index < static_cast<MLssize_t>(_listPtr->size()))) {
634 if (fromCurrentItem) {
635
636 // Copy _currentItem to list item
637 const MLssize_t oldId = (*_listPtr)[mlrange_cast<size_t>(index)].getId();
638 (*_listPtr)[mlrange_cast<size_t>(index)] = _currentItem;
639 (*_listPtr)[mlrange_cast<size_t>(index)].setId(oldId);
640 }
641 else if (field == _fldId) {
642 // Reject change to item id
643 _fldId->setIntValue((*_listPtr)[mlrange_cast<size_t>(index)].getId());
644 modified = false;
645 }
646 else if (field == _fldName) {
647 // Change item name
648 (*_listPtr)[mlrange_cast<size_t>(index)].setName(_fldName->getStringValue().c_str());
649 }
650 else {
651 // Not a property field
652 modified = false;
653 }
654 }
655 else {
656 // Invalid item or list
657 modified = false;
658 }
659
660 return modified;
661}
662
665{
666
667 typename T::itemType* item = &(*_listPtr)[mlrange_cast<size_t>(index)];
668
669 _fldNewName->setStringValue(item->name() ? item->name() : "");
670}
671
674{
675
676 _currentItem.setName(_fldNewName->getStringValue().c_str());
677}
678
682template <class T> void ListContainerTemplate<T>::initItem(MLssize_t index)
683{
684
685 typename T::itemType* item = &(*_listPtr)[mlrange_cast<size_t>(index)];
686
687 if (item->getId() == 0) {
688 // only assign a new id if the id is 0,
689 // because we do not want to assign a new id if the clear-field is triggered
690 // (0 is the initial id of an item, the first id returned by newId is 1)
691 item->setId(_listPtr->newId());
692 }
693
694 item->setName("");
695}
696
697
699
700#endif // __mlListContainer_H
@ T
Field to encapsulate a pointer to an ML base object.
Definition mlFields.h:813
void setBaseValueAndAddAllowedType(T *value)
Convenience routine for setting the base value and its type at the same time.
Definition mlFields.h:864
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:71
Class representing general ML objects that support import/export via strings (setPersistentState() an...
Definition mlBase.h:63
Field to encapsulate a boolean value.
Definition mlFields.h:56
Field to encapsulate an enumerated value.
Definition mlFields.h:185
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:123
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:234
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:150
Field without value for notifications.
Definition mlFields.h:687
RuntimeType contains type and inheritance information of a class and a static dictionary with informa...
Field to encapsulate a string value.
Definition mlFields.h:634
#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:844
#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)
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
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:563
bool ListContainerHandleNotificationCB(void *usrData, Field *field)
Callback for handleNotification forwarding (return false if notification should not be propagated to ...