Salome HOME
debug of test_export_telemac
[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 extern int MAIN_W, MAIN_H;
95
96 void test_Overview::create()
97 {
98   TestViewer::eraseAll( true, true );
99   
100   // Initialization of the empty viewer
101
102   static bool isCreated = false;
103   if( !isCreated )
104   {
105     myOverview = new HYDROGUI_Overview( "Test overview" );
106     myOverview->show();
107
108     // default mouse position
109     QTest::mouseMove( TestViewer::viewWindow(), QPoint( 0, 0 ) );
110
111     isCreated = true;
112   }
113   myOverview->setMainView( TestViewer::viewWindow() );
114   //TestViewer::viewWindow()->setGeometry( 400, 100, MAIN_W, MAIN_H );
115   TestViewer::viewWindow()->onTopView();
116   myOverview->setGeometry( 100, 100, 200, 200 );
117 }
118
119 void test_Overview::showShape()
120 {
121   // Show loaded shape in the viewer
122   BRep_Builder B;
123   TopoDS_Shape shape;
124   std::string fname = (REF_DATA_PATH + "/test_zone.brep").toStdString();
125   //std::cout << "Loading shape: " << fname << std::endl;
126   BRepTools::Read( shape, fname.c_str(), B );
127   TestViewer::show( shape, AIS_Shaded, true, 0x03399FF );
128   
129   qApp->processEvents();
130   myOverview->setTopView(); //TODO: automatic fit all on show???*/
131 }
132
133 void fitAllWithRestore( OCCViewer_ViewPort3d* vp )
134 {
135   double s = vp->getView()->Scale();
136   double x0, y0, z0; 
137   vp->getView()->Convert( 0, 0, x0, y0, z0 );
138   TestViewer::viewWindow()->onFitAll();
139   vp->getView()->SetScale( s );
140   int xp, yp;
141   vp->getView()->Convert( x0, y0, z0, xp, yp );
142   vp->pan( -xp, -yp );
143 }
144
145
146
147
148 void test_Overview::test_default()
149 {
150   create();
151   //QTest::qWait( 50000 );
152   CPPUNIT_ASSERT_OVERVIEW( "overview_empty" );
153 }
154
155
156 void test_Overview::test_presentation()
157 {
158   create();
159   showShape();
160   
161   //QTest::qWait( 20000 );
162   CPPUNIT_ASSERT_OVERVIEW( "overview_prs" );
163 }
164
165 void test_Overview::test_actions_in_main()
166 {
167   create();
168   showShape();
169   TestViewer::viewWindow()->onTopView();
170   qApp->processEvents();
171
172   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
173   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
174
175   // 1. selection in main view
176   QTest::mouseMove( vp, QPoint(150,150) );
177   TestViewer::context()->MoveTo(150, 150, vp->getView(), Standard_True );
178   TestViewer::context()->Select( Standard_True );
179   qApp->processEvents();
180   //QTest::qWait( 50000 );
181   //aMain->dumpView().save("/tmp/hydro/dump.png");
182   //aMain->dumpView().rgbSwapped().save("/tmp/hydro/dump2.png");
183   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
184
185   // 2. mouse wheel zoom
186   QWheelEvent we( QPoint( 243, 316 ), 120*20, Qt::NoButton, Qt::NoModifier );
187   qApp->sendEvent( vp, &we );
188   qApp->processEvents();
189   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
190
191   // 3. zoom via mouse
192   const int d = 100;
193   vp->zoom( 243, 316, 243+d, 316+d ); 
194   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_2" );
195
196   // 4. panning via mouse
197   vp->pan( 300, -250 ); 
198   CPPUNIT_ASSERT_OVERVIEW( "overview_panned_1" );
199
200   // 5. reverse zoom and rotation via mouse
201   vp->getView()->AutoZFit();
202   vp->getView()->ZFitAll();
203   vp->getView()->Camera()->SetZRange( -10, 10 );
204   vp->zoom( 243+d, 416+d, 243, 416 ); 
205   vp->startRotation( 400, 400, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
206   vp->rotate( 200, 300, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
207
208   fitAllWithRestore( vp );
209   // it is necessary to apply fit all to fit correct zmin/zmax 
210   // and after that we restore the previous aspect
211   CPPUNIT_ASSERT_OVERVIEW( "overview_rotated_1" );
212
213   //QTest::qWait( 50000 );
214 }
215
216 void test_Overview::test_set_mainview_2_times()
217 {
218   create();
219   showShape();
220   TestViewer::viewWindow()->onTopView();
221
222   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
223   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
224
225   QTest::mouseMove( vp, QPoint(150,150) );
226   TestViewer::context()->MoveTo(150, 150, vp->getView(), Standard_True );
227   TestViewer::context()->Select( Standard_True );
228   qApp->processEvents();
229   
230   //QTest::qWait( 20000 );
231   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
232
233   myOverview->setMainView( TestViewer::viewWindow() );
234   qApp->processEvents();
235
236   CPPUNIT_ASSERT_OVERVIEW( "overview_selection_a" );
237 }
238
239 void test_Overview::test_actions_in_overview()
240 {
241   create();
242   showShape();
243   TestViewer::viewWindow()->onTopView();
244
245   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
246   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
247
248   QTest::mouseMove( vp, QPoint(150,150) );
249   TestViewer::context()->MoveTo(150, 150, vp->getView(), Standard_True );
250   TestViewer::context()->Select( Standard_True );
251   qApp->processEvents();
252
253   QWheelEvent we( QPoint( 243, 316 ), 120*20, Qt::NoButton, Qt::NoModifier );
254   qApp->sendEvent( vp, &we );
255   qApp->processEvents();
256   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
257
258   QTest::mouseMove( myOverview->getViewPort(false), QPoint( 150, 50 ) );
259   QTest::mouseClick( myOverview->getViewPort(false), Qt::LeftButton, Qt::KeyboardModifiers(), QPoint( 150, 50 ) );
260   qApp->processEvents();
261
262   CPPUNIT_ASSERT_OVERVIEW( "overview_drag" );
263
264   //QTest::qWait( 50000 );
265 }