Salome HOME
74d98977d0a639c028149702585f5986c7a90826
[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     CPPUNIT_ASSERT_IMAGES2( &im, theCase );     \
85   }
86
87 void test_GraphicsView::test_wheel_zoom()
88 {
89   GraphicsView_ViewManager* aViewManager = new GraphicsView_ViewManager( 0, 0 );
90   GraphicsView_Viewer* aViewer = new GraphicsView_Viewer( "test", 0 );
91
92   aViewManager->setViewModel( aViewer );
93   GraphicsView_ViewFrame* aWindow = dynamic_cast<GraphicsView_ViewFrame*>( aViewManager->createViewWindow() );
94   aWindow->setGeometry( 100, 100, 600, 450 );
95   aWindow->show();
96
97   qApp->processEvents();
98
99   // 1. Initialize presentation
100   GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort();
101   aViewPort->setInteractionFlag( GraphicsView_ViewPort::GlobalWheelScaling );
102   // This is necessary to activate wheeling zoom
103
104   TestObject* obj = new TestObject();
105   aViewPort->addItem( obj );
106   aViewPort->fitAll();
107   CPPUNIT_ASSERT_VIEW( "gv_fitall" );
108
109   // 2. Mouse wheel zoom
110   QWheelEvent we1( QPoint( 10, 10 ), 120*10, Qt::NoButton, Qt::NoModifier );
111   qApp->sendEvent( aViewPort->viewport(), &we1 );
112   CPPUNIT_ASSERT_VIEW( "gv_zoomed_1" );
113   
114   //QTest::qWait( 50000 );
115
116   QWheelEvent we2( QPoint( 650, 500 ), 120*10, Qt::NoButton, Qt::NoModifier );
117   qApp->sendEvent( aViewPort->viewport(), &we2 );
118   CPPUNIT_ASSERT_VIEW( "gv_zoomed_2" );
119
120   //QTest::qWait( 50000 );
121 }