Salome HOME
Merge branch 'BR_1330' into BR_DEMO
[modules/hydro.git] / src / HYDRO_tests / test_GraphicsView.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_GraphicsView.h>
20 #include <GraphicsView_ViewManager.h>
21 #include <GraphicsView_Viewer.h>
22 #include <GraphicsView_ViewFrame.h>
23 #include <GraphicsView_ViewPort.h>
24 #include <GraphicsView_Object.h>
25 #include <GraphicsView_Scene.h>
26 #include <QTest>
27 #include <QGraphicsPixmapItem>
28 #include <QGraphicsSceneWheelEvent>
29 #include <TestViewer.h>
30
31 extern QString REF_DATA_PATH;
32
33 class TestObject : public GraphicsView_Object
34 {
35 public:
36   TestObject();
37   virtual ~TestObject();
38
39   virtual void compute();
40   virtual QRectF getRect() const;
41   virtual QRectF boundingRect() const;
42
43 private:
44   QGraphicsPixmapItem* myItem;
45 };
46
47
48 TestObject::TestObject()
49   : myItem( 0 )
50 {
51   compute();
52 }
53
54 TestObject::~TestObject()
55 {
56 }
57
58 void TestObject::compute()
59 {
60   QPixmap image( REF_DATA_PATH + "/garonne.png" );
61   myItem = new QGraphicsPixmapItem( image );
62   addToGroup( myItem );
63 }
64
65 QRectF TestObject::getRect() const
66 {
67   return QRectF( 0, 0, 400, 300 );
68 }
69
70 QRectF TestObject::boundingRect() const
71 {
72   if( myItem )
73     return myItem->boundingRect();
74   else
75     return QRectF();
76 }
77
78
79
80 #define CPPUNIT_ASSERT_VIEW( theCase )                              \
81   {                                                                 \
82     qApp->processEvents();                                          \
83     QImage im = aViewPort->dumpView();                              \
84     const bool SWAP_RGB_ORDER = true;                               \
85     if( SWAP_RGB_ORDER )                                            \
86     {                                                               \
87       /* A temporary patch for bug in SALOME/OCC dump;              \
88          the result image contains swapped RGB colors  */           \
89       im = im.rgbSwapped();                                         \
90     }                                                               \
91     CPPUNIT_ASSERT_IMAGES2( &im, theCase );                         \
92   }                                                                 \
93
94 void test_GraphicsView::test_wheel_zoom()
95 {
96   GraphicsView_ViewManager* aViewManager = new GraphicsView_ViewManager( 0, 0 );
97   GraphicsView_Viewer* aViewer = new GraphicsView_Viewer( "test", 0 );
98
99   aViewManager->setViewModel( aViewer );
100   GraphicsView_ViewFrame* aWindow = dynamic_cast<GraphicsView_ViewFrame*>( aViewManager->createViewWindow() );
101   aWindow->setGeometry( 100, 100, 600, 450 );
102   aWindow->show();
103
104   qApp->processEvents();
105
106   // 1. Initialize presentation
107   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
108   aViewPort->setInteractionFlag( GraphicsView_ViewPort::GlobalWheelScaling );
109   // This is necessary to activate wheeling zoom
110
111   TestObject* obj = new TestObject();
112   aViewPort->addItem( obj );
113   aViewPort->fitAll();
114   CPPUNIT_ASSERT_VIEW( "gv_fitall" );
115
116   // 2. Mouse wheel zoom
117   QWheelEvent we1( QPoint( 10, 10 ), 120*10, Qt::NoButton, Qt::NoModifier );
118   qApp->sendEvent( aViewPort->viewport(), &we1 );
119   CPPUNIT_ASSERT_VIEW( "gv_zoomed_1" );
120   
121   //QTest::qWait( 50000 );
122
123   QWheelEvent we2( QPoint( 650, 500 ), 120*10, Qt::NoButton, Qt::NoModifier );
124   qApp->sendEvent( aViewPort->viewport(), &we2 );
125   CPPUNIT_ASSERT_VIEW( "gv_zoomed_2" );
126
127   //QTest::qWait( 50000 );
128 }