Salome HOME
8092b1b6dc6c12139cfce0065a9fb8d8c476d8f2
[modules/gui.git] / src / OCCViewer / OCCViewer_CreateRestoreViewDlg.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "OCCViewer_CreateRestoreViewDlg.h"
20 #include "OCCViewer_ViewModel.h"
21 #include "OCCViewer_ViewPort3d.h"
22
23 #include <qpushbutton.h>
24 #include <qlayout.h>
25 #include <qpainter.h>
26 #include <qimage.h>
27
28 /*!
29   Constructor
30 */
31 OCCViewer_CreateRestoreViewDlg::OCCViewer_CreateRestoreViewDlg( QWidget* aWin, OCCViewer_Viewer* curModel )
32 : QDialog( aWin )
33 {
34   setCaption( tr( "CAPTION" ) );
35
36   myParametersMap = curModel->getViewAspects();
37
38   myKeyFlag = 0;
39
40   int aQuantityOfItems = myParametersMap.count();
41         
42   setFixedSize( 400, 300 );
43
44   QGridLayout* aGrid = new QGridLayout( this, 2, 1, 5, 10 ); 
45
46   QWidget* aWidget1 = new QWidget( this );
47   QWidget* aWidget2 = new QWidget( this );
48         
49   QHBoxLayout* aLayout = new QHBoxLayout( aWidget1 );
50         
51   myListBox = new QtxListBox( aWidget1 );
52   myListBox->installEventFilter( this );
53
54   myCurViewPort = new OCCViewer_ViewPort3d( aWidget1, curModel->getViewer3d(), V3d_ORTHOGRAPHIC );
55   myCurViewPort->getView()->SetBackgroundColor( Quantity_NOC_BLACK );
56
57   myListBox->setEditEnabled( 1 );
58         
59   if ( aQuantityOfItems )
60   {
61     myListBox->clear();
62     for( int i = 0; i < aQuantityOfItems; i++ )
63       myListBox->insertItem( myParametersMap[ i ].name );
64     
65     changeImage( myListBox->item( 0 ) );
66   }
67   else
68   {
69     myListBox->clear();
70     myListBox->insertItem( "No Items", 0 );
71     myListBox->setEditEnabled( 0 );
72   }
73
74   connect( myListBox, SIGNAL( clicked( QListBoxItem* ) ), this, SLOT( changeImage( QListBoxItem* ) ) );
75   connect( myListBox, SIGNAL( itemEdited( QListBoxItem* ) ), this, SLOT( editItemText( QListBoxItem* ) ) );
76         
77   aLayout->addWidget( myListBox );
78   aLayout->addWidget( myCurViewPort, 30 );
79
80   QHBoxLayout* aButtonLayout = new QHBoxLayout( aWidget2, 0, 5 );
81
82   QPushButton* theOk     = new QPushButton( tr( "Ok" ), aWidget2 );            theOk->setAutoDefault( false );
83   QPushButton* theCancel = new QPushButton( tr( "Cancel" ), aWidget2 );          theCancel->setAutoDefault( false );
84   QPushButton* theDelete = new QPushButton( tr( "Delete" ), aWidget2 );          theDelete->setAutoDefault( false );
85   QPushButton* theClearAll = new QPushButton( tr( "Clear List" ), aWidget2 );  theClearAll->setAutoDefault( false );
86
87   aButtonLayout->addWidget( theOk );
88   aButtonLayout->addWidget( theCancel );
89   aButtonLayout->addWidget( theDelete );
90   aButtonLayout->addWidget( theClearAll );
91
92   aGrid->addWidget( aWidget1, 0, 0 );
93   aGrid->addWidget( aWidget2, 1, 0 );
94         
95   connect( theOk, SIGNAL( clicked() ), this, SLOT( OKpressed() ) );
96   connect( theCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
97   connect( theDelete, SIGNAL( clicked() ), this, SLOT( deleteSelectedItems() ) );
98   connect( theClearAll, SIGNAL( clicked() ), this, SLOT( clearList() ) );
99 }
100
101 /*!
102   Destructor
103 */
104 OCCViewer_CreateRestoreViewDlg::~OCCViewer_CreateRestoreViewDlg()
105 {
106 }
107
108 /*!
109   Changes image in accordance with item
110   \param curItem - item contains info about view parameters
111 */
112 void OCCViewer_CreateRestoreViewDlg::changeImage( QListBoxItem* curItem )
113 {
114         if( curItem && myListBox->isEditEnabled() )
115         {
116                 int lowLevel  = -1;
117                 int highLevel = -1;
118                 int index = curItem->listBox()->index( curItem );
119                 if( myKeyFlag == 2 )
120                 {
121                         for( int i = 0; i < myListBox->count(); i++ )
122                         {
123                                 if( myListBox->isSelected( i ) && i != index )
124                                 {
125                                         myListBox->clearSelection();
126                                         if( i > index )
127                                         {
128                                                 lowLevel  = index;
129                                                 highLevel = i;
130                                         }
131                                         else
132                                         {
133                                                 lowLevel  = i;
134                                                 highLevel = index;
135                                         }
136                                         for( int j = lowLevel; j <= highLevel; j++ )
137                                                 myListBox->setSelected( j, TRUE );
138                                         break;
139                                 }
140                                 if( myListBox->isSelected( i ) && i == index )
141                                         myListBox->setSelected( i, TRUE );
142                         }
143                 }
144
145                 Handle(V3d_View) aView3d = myCurViewPort->getView();
146                 myCurrentItem = myParametersMap[ index ];
147
148                 Standard_Boolean prev = aView3d->SetImmediateUpdate( Standard_False );
149                 aView3d->SetScale( myCurrentItem.scale );
150                 aView3d->SetCenter( myCurrentItem.centerX, myCurrentItem.centerY );
151                 aView3d->SetProj( myCurrentItem.projX, myCurrentItem.projY, myCurrentItem.projZ );
152                 aView3d->SetTwist( myCurrentItem.twist );
153                 aView3d->SetAt( myCurrentItem.atX, myCurrentItem.atY, myCurrentItem.atZ );
154                 aView3d->SetImmediateUpdate( prev );
155                 aView3d->SetEye( myCurrentItem.eyeX, myCurrentItem.eyeY, myCurrentItem.eyeZ );
156          }
157 }
158
159 /*!
160   \return current view parameters (corresponding to current item)
161 */
162 viewAspect OCCViewer_CreateRestoreViewDlg::currentItem() const
163 {
164         return myCurrentItem;
165 }
166
167 /*!
168   Deletes selected items from list view
169 */
170 void OCCViewer_CreateRestoreViewDlg::deleteSelectedItems()
171 {
172         if( myListBox->count() && myListBox->isEditEnabled() )
173         {
174                 int curIndex = -1;
175                 for( int i = 0; i < myListBox->count(); i++ )
176                         if( myListBox->isSelected( i ) )
177                         {
178                                 myListBox->removeItem( i );
179                                 for( int j = i; j < myParametersMap.count(); j++ )
180                                         if( j != myParametersMap.count() - 1 )
181                                                 myParametersMap[ j ] = myParametersMap[ j + 1 ];
182                                         else
183                                                 myParametersMap.remove( myParametersMap.at(j) );
184                                 if( i != myListBox->count() )
185                                         curIndex = i;
186                                 else
187                                         curIndex = i - 1;
188                                 i--;
189                         }
190                 if( curIndex >= 0 )
191                 {
192                         myListBox->setCurrentItem( curIndex );
193                         changeImage( myListBox->item( curIndex ) );
194                 }
195         }
196         if( !myListBox->count() )
197         {
198                 myListBox->clear();
199                 myListBox->insertItem( "No Items", 0 );
200                 myListBox->setEditEnabled( 0 );
201         }
202 }
203
204 /*!
205   Clears list of view aspects
206 */
207 void OCCViewer_CreateRestoreViewDlg::clearList()
208 {
209         myListBox->clear();
210         myListBox->insertItem( "No Items", 0 );
211         myListBox->setEditEnabled( 0 );
212
213         myParametersMap.clear();
214 }
215
216 /*!
217   \return const reference to all view aspects
218 */
219 const viewAspectList& OCCViewer_CreateRestoreViewDlg::parameters() const
220 {
221         return myParametersMap;
222 }
223
224 /*!
225   Renames key of view aspect map in accordance with item name
226   \param anItem - item
227 */
228 void OCCViewer_CreateRestoreViewDlg::editItemText( QListBoxItem* anItem )
229 {
230         int index = anItem->listBox()->index( anItem );
231         myParametersMap[ index ].name = anItem->text().latin1();
232 }
233
234 /*!
235   Custom event filter
236 */
237 bool OCCViewer_CreateRestoreViewDlg::eventFilter( QObject* anObj, QEvent* anEv )
238 {
239         if( anEv->type() == QEvent::KeyPress )
240         {
241                 QKeyEvent* aKeyEv = ( QKeyEvent* )anEv;
242                 if( aKeyEv->key() == Qt::Key_Control )
243                 {
244                         myKeyFlag = 1;
245                         myListBox->setSelectionMode( QListBox::Multi ); 
246                 }
247                 else if( aKeyEv->key() == Qt::Key_Shift )
248                 {
249                         myKeyFlag = 2;
250                         myListBox->setSelectionMode( QListBox::Multi ); 
251                 }
252                 else
253                         myListBox->setSelectionMode( QListBox::Single );
254         }
255         if( anEv->type() == QEvent::KeyRelease )
256                 myKeyFlag = 0;
257         
258         if( !myKeyFlag )
259         {
260                 if( anEv->type() == QEvent::KeyPress || anEv->type() == QEvent::MouseButtonPress )
261                         myListBox->setSelectionMode( QListBox::Single );
262         }
263         return QWidget::eventFilter( anObj, anEv );
264 }
265
266 /*!
267   SLOT: called on OK click, emits dlgOk() and closes dialog
268 */
269 void OCCViewer_CreateRestoreViewDlg::OKpressed()
270 {
271         emit dlgOk();
272         accept();
273 }
274