Salome HOME
0021708: [CEA 586] Object browser sort only children:
authorakl <akl@opencascade.com>
Tue, 9 Jul 2013 08:02:24 +0000 (08:02 +0000)
committerakl <akl@opencascade.com>
Tue, 9 Jul 2013 08:02:24 +0000 (08:02 +0000)
Add sorting function to the UseCaseBuilder CORBA interface to sort child data objects of the specified parent data object according to the specified sort order (ascending or descending).

idl/SALOMEDS.idl
src/SALOMEDS/SALOMEDS_UseCaseBuilder.cxx
src/SALOMEDS/SALOMEDS_UseCaseBuilder.hxx
src/SALOMEDS/SALOMEDS_UseCaseBuilder_i.cxx
src/SALOMEDS/SALOMEDS_UseCaseBuilder_i.hxx
src/SALOMEDS/Test/SALOMEDSTest_UseCase.cxx
src/SALOMEDSClient/SALOMEDSClient_UseCaseBuilder.hxx
src/SALOMEDSImpl/SALOMEDSImpl_UseCaseBuilder.cxx
src/SALOMEDSImpl/SALOMEDSImpl_UseCaseBuilder.hxx

index e41297a14f263f3732f7d8e337fe91705d9968ad..69de7bf8405812be6decc94e69c61a48008e8d34 100644 (file)
@@ -1319,6 +1319,10 @@ Activates the %UseCaseIterator.
    Returns True if the given object <VAR>theObject</VAR> of the use case has child objects.
 */
     boolean HasChildren(in SObject theObject);
+/*!
+   Returns True if children of the given object <VAR>theObject</VAR> of the use case tree were sorted successfully.
+*/
+    boolean SortChildren(in SObject theObject, in boolean theAscendingOrder);
 /*!
    Gets father object of the given object <VAR>theObject</VAR> in the use cases tree.
 */
index 79a3248500086c519c1bcd0f875103a88b123b90..37dfc1450c16b579f96cbe87b9e02b3975fa0d6c 100644 (file)
@@ -139,6 +139,18 @@ bool SALOMEDS_UseCaseBuilder::HasChildren(const _PTR(SObject)& theObject)
   return ret;
 }
 
