Salome HOME
Merge branch 'BR_2017' of ssh://git.salome-platform.org/modules/hydro into BR_2017
[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_ViewPort3d.h>
31 #include <V3d_View.hxx>
32
33 extern QString REF_DATA_PATH;
34 HYDROGUI_Overview* test_Overview::myOverview = 0;
35
36 #define CPPUNIT_ASSERT_OVERVIEW( theCase )                               \
37   {                                                                      \
38     QString aMessage;                                                    \
39     QImage aDump = dumpViews();                                          \
40     if( !TestViewer::AssertImages( aMessage, &aDump, theCase, false ) )  \
41     {                                                                    \
42       TestViewer::showColorScale( false );                               \
43       std::string aMessageStl = aMessage.toStdString();                  \
44       CPPUNIT_FAIL( aMessageStl.c_str() );                               \
45     }                                                                    \
46   } 
47
48 const int m = 5; //margins
49 const QColor aColor = Qt::blue;
50 const QBrush SOLID( aColor );
51 const int WIDTH = 2;
52 const QPen PSOLID( SOLID, WIDTH );
53 const QColor BACK = Qt::white;
54
55 QImage test_Overview::dumpViews()
56 {
57   QImage aMain = TestViewer::viewWindow()->getView(OCCViewer_ViewFrame::MAIN_VIEW)->dumpView();
58   QImage anOverview = myOverview->dump();
59
60   aMain = aMain.rgbSwapped();              //PATCH for image came from OCCT dump
61   //anOverview = anOverview.rgbSwapped();  //overview dump already normalizes the image, the line is not necessary!!!
62
63   int w1 = aMain.width();
64   int w2 = anOverview.width();
65   int h1 = aMain.height();
66   int h2 = anOverview.height();
67   int w = w1 + w2 + 2*WIDTH;
68   int h = qMax( h1, h2 ) + 2*WIDTH;
69
70   QPixmap pix( w, h );
71   pix.fill( BACK );
72
73   QPainter painter( &pix );
74   painter.setPen( PSOLID );
75
76   painter.drawRect( WIDTH, WIDTH, w2, h2 );
77   painter.drawRect( w2+WIDTH, WIDTH, w1, h1 );
78
79   painter.drawImage( WIDTH, WIDTH, anOverview );
80   painter.drawImage( w2+WIDTH, WIDTH, aMain );
81
82   return pix.toImage();
83 }
84
85 void test_Overview::create()
86 {
87   static bool isPassed = false;
88   if( isPassed )
89     return;
90
91   // default mouse position
92   QTest::mouseMove( TestViewer::viewWindow(), QPoint( 0, 0 ) );
93
94   // Initialization of the empty viewer
95   myOverview = new HYDROGUI_Overview( "Test overview" );
96   myOverview->show();
97   myOverview->setMainView( TestViewer::viewWindow() );
98   TestViewer::viewWindow()->setGeometry( 400, 100, 800, 800 );
99   TestViewer::viewWindow()->onTopView();
100   myOverview->setGeometry( 100, 100, 200, 200 );
101
102   isPassed = true;
103 }
104
105 void test_Overview::showShape()
106 {
107   static bool isPassed = false;
108   if( isPassed )
109     return;
110
111   // Show loaded shape in the viewer
112   BRep_Builder B;
113   TopoDS_Shape shape;
114   std::string fname = (REF_DATA_PATH + "/test_zone.brep").toStdString();
115   //std::cout << "Loading shape: " << fname << std::endl;
116   BRepTools::Read( shape, fname.c_str(), B );
117   TestViewer::show( shape, AIS_Shaded, true, 0x03399FF );
118   
119   qApp->processEvents();
120   myOverview->setTopView(); //TODO: automatic fit all on show???*/
121
122   isPassed = true;
123 }
124
125 void fitAllWithRestore( OCCViewer_ViewPort3d* vp )
126 {
127   double s = vp->getView()->Scale();
128   double x0, y0, z0; 
129   vp->getView()->Convert( 0, 0, x0, y0, z0 );
130   TestViewer::viewWindow()->onFitAll();
131   vp->getView()->SetScale( s );
132   int xp, yp;
133   vp->getView()->Convert( x0, y0, z0, xp, yp );
134   vp->pan( -xp, -yp );
135 }
136
137
138
139
140 void test_Overview::test_default()
141 {
142   create();
143   //QTest::qWait( 50000 );
144   CPPUNIT_ASSERT_OVERVIEW( "overview_empty" );
145 }
146
147
148 void test_Overview::test_presentation()
149 {
150   create();
151   showShape();
152   
153   //QTest::qWait( 20000 );
154   CPPUNIT_ASSERT_OVERVIEW( "overview_prs" );
155 }
156
157 void test_Overview::test_actions_in_main()
158 {
159   create();
160   showShape();
161   TestViewer::viewWindow()->onTopView();
162
163   // 1. selection in main view
164   QTest::mouseMove( TestViewer::viewWindow() );
165   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
166
167   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
168   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
169
170   // 2. mouse wheel zoom
171   QWheelEvent we( QPoint( 243, 416 ), 120*20, Qt::NoButton, Qt::NoModifier );
172   qApp->sendEvent( vp, &we );
173   qApp->processEvents();
174   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
175
176   // 3. zoom via mouse
177   const int d = 100;
178   vp->zoom( 243, 416, 243+d, 416+d ); 
179   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_2" );
180
181   // 4. panning via mouse
182   vp->pan( 300, -250 ); 
183   CPPUNIT_ASSERT_OVERVIEW( "overview_panned_1" );
184
185   // 5. reverse zoom and rotation via mouse
186   vp->getView()->AutoZFit();
187   vp->getView()->ZFitAll();
188   vp->getView()->Camera()->SetZRange( -10, 10 );
189   vp->zoom( 243+d, 416+d, 243, 416 ); 
190   vp->startRotation( 400, 400, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
191   vp->rotate( 200, 300, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
192
193   fitAllWithRestore( vp );
194   // it is necessary to apply fit all to fit correct zmin/zmax 
195   // and after that we restore the previous aspect
196   CPPUNIT_ASSERT_OVERVIEW( "overview_rotated_1" );
197
198   //QTest::qWait( 50000 );
199 }
200
201 void test_Overview::test_set_mainview_2_times()
202 {
203   create();
204   showShape();
205   TestViewer::viewWindow()->onTopView();
206   QTest::mouseMove( TestViewer::viewWindow() );
207   
208   //QTest::qWait( 20000 );
209   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
210
211   myOverview->setMainView( TestViewer::viewWindow() );
212   qApp->processEvents();
213
214   CPPUNIT_ASSERT_OVERVIEW( "overview_selection_a" );
215 }
216
217 void test_Overview::test_actions_in_overview()
218 {
219   create();
220   showShape();
221   TestViewer::viewWindow()->onTopView();
222   QTest::mouseMove( TestViewer::viewWindow() );
223
224   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
225   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
226
227   QWheelEvent we( QPoint( 243, 416 ), 120*20, Qt::NoButton, Qt::NoModifier );
228   qApp->sendEvent( vp, &we );
229   qApp->processEvents();
230   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
231
232   QTest::mouseMove( myOverview->getViewPort(false), QPoint( 150, 50 ) );
233   QTest::mouseClick( myOverview->getViewPort(false), Qt::LeftButton, Qt::KeyboardModifiers(), QPoint( 150, 50 ) );
234   qApp->processEvents();
235
236   CPPUNIT_ASSERT_OVERVIEW( "overview_drag" );
237
238   //QTest::qWait( 50000 );
239 }