Salome HOME
refs #1458: disable chained panning on operations
[modules/gui.git] / src / OCCViewer / OCCViewer_CreateRestoreViewDlg.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "OCCViewer_CreateRestoreViewDlg.h"
24 #include "OCCViewer_ViewPort3d.h"
25
26 #include <QPushButton>
27 #include <QGridLayout>
28 #include <QHBoxLayout>
29 #include <QListWidgetItem>
30 #include <QEvent>
31 #include <QKeyEvent>
32
33 /*!
34   Constructor
35 */
36 OCCViewer_CreateRestoreViewDlg::OCCViewer_CreateRestoreViewDlg( QWidget* aWin, OCCViewer_ViewWindow* theViewWindow )
37 : QDialog( aWin )
38 {
39   setWindowTitle( tr( "CAPTION" ) );
40
41   myParametersMap = theViewWindow->getViewAspects();
42
43   int aQuantityOfItems = myParametersMap.count();
44         
45   setFixedSize( 400, 300 );
46
47   QGridLayout* aGrid = new QGridLayout( this );
48   aGrid->setMargin( 5 );
49   aGrid->setSpacing( 10 );
50
51   QWidget* aWidget1 = new QWidget( this );
52   QWidget* aWidget2 = new QWidget( this );
53         
54   QHBoxLayout* aLayout = new QHBoxLayout( aWidget1 );
55         
56   myListBox = new QListWidget( aWidget1 );
57   myListBox->installEventFilter( this );
58
59   myCurViewPort = new OCCViewer_ViewPort3d( aWidget1, theViewWindow->getViewPort()->getViewer(), V3d_ORTHOGRAPHIC );
60   myCurViewPort->getView()->SetBackgroundColor( Quantity_NOC_BLACK );
61
62   myListBox->setEditTriggers( QAbstractItemView::DoubleClicked );
63         
64   if ( aQuantityOfItems )
65   {
66     myListBox->clear();
67     for( int i = 0; i < aQuantityOfItems; i++ ) {
68       myListBox->insertItem( i, myParametersMap[ i ].name );
69       myListBox->item( i )->setFlags( myListBox->item( i )->flags() | Qt::ItemIsEditable );
70     }
71     myListBox->item( 0 )->setSelected( true );
72     changeImage( myListBox->item( 0 ) );
73   }
74   else
75   {
76     myListBox->clear();
77     myListBox->insertItem( 0, "No Items" );
78   }
79   myListBox->setSelectionMode( QAbstractItemView::ExtendedSelection );
80   connect( myListBox, SIGNAL( currentItemChanged(QListWidgetItem*, QListWidgetItem *)), this, SLOT( changeImage( QListWidgetItem* ) ) );
81   connect( myListBox, SIGNAL( itemChanged( QListWidgetItem* ) ), this, SLOT( editItemText( QListWidgetItem* ) ) );
82         
83   aLayout->addWidget( myListBox );
84   aLayout->addWidget( myCurViewPort, 30 );
85
86   QHBoxLayout* aButtonLayout = new QHBoxLayout( aWidget2 );
87   aButtonLayout->setMargin( 0 );
88   aButtonLayout->setSpacing( 5 );
89
90   QPushButton* theOk     = new QPushButton( tr( "Ok" ), aWidget2 );            theOk->setAutoDefault( false );
91   QPushButton* theCancel = new QPushButton( tr( "Cancel" ), aWidget2 );          theCancel->setAutoDefault( false );
92   QPushButton* theDelete = new QPushButton( tr( "Delete" ), aWidget2 );          theDelete->setAutoDefault( false );
93   QPushButton* theClearAll = new QPushButton( tr( "Clear List" ), aWidget2 );  theClearAll->setAutoDefault( false );
94
95   aButtonLayout->addWidget( theOk );
96   aButtonLayout->addWidget( theCancel );
97   aButtonLayout->addWidget( theDelete );
98   aButtonLayout->addWidget( theClearAll );
99
100   aGrid->addWidget( aWidget1, 0, 0 );
101   aGrid->addWidget( aWidget2, 1, 0 );
102         
103   connect( theOk, SIGNAL( clicked() ), this, SLOT( OKpressed() ) );
104   connect( theCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
105   connect( theDelete, SIGNAL( clicked() ), this, SLOT( deleteSelectedItems() ) );
106   connect( theClearAll, SIGNAL( clicked() ), this, SLOT( clearList() ) );
107 }
108
109 /*!
110   Destructor
111 */
112 OCCViewer_CreateRestoreViewDlg::~OCCViewer_CreateRestoreViewDlg()
113 {
114 }
115
116 /*!
117   Changes image in accordance with item
118   \param curItem - item contains info about view parameters
119 */
120 void OCCViewer_CreateRestoreViewDlg::changeImage( QListWidgetItem* curItem )
121 {
122   if( curItem && ( curItem->flags() & Qt::ItemIsEditable ) )
123   {
124     int lowLevel  = -1;
125     int highLevel = -1;
126     int index = curItem->listWidget()->row( curItem );  
127     Handle(V3d_View) aView3d = myCurViewPort->getView();
128     myCurrentItem = myParametersMap[ index ];
129     
130     Standard_Boolean prev = aView3d->SetImmediateUpdate( Standard_False );
131     aView3d->SetScale( myCurrentItem.scale );
132 #if OCC_VERSION_LARGE <= 0x06070100
133     aView3d->SetCenter( myCurrentItem.centerX, myCurrentItem.centerY );
134 #endif
135     aView3d->SetProj( myCurrentItem.projX, myCurrentItem.projY, myCurrentItem.projZ );
136     aView3d->SetTwist( myCurrentItem.twist );
137     aView3d->SetAt( myCurrentItem.atX, myCurrentItem.atY, myCurrentItem.atZ );
138     aView3d->SetImmediateUpdate( prev );
139     aView3d->SetEye( myCurrentItem.eyeX, myCurrentItem.eyeY, myCurrentItem.eyeZ );
140     aView3d->SetAxialScale( myCurrentItem.scaleX, myCurrentItem.scaleY, myCurrentItem.scaleZ );
141   }
142 }
143
144 /*!
145   \return current view parameters (corresponding to current item)
146 */
147 viewAspect OCCViewer_CreateRestoreViewDlg::currentItem() const
148 {
149   return myCurrentItem;
150 }
151
152 /*!
153   Deletes selected items from list view
154 */
155 void OCCViewer_CreateRestoreViewDlg::deleteSelectedItems()
156 {
157   QList<QListWidgetItem*> selectedItems = myListBox->selectedItems();
158   if( myListBox->count() && selectedItems.count())
159   {
160     int curIndex = -1;
161     // Iterate by all selected items
162     for(int i = 0; i < selectedItems.count(); i++) 
163     {
164       QListWidgetItem* item =  selectedItems.at(i);
165       // get position of the selected item in the list
166       int position = myListBox->row(item);
167
168       //Calculate current index in case if "item" is last selected item.
169       if(i == selectedItems.count() - 1)
170       {
171         if(position != myListBox->count() - 1)
172           curIndex = position;
173         else 
174           curIndex = position - 1;
175       }
176
177       //Delete item
178       delete item;
179
180       //Shift parameters in the map
181       for( int j = position; j < (int)myParametersMap.count(); j++ )
182       {
183         if( j != myParametersMap.count() - 1 )
184           myParametersMap[ j ] = myParametersMap[ j + 1 ];
185         else
186           myParametersMap.removeAt( j );
187       }
188     }
189     if( curIndex >= 0 )
190     {
191       myListBox->setItemSelected( myListBox->item( curIndex ), true );
192     }
193   }
194   if( !myListBox->count() )
195   {
196     clearList();
197   }
198 }
199
200 /*!
201   Clears list of view aspects
202 */
203 void OCCViewer_CreateRestoreViewDlg::clearList()
204 {
205   myListBox->clear();
206   myListBox->insertItem( 0, "No Items" );
207   myParametersMap.clear();
208         
209   //Clear view
210   myCurViewPort->reset();
211 }
212
213 /*!
214   \return const reference to all view aspects
215 */
216 const viewAspectList& OCCViewer_CreateRestoreViewDlg::parameters() const
217 {
218   return myParametersMap;
219 }
220
221 /*!
222   Renames key of view aspect map in accordance with item name
223   \param anItem - item
224 */
225 void OCCViewer_CreateRestoreViewDlg::editItemText( QListWidgetItem* anItem )
226 {
227   int index = anItem->listWidget()->row( anItem );
228   myParametersMap[ index ].name = anItem->text().toLatin1();
229 }
230
231 /*!
232   SLOT: called on OK click, emits dlgOk() and closes dialog
233 */
234 void OCCViewer_CreateRestoreViewDlg::OKpressed()
235 {
236   emit dlgOk();
237   accept();
238 }
239