1 /*
2  *
3  *  Copyright (C) 1999-2010, OFFIS e.V.
4  *  All rights reserved.  See COPYRIGHT file for details.
5  *
6  *  This software and supporting documentation were developed by
7  *
8  *    OFFIS e.V.
9  *    R&D Division Health
10  *    Escherweg 2
11  *    D-26121 Oldenburg, Germany
12  *
13  *
14  *  Module: dcmpstat
15  *
16  *  Author: Marco Eichelberg
17  *
18  *  Purpose:
19  *    classes: DVPSAnnotationContent_PList
20  *
21  */
22 
23 #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
24 #include "dcmtk/dcmdata/dcdeftag.h"
25 #include "dcmtk/dcmpstat/dvpsabl.h"
26 #include "dcmtk/dcmpstat/dvpsab.h"      /* for DVPSAnnotationContent */
27 #include "dcmtk/dcmpstat/dvpshlp.h"     /* for class DVPSHelper */
28 
29 /* --------------- class DVPSAnnotationContent_PList --------------- */
30 
DVPSAnnotationContent_PList()31 DVPSAnnotationContent_PList::DVPSAnnotationContent_PList()
32 : list_()
33 {
34 }
35 
DVPSAnnotationContent_PList(const DVPSAnnotationContent_PList & arg)36 DVPSAnnotationContent_PList::DVPSAnnotationContent_PList(const DVPSAnnotationContent_PList &arg)
37 : list_()
38 {
39   OFListConstIterator(DVPSAnnotationContent *) first = arg.list_.begin();
40   OFListConstIterator(DVPSAnnotationContent *) last = arg.list_.end();
41   while (first != last)
42   {
43     list_.push_back((*first)->clone());
44     ++first;
45   }
46 }
47 
~DVPSAnnotationContent_PList()48 DVPSAnnotationContent_PList::~DVPSAnnotationContent_PList()
49 {
50   clear();
51 }
52 
clear()53 void DVPSAnnotationContent_PList::clear()
54 {
55   OFListIterator(DVPSAnnotationContent *) first = list_.begin();
56   OFListIterator(DVPSAnnotationContent *) last = list_.end();
57   while (first != last)
58   {
59     delete (*first);
60     first = list_.erase(first);
61   }
62 }
63 
read(DcmItem & dset)64 OFCondition DVPSAnnotationContent_PList::read(DcmItem &dset)
65 {
66   OFCondition result = EC_Normal;
67   DcmStack stack;
68   DVPSAnnotationContent *newAnnotation = NULL;
69   DcmSequenceOfItems *dseq=NULL;
70   DcmItem *ditem=NULL;
71 
72   if (EC_Normal == dset.search(DCM_RETIRED_AnnotationContentSequence, stack, ESM_fromHere, OFFalse))
73   {
74     dseq=(DcmSequenceOfItems *)stack.top();
75     if (dseq)
76     {
77       unsigned long numItems = dseq->card();
78       for (unsigned int i=0; i<numItems; i++)
79       {
80         ditem = dseq->getItem(i);
81         newAnnotation = new DVPSAnnotationContent();
82         if (newAnnotation && ditem)
83         {
84           result = newAnnotation->read(*ditem);
85           list_.push_back(newAnnotation);
86         } else result = EC_MemoryExhausted;
87       }
88     }
89   }
90 
91   return result;
92 }
93 
write(DcmItem & dset)94 OFCondition DVPSAnnotationContent_PList::write(DcmItem &dset)
95 {
96   if (size()==0) return EC_Normal; // don't write if sequence is empty
97 
98   OFCondition result = EC_Normal;
99   DcmSequenceOfItems *dseq=NULL;
100   DcmItem *ditem=NULL;
101 
102   dseq = new DcmSequenceOfItems(DCM_RETIRED_AnnotationContentSequence);
103   if (dseq)
104   {
105     OFListIterator(DVPSAnnotationContent *) first = list_.begin();
106     OFListIterator(DVPSAnnotationContent *) last = list_.end();
107     while (first != last)
108     {
109       if (result==EC_Normal)
110       {
111         ditem = new DcmItem();
112         if (ditem)
113         {
114           result = (*first)->write(*ditem);
115           if (result==EC_Normal) dseq->insert(ditem); else delete ditem;
116         } else result = EC_MemoryExhausted;
117       }
118       ++first;
119     }
120     if (result==EC_Normal) dset.insert(dseq, OFTrue /*replaceOld*/); else delete dseq;
121   } else result = EC_MemoryExhausted;
122   return result;
123 }
124 
125 
addAnnotationBox(const char * instanceuid,const char * text,Uint16 position)126 OFCondition DVPSAnnotationContent_PList::addAnnotationBox(
127     const char *instanceuid,
128     const char *text,
129     Uint16 position)
130 {
131   OFCondition result = EC_Normal;
132   DVPSAnnotationContent *newAnnotation = new DVPSAnnotationContent();
133   if (newAnnotation)
134   {
135     result = newAnnotation->setContent(instanceuid, text, position);
136     if (EC_Normal == result) list_.push_back(newAnnotation); else delete newAnnotation;
137   } else result = EC_MemoryExhausted;
138   return result;
139 }
140 
141 
deleteAnnotation(size_t idx)142 OFCondition DVPSAnnotationContent_PList::deleteAnnotation(size_t idx)
143 {
144   OFListIterator(DVPSAnnotationContent *) first = list_.begin();
145   OFListIterator(DVPSAnnotationContent *) last = list_.end();
146   while ((first != last)&&(idx--)) ++first;
147   if (first != last)
148   {
149     delete (*first);
150     list_.erase(first);
151     return EC_Normal;
152   }
153   return EC_IllegalCall;
154 }
155 
deleteMultipleAnnotations(size_t number)156 OFCondition DVPSAnnotationContent_PList::deleteMultipleAnnotations(size_t number)
157 {
158   OFListIterator(DVPSAnnotationContent *) first = list_.begin();
159   OFListIterator(DVPSAnnotationContent *) last = list_.end();
160   while ((first != last)&&(number--))
161   {
162     delete (*first);
163     first = list_.erase(first);
164   }
165   return EC_Normal;
166 }
167 
getAnnotationBox(size_t idx)168 DVPSAnnotationContent *DVPSAnnotationContent_PList::getAnnotationBox(size_t idx)
169 {
170   OFListIterator(DVPSAnnotationContent *) first = list_.begin();
171   OFListIterator(DVPSAnnotationContent *) last = list_.end();
172   while (first != last)
173   {
174     if (idx==0) return *first;
175     idx--;
176     ++first;
177   }
178   return NULL;
179 }
180 
setAnnotationSOPInstanceUID(size_t idx,const char * value)181 OFCondition DVPSAnnotationContent_PList::setAnnotationSOPInstanceUID(size_t idx, const char *value)
182 {
183   DVPSAnnotationContent *box = getAnnotationBox(idx);
184   if (box) return box->setSOPInstanceUID(value);
185   return EC_IllegalCall;
186 }
187 
getSOPInstanceUID(size_t idx)188 const char *DVPSAnnotationContent_PList::getSOPInstanceUID(size_t idx)
189 {
190   DVPSAnnotationContent *box = getAnnotationBox(idx);
191   if (box) return box->getSOPInstanceUID();
192   return NULL;
193 }
194 
prepareBasicAnnotationBox(size_t idx,DcmItem & dset)195 OFCondition DVPSAnnotationContent_PList::prepareBasicAnnotationBox(size_t idx, DcmItem &dset)
196 {
197   DVPSAnnotationContent *box = getAnnotationBox(idx);
198   if (box) return box->prepareBasicAnnotationBox(dset);
199   return EC_IllegalCall;
200 }
201 
clearAnnotationSOPInstanceUIDs()202 void DVPSAnnotationContent_PList::clearAnnotationSOPInstanceUIDs()
203 {
204   OFListIterator(DVPSAnnotationContent *) first = list_.begin();
205   OFListIterator(DVPSAnnotationContent *) last = list_.end();
206   while (first != last)
207   {
208     (*first)->setSOPInstanceUID(NULL);
209     ++first;
210   }
211 }
212