+bool SALOMEDS_UseCaseBuilder::SortChildren(const _PTR(SObject)& theObject, bool theAscendingOrder)
+{
+  bool ret;
+  SALOMEDS_SObject* obj = dynamic_cast<SALOMEDS_SObject*>(theObject.get());
+  if (_isLocal) {
+    SALOMEDS::Locker lock;
+    ret = _local_impl->SortChildren(*(obj->GetLocalImpl()), theAscendingOrder);
+  }
+  else ret = _corba_impl->SortChildren(obj->GetCORBAImpl(), theAscendingOrder);
+  return ret;
+}
+
 _PTR(SObject) SALOMEDS_UseCaseBuilder::GetFather(const _PTR(SObject)& theObject)
 {
   SALOMEDS_SObject* father = NULL;
index 055158da680d5b622a01fb12ec98552606ab990f..4162810ec6e191c29d87c0e974479e7e99d354c1 100644 (file)
@@ -55,6 +55,7 @@ public:
   virtual bool  SetCurrentObject(const _PTR(SObject)& theObject);
   virtual bool SetRootCurrent();
   virtual bool  HasChildren(const _PTR(SObject)& theObject);
+  virtual bool SortChildren(const _PTR(SObject)& theObject, bool theAscendingOrder);
   virtual _PTR(SObject) GetFather(const _PTR(SObject)& theObject);
   virtual bool  IsUseCase(const _PTR(SObject)& theObject);
   virtual bool  IsUseCaseNode(const _PTR(SObject)& theObject);
index 826a0bcc18e059517695fafcbd88f5d77b7e8b88..03846838638c1bf9c024438aba3faaf69165ede9 100644 (file)
@@ -141,6 +141,18 @@ CORBA::Boolean SALOMEDS_UseCaseBuilder_i::HasChildren(SALOMEDS::SObject_ptr theO
   return _impl->HasChildren(_impl->GetSObject(theObject->GetID()));
 }
 
+//============================================================================
+/*! Function : SortChildren
+ *  Purpose  :
+ */
+//============================================================================
+CORBA::Boolean SALOMEDS_UseCaseBuilder_i::SortChildren(SALOMEDS::SObject_ptr theObject, CORBA::Boolean theAscendingOrder)
+{
+  SALOMEDS::Locker lock;
+  if(!_impl) return 0;
+  return _impl->SortChildren(_impl->GetSObject(theObject->GetID()), theAscendingOrder);
+}
+
 //============================================================================
 /*! Function : GetFather
  *  Purpose  :
index a2caee686916ed634c3642227de5e9db3e8bc0a0..64f34cfb3cfc5733ec7c3ae097280e97f38c10ab 100644 (file)
@@ -73,6 +73,8 @@ public:
 
   virtual CORBA::Boolean  HasChildren(SALOMEDS::SObject_ptr theObject);
 
+  virtual CORBA::Boolean  SortChildren(SALOMEDS::SObject_ptr theObject, CORBA::Boolean theAscendingOrder);
+
   virtual SALOMEDS::SObject_ptr GetFather(SALOMEDS::SObject_ptr theObject);
 
   virtual CORBA::Boolean  IsUseCase(SALOMEDS::SObject_ptr theObject);
index 23a07d3a5e795fc9e40b85b163c8f601a1b188e6..079d3dd5c9c6dae9c99c44660ca7a321976d8053 100755 (executable)
@@ -108,6 +108,10 @@ void SALOMEDSTest::testUseCase()
   CPPUNIT_ASSERT(builder->HasChildren(so1));
   CPPUNIT_ASSERT(!builder->HasChildren(so2));
 
+  //Check method SortChildren
+  CPPUNIT_ASSERT(builder->SortChildren(so1, true));
+  CPPUNIT_ASSERT(!builder->SortChildren(so2, false));
+
   //Check method Init of UseCaseIterator
   it->Init(false); //Only the first level
   CPPUNIT_ASSERT(it->More());
index acc3e05490d9c5f229b3ac745908b225a7a7d03f..6f4f29320de07fca078315bb2ff8b93a2e26e525 100644 (file)
@@ -44,6 +44,7 @@ public:
   virtual bool SetCurrentObject(const _PTR(SObject)& theObject) = 0;
   virtual bool SetRootCurrent() = 0;
   virtual bool HasChildren(const _PTR(SObject)& theObject) = 0;
+  virtual bool SortChildren(const _PTR(SObject)& theObject, bool theAscendingOrder) = 0;
   virtual _PTR(SObject) GetFather(const _PTR(SObject)& theObject) = 0;
   virtual bool IsUseCase(const _PTR(SObject)& theObject) = 0;
   virtual bool IsUseCaseNode(const _PTR(SObject)& theObject) = 0;
index 515bc8d0735ed7e1df3b6bc2052bc7f824c47a4d..f3652b6823b8a0ffbba5f8d17948a36298d0ed94 100644 (file)
 
 #include "DF_ChildIterator.hxx"
 
+#include <list>
+
 #define USE_CASE_LABEL_TAG           2
 #define USE_CASE_GUID                "AA43BB12-D9CD-11d6-945D-0050DA506788"
 
+namespace {
+  // comparator to sort use case nodes in ascending order
+  struct AscSortSOs {
+    bool operator()( SALOMEDSImpl_SObject firstSO, SALOMEDSImpl_SObject secondSO ) const {
+      std::string firstName, secondName;
+      SALOMEDSImpl_SObject refSO;
+      firstSO.ReferencedObject(refSO) ? 
+       firstName = refSO.GetName() : 
+       firstName = firstSO.GetName();
+      secondSO.ReferencedObject(refSO) ? 
+       secondName = refSO.GetName() : 
+       secondName = secondSO.GetName();
+      return firstName < secondName;
+    }
+  };
+
+  // comparator to sort use case nodes in descending order
+  struct DescSortSOs {
+    bool operator()( SALOMEDSImpl_SObject firstSO, SALOMEDSImpl_SObject secondSO ) const {
+      std::string firstName, secondName;
+      SALOMEDSImpl_SObject refSO;
+      firstSO.ReferencedObject(refSO) ? 
+       firstName = refSO.GetName() : 
+       firstName = firstSO.GetName();
+      secondSO.ReferencedObject(refSO) ? 
+       secondName = refSO.GetName() : 
+       secondName = secondSO.GetName();
+      return firstName > secondName;
+    }
+  };
+}
 
 //============================================================================
 /*! Function : constructor
@@ -279,6 +312,44 @@ bool SALOMEDSImpl_UseCaseBuilder::HasChildren(const SALOMEDSImpl_SObject& theObj
   return (aNode->GetFirst());
 }
 
+//============================================================================
+/*! Function : SortChildren
+ *  Purpose  :
+ */
+//============================================================================
+bool SALOMEDSImpl_UseCaseBuilder::SortChildren(const SALOMEDSImpl_SObject& theObject, bool theAscendingOrder)
+{
+  if(!_root) return false;
+
+  DF_Label aLabel;
+  if (!theObject) aLabel = _root->Label();
+  else 
+    aLabel = theObject.GetLabel(); 
+  if(aLabel.IsNull()) return false;
+
+  SALOMEDSImpl_AttributeTreeNode* aNode = NULL;
+  if (!(aNode=(SALOMEDSImpl_AttributeTreeNode*)aLabel.FindAttribute(_root->ID()))) return false;
+
+  std::list<SALOMEDSImpl_SObject> aNodeSOs;
+  for (SALOMEDSImpl_AttributeTreeNode* aChildNode=aNode->GetFirst(); aChildNode; aChildNode=aChildNode->GetNext() ) {
+    SALOMEDSImpl_SObject aSO = SALOMEDSImpl_Study::SObject(aChildNode->Label());
+    if (aSO) {
+      aNodeSOs.push_back(aSO);
+    }
+  }
+  if (aNodeSOs.empty()) return false;
+  
+  //sort items by names in ascending/descending order
+  theAscendingOrder ? aNodeSOs.sort( AscSortSOs() ) : aNodeSOs.sort( DescSortSOs() );
+
+  std::list<SALOMEDSImpl_SObject>::iterator it;
+  for (it=aNodeSOs.begin(); it!=aNodeSOs.end(); ++it) {
+    AppendTo(aNode->GetSObject(), *it);
+  }
+
+  return true;
+}
+
 //============================================================================
 /*! Function : GetFather
  *  Purpose  :
index 5c190d102ee95b67558fccb8b1550b05d9d815d5..f83363607c558074fd4e0401d08935df1f67d09e 100644 (file)
@@ -65,6 +65,8 @@ public:
 
   virtual bool  HasChildren(const SALOMEDSImpl_SObject& theObject);
 
+  virtual bool SortChildren(const SALOMEDSImpl_SObject& theObject, bool theAscendingOrder);
+
   virtual SALOMEDSImpl_SObject GetFather(const SALOMEDSImpl_SObject& theObject);
 
   virtual bool  IsUseCase(const SALOMEDSImpl_SObject& theObject);