Salome HOME
lot 10 - warnings for DTM - untested
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ViewerDlg.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 "HYDROGUI_ViewerDlg.h"
20
21 #include "HYDROGUI_Module.h"
22 #include "HYDROGUI_Tool.h"
23 #include "HYDROGUI_AISTrihedron.h"
24
25 #include <CurveCreator_Widget.h>
26 #include <CurveCreator_ICurve.hxx>
27 #include <CurveCreator_Utils.hxx>
28
29 #include <OCCViewer_ViewPort3d.h>
30 #include <OCCViewer_Utilities.h>
31 #include <OCCViewer_ViewManager.h>
32 #include <OCCViewer_ViewFrame.h>
33
34 #include <LightApp_Application.h>
35 #include <LightApp_SelectionMgr.h>
36
37 #include <SUIT_Session.h>
38 #include <SUIT_ResourceMgr.h>
39
40 #include <QGroupBox>
41 #include <QHBoxLayout>
42 #include <QLabel>
43 #include <QLineEdit>
44 #include <QMouseEvent>
45
46 HYDROGUI_ViewerDlg::HYDROGUI_ViewerDlg( HYDROGUI_Module* theModule, const QString& theTitle, bool isSplitter, bool dispTrihedron )
47 : HYDROGUI_InputPanel( theModule, theTitle, true, isSplitter )
48 {
49   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
50 #ifndef TEST_MODE
51   SUIT_Study* aStudy = theModule ? theModule->application()->activeStudy() : 0;
52 #else
53   SUIT_Study* aStudy = 0;
54 #endif
55   myViewManager = new OCCViewer_ViewManager( aStudy, 0 );
56   bool IsChainedPan = aResMgr->booleanValue( "HYDRO", "chained_panning" );
57   myViewManager->setChainedOperations(IsChainedPan);
58   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( dispTrihedron );
59
60   aViewer->setBackground( OCCViewer_ViewFrame::TOP_LEFT,
61                           aResMgr->backgroundValue( "OCCViewer", "xz_background", aViewer->background(OCCViewer_ViewFrame::TOP_LEFT) ) );
62   aViewer->setBackground( OCCViewer_ViewFrame::TOP_RIGHT,
63                           aResMgr->backgroundValue( "OCCViewer", "yz_background", aViewer->background(OCCViewer_ViewFrame::TOP_RIGHT) ) );
64   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_LEFT,
65                           aResMgr->backgroundValue( "OCCViewer", "xy_background", aViewer->background(OCCViewer_ViewFrame::BOTTOM_LEFT) ) );
66   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_RIGHT,
67                           aResMgr->backgroundValue( "OCCViewer", "background", aViewer->background(OCCViewer_ViewFrame::MAIN_VIEW) ) );
68
69   aViewer->setTrihedronSize( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ),
70                              aResMgr->booleanValue( "3DViewer", "relative_size", aViewer->trihedronRelative() ));
71   aViewer->setInteractionStyle( aResMgr->integerValue( "3DViewer", "navigation_mode", aViewer->interactionStyle() ) );
72   aViewer->setZoomingStyle( aResMgr->integerValue( "3DViewer", "zooming_mode", aViewer->zoomingStyle() ) );
73   aViewer->enablePreselection( aResMgr->booleanValue( "OCCViewer", "enable_preselection", aViewer->isPreselectionEnabled() ) );
74   aViewer->enableSelection( aResMgr->booleanValue( "OCCViewer", "enable_selection", aViewer->isSelectionEnabled() ) );
75
76   myViewManager->setViewModel( aViewer );// custom view model, which extends SALOME_View interface
77
78   //aViewer->enableMultiselection( false );
79
80   SUIT_ViewWindow* aViewWin = myViewManager->createViewWindow();
81   aViewer->setStaticTrihedronDisplayed( false );
82   /*
83   Handle(AIS_Trihedron) aTrihedron =
84       HYDROGUI_AISTrihedron::createTrihedron( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ) );
85   Handle(AIS_InteractiveContext) anAISContext = aViewer->getAISContext();
86   if ( !anAISContext.IsNull() )
87   {
88     anAISContext->Display( aTrihedron );
89     anAISContext->Deactivate( aTrihedron );
90   }
91   */
92   addWidget( aViewWin, 1 );
93
94   // Coordinates
95   connect( myViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
96            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
97
98   if ( aViewWin )
99   {
100     OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aViewWin );
101     if ( aViewFrame && aViewFrame->getViewPort() )
102       aViewFrame->getViewPort()->installEventFilter( this );
103   }
104
105   myCoordLabel = new QLabel( mainFrame() );
106   addWidget( myCoordLabel, 0 );
107 }
108
109 HYDROGUI_ViewerDlg::~HYDROGUI_ViewerDlg()
110 {
111   myViewManager->closeAllViews();
112   delete myViewManager;
113 }
114
115 bool HYDROGUI_ViewerDlg::event( QEvent* e )
116 {
117     if ( e->type() == QEvent::Polish )
118     {
119         //addWidget( myCoordLabel->parentWidget(), 4 );
120
121         Handle(AIS_Trihedron) aTrihedron = trihedron();
122         Handle(AIS_InteractiveContext) anAISContext = getAISContext();
123         if ( !anAISContext.IsNull() && !aTrihedron.IsNull() )
124         {
125             viewer()->setTrihedronShown( false );
126             anAISContext->Display( aTrihedron, true );
127             anAISContext->Deactivate( aTrihedron );
128         }
129     }
130
131     return HYDROGUI_InputPanel::event( e );
132 }
133
134 Handle(AIS_InteractiveContext) HYDROGUI_ViewerDlg::getAISContext()
135 {
136   OCCViewer_Viewer* aViewer = (OCCViewer_Viewer*)myViewManager->getViewModel();
137   return aViewer ? aViewer->getAISContext() : 0;
138 }
139
140 OCCViewer_Viewer* HYDROGUI_ViewerDlg::viewer() const
141 {
142     return ::qobject_cast<OCCViewer_Viewer*>( myViewManager->getViewModel() );
143 }
144
145 OCCViewer_ViewManager* HYDROGUI_ViewerDlg::viewManager() const
146 {
147     return myViewManager;
148 }
149
150 SUIT_SelectionMgr* HYDROGUI_ViewerDlg::selectionMgr() const
151 {
152 #ifdef TEST_MODE
153   static SUIT_SelectionMgr* mgr = new SUIT_SelectionMgr();
154   return mgr;
155 #else
156     SUIT_SelectionMgr* aSelMgr = 0;
157     if ( module() )
158     {
159         LightApp_Application* app = module()->getApp();
160         if ( app )
161             aSelMgr = app->selectionMgr();
162     }
163     return aSelMgr;
164 #endif
165 }
166
167 void HYDROGUI_ViewerDlg::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
168 {
169   OCCViewer_ViewWindow* anOCCViewWindow = 
170     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
171   if ( anOCCViewWindow && anOCCViewWindow->getViewPort() )
172   {
173     gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), anOCCViewWindow->getViewPort()->getView() );
174
175     // Show the coordinates
176     QString aX = HYDROGUI_Tool::GetCoordinateString( aPnt.X(), true );
177     QString anY = HYDROGUI_Tool::GetCoordinateString( aPnt.Y(), true );
178     myCoordLabel->setText( tr("UZ_COORDINATES_INFO").arg( aX ).arg( anY ) );
179   }
180 }
181
182 bool HYDROGUI_ViewerDlg::eventFilter( QObject* theObj, QEvent* theEvent )
183 {
184   if ( theObj->inherits( "OCCViewer_ViewPort" ) )
185   {
186     if ( theEvent->type() == QEvent::Leave )
187       myCoordLabel->clear();
188
189     return false;
190   }
191
192   return HYDROGUI_InputPanel::eventFilter( theObj, theEvent );
193 }
194
195 Handle(AIS_Trihedron) HYDROGUI_ViewerDlg::trihedron()
196 {
197     return Handle(AIS_Trihedron)();
198 }