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