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