]> SALOME platform Git repositories - modules/kernel.git/blob - src/SALOMEDSImpl/SALOMEDSImpl_AttributeTableOfInteger.cxx
Salome HOME
PR: merge from branch BR_UnitTests tag mergeto_trunk_17oct05
[modules/kernel.git] / src / SALOMEDSImpl / SALOMEDSImpl_AttributeTableOfInteger.cxx
1 //  File   : SALOMEDSImpl_AttributeTableOfInteger.cxx
2 //  Author : Michael Ponikarov
3 //  Module : SALOME
4
5 #include <SALOMEDSImpl_AttributeTableOfInteger.hxx>
6 #include <Standard_Failure.hxx>
7 #include <TColStd_DataMapIteratorOfDataMapOfIntegerInteger.hxx>
8 #include <Standard_GUID.hxx>
9 #include <TColStd_HSequenceOfExtendedString.hxx>
10
11 using namespace std;
12
13 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeTableOfInteger, SALOMEDSImpl_GenericAttribute )
14 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeTableOfInteger, SALOMEDSImpl_GenericAttribute )
15
16 #define SEPARATOR '\1'
17
18 static TCollection_ExtendedString getUnit(TCollection_ExtendedString theString)
19 {
20   TCollection_ExtendedString aString(theString);
21   int aPos = aString.Search(SEPARATOR);
22   if(aPos <= 0 || aPos == aString.Length() ) return TCollection_ExtendedString();
23   return aString.Split(aPos);
24 }
25
26 static TCollection_ExtendedString getTitle(TCollection_ExtendedString theString)
27 {
28   TCollection_ExtendedString aString(theString);
29   int aPos = aString.Search(SEPARATOR);
30   if(aPos < 1) return aString;
31   if(aPos == 1) return TCollection_ExtendedString();
32   aString.Split(aPos-1);
33   return aString;
34 }
35
36 const Standard_GUID& SALOMEDSImpl_AttributeTableOfInteger::GetID() 
37 {
38   static Standard_GUID SALOMEDSImpl_AttributeTableOfIntegerID ("128371A0-8F52-11d6-A8A3-0001021E8C7F");
39   return SALOMEDSImpl_AttributeTableOfIntegerID;
40 }
41
42 Handle(SALOMEDSImpl_AttributeTableOfInteger) SALOMEDSImpl_AttributeTableOfInteger::Set(const TDF_Label& label) 
43 {
44   Handle(SALOMEDSImpl_AttributeTableOfInteger) anAttr;
45   if (!label.FindAttribute(SALOMEDSImpl_AttributeTableOfInteger::GetID(),anAttr)) {
46     anAttr = new SALOMEDSImpl_AttributeTableOfInteger();
47     label.AddAttribute(anAttr);
48   }
49   return anAttr;
50 }
51
52 SALOMEDSImpl_AttributeTableOfInteger::SALOMEDSImpl_AttributeTableOfInteger() 
53 :SALOMEDSImpl_GenericAttribute("AttributeTableOfInteger")
54 {
55   myRows = new TColStd_HSequenceOfExtendedString();
56   myCols = new TColStd_HSequenceOfExtendedString();
57   myNbRows = 0;
58   myNbColumns = 0;
59 }
60
61 void SALOMEDSImpl_AttributeTableOfInteger::SetNbColumns(const Standard_Integer theNbColumns)
62 {
63   CheckLocked();  
64   Backup();
65   
66   TColStd_DataMapOfIntegerInteger aMap;
67   aMap = myTable;
68   myTable.Clear();
69
70   TColStd_DataMapIteratorOfDataMapOfIntegerInteger anIterator(aMap);
71   for(; anIterator.More(); anIterator.Next()) {
72     int aRow = (int)(anIterator.Key()/myNbColumns) + 1;
73     int aCol = (int)(anIterator.Key() - myNbColumns*(aRow-1));
74     if(aCol == 0) { aCol = myNbColumns; aRow--; }
75     if(aCol > theNbColumns) continue;
76     int aKey = (aRow-1)*theNbColumns+aCol;
77     myTable.Bind(aKey, anIterator.Value());
78   }
79
80   myNbColumns = theNbColumns;
81
82   while (myCols->Length() < myNbColumns) { // append empty columns titles
83     myCols->Append(TCollection_ExtendedString(""));
84   }
85
86   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
87
88 }
89
90 void SALOMEDSImpl_AttributeTableOfInteger::SetTitle(const TCollection_ExtendedString& theTitle) 
91 {
92   CheckLocked();  
93   Backup();
94   myTitle = theTitle;
95   
96   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
97 }
98
99 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetTitle() const 
100 {
101   return myTitle;
102 }
103
104 void SALOMEDSImpl_AttributeTableOfInteger::SetRowData(const Standard_Integer theRow,
105                                                   const Handle(TColStd_HSequenceOfInteger)& theData) 
106 {
107   CheckLocked();  
108   if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
109
110   Backup();
111
112   while (myRows->Length() < theRow) { // append new row titles
113     myRows->Append(TCollection_ExtendedString(""));
114   }
115
116   Standard_Integer i, aShift = (theRow-1)*myNbColumns, aLength = theData->Length();
117   for(i = 1; i <= aLength; i++) {
118     myTable.Bind(aShift + i, theData->Value(i));
119   }
120
121   if(theRow > myNbRows) myNbRows = theRow;
122   
123   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
124 }
125
126 Handle(TColStd_HSequenceOfInteger) SALOMEDSImpl_AttributeTableOfInteger::GetRowData(const Standard_Integer theRow)
127 {
128   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger();
129   Standard_Integer i, aShift = (theRow-1)*myNbColumns;
130   for(i = 1; i <= myNbColumns; i++) {
131      if(myTable.IsBound(aShift+i)) 
132        aSeq->Append(myTable.Find(aShift+i));
133      else
134        aSeq->Append(0);
135   }
136   
137   return aSeq;
138 }
139
140 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitle(const Standard_Integer theRow,
141                                                        const TCollection_ExtendedString& theTitle) 
142 {
143   CheckLocked();  
144   Backup();
145   TCollection_ExtendedString aTitle(theTitle), aUnit = GetRowUnit(theRow);
146   if(aUnit.Length()>0) {
147     aTitle += SEPARATOR;
148     aTitle += aUnit;
149   }
150   myRows->SetValue(theRow, aTitle);
151   
152   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
153 }
154
155 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnit(const Standard_Integer theRow,
156                                                       const TCollection_ExtendedString& theUnit) 
157 {
158   CheckLocked();  
159   Backup();
160   TCollection_ExtendedString aTitle = GetRowTitle(theRow);
161   aTitle += SEPARATOR;
162   aTitle += theUnit;
163
164   myRows->SetValue(theRow, aTitle);
165   
166   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
167 }
168
169 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnits(const Handle(TColStd_HSequenceOfExtendedString)& theUnits)
170 {
171   if (theUnits->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
172   int aLength = theUnits->Length(), i;
173   for(i = 1; i <= aLength; i++) SetRowUnit(i, theUnits->Value(i));
174   
175   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
176 }
177
178 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfInteger::GetRowUnits()
179 {
180   Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
181   int aLength = myRows->Length(), i;
182   for(i=1; i<=aLength; i++) aSeq->Append(getUnit(myRows->Value(i)));
183   return aSeq;
184 }
185
186 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
187 {
188   if (theTitles->Length() != GetNbRows()) Standard_Failure::Raise("Invalid number of rows");
189   int aLength = theTitles->Length(), i;
190   for(i = 1; i <= aLength; i++) SetRowTitle(i, theTitles->Value(i));
191   
192   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
193 }
194
195 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfInteger::GetRowTitles()
196 {
197   Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
198   int aLength = myRows->Length(), i;
199   for(i=1; i<=aLength; i++) aSeq->Append(getTitle(myRows->Value(i)));
200   return aSeq;
201 }
202
203
204 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetRowTitle(const Standard_Integer theRow) const 
205 {
206   return getTitle(myRows->Value(theRow));
207 }
208
209
210 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetRowUnit(const Standard_Integer theRow) const 
211 {
212   return getUnit(myRows->Value(theRow));
213 }
214
215
216 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnData(const Standard_Integer theColumn,
217                                                      const Handle(TColStd_HSequenceOfInteger)& theData) 
218 {
219   CheckLocked();  
220   if(theColumn > myNbColumns) SetNbColumns(theColumn);
221
222   Backup();
223
224   Standard_Integer i, aLength = theData->Length();
225   for(i = 1; i <= aLength; i++) {
226     myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
227   }
228
229   if(aLength > myNbRows) {
230     myNbRows = aLength;
231     while (myRows->Length() < myNbRows) { // append empty row titles
232       myRows->Append(TCollection_ExtendedString(""));
233     }
234   }
235   
236   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
237 }
238
239
240 Handle(TColStd_HSequenceOfInteger) SALOMEDSImpl_AttributeTableOfInteger::GetColumnData(const Standard_Integer theColumn)
241 {
242   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
243   
244   Standard_Integer i, anIndex;
245   for(i = 1; i <= myNbRows; i++) {
246     anIndex = myNbColumns*(i-1) + theColumn;
247     if(myTable.IsBound(anIndex)) 
248       aSeq->Append(myTable.Find(anIndex));
249     else
250       aSeq->Append(0);
251   }
252   
253   return aSeq;
254 }
255
256 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitle(const Standard_Integer theColumn,
257                                                       const TCollection_ExtendedString& theTitle) 
258 {
259   CheckLocked();                                                      
260   Backup();
261   while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
262   myCols->SetValue(theColumn,theTitle);
263   
264   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
265 }
266
267 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitle(const Standard_Integer theColumn) const 
268 {
269   if(myCols.IsNull()) return "";
270   if(myCols->Length() < theColumn) return "";
271   return myCols->Value(theColumn);
272 }
273
274 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
275 {
276   if (theTitles->Length() != myNbColumns) Standard_Failure::Raise("Invalid number of columns");
277   int aLength = theTitles->Length(), i;
278   for(i = 1; i <= aLength; i++)  myCols->SetValue(i, theTitles->Value(i));
279   
280   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
281 }
282
283 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitles()
284 {
285   Handle(TColStd_HSequenceOfExtendedString) aSeq = new TColStd_HSequenceOfExtendedString;
286   int aLength = myCols->Length(), i;
287   for(i=1; i<=aLength; i++) aSeq->Append(myCols->Value(i));
288   return aSeq;
289 }
290
291 Standard_Integer SALOMEDSImpl_AttributeTableOfInteger::GetNbRows() const
292 {
293   return myNbRows;
294 }
295
296 Standard_Integer SALOMEDSImpl_AttributeTableOfInteger::GetNbColumns() const
297 {
298   return myNbColumns;
299 }
300
301 void SALOMEDSImpl_AttributeTableOfInteger::PutValue(const Standard_Integer theValue,
302                                                 const Standard_Integer theRow,
303                                                 const Standard_Integer theColumn) 
304 {
305   CheckLocked();  
306   if(theColumn > myNbColumns) SetNbColumns(theColumn);
307
308   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
309   myTable.Bind(anIndex, theValue);
310
311   if(theRow > myNbRows) {
312     while (myRows->Length() < theRow) { // append empty row titles
313       myRows->Append(TCollection_ExtendedString(""));
314     }
315     myNbRows = theRow;
316   }
317   
318   SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved 
319 }
320
321 Standard_Boolean SALOMEDSImpl_AttributeTableOfInteger::HasValue(const Standard_Integer theRow,
322                                                             const Standard_Integer theColumn) 
323 {
324   if(theRow > myNbRows || theRow < 1) return Standard_False;
325   if(theColumn > myNbColumns || theColumn < 1) return Standard_False;
326   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
327   return myTable.IsBound(anIndex); 
328 }
329
330 Standard_Integer SALOMEDSImpl_AttributeTableOfInteger::GetValue(const Standard_Integer theRow,
331                                                             const Standard_Integer theColumn) 
332 {
333   if(theRow > myNbRows || theRow < 1) Standard_Failure::Raise("Invalid cell index");
334   if(theColumn > myNbColumns || theColumn < 1) Standard_Failure::Raise("Invalid cell index");
335
336   Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
337   if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
338   
339   Standard_Failure::Raise("Invalid cell index");
340   return 0;
341 }
342
343 const Standard_GUID& SALOMEDSImpl_AttributeTableOfInteger::ID() const
344 {
345   return GetID();
346 }
347
348 void SALOMEDSImpl_AttributeTableOfInteger::Restore(const Handle(TDF_Attribute)& with) 
349 {
350   Standard_Integer anIndex;
351   Handle(SALOMEDSImpl_AttributeTableOfInteger) aTable = Handle(SALOMEDSImpl_AttributeTableOfInteger)::DownCast(with);
352
353   myTable.Clear();
354   myCols->Clear();
355   myRows->Clear();
356
357   myTable = aTable->myTable;
358   myNbRows = aTable->myNbRows;
359   myNbColumns = aTable->myNbColumns;
360   myTitle = aTable->myTitle;
361   
362   for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
363     myRows->Append(aTable->GetRowTitle(anIndex));
364
365   for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++) 
366     myCols->Append(aTable->GetColumnTitle(anIndex));
367 }
368
369 Handle(TDF_Attribute) SALOMEDSImpl_AttributeTableOfInteger::NewEmpty() const
370 {
371   return new SALOMEDSImpl_AttributeTableOfInteger();
372 }
373
374 void SALOMEDSImpl_AttributeTableOfInteger::Paste(const Handle(TDF_Attribute)& into,
375                                              const Handle(TDF_RelocationTable)&) const
376 {
377   Standard_Integer anIndex;
378   Handle(SALOMEDSImpl_AttributeTableOfInteger) aTable = Handle(SALOMEDSImpl_AttributeTableOfInteger)::DownCast(into);
379
380   aTable->myTable.Clear();
381   aTable->myCols->Clear();
382   aTable->myRows->Clear();
383
384   aTable->myTable = myTable;
385   aTable->myTitle = myTitle;
386   aTable->myNbRows = myNbRows;
387   aTable->myNbColumns = myNbColumns;
388
389   for(anIndex = 1; anIndex <= GetNbRows();anIndex++)
390     aTable->myRows->Append(GetRowTitle(anIndex));
391   for(anIndex = 1; anIndex <= GetNbColumns(); anIndex++) 
392     aTable->myCols->Append(GetColumnTitle(anIndex));
393 }
394
395
396 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfInteger::GetSetRowIndices(const Standard_Integer theRow)
397 {
398   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
399
400   Standard_Integer i, aShift = myNbColumns*(theRow-1);
401   for(i = 1; i <= myNbColumns; i++) {
402     if(myTable.IsBound(aShift + i)) aSeq->Append(i);
403   }
404   
405   return aSeq;
406 }
407
408 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfInteger::GetSetColumnIndices(const Standard_Integer theColumn)
409 {
410   Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
411
412   Standard_Integer i, anIndex;
413   for(i = 1; i <= myNbRows; i++) {
414     anIndex = myNbColumns*(i-1)+theColumn;
415     if(myTable.IsBound(anIndex)) aSeq->Append(i);
416   }
417   
418   return aSeq;
419 }
420
421
422 void SALOMEDSImpl_AttributeTableOfInteger::ConvertToString(ostrstream& theStream)
423 {
424   int i, j, l;
425
426   theStream.precision(64);
427   
428   //Title
429   l = myTitle.Length();
430   theStream << l << "\n";
431   for(i=1; i<=l; i++)
432     theStream << myTitle.Value(i) << "\n";
433
434   //Nb rows
435   theStream << myNbRows << "\n";
436
437   //Rows titles
438   for(i=1; i<=myNbRows; i++) {
439     l = myRows->Value(i).Length();
440     theStream << l << "\n";
441     for(j=1; j<=l; j++)
442       theStream << myRows->Value(i).Value(j) << "\n";
443   }
444
445   //Nb columns
446   theStream << myNbColumns << "\n";
447
448   //Columns titles
449   for(i=1; i<=myNbColumns; i++) {
450     l = myCols->Value(i).Length();
451     theStream << l << "\n";
452     for(j=1; j<=l; j++)
453       theStream << myCols->Value(i).Value(j) << "\n";
454   }
455
456   //Store the table values
457   l = myTable.Extent();
458   theStream << l << "\n";
459   TColStd_DataMapIteratorOfDataMapOfIntegerInteger anIterator(myTable);
460   for(; anIterator.More(); anIterator.Next()) {
461     theStream << anIterator.Key() << "\n";
462     theStream << anIterator.Value() << "\n";
463   }
464
465   return;
466 }
467
468 bool SALOMEDSImpl_AttributeTableOfInteger::RestoreFromString(istrstream& theStream)
469 {
470   Backup();
471
472   int i, j, l;
473
474   Standard_ExtCharacter anExtChar;
475   TCollection_ExtendedString aStr;
476
477   //Title
478   theStream >> l;
479
480   myTitle = TCollection_ExtendedString(l, 0);
481   for(i=1; i<=l; i++) {
482     theStream >> anExtChar;
483     myTitle.SetValue(i, anExtChar);
484   }
485
486   //Nb rows
487   theStream >> myNbRows;
488
489   //Rows titles
490   myRows->Clear();  
491   for(i=1; i<=myNbRows; i++) { 
492     theStream >> l;
493     aStr = TCollection_ExtendedString(l,0);
494     for(j=1; j<=l; j++) {
495       theStream >> anExtChar;
496       aStr.SetValue(j, anExtChar);
497     }
498     myRows->Append(aStr);
499   }
500
501   //Nb columns
502   theStream >> myNbColumns;
503
504   //Columns titles
505   myCols->Clear();
506   for(i=1; i<=myNbColumns; i++) {
507     theStream >> l;
508     aStr = TCollection_ExtendedString(l,0);
509     for(j=1; j<=l; j++) {
510       theStream >> anExtChar;
511       aStr.SetValue(j, anExtChar);
512     }
513     myCols->Append(aStr);
514   }
515
516   //Restore the table values
517   theStream >> l;
518   myTable.Clear();
519   for(i=1; i<=l; i++) {
520     int aKey, aValue;
521     theStream >> aKey;
522     theStream >> aValue;
523     myTable.Bind(aKey, aValue);
524   }
525
526   return true;
527 }
528
529
530 TCollection_AsciiString SALOMEDSImpl_AttributeTableOfInteger::Save() 
531 {
532   ostrstream ostr;
533   ConvertToString(ostr);
534   TCollection_AsciiString aString((char*)ostr.rdbuf()->str());
535   return aString;
536 }
537
538 void SALOMEDSImpl_AttributeTableOfInteger::Load(const TCollection_AsciiString& value) 
539 {
540   istrstream aStream(value.ToCString(), strlen(value.ToCString()));
541   RestoreFromString(aStream);
542 }