]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDRO_tests/test_Overview.cxx
Salome HOME
4fef7e2185848d10bce4e1edd591bfbcccc582bb
[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   aMain = aMain.rgbSwapped();              //PATCH for image came from OCCT dump
62   //anOverview = anOverview.rgbSwapped();  //overview dump already normalizes the image, the line is not necessary!!!
63
64   int w1 = aMain.width();
65   int w2 = anOverview.width();
66   int h1 = aMain.height();
67   int h2 = anOverview.height();
68   int w = w1 + w2 + 2*WIDTH;
69   int h = qMax( h1, h2 ) + 2*WIDTH;
70
71   QPixmap pix( w, h );
72   pix.fill( BACK );
73
74   QPainter painter( &pix );
75   painter.setPen( PSOLID );
76
77   painter.drawRect( WIDTH, WIDTH, w2, h2 );
78   painter.drawRect( w2+WIDTH, WIDTH, w1, h1 );
79
80   //static int q = 0;
81   //q++;
82   //anOverview.save( QString( "/tmp/hydro/overview_" ) + QString::number( q ) + ".png" );
83   //aMain.save( QString( "/tmp/hydro/main_" ) + QString::number( q ) + ".png" );
84
85   painter.drawImage( WIDTH, WIDTH, anOverview );
86   painter.drawImage( w2+WIDTH, WIDTH, aMain );
87
88   QImage res = pix.toImage();
89   //res.save( QString( "/tmp/hydro/result_" ) + QString::number( q ) + ".png" );
90   
91   return res;
92 }
93
94 void test_Overview::create()
95 {
96   TestViewer::eraseAll( true, true );
97   
98   // Initialization of the empty viewer
99
100   static bool isCreated = false;
101   if( !isCreated )
102   {
103     myOverview = new HYDROGUI_Overview( "Test overview" );
104     myOverview->show();
105
106     // default mouse position
107     QTest::mouseMove( TestViewer::viewWindow(), QPoint( 0, 0 ) );
108
109     isCreated = true;
110   }
111   myOverview->setMainView( TestViewer::viewWindow() );
112   TestViewer::viewWindow()->setGeometry( 400, 100, 800, 800 );
113   TestViewer::viewWindow()->onTopView();
114   myOverview->setGeometry( 100, 100, 200, 200 );
115 }
116
117 void test_Overview::showShape()
118 {
119   // Show loaded shape in the viewer
120   BRep_Builder B;
121   TopoDS_Shape shape;
122   std::string fname = (REF_DATA_PATH + "/test_zone.brep").toStdString();
123   //std::cout << "Loading shape: " << fname << std::endl;
124   BRepTools::Read( shape, fname.c_str(), B );
125   TestViewer::show( shape, AIS_Shaded, true, 0x03399FF );
126   
127   qApp->processEvents();
128   myOverview->setTopView(); //TODO: automatic fit all on show???*/
129 }
130
131 void fitAllWithRestore( OCCViewer_ViewPort3d* vp )
132 {
133   double s = vp->getView()->Scale();
134   double x0, y0, z0; 
135   vp->getView()->Convert( 0, 0, x0, y0, z0 );
136   TestViewer::viewWindow()->onFitAll();
137   vp->getView()->SetScale( s );
138   int xp, yp;
139   vp->getView()->Convert( x0, y0, z0, xp, yp );
140   vp->pan( -xp, -yp );
141 }
142
143
144
145
146 void test_Overview::test_default()
147 {
148   create();
149   //QTest::qWait( 50000 );
150   CPPUNIT_ASSERT_OVERVIEW( "overview_empty" );
151 }
152
153
154 void test_Overview::test_presentation()
155 {
156   create();
157   showShape();
158   
159   //QTest::qWait( 20000 );
160   CPPUNIT_ASSERT_OVERVIEW( "overview_prs" );
161 }
162
163 void test_Overview::test_actions_in_main()
164 {
165   create();
166   showShape();
167   TestViewer::viewWindow()->onTopView();
168   qApp->processEvents();
169
170   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
171   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
172
173   // 1. selection in main view
174   QTest::mouseMove( vp, QPoint(150,150) );
175   TestViewer::context()->MoveTo(150, 150, vp->getView(), Standard_True );
176   TestViewer::context()->Select( Standard_True );
177   qApp->processEvents();
178   //QTest::qWait( 50000 );
179   //aMain->dumpView().save("/tmp/hydro/dump.png");
180   //aMain->dumpView().rgbSwapped().save("/tmp/hydro/dump2.png");
181   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
182
183   // 2. mouse wheel zoom
184   QWheelEvent we( QPoint( 243, 416 ), 120*20, Qt::NoButton, Qt::NoModifier );
185   qApp->sendEvent( vp, &we );
186   qApp->processEvents();
187   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
188
189   // 3. zoom via mouse
190   const int d = 100;
191   vp->zoom( 243, 416, 243+d, 416+d ); 
192   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_2" );
193
194   // 4. panning via mouse
195   vp->pan( 300, -250 ); 
196   CPPUNIT_ASSERT_OVERVIEW( "overview_panned_1" );
197
198   // 5. reverse zoom and rotation via mouse
199   vp->getView()->AutoZFit();
200   vp->getView()->ZFitAll();
201   vp->getView()->Camera()->SetZRange( -10, 10 );
202   vp->zoom( 243+d, 416+d, 243, 416 ); 
203   vp->startRotation( 400, 400, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
204   vp->rotate( 200, 300, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
205
206   fitAllWithRestore( vp );
207   // it is necessary to apply fit all to fit correct zmin/zmax 
208   // and after that we restore the previous aspect
209   CPPUNIT_ASSERT_OVERVIEW( "overview_rotated_1" );
210
211   //QTest::qWait( 50000 );
212 }
213
214 void test_Overview::test_set_mainview_2_times()
215 {
216   create();
217   showShape();
218   TestViewer::viewWindow()->onTopView();
219
220   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
221   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
222
223   QTest::mouseMove( vp, QPoint(150,150) );
224   TestViewer::context()->MoveTo(150, 150, vp->getView(), Standard_True );
225   TestViewer::context()->Select( Standard_True );
226   qApp->processEvents();
227   
228   //QTest::qWait( 20000 );
229   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
230
231   myOverview->setMainView( TestViewer::viewWindow() );
232   qApp->processEvents();
233
234   CPPUNIT_ASSERT_OVERVIEW( "overview_selection_a" );
235 }
236
237 void test_Overview::test_actions_in_overview()
238 {
239   create();
240   showShape();
241   TestViewer::viewWindow()->onTopView();
242
243   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
244   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
245
246   QTest::mouseMove( vp, QPoint(150,150) );
247   TestViewer::context()->MoveTo(150, 150, vp->getView(), Standard_True );
248   TestViewer::context()->Select( Standard_True );
249   qApp->processEvents();
250
251   QWheelEvent we( QPoint( 243, 416 ), 120*20, Qt::NoButton, Qt::NoModifier );
252   qApp->sendEvent( vp, &we );
253   qApp->processEvents();
254   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
255
256   QTest::mouseMove( myOverview->getViewPort(false), QPoint( 150, 50 ) );
257   QTest::mouseClick( myOverview->getViewPort(false), Qt::LeftButton, Qt::KeyboardModifiers(), QPoint( 150, 50 ) );
258   qApp->processEvents();
259
260   CPPUNIT_ASSERT_OVERVIEW( "overview_drag" );
261
262   //QTest::qWait( 50000 );
263 }