1 // File : SALOMEDSImpl_AttributeTableOfInteger.cxx
2 // Author : Michael Ponikarov
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>
13 IMPLEMENT_STANDARD_HANDLE( SALOMEDSImpl_AttributeTableOfInteger, SALOMEDSImpl_GenericAttribute )
14 IMPLEMENT_STANDARD_RTTIEXT( SALOMEDSImpl_AttributeTableOfInteger, SALOMEDSImpl_GenericAttribute )
16 #define SEPARATOR '\1'
18 static TCollection_ExtendedString getUnit(TCollection_ExtendedString theString)
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);
26 static TCollection_ExtendedString getTitle(TCollection_ExtendedString theString)
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);
36 const Standard_GUID& SALOMEDSImpl_AttributeTableOfInteger::GetID()
38 static Standard_GUID SALOMEDSImpl_AttributeTableOfIntegerID ("128371A0-8F52-11d6-A8A3-0001021E8C7F");
39 return SALOMEDSImpl_AttributeTableOfIntegerID;
42 Handle(SALOMEDSImpl_AttributeTableOfInteger) SALOMEDSImpl_AttributeTableOfInteger::Set(const TDF_Label& label)
44 Handle(SALOMEDSImpl_AttributeTableOfInteger) anAttr;
45 if (!label.FindAttribute(SALOMEDSImpl_AttributeTableOfInteger::GetID(),anAttr)) {
46 anAttr = new SALOMEDSImpl_AttributeTableOfInteger();
47 label.AddAttribute(anAttr);
52 SALOMEDSImpl_AttributeTableOfInteger::SALOMEDSImpl_AttributeTableOfInteger()
53 :SALOMEDSImpl_GenericAttribute("AttributeTableOfInteger")
55 myRows = new TColStd_HSequenceOfExtendedString();
56 myCols = new TColStd_HSequenceOfExtendedString();
61 void SALOMEDSImpl_AttributeTableOfInteger::SetNbColumns(const Standard_Integer theNbColumns)
66 TColStd_DataMapOfIntegerInteger aMap;
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());
80 myNbColumns = theNbColumns;
82 while (myCols->Length() < myNbColumns) { // append empty columns titles
83 myCols->Append(TCollection_ExtendedString(""));
86 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
90 void SALOMEDSImpl_AttributeTableOfInteger::SetTitle(const TCollection_ExtendedString& theTitle)
96 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
99 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetTitle() const
104 void SALOMEDSImpl_AttributeTableOfInteger::SetRowData(const Standard_Integer theRow,
105 const Handle(TColStd_HSequenceOfInteger)& theData)
108 if(theData->Length() > myNbColumns) SetNbColumns(theData->Length());
112 while (myRows->Length() < theRow) { // append new row titles
113 myRows->Append(TCollection_ExtendedString(""));
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));
121 if(theRow > myNbRows) myNbRows = theRow;
123 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
126 Handle(TColStd_HSequenceOfInteger) SALOMEDSImpl_AttributeTableOfInteger::GetRowData(const Standard_Integer theRow)
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));
140 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitle(const Standard_Integer theRow,
141 const TCollection_ExtendedString& theTitle)
145 TCollection_ExtendedString aTitle(theTitle), aUnit = GetRowUnit(theRow);
146 if(aUnit.Length()>0) {
150 myRows->SetValue(theRow, aTitle);
152 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
155 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnit(const Standard_Integer theRow,
156 const TCollection_ExtendedString& theUnit)
160 TCollection_ExtendedString aTitle = GetRowTitle(theRow);
164 myRows->SetValue(theRow, aTitle);
166 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
169 void SALOMEDSImpl_AttributeTableOfInteger::SetRowUnits(const Handle(TColStd_HSequenceOfExtendedString)& theUnits)
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));
175 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
178 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfInteger::GetRowUnits()
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)));
186 void SALOMEDSImpl_AttributeTableOfInteger::SetRowTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
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));
192 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
195 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfInteger::GetRowTitles()
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)));
204 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetRowTitle(const Standard_Integer theRow) const
206 return getTitle(myRows->Value(theRow));
210 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetRowUnit(const Standard_Integer theRow) const
212 return getUnit(myRows->Value(theRow));
216 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnData(const Standard_Integer theColumn,
217 const Handle(TColStd_HSequenceOfInteger)& theData)
220 if(theColumn > myNbColumns) SetNbColumns(theColumn);
224 Standard_Integer i, aLength = theData->Length();
225 for(i = 1; i <= aLength; i++) {
226 myTable.Bind(myNbColumns*(i-1)+theColumn, theData->Value(i));
229 if(aLength > myNbRows) {
231 while (myRows->Length() < myNbRows) { // append empty row titles
232 myRows->Append(TCollection_ExtendedString(""));
236 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
240 Handle(TColStd_HSequenceOfInteger) SALOMEDSImpl_AttributeTableOfInteger::GetColumnData(const Standard_Integer theColumn)
242 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
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));
256 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitle(const Standard_Integer theColumn,
257 const TCollection_ExtendedString& theTitle)
261 while(myCols->Length() < theColumn) myCols->Append(TCollection_ExtendedString(""));
262 myCols->SetValue(theColumn,theTitle);
264 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
267 TCollection_ExtendedString SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitle(const Standard_Integer theColumn) const
269 if(myCols.IsNull()) return "";
270 if(myCols->Length() < theColumn) return "";
271 return myCols->Value(theColumn);
274 void SALOMEDSImpl_AttributeTableOfInteger::SetColumnTitles(const Handle(TColStd_HSequenceOfExtendedString)& theTitles)
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));
280 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
283 Handle(TColStd_HSequenceOfExtendedString) SALOMEDSImpl_AttributeTableOfInteger::GetColumnTitles()
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));
291 Standard_Integer SALOMEDSImpl_AttributeTableOfInteger::GetNbRows() const
296 Standard_Integer SALOMEDSImpl_AttributeTableOfInteger::GetNbColumns() const
301 void SALOMEDSImpl_AttributeTableOfInteger::PutValue(const Standard_Integer theValue,
302 const Standard_Integer theRow,
303 const Standard_Integer theColumn)
306 if(theColumn > myNbColumns) SetNbColumns(theColumn);
308 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
309 myTable.Bind(anIndex, theValue);
311 if(theRow > myNbRows) {
312 while (myRows->Length() < theRow) { // append empty row titles
313 myRows->Append(TCollection_ExtendedString(""));
318 SetModifyFlag(); //SRN: Mark the study as being modified, so it could be saved
321 Standard_Boolean SALOMEDSImpl_AttributeTableOfInteger::HasValue(const Standard_Integer theRow,
322 const Standard_Integer theColumn)
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);
330 Standard_Integer SALOMEDSImpl_AttributeTableOfInteger::GetValue(const Standard_Integer theRow,
331 const Standard_Integer theColumn)
333 if(theRow > myNbRows || theRow < 1) Standard_Failure::Raise("Invalid cell index");
334 if(theColumn > myNbColumns || theColumn < 1) Standard_Failure::Raise("Invalid cell index");
336 Standard_Integer anIndex = (theRow-1)*myNbColumns + theColumn;
337 if(myTable.IsBound(anIndex)) return myTable.Find(anIndex);
339 Standard_Failure::Raise("Invalid cell index");
343 const Standard_GUID& SALOMEDSImpl_AttributeTableOfInteger::ID() const
348 void SALOMEDSImpl_AttributeTableOfInteger::Restore(const Handle(TDF_Attribute)& with)
350 Standard_Integer anIndex;
351 Handle(SALOMEDSImpl_AttributeTableOfInteger) aTable = Handle(SALOMEDSImpl_AttributeTableOfInteger)::DownCast(with);
357 myTable = aTable->myTable;
358 myNbRows = aTable->myNbRows;
359 myNbColumns = aTable->myNbColumns;
360 myTitle = aTable->myTitle;
362 for(anIndex = 1; anIndex <= aTable->GetNbRows();anIndex++)
363 myRows->Append(aTable->GetRowTitle(anIndex));
365 for(anIndex = 1; anIndex <= aTable->GetNbColumns(); anIndex++)
366 myCols->Append(aTable->GetColumnTitle(anIndex));
369 Handle(TDF_Attribute) SALOMEDSImpl_AttributeTableOfInteger::NewEmpty() const
371 return new SALOMEDSImpl_AttributeTableOfInteger();
374 void SALOMEDSImpl_AttributeTableOfInteger::Paste(const Handle(TDF_Attribute)& into,
375 const Handle(TDF_RelocationTable)&) const
377 Standard_Integer anIndex;
378 Handle(SALOMEDSImpl_AttributeTableOfInteger) aTable = Handle(SALOMEDSImpl_AttributeTableOfInteger)::DownCast(into);
380 aTable->myTable.Clear();
381 aTable->myCols->Clear();
382 aTable->myRows->Clear();
384 aTable->myTable = myTable;
385 aTable->myTitle = myTitle;
386 aTable->myNbRows = myNbRows;
387 aTable->myNbColumns = myNbColumns;
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));
396 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfInteger::GetSetRowIndices(const Standard_Integer theRow)
398 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
400 Standard_Integer i, aShift = myNbColumns*(theRow-1);
401 for(i = 1; i <= myNbColumns; i++) {
402 if(myTable.IsBound(aShift + i)) aSeq->Append(i);
408 Handle_TColStd_HSequenceOfInteger SALOMEDSImpl_AttributeTableOfInteger::GetSetColumnIndices(const Standard_Integer theColumn)
410 Handle(TColStd_HSequenceOfInteger) aSeq = new TColStd_HSequenceOfInteger;
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);
422 void SALOMEDSImpl_AttributeTableOfInteger::ConvertToString(ostrstream& theStream)
426 theStream.precision(64);
429 l = myTitle.Length();
430 theStream << l << "\n";
432 theStream << myTitle.Value(i) << "\n";
435 theStream << myNbRows << "\n";
438 for(i=1; i<=myNbRows; i++) {
439 l = myRows->Value(i).Length();
440 theStream << l << "\n";
442 theStream << myRows->Value(i).Value(j) << "\n";
446 theStream << myNbColumns << "\n";
449 for(i=1; i<=myNbColumns; i++) {
450 l = myCols->Value(i).Length();
451 theStream << l << "\n";
453 theStream << myCols->Value(i).Value(j) << "\n";
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";
468 bool SALOMEDSImpl_AttributeTableOfInteger::RestoreFromString(istrstream& theStream)
474 Standard_ExtCharacter anExtChar;
475 TCollection_ExtendedString aStr;
480 myTitle = TCollection_ExtendedString(l, 0);
481 for(i=1; i<=l; i++) {
482 theStream >> anExtChar;
483 myTitle.SetValue(i, anExtChar);
487 theStream >> myNbRows;
491 for(i=1; i<=myNbRows; i++) {
493 aStr = TCollection_ExtendedString(l,0);
494 for(j=1; j<=l; j++) {
495 theStream >> anExtChar;
496 aStr.SetValue(j, anExtChar);
498 myRows->Append(aStr);
502 theStream >> myNbColumns;
506 for(i=1; i<=myNbColumns; i++) {
508 aStr = TCollection_ExtendedString(l,0);
509 for(j=1; j<=l; j++) {
510 theStream >> anExtChar;
511 aStr.SetValue(j, anExtChar);
513 myCols->Append(aStr);
516 //Restore the table values
519 for(i=1; i<=l; i++) {
523 myTable.Bind(aKey, aValue);
530 TCollection_AsciiString SALOMEDSImpl_AttributeTableOfInteger::Save()
533 ConvertToString(ostr);
534 TCollection_AsciiString aString((char*)ostr.rdbuf()->str());
538 void SALOMEDSImpl_AttributeTableOfInteger::Load(const TCollection_AsciiString& value)
540 istrstream aStream(value.ToCString(), strlen(value.ToCString()));
541 RestoreFromString(aStream);