Salome HOME
Stream object creation improved to do not use the transaction during it modification.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ZLevelsModel.cxx
index f4ac8eae6e33ce2c3f188b4fa7f498ee79425a32..20fb48f5f3c685dc9f038da3338bdfe4f50b407a 100644 (file)
 //
 
 #include "HYDROGUI_ZLevelsModel.h"
+
+#include <SUIT_Session.h>
+#include <SUIT_ResourceMgr.h>
+
 #include <QMimeData>
 
 const QString OBJ_LIST_MIME_TYPE = "application/hydro.objects.list";
@@ -28,9 +32,11 @@ const QString OBJ_LIST_MIME_TYPE = "application/hydro.objects.list";
 HYDROGUI_ZLevelsModel::HYDROGUI_ZLevelsModel( QObject* theParent )
  : QAbstractListModel( theParent )
 {
+  SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
+
   myEmpty = QPixmap( 16, 16 );
   myEmpty.fill( Qt::white );
-  myEye = QPixmap( "eye.png" );//TODO: loading from resources
+  myEye = aResMgr->loadPixmap( "HYDRO", tr( "EYE_ICO" ) );
   setSupportedDragActions( Qt::MoveAction | Qt::CopyAction );
 }
 
@@ -50,7 +56,7 @@ QVariant HYDROGUI_ZLevelsModel::data( const QModelIndex &theIndex, int theRole )
   case Qt::DisplayRole:
     {
       if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
-        return myObjects.at( aRow ).first;
+        return myObjects.at( aRow ).first->GetName();
       else
         return QVariant();
     }
