Salome HOME
Merge branch 'BR_1330' into BR_DEMO
[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 ) )         \
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   myOverview->setGeometry( 100, 100, 200, 200 );
100
101   isPassed = true;
102 }
103
104 void test_Overview::showShape()
105 {
106   static bool isPassed = false;
107   if( isPassed )
108     return;
109
110   // Show loaded shape in the viewer
111   BRep_Builder B;
112   TopoDS_Shape shape;
113   std::string fname = (REF_DATA_PATH + "/test_zone.brep").toStdString();
114   BRepTools::Read( shape, fname.c_str(), B );
115   TestViewer::show( shape, AIS_Shaded, true, 0x03399FF );
116   
117   qApp->processEvents();
118   myOverview->setTopView(); //TODO: automatic fit all on show???*/
119
120   isPassed = true;
121 }
122
123 void fitAllWithRestore( OCCViewer_ViewPort3d* vp )
124 {
125   double s = vp->getView()->Scale();
126   double x0, y0, z0; 
127   vp->getView()->Convert( 0, 0, x0, y0, z0 );
128   TestViewer::viewWindow()->onFitAll();
129   vp->getView()->SetScale( s );
130   int xp, yp;
131   vp->getView()->Convert( x0, y0, z0, xp, yp );
132   vp->pan( -xp, -yp );
133 }
134
135
136
137
138 void test_Overview::test_default()
139 {
140   create();
141   CPPUNIT_ASSERT_OVERVIEW( "overview_empty" );
142 }
143
144
145 void test_Overview::test_presentation()
146 {
147   create();
148   showShape();
149   
150   //QTest::qWait( 20000 );
151   CPPUNIT_ASSERT_OVERVIEW( "overview_prs" );
152 }
153
154 void test_Overview::test_actions_in_main()
155 {
156   create();
157   showShape();
158
159   OCCViewer_ViewWindow* aMain = TestViewer::viewWindow()->getView( OCCViewer_ViewFrame::MAIN_VIEW );
160   OCCViewer_ViewPort3d* vp = aMain->getViewPort();
161
162   // 1. selection in main view
163   QTest::mouseMove( TestViewer::viewWindow() );
164   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
165
166   // 2. mouse wheel zoom
167   QWheelEvent we( QPoint( 243, 416 ), 120*20, Qt::NoButton, Qt::NoModifier );
168   qApp->sendEvent( vp, &we );
169   qApp->processEvents();
170   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_1" );
171
172   // 3. zoom via mouse
173   const int d = 100;
174   vp->zoom( 243, 416, 243+d, 416+d ); 
175   CPPUNIT_ASSERT_OVERVIEW( "overview_zoomed_2" );
176
177   // 4. panning via mouse
178   vp->pan( 300, -250 ); 
179   CPPUNIT_ASSERT_OVERVIEW( "overview_panned_1" );
180
181   // 5. reverse zoom and rotation via mouse
182   vp->getView()->AutoZFit();
183   vp->getView()->ZFitAll();
184   vp->getView()->Camera()->SetZRange( -10, 10 );
185   vp->zoom( 243+d, 416+d, 243, 416 ); 
186   vp->startRotation( 400, 400, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
187   vp->rotate( 200, 300, OCCViewer_ViewWindow::BBCENTER, gp_Pnt() );
188
189   fitAllWithRestore( vp );
190   // it is necessary to apply fit all to fit correct zmin/zmax 
191   // and after that we restore the previous aspect
192   CPPUNIT_ASSERT_OVERVIEW( "overview_rotated_1" );
193
194   //QTest::qWait( 50000 );
195 }
196
197 void test_Overview::test_set_mainview_2_times()
198 {
199   create();
200   showShape();
201   TestViewer::viewWindow()->onTopView();
202   QTest::mouseMove( TestViewer::viewWindow() );
203   
204   //QTest::qWait( 20000 );
205   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
206
207   myOverview->setMainView( TestViewer::viewWindow() );
208   qApp->processEvents();
209
210   CPPUNIT_ASSERT_OVERVIEW( "overview_selection" );
211 }
212
213 void test_Overview::test_actions_in_overview()
214 {
215   //TODO
216 }