Back to index

salome-gui  6.5.0
OCCViewer_CreateRestoreViewDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "OCCViewer_CreateRestoreViewDlg.h"
00024 #include "OCCViewer_ViewPort3d.h"
00025 
00026 #include <QPushButton>
00027 #include <QGridLayout>
00028 #include <QHBoxLayout>
00029 #include <QListWidgetItem>
00030 #include <QEvent>
00031 #include <QKeyEvent>
00032 
00036 OCCViewer_CreateRestoreViewDlg::OCCViewer_CreateRestoreViewDlg( QWidget* aWin, OCCViewer_ViewWindow* theViewWindow )
00037 : QDialog( aWin )
00038 {
00039   setWindowTitle( tr( "CAPTION" ) );
00040 
00041   myParametersMap = theViewWindow->getViewAspects();
00042 
00043   myKeyFlag = 0;
00044 
00045   int aQuantityOfItems = myParametersMap.count();
00046         
00047   setFixedSize( 400, 300 );
00048 
00049   QGridLayout* aGrid = new QGridLayout( this );
00050   aGrid->setMargin( 5 );
00051   aGrid->setSpacing( 10 );
00052 
00053   QWidget* aWidget1 = new QWidget( this );
00054   QWidget* aWidget2 = new QWidget( this );
00055         
00056   QHBoxLayout* aLayout = new QHBoxLayout( aWidget1 );
00057         
00058   myListBox = new QListWidget( aWidget1 );
00059   myListBox->installEventFilter( this );
00060 
00061   myCurViewPort = new OCCViewer_ViewPort3d( aWidget1, theViewWindow->getViewPort()->getViewer(), V3d_ORTHOGRAPHIC );
00062   myCurViewPort->getView()->SetBackgroundColor( Quantity_NOC_BLACK );
00063 
00064   myListBox->setEditTriggers( QAbstractItemView::DoubleClicked );
00065         
00066   if ( aQuantityOfItems )
00067   {
00068     myListBox->clear();
00069     for( int i = 0; i < aQuantityOfItems; i++ ) {
00070       myListBox->insertItem( i, myParametersMap[ i ].name );
00071       myListBox->item( i )->setFlags( myListBox->item( i )->flags() | Qt::ItemIsEditable );
00072     }
00073     
00074     changeImage( myListBox->item( 0 ) );
00075   }
00076   else
00077   {
00078     myListBox->clear();
00079     myListBox->insertItem( 0, "No Items" );
00080   }
00081 
00082   connect( myListBox, SIGNAL( itemClicked( QListWidgetItem* ) ), this, SLOT( changeImage( QListWidgetItem* ) ) );
00083   connect( myListBox, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( editItemText( QListWidgetItem* ) ) );
00084         
00085   aLayout->addWidget( myListBox );
00086   aLayout->addWidget( myCurViewPort, 30 );
00087 
00088   QHBoxLayout* aButtonLayout = new QHBoxLayout( aWidget2 );
00089   aButtonLayout->setMargin( 0 );
00090   aButtonLayout->setSpacing( 5 );
00091 
00092   QPushButton* theOk     = new QPushButton( tr( "Ok" ), aWidget2 );            theOk->setAutoDefault( false );
00093   QPushButton* theCancel = new QPushButton( tr( "Cancel" ), aWidget2 );          theCancel->setAutoDefault( false );
00094   QPushButton* theDelete = new QPushButton( tr( "Delete" ), aWidget2 );          theDelete->setAutoDefault( false );
00095   QPushButton* theClearAll = new QPushButton( tr( "Clear List" ), aWidget2 );  theClearAll->setAutoDefault( false );
00096 
00097   aButtonLayout->addWidget( theOk );
00098   aButtonLayout->addWidget( theCancel );
00099   aButtonLayout->addWidget( theDelete );
00100   aButtonLayout->addWidget( theClearAll );
00101 
00102   aGrid->addWidget( aWidget1, 0, 0 );
00103   aGrid->addWidget( aWidget2, 1, 0 );
00104         
00105   connect( theOk, SIGNAL( clicked() ), this, SLOT( OKpressed() ) );
00106   connect( theCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
00107   connect( theDelete, SIGNAL( clicked() ), this, SLOT( deleteSelectedItems() ) );
00108   connect( theClearAll, SIGNAL( clicked() ), this, SLOT( clearList() ) );
00109 }
00110 
00114 OCCViewer_CreateRestoreViewDlg::~OCCViewer_CreateRestoreViewDlg()
00115 {
00116 }
00117 
00122 void OCCViewer_CreateRestoreViewDlg::changeImage( QListWidgetItem* curItem )
00123 {
00124   if( curItem && ( curItem->flags() & Qt::ItemIsEditable ) )
00125   {
00126     int lowLevel  = -1;
00127     int highLevel = -1;
00128     int index = curItem->listWidget()->row( curItem );
00129     if( myKeyFlag == 2 )
00130     {
00131       for( int i = 0; i < (int)myListBox->count(); i++ )
00132       {
00133         if( myListBox->item( i )->isSelected() && i != index )
00134         {
00135           myListBox->clearSelection();
00136           if( i > index )
00137           {
00138             lowLevel  = index;
00139             highLevel = i;
00140           }
00141           else
00142           {
00143             lowLevel  = i;
00144             highLevel = index;
00145           }
00146           for( int j = lowLevel; j <= highLevel; j++ )
00147             myListBox->item( j )->setSelected( TRUE );
00148           break;
00149         }
00150         if( myListBox->item( i )->isSelected() && i == index )
00151           myListBox->item( i )->setSelected( TRUE );
00152       }
00153     }
00154     
00155     Handle(V3d_View) aView3d = myCurViewPort->getView();
00156     myCurrentItem = myParametersMap[ index ];
00157     
00158     Standard_Boolean prev = aView3d->SetImmediateUpdate( Standard_False );
00159     aView3d->SetScale( myCurrentItem.scale );
00160     aView3d->SetCenter( myCurrentItem.centerX, myCurrentItem.centerY );
00161     aView3d->SetProj( myCurrentItem.projX, myCurrentItem.projY, myCurrentItem.projZ );
00162     aView3d->SetTwist( myCurrentItem.twist );
00163     aView3d->SetAt( myCurrentItem.atX, myCurrentItem.atY, myCurrentItem.atZ );
00164     aView3d->SetImmediateUpdate( prev );
00165     aView3d->SetEye( myCurrentItem.eyeX, myCurrentItem.eyeY, myCurrentItem.eyeZ );
00166     aView3d->SetAxialScale( myCurrentItem.scaleX, myCurrentItem.scaleY, myCurrentItem.scaleZ );
00167   }
00168 }
00169 
00173 viewAspect OCCViewer_CreateRestoreViewDlg::currentItem() const
00174 {
00175   return myCurrentItem;
00176 }
00177 
00181 void OCCViewer_CreateRestoreViewDlg::deleteSelectedItems()
00182 {
00183   QList<QListWidgetItem*> selectedItems = myListBox->selectedItems();
00184   if( myListBox->count() && selectedItems.count())
00185   {
00186     int curIndex = -1;
00187     // Iterate by all selected items
00188     for(int i = 0; i < selectedItems.count(); i++) 
00189     {
00190       QListWidgetItem* item =  selectedItems.at(i);
00191       // get position of the selected item in the list
00192       int position = myListBox->row(item);
00193 
00194       //Calculate current index in case if "item" is last selected item.
00195       if(i == selectedItems.count() - 1)
00196       {
00197        if(position != myListBox->count() - 1)
00198          curIndex = position;
00199        else 
00200          curIndex = position - 1;
00201       }
00202 
00203       //Delete item
00204       delete item;
00205 
00206       //Shift parameters in the map
00207       for( int j = position; j < (int)myParametersMap.count(); j++ )
00208       {
00209        if( j != myParametersMap.count() - 1 )
00210          myParametersMap[ j ] = myParametersMap[ j + 1 ];
00211        else
00212          myParametersMap.removeAt( j );
00213       }
00214     }
00215     if( curIndex >= 0 )
00216     {
00217       myListBox->setCurrentItem( myListBox->item( curIndex ) );
00218       changeImage( myListBox->item( curIndex ) );
00219     }
00220   }
00221   if( !myListBox->count() )
00222   {
00223     clearList();
00224   }
00225 }
00226 
00230 void OCCViewer_CreateRestoreViewDlg::clearList()
00231 {
00232   myListBox->clear();
00233   myListBox->insertItem( 0, "No Items" );
00234   myParametersMap.clear();
00235        
00236   //Clear view
00237   myCurViewPort->reset();
00238 }
00239 
00243 const viewAspectList& OCCViewer_CreateRestoreViewDlg::parameters() const
00244 {
00245   return myParametersMap;
00246 }
00247 
00252 void OCCViewer_CreateRestoreViewDlg::editItemText( QListWidgetItem* anItem )
00253 {
00254   int index = anItem->listWidget()->row( anItem );
00255   myParametersMap[ index ].name = anItem->text().toLatin1();
00256 }
00257 
00261 bool OCCViewer_CreateRestoreViewDlg::eventFilter( QObject* anObj, QEvent* anEv )
00262 {
00263   if( anEv->type() == QEvent::KeyPress )
00264   {
00265     QKeyEvent* aKeyEv = ( QKeyEvent* )anEv;
00266     if( aKeyEv->key() == Qt::Key_Control )
00267     {
00268       myKeyFlag = 1;
00269       myListBox->setSelectionMode( QAbstractItemView::MultiSelection ); 
00270     }
00271     else if( aKeyEv->key() == Qt::Key_Shift )
00272     {
00273       myKeyFlag = 2;
00274       myListBox->setSelectionMode( QAbstractItemView::MultiSelection ); 
00275     }
00276     else
00277       myListBox->setSelectionMode( QAbstractItemView::SingleSelection );
00278   }
00279   if( anEv->type() == QEvent::KeyRelease )
00280     myKeyFlag = 0;
00281   
00282   if( !myKeyFlag )
00283   {
00284     if( anEv->type() == QEvent::KeyPress || anEv->type() == QEvent::MouseButtonPress )
00285       myListBox->setSelectionMode( QAbstractItemView::SingleSelection );
00286   }
00287   return QWidget::eventFilter( anObj, anEv );
00288 }
00289 
00293 void OCCViewer_CreateRestoreViewDlg::OKpressed()
00294 {
00295   emit dlgOk();
00296   accept();
00297 }
00298