Salome HOME
Copyrights update
[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 OCCViewer_CreateRestoreViewDlg::OCCViewer_CreateRestoreViewDlg( QWidget* aWin, OCCViewer_Viewer* curModel )
29 : QDialog( aWin )
30 {
31   setCaption( tr( "CAPTION" ) );
32
33   myParametersMap = curModel->getViewAspects();
34
35   myKeyFlag = 0;
36
37   int aQuantityOfItems = myParametersMap.count();
38         
39   setFixedSize( 400, 300 );
40
41   QGridLayout* aGrid = new QGridLayout( this, 2, 1, 5, 10 ); 
42
43   QWidget* aWidget1 = new QWidget( this );
44   QWidget* aWidget2 = new QWidget( this );
45         
46   QHBoxLayout* aLayout = new QHBoxLayout( aWidget1 );
47         
48   myListBox = new QtxListBox( aWidget1 );
49   myListBox->installEventFilter( this );
50
51   myCurViewPort = new OCCViewer_ViewPort3d( aWidget1, curModel->getViewer3d(), V3d_ORTHOGRAPHIC );
52   myCurViewPort->getView()->SetBackgroundColor( Quantity_NOC_BLACK );
53
54   myListBox->setEditEnabled( 1 );
55         
56   if ( aQuantityOfItems )
57   {
58     myListBox->clear();
59     for( int i = 0; i < aQuantityOfItems; i++ )
60       myListBox->insertItem( myParametersMap[ i ].name );
61     
62     changeImage( myListBox->item( 0 ) );
63   }
64   else
65   {
66     myListBox->clear();
67     myListBox->insertItem( "No Items", 0 );
68     myListBox->setEditEnabled( 0 );
69   }
70
71   connect( myListBox, SIGNAL( clicked( QListBoxItem* ) ), this, SLOT( changeImage( QListBoxItem* ) ) );
72   connect( myListBox, SIGNAL( itemEdited( QListBoxItem* ) ), this, SLOT( editItemText( QListBoxItem* ) ) );
73         
74   aLayout->addWidget( myListBox );
75   aLayout->addWidget( myCurViewPort, 30 );
76
77   QHBoxLayout* aButtonLayout = new QHBoxLayout( aWidget2, 0, 5 );
78
79   QPushButton* theOk     = new QPushButton( tr( "Ok" ), aWidget2 );            theOk->setAutoDefault( false );
80   QPushButton* theCancel = new QPushButton( tr( "Cancel" ), aWidget2 );          theCancel->setAutoDefault( false );
81   QPushButton* theDelete = new QPushButton( tr( "Delete" ), aWidget2 );          theDelete->setAutoDefault( false );
82   QPushButton* theClearAll = new QPushButton( tr( "Clear List" ), aWidget2 );  theClearAll->setAutoDefault( false );
83
84   aButtonLayout->addWidget( theOk );
85   aButtonLayout->addWidget( theCancel );
86   aButtonLayout->addWidget( theDelete );
87   aButtonLayout->addWidget( theClearAll );
88
89   aGrid->addWidget( aWidget1, 0, 0 );
90   aGrid->addWidget( aWidget2, 1, 0 );
91         
92   connect( theOk, SIGNAL( clicked() ), this, SLOT( OKpressed() ) );
93   connect( theCancel, SIGNAL( clicked() ), this, SLOT( reject() ) );
94   connect( theDelete, SIGNAL( clicked() ), this, SLOT( deleteSelectedItems() ) );
95   connect( theClearAll, SIGNAL( clicked() ), this, SLOT( clearList() ) );
96 }
97
98 OCCViewer_CreateRestoreViewDlg::~OCCViewer_CreateRestoreViewDlg()
99 {
100 }
101
102 void OCCViewer_CreateRestoreViewDlg::changeImage( QListBoxItem* curItem )
103 {
104         if( curItem && myListBox->isEditEnabled() )
105         {
106                 int lowLevel  = -1;
107                 int highLevel = -1;
108                 int index = curItem->listBox()->index( curItem );
109                 if( myKeyFlag == 2 )
110                 {
111                         for( int i = 0; i < myListBox->count(); i++ )
112                         {
113                                 if( myListBox->isSelected( i ) && i != index )
114                                 {
115                                         myListBox->clearSelection();
116                                         if( i > index )
117                                         {
118                                                 lowLevel  = index;
119                                                 highLevel = i;
120                                         }
121                                         else
122                                         {
123                                                 lowLevel  = i;
124                                                 highLevel = index;
125                                         }
126                                         for( int j = lowLevel; j <= highLevel; j++ )
127                                                 myListBox->setSelected( j, TRUE );
128                                         break;
129                                 }
130                                 if( myListBox->isSelected( i ) && i == index )
131                                         myListBox->setSelected( i, TRUE );
132                         }
133                 }
134
135                 Handle(V3d_View) aView3d = myCurViewPort->getView();
136                 myCurrentItem = myParametersMap[ index ];
137
138                 Standard_Boolean prev = aView3d->SetImmediateUpdate( Standard_False );
139                 aView3d->SetScale( myCurrentItem.scale );
140                 aView3d->SetCenter( myCurrentItem.centerX, myCurrentItem.centerY );
141                 aView3d->SetProj( myCurrentItem.projX, myCurrentItem.projY, myCurrentItem.projZ );
142                 aView3d->SetTwist( myCurrentItem.twist );
143                 aView3d->SetAt( myCurrentItem.atX, myCurrentItem.atY, myCurrentItem.atZ );
144                 aView3d->SetImmediateUpdate( prev );
145                 aView3d->SetEye( myCurrentItem.eyeX, myCurrentItem.eyeY, myCurrentItem.eyeZ );
146          }
147 }
148
149 viewAspect OCCViewer_CreateRestoreViewDlg::currentItem() const
150 {
151         return myCurrentItem;
152 }
153
154 void OCCViewer_CreateRestoreViewDlg::deleteSelectedItems()
155 {
156         if( myListBox->count() && myListBox->isEditEnabled() )
157         {
158                 int curIndex = -1;
159                 for( int i = 0; i < myListBox->count(); i++ )
160                         if( myListBox->isSelected( i ) )
161                         {
162                                 myListBox->removeItem( i );
163                                 for( int j = i; j < myParametersMap.count(); j++ )
164                                         if( j != myParametersMap.count() - 1 )
165                                                 myParametersMap[ j ] = myParametersMap[ j + 1 ];
166                                         else
167                                                 myParametersMap.remove( myParametersMap.at(j) );
168                                 if( i != myListBox->count() )
169                                         curIndex = i;
170                                 else
171                                         curIndex = i - 1;
172                                 i--;
173                         }
174                 if( curIndex >= 0 )
175                 {
176                         myListBox->setCurrentItem( curIndex );
177                         changeImage( myListBox->item( curIndex ) );
178                 }
179         }
180         if( !myListBox->count() )
181         {
182                 myListBox->clear();
183                 myListBox->insertItem( "No Items", 0 );
184                 myListBox->setEditEnabled( 0 );
185         }
186 }
187
188 void OCCViewer_CreateRestoreViewDlg::clearList()
189 {
190         myListBox->clear();
191         myListBox->insertItem( "No Items", 0 );
192         myListBox->setEditEnabled( 0 );
193
194         myParametersMap.clear();
195 }
196
197 const viewAspectList& OCCViewer_CreateRestoreViewDlg::parameters() const
198 {
199         return myParametersMap;
200 }
201
202 void OCCViewer_CreateRestoreViewDlg::editItemText( QListBoxItem* anItem )
203 {
204         int index = anItem->listBox()->index( anItem );
205         myParametersMap[ index ].name = anItem->text().latin1();
206 }
207
208 bool OCCViewer_CreateRestoreViewDlg::eventFilter( QObject* anObj, QEvent* anEv )
209 {
210         if( anEv->type() == QEvent::KeyPress )
211         {
212                 QKeyEvent* aKeyEv = ( QKeyEvent* )anEv;
213                 if( aKeyEv->key() == Qt::Key_Control )
214                 {
215                         myKeyFlag = 1;
216                         myListBox->setSelectionMode( QListBox::Multi ); 
217                 }
218                 else if( aKeyEv->key() == Qt::Key_Shift )
219                 {
220                         myKeyFlag = 2;
221                         myListBox->setSelectionMode( QListBox::Multi ); 
222                 }
223                 else
224                         myListBox->setSelectionMode( QListBox::Single );
225         }
226         if( anEv->type() == QEvent::KeyRelease )
227                 myKeyFlag = 0;
228         
229         if( !myKeyFlag )
230         {
231                 if( anEv->type() == QEvent::KeyPress || anEv->type() == QEvent::MouseButtonPress )
232                         myListBox->setSelectionMode( QListBox::Single );
233         }
234         return QWidget::eventFilter( anObj, anEv );
235 }
236
237 void OCCViewer_CreateRestoreViewDlg::OKpressed()
238 {
239         emit dlgOk();
240         accept();
241 }
242