@@ -60,7 +66,7 @@ QVariant HYDROGUI_ZLevelsModel::data( const QModelIndex &theIndex, int theRole )
     {
       if( aColumn==0 && aRow >=0 && aRow < myObjects.count() )
       {
-        bool isVisible = IsObjectVisible( aRow );
+        bool isVisible = isObjectVisible( aRow );
         if( isVisible )
           return myEye;
         else
@@ -72,7 +78,7 @@ QVariant HYDROGUI_ZLevelsModel::data( const QModelIndex &theIndex, int theRole )
 
   case HYDROGUI_VisibleRole:
     {
-      bool isVisible = IsObjectVisible( aRow );
+      bool isVisible = isObjectVisible( aRow );
       return QVariant( isVisible ).toString();
     }
     break;
@@ -86,14 +92,25 @@ int HYDROGUI_ZLevelsModel::rowCount( const QModelIndex &theParent ) const
   return myObjects.count();
 }
 
-void HYDROGUI_ZLevelsModel::setObjects( const QList<QPair<QString, bool>>& theObjects )
+void HYDROGUI_ZLevelsModel::setObjects( const Object2VisibleList& theObjects )
 {
   myObjects = theObjects;
 
   reset();
 }
 
-bool HYDROGUI_ZLevelsModel::IsObjectVisible( int theIndex ) const
+HYDROGUI_ZLevelsModel::ObjectList HYDROGUI_ZLevelsModel::getObjects() const
+{
+  ObjectList anObjects;
+
+  foreach( const Object2Visible& anItem, myObjects ) {
+    anObjects << anItem.first;
+  }
+
+  return anObjects;
+}
+
+bool HYDROGUI_ZLevelsModel::isObjectVisible( int theIndex ) const
 {
   bool isVisible = false;
 
@@ -174,7 +191,8 @@ bool HYDROGUI_ZLevelsModel::dropMimeData( const QMimeData* theData, Qt::DropActi
     aStream >> anId;
     anIdsList << anId;
   }
-  move( anIdsList, DragAndDrop, aDropItemId );
+  qSort( anIdsList ); // TODO should be moved?
+  move( anIdsList, DragAndDrop, false, aDropItemId ); //TODO set visibility?
   return true;
 }
 
@@ -206,13 +224,15 @@ bool HYDROGUI_ZLevelsModel::move( const int theItem, const OpType theType,
   }
 
   int aDestinationIndex = -1;
+  bool isInsertBefore = false;
 
   switch ( theType ) {
     case Up:
+      isInsertBefore = true;
       if ( theItem > 0 ) {
         aDestinationIndex = theItem - 1;
         if ( theIsVisibleOnly ) {
-          while ( aDestinationIndex >= 0 && !IsObjectVisible( aDestinationIndex ) ) {
+          while ( aDestinationIndex >= 0 && !isObjectVisible( aDestinationIndex ) ) {
             aDestinationIndex--;
           }
         }
@@ -222,13 +242,14 @@ bool HYDROGUI_ZLevelsModel::move( const int theItem, const OpType theType,
       if ( theItem < myObjects.count() - 1 ) {
         aDestinationIndex = theItem + 1;
         if ( theIsVisibleOnly ) {
-          while ( aDestinationIndex < myObjects.count() && !IsObjectVisible( aDestinationIndex ) ) {
+          while ( aDestinationIndex < myObjects.count() && !isObjectVisible( aDestinationIndex ) ) {
             aDestinationIndex++;
           }
         }
       }
       break;
     case Top:
+      isInsertBefore = true;
       if ( theItem > 0 ) {
         aDestinationIndex = 0;
       }
@@ -238,10 +259,18 @@ bool HYDROGUI_ZLevelsModel::move( const int theItem, const OpType theType,
         aDestinationIndex = myObjects.count() - 1;
       }
       break;
+    case DragAndDrop:
+      if ( theItem > theDropItem ) {
+        isInsertBefore = true;
+        aDestinationIndex = theDropItem;
+      } else {
+        aDestinationIndex = theDropItem - 1;
+      }
+      break;
   }
 
   if ( aDestinationIndex >= 0 && aDestinationIndex < myObjects.count() ) {
-    int aDestinationRow = (theType == Up || theType == Top) ? aDestinationIndex : aDestinationIndex + 1;
+    int aDestinationRow = isInsertBefore ? aDestinationIndex : aDestinationIndex + 1;
     if ( beginMoveRows( QModelIndex(), theItem, theItem, QModelIndex(), aDestinationRow ) ) {
       myObjects.move( theItem, aDestinationIndex );
       endMoveRows();
@@ -257,38 +286,80 @@ bool HYDROGUI_ZLevelsModel::move( const QList<int>& theItems, const OpType theTy
 {
   bool aRes = true;
 
-  bool isReverse = theType == Top || theType == Down;
   QListIterator<int> anIt( theItems );
-  int aShift = 0;
-  if ( isReverse ) {
-    anIt.toBack();
-    while ( anIt.hasPrevious() ) {
-      int anId = anIt.previous();
-      if ( theType == Top ) {
-        anId += aShift;
-        aShift++;
-      }
-      if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
-        aRes = false;
-        break;
+  int aDragShift = 0;
+
+  switch ( theType ) {
+    case Top:
+    case Down:
+      // reverse order
+      anIt.toBack();
+      while ( anIt.hasPrevious() ) {
+        int anId = anIt.previous();
+        if ( theType == Top ) {
+          anId += aDragShift;
+          aDragShift++;
+        }
+        if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
+          aRes = false;
+          break;
+        }
       }
-    }
-  } else {
-    while ( anIt.hasNext() ) {
-      int anId = anIt.next();
-      if ( theType == Bottom ) {
-        anId -= aShift;
-        aShift++;
+      break;
+    case Bottom:
+    case Up:
+      // direct order
+      while ( anIt.hasNext() ) {
+        int anId = anIt.next();
+        if ( theType == Bottom ) {
+          anId -= aDragShift;
+          aDragShift++;
+        }
+        if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
+          aRes = false;
+          break;
+        }
       }
-      if ( !move( anId, theType, theIsVisibleOnly, theDropItem ) ) {
-        aRes = false;
-        break;
+      break;
+    case DragAndDrop:
+      // direct order
+      aRes = isDragAndDropAllowed( theItems, theDropItem );
+      if ( aRes ) {
+        int aDropShift = 0;
+        int aDropItem = theDropItem;
+        while ( anIt.hasNext() ) {
+          int anId = anIt.next();
+          aDropItem = theDropItem + aDropShift;
+          if ( anId > aDropItem ) {
+            aDragShift = 0;
+            aDropShift++;
+          } else {
+            anId -= aDragShift;
+            if ( ( aDropItem - anId ) != 1 ) {
+              aDragShift++;
+            }
+          }
+          move( anId, theType, theIsVisibleOnly, aDropItem );
+        }
       }
-    }
+      break;
+    default:
+      aRes = false;
   }
-
-  // reset(); //TODO dataChanged?
-
+  
   return aRes;
 }
 
+bool HYDROGUI_ZLevelsModel::isDragAndDropAllowed( const QList<int>& theItems, 
+                                                  const int theDropItem ) const
+{
+  bool isAllowed = false;
+
+  if ( theDropItem >= 0 && theDropItem < myObjects.count() &&
+       !theItems.empty() && theItems.count() < myObjects.count() &&
+       !theItems.contains( theDropItem )) {
+    isAllowed = true;
+  }
+
+  return isAllowed;
+}