]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDRO_tests/TestViewer.cxx
Salome HOME
#650: land cover addition test
[modules/hydro.git] / src / HYDRO_tests / TestViewer.cxx
1
2 #include <TestViewer.h>
3 #include <OCCViewer_ViewManager.h>
4 #include <OCCViewer_ViewModel.h>
5 #include <OCCViewer_ViewWindow.h>
6 #include <AIS_InteractiveContext.hxx>
7 #include <AIS_Shape.hxx>
8 #include <TopoDS_Iterator.hxx>
9 #include <QDir>
10
11 OCCViewer_ViewManager* TestViewer::myViewManager = 0;
12 OCCViewer_ViewWindow* TestViewer::myViewWindow = 0;
13
14 OCCViewer_ViewManager* TestViewer::viewManager()
15 {
16   if( myViewManager )
17     return myViewManager;
18
19   myViewManager = new OCCViewer_ViewManager( 0, 0 );
20   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
21
22   aViewer->setTrihedronSize( 100, true );
23   aViewer->setInteractionStyle( 0 );
24   aViewer->setZoomingStyle( 1 );
25
26   myViewManager->setViewModel( aViewer );
27   myViewWindow = dynamic_cast<OCCViewer_ViewWindow*>( myViewManager->createViewWindow() );
28
29   return myViewManager;
30 }
31
32 OCCViewer_Viewer* TestViewer::viewer()
33 {
34   return dynamic_cast<OCCViewer_Viewer*>( viewManager()->getViewModel() );
35 }
36
37 OCCViewer_ViewWindow* TestViewer::viewWindow()
38 {
39   viewManager(); //to create the view if it was not created earlier
40   return myViewWindow;
41 }
42
43 Handle(AIS_InteractiveContext) context()
44 {
45   return TestViewer::viewer()->getAISContext();
46 }
47
48 void TestViewer::show( const TopoDS_Shape& theShape, const QColor& theColor, int theMode )
49 {
50   QColor aColor = theColor;
51   if( !aColor.isValid() )
52   {
53     // random color
54     int aHue = rand()%255;
55     aColor = QColor::fromHsl( aHue, 255, 128 );
56   }
57
58   double r = aColor.red() / 255.0;
59   double g = aColor.green() / 255.0;
60   double b = aColor.blue() / 255.0;
61
62   Handle(AIS_Shape) aShape = new AIS_Shape( theShape );
63   aShape->SetMaterial( Graphic3d_NOM_PLASTIC );
64   aShape->SetColor( Quantity_Color( r, g, b, Quantity_TOC_RGB ) );
65   context()->Display( aShape, theMode, 0, Standard_False );
66 }
67
68 void TestViewer::show( const TopoDS_Shape& theShape, int theMode, bool isFitAll )
69 {
70   context()->EraseAll();
71   
72   if( theShape.ShapeType()==TopAbs_COMPOUND )
73   {
74     TopoDS_Iterator anIt( theShape );
75     for( ; anIt.More(); anIt.Next() )
76       show( anIt.Value(), QColor(), theMode );
77   }
78   else
79     show( theShape, QColor(), theMode );
80
81   viewWindow()->onTopView();
82   viewWindow()->onFitAll();
83 }
84
85 void TestViewer::dump( const TopoDS_Shape& theShape, int theMode, const QString& theName )
86 {
87   show( theShape, theMode, true );
88   QImage anImage = viewWindow()->dumpView();
89
90   QString aPath = QDir::tempPath() + "/" + theName + ".png";
91   anImage.save( aPath );
92 }