Salome HOME
Corrections of examples path after install with scbi
[modules/hydro.git] / src / HYDRO_tests / test_Overview.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include <test_Overview.h>
20 #include <BRep_Builder.hxx>
21 #include <BRepTools.hxx>
22 #include <AIS_Shape.hxx>
23 #include <TestViewer.h>
24 #include <QString>
25 #include <QTest>
26 #include <QPainter>
27
28 #include <HYDROGUI_Overview.h>
29 #include <OCCViewer_ViewFrame.h>
30 #include <OCCViewer_ViewModel.h>
31 #include <OCCViewer_ViewPort3d.h>
32 #include <V3d_View.hxx>
33
34 extern QString REF_DATA_PATH;
35 HYDROGUI_Overview* test_Overview::myOverview = 0;
36
37 #define CPPUNIT_ASSERT_OVERVIEW( theCase )                               \
38   {                                                                      \
39     QString aMessage;                                                    \
40     QImage aDump = dumpViews();                                          \
41     if( !TestViewer::AssertImages( aMessage, &aDump, theCase, false ) )  \
42     {                                                                    \
43       TestViewer::showColorScale( false );                               \
44       std::string aMessageStl = aMessage.toStdString();                  \
45       CPPUNIT_FAIL( aMessageStl.c_str() );                               \
46     }                                                                    \
47   } 
48
49 const int m = 5; //margins
50 const QColor aColor = Qt::blue;
51 const QBrush SOLID( aColor );
52 const int WIDTH = 2;
53 const QPen PSOLID( SOLID, WIDTH );
54 const QColor BACK = Qt::white;
55
56 QImage test_Overview::dumpViews()
57 {
58   QImage aMain = TestViewer::viewWindow()->getView(OCCViewer_ViewFrame::MAIN_VIEW)->dumpView();
59   QImage anOverview = myOverview->dump();
60
61   if (SWAP_RGB)
62     aMain = aMain.rgbSwapped();              //PATCH for image came from OCCT dump
63   //anOverview = anOverview.rgbSwapped();  //overview dump already normalizes the image, the line is not necessary!!!
64
65   int w1 = aMain.width();
66   int w2 = anOverview.width();
67   int h1 = aMain.height();
68   int h2 = anOverview.height();
69   int w = w1 + w2 + 2*WIDTH;
70   int h = qMax( h1, h2 ) + 2*WIDTH;
71
72   QPixmap pix( w, h );
73   pix.fill( BACK );
74
75   QPainter painter( &pix );
76   painter.setPen( PSOLID );
77
78   //static int q = 0;
79   //q++;
80   //anOverview.save( QString( "/tmp/hydro/overview_" ) + QString::number( q ) + ".png" );
81   //aMain.save( QString( "/tmp/hydro/main_" ) + QString::number( q ) + ".png" );
82
83   painter.drawImage( WIDTH, WIDTH, anOverview );
84   painter.drawImage( w2+WIDTH, WIDTH, aMain );
85
86   painter.drawRect( WIDTH, WIDTH, w2, h2 );
87   painter.drawRect( w2+WIDTH, WIDTH, w1, h1 );
88
89   QImage res = pix.toImage();
90   //res.save( QString( "/tmp/hydro/result_" ) + QString::number( q ) + ".png" );
91   
92   return res;
93 }
94
95 extern int MAIN_W, MAIN_H;
96
97 void test_Overview::create()
98 {
99   TestViewer::eraseAll( true, true );
100   
101   // Initialization of the empty viewer
102
103   static bool isCreated = false;
104   if( !isCreated )
105   {
106     myOverview = new HYDROGUI_Overview( "Test overview" );
107     myOverview->show();
108
109     // default mouse position
110     QTest::mouseMove( TestViewer::viewWindow(), QPoint( 0, 0 ) );
111
112     isCreated = true;
113   }
114   myOverview->setMainView( TestViewer::viewWindow() );
115   //TestViewer::viewWindow()->setGeometry( 400, 100, MAIN_W, MAIN_H );
116   TestViewer::viewWindow()->onTopView();
117   myOverview->setGeometry( 100, 100, 200, 200 );
118 }
119
120 void test_Overview::showShape()
121 {
122   // Show loaded shape in the viewer
123   BRep_Builder B;
124   TopoDS_Shape shape;
125   std::string fname = (REF_DATA_PATH + "/test_zone.brep").toStdString();
126   //std::cout << "Loading shape: " << fname << std::endl;
127   BRepTools::Read( shape, fname.c_str(), B );
128   TestViewer::show( shape, AIS_Shaded, true, 0x03399FF );
129   
130   qApp->processEvents();
131   myOverview->setTopView(); //TODO: automatic fit all on show???*/
132 }
133
134 void fitAllWithRestore( OCCViewer_ViewPort3d* vp )
135 {
136   double s = vp->getView()->Scale();
137   double x0, y0, z0; 
138   vp->getView()->Convert( 0, 0, x0, y0, z0 );
139   TestViewer::viewWindow()->onFitAll();
140   vp->getView()->SetScale( s );
141   int xp, yp;
142   vp->getView()->Convert( x0, y0, z0, xp, yp );
143   vp->pan( -xp, -yp );
144 }
145
146
147
148
149 void test_Overview::test_default()
150 {
151   create();
152   //QTest::qWait( 50000 );
153   CPPUNIT_ASSERT_OVERVIEW( "overview_empty" );
154 }
155
156
157 void test_Overview::test_presentation()
158 {
159   create();
160   showShape();
161   
162   //QTest::qWait( 20000 );
163   CPPUNIT_ASSERT_OVERVIEW( "overview_prs" );
164 }
165
166 void test_Overview::test_actions_in_main()
167 {
168   create();
169   showShape();
170   TestViewer::viewWindow()->onTopView();
171   qApp->processEvents();
172
173   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
174   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
175
176   // 1. selection in main view
177   QTest::mouseMove( vp, QPoint(150,150) );
178   TestViewer::context()->MoveTo(150, 150, vp->getView(), Standard_True );
179   TestViewer::context()->Select( Standard_True );
180   qApp->processEvents();
181   //QTest::qWait( 50000 );
182   //aMain->dumpView().save("/tmp/hydro/dump.png");
183   //aMain->dumpView().rgbSwapped().save("/tmp/hydro/dump2.png");
184   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
185
186   // 2. mouse wheel zoom
187   QWheelEvent we( QPoint( 243, 316 ), 120*20, Qt::NoButton, Qt::NoModifier );
188   qApp->sendEvent( vp, &we );
189   qApp->processEvents();
190   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
191
192   // 3. zoom via mouse
193   const int d = 100;
194   vp->zoom( 243, 316, 243+d, 316+d ); 
195   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_2" );
196
197   // 4. panning via mouse
198   vp->pan( 300, -250 ); 
199   CPPUNIT_ASSERT_OVERVIEW( "overview_panned_1" );
200
201   // 5. reverse zoom and rotation via mouse
202   vp->getView()->AutoZFit();
203   vp->getView()->ZFitAll();
204   vp->getView()->Camera()->SetZRange( -10, 10 );
205   vp->zoom( 243+d, 416+d, 243, 416 ); 
206   vp->startRotation( 400, 400, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
207   vp->rotate( 200, 300, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
208
209   fitAllWithRestore( vp );
210   // it is necessary to apply fit all to fit correct zmin/zmax 
211   // and after that we restore the previous aspect
212   CPPUNIT_ASSERT_OVERVIEW( "overview_rotated_1" );
213
214   //QTest::qWait( 50000 );
215 }
216
217 void test_Overview::test_set_mainview_2_times()
218 {
219   create();
220   showShape();
221   TestViewer::viewWindow()->onTopView();
222
223   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
224   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
225
226   QTest::mouseMove( vp, QPoint(150,150) );
227   TestViewer::context()->MoveTo(150, 150, vp->getView(), Standard_True );
228   TestViewer::context()->Select( Standard_True );
229   qApp->processEvents();
230   
231   //QTest::qWait( 20000 );
232   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
233
234   myOverview->setMainView( TestViewer::viewWindow() );
235   qApp->processEvents();
236
237   CPPUNIT_ASSERT_OVERVIEW( "overview_selection_a" );
238 }
239
240 void test_Overview::test_actions_in_overview()
241 {
242   create();
243   showShape();
244   TestViewer::viewWindow()->onTopView();
245
246   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
247   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
248
249   QTest::mouseMove( vp, QPoint(150,150) );
250   TestViewer::context()->MoveTo(150, 150, vp->getView(), Standard_True );
251   TestViewer::context()->Select( Standard_True );
252   qApp->processEvents();
253
254   QWheelEvent we( QPoint( 243, 316 ), 120*20, Qt::NoButton, Qt::NoModifier );
255   qApp->sendEvent( vp, &we );
256   qApp->processEvents();
257   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
258
259   QTest::mouseMove( myOverview->getViewPort(false), QPoint( 150, 50 ) );
260   QTest::mouseClick( myOverview->getViewPort(false), Qt::LeftButton, Qt::KeyboardModifiers(), QPoint( 150, 50 ) );
261   qApp->processEvents();
262
263   CPPUNIT_ASSERT_OVERVIEW( "overview_drag" );
264
265   //QTest::qWait( 50000 );
266 }