Salome HOME
Merge branch 'master' into BR_RIVER
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ViewerDlg.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROGUI_ViewerDlg.h"
24
25 #include "HYDROGUI_Module.h"
26 #include "HYDROGUI_Tool.h"
27 #include "HYDROGUI_AISTrihedron.h"
28
29 #include <CurveCreator_Widget.h>
30 #include <CurveCreator_ICurve.hxx>
31 #include <CurveCreator_Utils.hxx>
32
33 #include <OCCViewer_ViewPort3d.h>
34 #include <OCCViewer_Utilities.h>
35 #include <OCCViewer_ViewManager.h>
36 #include <OCCViewer_ViewFrame.h>
37
38 #include <LightApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
40
41 #include <SUIT_Session.h>
42 #include <SUIT_ResourceMgr.h>
43
44 #include <QGroupBox>
45 #include <QHBoxLayout>
46 #include <QLabel>
47 #include <QLineEdit>
48 #include <QMouseEvent>
49
50 HYDROGUI_ViewerDlg::HYDROGUI_ViewerDlg( HYDROGUI_Module* theModule, const QString& theTitle )
51 : HYDROGUI_InputPanel( theModule, theTitle )
52 {
53   QWidget* viewMain = new QWidget( mainFrame() );
54   QVBoxLayout* viewBase = new QVBoxLayout( viewMain );
55   viewBase->setMargin( 0 );
56
57   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
58   myViewManager = new OCCViewer_ViewManager( theModule->getApp()->activeStudy(), 0 );
59   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( true );
60
61   aViewer->setBackground( OCCViewer_ViewFrame::TOP_LEFT,
62                           aResMgr->backgroundValue( "OCCViewer", "xz_background", aViewer->background(OCCViewer_ViewFrame::TOP_LEFT) ) );
63   aViewer->setBackground( OCCViewer_ViewFrame::TOP_RIGHT,
64                           aResMgr->backgroundValue( "OCCViewer", "yz_background", aViewer->background(OCCViewer_ViewFrame::TOP_RIGHT) ) );
65   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_LEFT,
66                           aResMgr->backgroundValue( "OCCViewer", "xy_background", aViewer->background(OCCViewer_ViewFrame::BOTTOM_LEFT) ) );
67   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_RIGHT,
68                           aResMgr->backgroundValue( "OCCViewer", "background", aViewer->background(OCCViewer_ViewFrame::MAIN_VIEW) ) );
69
70   aViewer->setTrihedronSize( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ),
71                              aResMgr->booleanValue( "3DViewer", "relative_size", aViewer->trihedronRelative() ));
72   aViewer->setInteractionStyle( aResMgr->integerValue( "3DViewer", "navigation_mode", aViewer->interactionStyle() ) );
73   aViewer->setZoomingStyle( aResMgr->integerValue( "3DViewer", "zooming_mode", aViewer->zoomingStyle() ) );
74   aViewer->enablePreselection( aResMgr->booleanValue( "OCCViewer", "enable_preselection", aViewer->isPreselectionEnabled() ) );
75   aViewer->enableSelection( aResMgr->booleanValue( "OCCViewer", "enable_selection", aViewer->isSelectionEnabled() ) );
76
77   myViewManager->setViewModel( aViewer );// custom view model, which extends SALOME_View interface
78
79   aViewer->enableMultiselection( false );
80
81   SUIT_ViewWindow* aViewWin = myViewManager->createViewWindow();
82   aViewer->setStaticTrihedronDisplayed( false );
83   /*
84   Handle(AIS_Trihedron) aTrihedron =
85       HYDROGUI_AISTrihedron::createTrihedron( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ) );
86   Handle(AIS_InteractiveContext) anAISContext = aViewer->getAISContext();
87   if ( !anAISContext.IsNull() )
88   {
89     anAISContext->Display( aTrihedron );
90     anAISContext->Deactivate( aTrihedron );
91   }
92   */
93   viewBase->addWidget( aViewWin );
94
95   // Coordinates
96   connect( myViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
97            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
98
99   if ( aViewWin )
100   {
101     OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aViewWin );
102     if ( aViewFrame && aViewFrame->getViewPort() )
103       aViewFrame->getViewPort()->installEventFilter( this );
104   }
105
106   myCoordLabel = new QLabel( viewMain );
107   viewBase->addWidget( myCoordLabel );
108 }
109
110 HYDROGUI_ViewerDlg::~HYDROGUI_ViewerDlg()
111 {
112   myViewManager->closeAllViews();
113   delete myViewManager;
114 }
115
116 bool HYDROGUI_ViewerDlg::event( QEvent* e )
117 {
118     if ( e->type() == QEvent::Polish )
119     {
120         addWidget( myCoordLabel->parentWidget(), 4 );
121
122         Handle(AIS_Trihedron) aTrihedron = trihedron();
123         Handle(AIS_InteractiveContext) anAISContext = getAISContext();
124         if ( !anAISContext.IsNull() && !aTrihedron.IsNull() )
125         {
126             viewer()->setTrihedronShown( false );
127             anAISContext->Display( aTrihedron );
128             anAISContext->Deactivate( aTrihedron );
129         }
130     }
131
132     return HYDROGUI_InputPanel::event( e );
133 }
134
135 Handle(AIS_InteractiveContext) HYDROGUI_ViewerDlg::getAISContext()
136 {
137   OCCViewer_Viewer* aViewer = (OCCViewer_Viewer*)myViewManager->getViewModel();
138   return aViewer ? aViewer->getAISContext() : 0;
139 }
140
141 OCCViewer_Viewer* HYDROGUI_ViewerDlg::viewer() const
142 {
143     return ::qobject_cast<OCCViewer_Viewer*>( myViewManager->getViewModel() );
144 }
145
146 OCCViewer_ViewManager* HYDROGUI_ViewerDlg::viewManager() const
147 {
148     return myViewManager;
149 }
150
151 SUIT_SelectionMgr* HYDROGUI_ViewerDlg::selectionMgr() const
152 {
153     SUIT_SelectionMgr* aSelMgr = 0;
154     if ( module() )
155     {
156         LightApp_Application* app = module()->getApp();
157         if ( app )
158             aSelMgr = app->selectionMgr();
159     }
160     return aSelMgr;
161 }
162
163 void HYDROGUI_ViewerDlg::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
164 {
165   OCCViewer_ViewWindow* anOCCViewWindow = 
166     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
167   if ( anOCCViewWindow && anOCCViewWindow->getViewPort() )
168   {
169     gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), anOCCViewWindow->getViewPort()->getView() );
170
171     // Show the coordinates
172     QString aX = HYDROGUI_Tool::GetCoordinateString( aPnt.X(), true );
173     QString anY = HYDROGUI_Tool::GetCoordinateString( aPnt.Y(), true );
174     myCoordLabel->setText( tr("UZ_COORDINATES_INFO").arg( aX ).arg( anY ) );
175   }
176 }
177
178 bool HYDROGUI_ViewerDlg::eventFilter( QObject* theObj, QEvent* theEvent )
179 {
180   if ( theObj->inherits( "OCCViewer_ViewPort" ) )
181   {
182     if ( theEvent->type() == QEvent::Leave )
183       myCoordLabel->clear();
184
185     return false;
186   }
187
188   return HYDROGUI_InputPanel::eventFilter( theObj, theEvent );
189 }
190
191 Handle(AIS_Trihedron) HYDROGUI_ViewerDlg::trihedron()
192 {
193     return Handle(AIS_Trihedron)();
194 }