Salome HOME
debug of DTM object
[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 )
47 : HYDROGUI_InputPanel( theModule, theTitle )
48 {
49   QWidget* viewMain = new QWidget( mainFrame() );
50   QVBoxLayout* viewBase = new QVBoxLayout( viewMain );
51   viewBase->setMargin( 0 );
52
53   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
54   myViewManager = new OCCViewer_ViewManager( theModule->getApp()->activeStudy(), 0 );
55   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
56
57   aViewer->setBackground( OCCViewer_ViewFrame::TOP_LEFT,
58                           aResMgr->backgroundValue( "OCCViewer", "xz_background", aViewer->background(OCCViewer_ViewFrame::TOP_LEFT) ) );
59   aViewer->setBackground( OCCViewer_ViewFrame::TOP_RIGHT,
60                           aResMgr->backgroundValue( "OCCViewer", "yz_background", aViewer->background(OCCViewer_ViewFrame::TOP_RIGHT) ) );
61   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_LEFT,
62                           aResMgr->backgroundValue( "OCCViewer", "xy_background", aViewer->background(OCCViewer_ViewFrame::BOTTOM_LEFT) ) );
63   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_RIGHT,
64                           aResMgr->backgroundValue( "OCCViewer", "background", aViewer->background(OCCViewer_ViewFrame::MAIN_VIEW) ) );
65
66   aViewer->setTrihedronSize( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ),
67                              aResMgr->booleanValue( "3DViewer", "relative_size", aViewer->trihedronRelative() ));
68   aViewer->setInteractionStyle( aResMgr->integerValue( "3DViewer", "navigation_mode", aViewer->interactionStyle() ) );
69   aViewer->setZoomingStyle( aResMgr->integerValue( "3DViewer", "zooming_mode", aViewer->zoomingStyle() ) );
70   aViewer->enablePreselection( aResMgr->booleanValue( "OCCViewer", "enable_preselection", aViewer->isPreselectionEnabled() ) );
71   aViewer->enableSelection( aResMgr->booleanValue( "OCCViewer", "enable_selection", aViewer->isSelectionEnabled() ) );
72
73   myViewManager->setViewModel( aViewer );// custom view model, which extends SALOME_View interface
74
75   //aViewer->enableMultiselection( false );
76
77   SUIT_ViewWindow* aViewWin = myViewManager->createViewWindow();
78   aViewer->setStaticTrihedronDisplayed( false );
79   /*
80   Handle(AIS_Trihedron) aTrihedron =
81       HYDROGUI_AISTrihedron::createTrihedron( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ) );
82   Handle(AIS_InteractiveContext) anAISContext = aViewer->getAISContext();
83   if ( !anAISContext.IsNull() )
84   {
85     anAISContext->Display( aTrihedron );
86     anAISContext->Deactivate( aTrihedron );
87   }
88   */
89   viewBase->addWidget( aViewWin );
90
91   // Coordinates
92   connect( myViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
93            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
94
95   if ( aViewWin )
96   {
97     OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aViewWin );
98     if ( aViewFrame && aViewFrame->getViewPort() )
99       aViewFrame->getViewPort()->installEventFilter( this );
100   }
101
102   myCoordLabel = new QLabel( viewMain );
103   viewBase->addWidget( myCoordLabel );
104 }
105
106 HYDROGUI_ViewerDlg::~HYDROGUI_ViewerDlg()
107 {
108   myViewManager->closeAllViews();
109   delete myViewManager;
110 }
111
112 bool HYDROGUI_ViewerDlg::event( QEvent* e )
113 {
114     if ( e->type() == QEvent::Polish )
115     {
116         addWidget( myCoordLabel->parentWidget(), 4 );
117
118         Handle(AIS_Trihedron) aTrihedron = trihedron();
119         Handle(AIS_InteractiveContext) anAISContext = getAISContext();
120         if ( !anAISContext.IsNull() && !aTrihedron.IsNull() )
121         {
122             viewer()->setTrihedronShown( false );
123             anAISContext->Display( aTrihedron );
124             anAISContext->Deactivate( aTrihedron );
125         }
126     }
127
128     return HYDROGUI_InputPanel::event( e );
129 }
130
131 Handle(AIS_InteractiveContext) HYDROGUI_ViewerDlg::getAISContext()
132 {
133   OCCViewer_Viewer* aViewer = (OCCViewer_Viewer*)myViewManager->getViewModel();
134   return aViewer ? aViewer->getAISContext() : 0;
135 }
136
137 OCCViewer_Viewer* HYDROGUI_ViewerDlg::viewer() const
138 {
139     return ::qobject_cast<OCCViewer_Viewer*>( myViewManager->getViewModel() );
140 }
141
142 OCCViewer_ViewManager* HYDROGUI_ViewerDlg::viewManager() const
143 {
144     return myViewManager;
145 }
146
147 SUIT_SelectionMgr* HYDROGUI_ViewerDlg::selectionMgr() const
148 {
149     SUIT_SelectionMgr* aSelMgr = 0;
150     if ( module() )
151     {
152         LightApp_Application* app = module()->getApp();
153         if ( app )
154             aSelMgr = app->selectionMgr();
155     }
156     return aSelMgr;
157 }
158
159 void HYDROGUI_ViewerDlg::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
160 {
161   OCCViewer_ViewWindow* anOCCViewWindow = 
162     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
163   if ( anOCCViewWindow && anOCCViewWindow->getViewPort() )
164   {
165     gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), anOCCViewWindow->getViewPort()->getView() );
166
167     // Show the coordinates
168     QString aX = HYDROGUI_Tool::GetCoordinateString( aPnt.X(), true );
169     QString anY = HYDROGUI_Tool::GetCoordinateString( aPnt.Y(), true );
170     myCoordLabel->setText( tr("UZ_COORDINATES_INFO").arg( aX ).arg( anY ) );
171   }
172 }
173
174 bool HYDROGUI_ViewerDlg::eventFilter( QObject* theObj, QEvent* theEvent )
175 {
176   if ( theObj->inherits( "OCCViewer_ViewPort" ) )
177   {
178     if ( theEvent->type() == QEvent::Leave )
179       myCoordLabel->clear();
180
181     return false;
182   }
183
184   return HYDROGUI_InputPanel::eventFilter( theObj, theEvent );
185 }
186
187 Handle(AIS_Trihedron) HYDROGUI_ViewerDlg::trihedron()
188 {
189     return Handle(AIS_Trihedron)();
190 }