Salome HOME
Merge remote-tracking branch 'remotes/origin/BR_2017' into HEAD
[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   BRepTools::Read( shape, fname.c_str(), B );
116   TestViewer::show( shape, AIS_Shaded, true, 0x03399FF );
117   
118   qApp->processEvents();
119   myOverview->setTopView(); //TODO: automatic fit all on show???*/
120
121   isPassed = true;
122 }
123
124 void fitAllWithRestore( OCCViewer_ViewPort3d* vp )
125 {
126   double s = vp->getView()->Scale();
127   double x0, y0, z0; 
128   vp->getView()->Convert( 0, 0, x0, y0, z0 );
129   TestViewer::viewWindow()->onFitAll();
130   vp->getView()->SetScale( s );
131   int xp, yp;
132   vp->getView()->Convert( x0, y0, z0, xp, yp );
133   vp->pan( -xp, -yp );
134 }
135
136
137
138
139 void test_Overview::test_default()
140 {
141   create();
142   //QTest::qWait( 50000 );
143   CPPUNIT_ASSERT_OVERVIEW( "overview_empty" );
144 }
145
146
147 void test_Overview::test_presentation()
148 {
149   create();
150   showShape();
151   
152   //QTest::qWait( 20000 );
153   CPPUNIT_ASSERT_OVERVIEW( "overview_prs" );
154 }
155
156 void test_Overview::test_actions_in_main()
157 {
158   create();
159   showShape();
160   TestViewer::viewWindow()->onTopView();
161
162   // 1. selection in main view
163   QTest::mouseMove( TestViewer::viewWindow() );
164   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
165
166   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
167   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
168
169   // 2. mouse wheel zoom
170   QWheelEvent we( QPoint( 243, 416 ), 120*20, Qt::NoButton, Qt::NoModifier );
171   qApp->sendEvent( vp, &we );
172   qApp->processEvents();
173   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
174
175   // 3. zoom via mouse
176   const int d = 100;
177   vp->zoom( 243, 416, 243+d, 416+d ); 
178   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_2" );
179
180   // 4. panning via mouse
181   vp->pan( 300, -250 ); 
182   CPPUNIT_ASSERT_OVERVIEW( "overview_panned_1" );
183
184   // 5. reverse zoom and rotation via mouse
185   vp->getView()->AutoZFit();
186   vp->getView()->ZFitAll();
187   vp->getView()->Camera()->SetZRange( -10, 10 );
188   vp->zoom( 243+d, 416+d, 243, 416 ); 
189   vp->startRotation( 400, 400, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
190   vp->rotate( 200, 300, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
191
192   fitAllWithRestore( vp );
193   // it is necessary to apply fit all to fit correct zmin/zmax 
194   // and after that we restore the previous aspect
195   CPPUNIT_ASSERT_OVERVIEW( "overview_rotated_1" );
196
197   //QTest::qWait( 50000 );
198 }
199
200 void test_Overview::test_set_mainview_2_times()
201 {
202   create();
203   showShape();
204   TestViewer::viewWindow()->onTopView();
205   QTest::mouseMove( TestViewer::viewWindow() );
206   
207   //QTest::qWait( 20000 );
208   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
209
210   myOverview->setMainView( TestViewer::viewWindow() );
211   qApp->processEvents();
212
213   CPPUNIT_ASSERT_OVERVIEW( "overview_selection_a" );
214 }
215
216 void test_Overview::test_actions_in_overview()
217 {
218   create();
219   showShape();
220   TestViewer::viewWindow()->onTopView();
221   QTest::mouseMove( TestViewer::viewWindow() );
222
223   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
224   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
225
226   QWheelEvent we( QPoint( 243, 416 ), 120*20, Qt::NoButton, Qt::NoModifier );
227   qApp->sendEvent( vp, &we );
228   qApp->processEvents();
229   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
230
231   QTest::mouseMove( myOverview->getViewPort(false), QPoint( 150, 50 ) );
232   QTest::mouseClick( myOverview->getViewPort(false), Qt::LeftButton, Qt::KeyboardModifiers(), QPoint( 150, 50 ) );
233   qApp->processEvents();
234
235   CPPUNIT_ASSERT_OVERVIEW( "overview_drag" );
236
237   //QTest::qWait( 50000 );
238 }