Salome HOME
Copyright update 2022
[modules/gui.git] / src / OCCViewer / OCCViewer_CreateRestoreViewDlg.cxx
old mode 100755 (executable)
new mode 100644 (file)
index e97cde8..aa10ac1
@@ -1,87 +1,95 @@
-// Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
-// 
+// Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
-// License as published by the Free Software Foundation; either 
-// version 2.1 of the License.
-// 
-// This library is distributed in the hope that it will be useful 
-// but WITHOUT ANY WARRANTY; without even the implied warranty of 
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 // Lesser General Public License for more details.
 //
-// You should have received a copy of the GNU Lesser General Public  
-// License along with this library; if not, write to the Free Software 
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 //
 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
 #include "OCCViewer_CreateRestoreViewDlg.h"
-#include "OCCViewer_ViewModel.h"
 #include "OCCViewer_ViewPort3d.h"
 
-#include <qpushbutton.h>
-#include <qlayout.h>
-#include <qpainter.h>
-#include <qimage.h>
+#include <QPushButton>
+#include <QGridLayout>
+#include <QHBoxLayout>
+#include <QListWidgetItem>
+#include <QEvent>
+#include <QKeyEvent>
 
 /*!
   Constructor
 */
-OCCViewer_CreateRestoreViewDlg::OCCViewer_CreateRestoreViewDlg( QWidget* aWin, OCCViewer_Viewer* curModel )
+OCCViewer_CreateRestoreViewDlg::OCCViewer_CreateRestoreViewDlg( QWidget* aWin, OCCViewer_ViewWindow* theViewWindow )
 : QDialog( aWin )
 {
-  setCaption( tr( "CAPTION" ) );
+  setWindowTitle( tr( "CAPTION" ) );
 
-  myParametersMap = curModel->getViewAspects();
-
-  myKeyFlag = 0;
+  myParametersMap = theViewWindow->getViewAspects();
 
   int aQuantityOfItems = myParametersMap.count();
-       
+        
   setFixedSize( 400, 300 );
 
-  QGridLayout* aGrid = new QGridLayout( this, 2, 1, 5, 10 ); 
+  QGridLayout* aGrid = new QGridLayout( this );
+  aGrid->setMargin( 5 );
+  aGrid->setSpacing( 10 );
 
   QWidget* aWidget1 = new QWidget( this );
   QWidget* aWidget2 = new QWidget( this );
-       
+        
   QHBoxLayout* aLayout = new QHBoxLayout( aWidget1 );
-       
-  myListBox = new QtxListBox( aWidget1 );
+        
+  myListBox = new QListWidget( aWidget1 );
   myListBox->installEventFilter( this );
 
-  myCurViewPort = new OCCViewer_ViewPort3d( aWidget1, curModel->getViewer3d(), V3d_ORTHOGRAPHIC );
+  myCurViewPort = new OCCViewer_ViewPort3d( aWidget1, theViewWindow->getViewPort()->getViewer(), V3d_ORTHOGRAPHIC );
   myCurViewPort->getView()->SetBackgroundColor( Quantity_NOC_BLACK );
 
-  myListBox->setEditEnabled( 1 );
-       
+  myListBox->setEditTriggers( QAbstractItemView::DoubleClicked );
+        
   if ( aQuantityOfItems )
   {
     myListBox->clear();
-    for( int i = 0; i < aQuantityOfItems; i++ )
-      myListBox->insertItem( myParametersMap[ i ].name );
-    
+    for( int i = 0; i < aQuantityOfItems; i++ ) {
+      myListBox->insertItem( i, myParametersMap[ i ].name );
+      myListBox->item( i )->setFlags( myListBox->item( i )->flags() | Qt::ItemIsEditable );
+    }
+    myListBox->item( 0 )->setSelected( true );
     changeImage( myListBox->item( 0 ) );
   }
   else
   {
     myListBox->clear();
-    myListBox->insertItem( "No Items", 0 );
-    myListBox->setEditEnabled( 0 );
+    myListBox->insertItem( 0, "No Items" );
   }
-
-  connect( myListBox, SIGNAL( clicked( QListBoxItem* ) ), this, SLOT( changeImage( QListBoxItem* ) ) );
-  connect( myListBox, SIGNAL( itemEdited( QListBoxItem* ) ), this, SLOT( editItemText( QListBoxItem* ) ) );
-       
+  myListBox->setSelectionMode( QAbstractItemView::ExtendedSelection );
+  connect( myListBox, SIGNAL( currentItemChanged(QListWidgetItem*, QListWidgetItem *)), this, SLOT( changeImage( QListWidgetItem* ) ) );
+  connect( myListBox, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( editItemText( QListWidgetItem* ) ) );
+        
   aLayout->addWidget( myListBox );
   aLayout->addWidget( myCurViewPort, 30 );
 
-  QHBoxLayout* aButtonLayout = new QHBoxLayout( aWidget2, 0, 5 );
+  QHBoxLayout* aButtonLayout = new QHBoxLayout( aWidget2 );
+  aButtonLayout->setMargin( 0 );
+  aButtonLayout->setSpacing( 5 );
 
   QPushButton* theOk     = new QPushButton( tr( "Ok" ), aWidget2 );            theOk->setAutoDefault( false );
-  QPushButton* theCancel = new QPushButton( tr( "Cancel" ), aWidget2 );                 theCancel->setAutoDefault( false );
-  QPushButton* theDelete = new QPushButton( tr( "Delete" ), aWidget2 );                 theDelete->setAutoDefault( false );
+  QPushButton* theCancel = new QPushButton( tr( "Cancel" ), aWidget2 );          theCancel->setAutoDefault( false );
+  QPushButton* theDelete = new QPushButton( tr( "Delete" ), aWidget2 );          theDelete->setAutoDefault( false );
   QPushButton* theClearAll = new QPushButton( tr( "Clear List" ), aWidget2 );  theClearAll->setAutoDefault( false );
 
   aButtonLayout->addWidget( theOk );
@@ -91,7 +99,7 @@ OCCViewer_CreateRestoreViewDlg::OCCViewer_CreateRestoreViewDlg( QWidget* aWin, O
 
   aGrid->addWidget( aWidget1, 0, 0 );
   aGrid->addWidget( aWidget2, 1, 0 );
-       
+        
   connect( theOk, SIGNAL( clicked() ), this, SLOT( OKpressed() ) );
   connect( theCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
   connect( theDelete, SIGNAL( clicked() ), this, SLOT( deleteSelectedItems() ) );
@@ -109,51 +117,23 @@ OCCViewer_CreateRestoreViewDlg::~OCCViewer_CreateRestoreViewDlg()
   Changes image in accordance with item
   \param curItem - item contains info about view parameters
 */
-void OCCViewer_CreateRestoreViewDlg::changeImage( QListBoxItem* curItem )
+void OCCViewer_CreateRestoreViewDlg::changeImage( QListWidgetItem* curItem )
 {
-       if( curItem && myListBox->isEditEnabled() )
-       {
-               int lowLevel  = -1;
-               int highLevel = -1;
-               int index = curItem->listBox()->index( curItem );
-               if( myKeyFlag == 2 )
-               {
-                       for( int i = 0; i < (int)myListBox->count(); i++ )
-                       {
-                               if( myListBox->isSelected( i ) && i != index )
-                               {
-                                       myListBox->clearSelection();
-                                       if( i > index )
-                                       {
-                                               lowLevel  = index;
-                                               highLevel = i;
-                                       }
-                                       else
-                                       {
-                                               lowLevel  = i;
-                                               highLevel = index;
-                                       }
-                                       for( int j = lowLevel; j <= highLevel; j++ )
-                                               myListBox->setSelected( j, TRUE );
-                                       break;
-                               }
-                               if( myListBox->isSelected( i ) && i == index )
-                                       myListBox->setSelected( i, TRUE );
-                       }
-               }
-
-               Handle(V3d_View) aView3d = myCurViewPort->getView();
-               myCurrentItem = myParametersMap[ index ];
-
-               Standard_Boolean prev = aView3d->SetImmediateUpdate( Standard_False );
-               aView3d->SetScale( myCurrentItem.scale );
-               aView3d->SetCenter( myCurrentItem.centerX, myCurrentItem.centerY );
-               aView3d->SetProj( myCurrentItem.projX, myCurrentItem.projY, myCurrentItem.projZ );
-               aView3d->SetTwist( myCurrentItem.twist );
-               aView3d->SetAt( myCurrentItem.atX, myCurrentItem.atY, myCurrentItem.atZ );
-               aView3d->SetImmediateUpdate( prev );
-               aView3d->SetEye( myCurrentItem.eyeX, myCurrentItem.eyeY, myCurrentItem.eyeZ );
-        }
+  if( curItem && ( curItem->flags() & Qt::ItemIsEditable ) )
+  {
+    int index = curItem->listWidget()->row( curItem );  
+    Handle(V3d_View) aView3d = myCurViewPort->getView();
+    myCurrentItem = myParametersMap[ index ];
+    
+    Standard_Boolean prev = aView3d->SetImmediateUpdate( Standard_False );
+    aView3d->SetScale( myCurrentItem.scale );
+    aView3d->SetProj( myCurrentItem.projX, myCurrentItem.projY, myCurrentItem.projZ );
+    aView3d->SetTwist( myCurrentItem.twist );
+    aView3d->SetAt( myCurrentItem.atX, myCurrentItem.atY, myCurrentItem.atZ );
+    aView3d->SetImmediateUpdate( prev );
+    aView3d->SetEye( myCurrentItem.eyeX, myCurrentItem.eyeY, myCurrentItem.eyeZ );
+    aView3d->SetAxialScale( myCurrentItem.scaleX, myCurrentItem.scaleY, myCurrentItem.scaleZ );
+  }
 }
 
 /*!
@@ -161,7 +141,7 @@ void OCCViewer_CreateRestoreViewDlg::changeImage( QListBoxItem* curItem )
 */
 viewAspect OCCViewer_CreateRestoreViewDlg::currentItem() const
 {
-       return myCurrentItem;
+  return myCurrentItem;
 }
 
 /*!
@@ -169,36 +149,47 @@ viewAspect OCCViewer_CreateRestoreViewDlg::currentItem() const
 */
 void OCCViewer_CreateRestoreViewDlg::deleteSelectedItems()
 {
-       if( myListBox->count() && myListBox->isEditEnabled() )
-       {
-               int curIndex = -1;
-               for( int i = 0; i < (int)myListBox->count(); i++ )
-                       if( myListBox->isSelected( i ) )
-                       {
-                               myListBox->removeItem( i );
-                               for( int j = i; j < (int)myParametersMap.count(); j++ )
-                                       if( j != myParametersMap.count() - 1 )
-                                               myParametersMap[ j ] = myParametersMap[ j + 1 ];
-                                       else
-                                               myParametersMap.remove( myParametersMap.at(j) );
-                               if( i != myListBox->count() )
-                                       curIndex = i;
-                               else
-                                       curIndex = i - 1;
-                               i--;
-                       }
-               if( curIndex >= 0 )
-               {
-                       myListBox->setCurrentItem( curIndex );
-                       changeImage( myListBox->item( curIndex ) );
-               }
-       }
-       if( !myListBox->count() )
-       {
-               myListBox->clear();
-               myListBox->insertItem( "No Items", 0 );
-               myListBox->setEditEnabled( 0 );
-       }
+  QList<QListWidgetItem*> selectedItems = myListBox->selectedItems();
+  if( myListBox->count() && selectedItems.count())
+  {
+    int curIndex = -1;
+    // Iterate by all selected items
+    for(int i = 0; i < selectedItems.count(); i++) 
+    {
+      QListWidgetItem* item =  selectedItems.at(i);
+      // get position of the selected item in the list
+      int position = myListBox->row(item);
+
+      //Calculate current index in case if "item" is last selected item.
+      if(i == selectedItems.count() - 1)
+      {
+        if(position != myListBox->count() - 1)
+          curIndex = position;
+        else 
+          curIndex = position - 1;
+      }
+
+      //Delete item
+      delete item;
+
+      //Shift parameters in the map
+      for( int j = position; j < (int)myParametersMap.count(); j++ )
+      {
+        if( j != myParametersMap.count() - 1 )
+          myParametersMap[ j ] = myParametersMap[ j + 1 ];
+        else
+          myParametersMap.removeAt( j );
+      }
+    }
+    if( curIndex >= 0 )
+    {
+      myListBox->setItemSelected( myListBox->item( curIndex ), true );
+    }
+  }
+  if( !myListBox->count() )
+  {
+    clearList();
+  }
 }
 
 /*!
@@ -206,11 +197,12 @@ void OCCViewer_CreateRestoreViewDlg::deleteSelectedItems()
 */
 void OCCViewer_CreateRestoreViewDlg::clearList()
 {
-       myListBox->clear();
-       myListBox->insertItem( "No Items", 0 );
-       myListBox->setEditEnabled( 0 );
-
-       myParametersMap.clear();
+  myListBox->clear();
+  myListBox->insertItem( 0, "No Items" );
+  myParametersMap.clear();
+       
+  //Clear view
+  myCurViewPort->reset();
 }
 
 /*!
@@ -218,49 +210,17 @@ void OCCViewer_CreateRestoreViewDlg::clearList()
 */
 const viewAspectList& OCCViewer_CreateRestoreViewDlg::parameters() const
 {
-       return myParametersMap;
+  return myParametersMap;
 }
 
 /*!
   Renames key of view aspect map in accordance with item name
   \param anItem - item
 */
-void OCCViewer_CreateRestoreViewDlg::editItemText( QListBoxItem* anItem )
+void OCCViewer_CreateRestoreViewDlg::editItemText( QListWidgetItem* anItem )
 {
-       int index = anItem->listBox()->index( anItem );
-       myParametersMap[ index ].name = anItem->text().latin1();
-}
-
-/*!
-  Custom event filter
-*/
-bool OCCViewer_CreateRestoreViewDlg::eventFilter( QObject* anObj, QEvent* anEv )
-{
-       if( anEv->type() == QEvent::KeyPress )
-       {
-               QKeyEvent* aKeyEv = ( QKeyEvent* )anEv;
-               if( aKeyEv->key() == Qt::Key_Control )
-               {
-                       myKeyFlag = 1;
-                       myListBox->setSelectionMode( QListBox::Multi ); 
-               }
-               else if( aKeyEv->key() == Qt::Key_Shift )
-               {
-                       myKeyFlag = 2;
-                       myListBox->setSelectionMode( QListBox::Multi ); 
-               }
-               else
-                       myListBox->setSelectionMode( QListBox::Single );
-       }
-       if( anEv->type() == QEvent::KeyRelease )
-               myKeyFlag = 0;
-       
-       if( !myKeyFlag )
-       {
-               if( anEv->type() == QEvent::KeyPress || anEv->type() == QEvent::MouseButtonPress )
-                       myListBox->setSelectionMode( QListBox::Single );
-       }
-       return QWidget::eventFilter( anObj, anEv );
+  int index = anItem->listWidget()->row( anItem );
+  myParametersMap[ index ].name = anItem->text().toUtf8();
 }
 
 /*!
@@ -268,7 +228,7 @@ bool OCCViewer_CreateRestoreViewDlg::eventFilter( QObject* anObj, QEvent* anEv )
 */
 void OCCViewer_CreateRestoreViewDlg::OKpressed()
 {
-       emit dlgOk();
-       accept();
+  emit dlgOk();
+  accept();
 }