Salome HOME
Draft version of "Find River Bottom", new viewer dialog base class, new component...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ViewerDlg.cxx
1 // Copyright (C) 2007-2013  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.
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 #include <LightApp_Application.h>
38
39 #include <SUIT_Session.h>
40 #include <SUIT_ResourceMgr.h>
41
42 #include <QGroupBox>
43 #include <QHBoxLayout>
44 #include <QLabel>
45 #include <QLineEdit>
46 #include <QMouseEvent>
47
48 HYDROGUI_ViewerDlg::HYDROGUI_ViewerDlg( HYDROGUI_Module* theModule, const QString& theTitle )
49 : HYDROGUI_InputPanel( theModule, theTitle )
50 {
51   QWidget* viewMain = new QWidget( mainFrame() );
52   QVBoxLayout* viewBase = new QVBoxLayout( viewMain );
53   viewBase->setMargin( 0 );
54
55   SUIT_ResourceMgr* aResMgr = SUIT_Session::session()->resourceMgr();
56   myViewManager = new OCCViewer_ViewManager( theModule->getApp()->activeStudy(), 0 );
57   OCCViewer_Viewer* aViewer = new OCCViewer_Viewer( false );
58
59   aViewer->setBackground( OCCViewer_ViewFrame::TOP_LEFT,
60                           aResMgr->backgroundValue( "OCCViewer", "xz_background", aViewer->background(OCCViewer_ViewFrame::TOP_LEFT) ) );
61   aViewer->setBackground( OCCViewer_ViewFrame::TOP_RIGHT,
62                           aResMgr->backgroundValue( "OCCViewer", "yz_background", aViewer->background(OCCViewer_ViewFrame::TOP_RIGHT) ) );
63   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_LEFT,
64                           aResMgr->backgroundValue( "OCCViewer", "xy_background", aViewer->background(OCCViewer_ViewFrame::BOTTOM_LEFT) ) );
65   aViewer->setBackground( OCCViewer_ViewFrame::BOTTOM_RIGHT,
66                           aResMgr->backgroundValue( "OCCViewer", "background", aViewer->background(OCCViewer_ViewFrame::MAIN_VIEW) ) );
67
68   aViewer->setTrihedronSize( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ),
69                              aResMgr->booleanValue( "3DViewer", "relative_size", aViewer->trihedronRelative() ));
70   aViewer->setInteractionStyle( aResMgr->integerValue( "3DViewer", "navigation_mode", aViewer->interactionStyle() ) );
71   aViewer->setZoomingStyle( aResMgr->integerValue( "3DViewer", "zooming_mode", aViewer->zoomingStyle() ) );
72   aViewer->enablePreselection( aResMgr->booleanValue( "OCCViewer", "enable_preselection", aViewer->isPreselectionEnabled() ) );
73   aViewer->enableSelection( aResMgr->booleanValue( "OCCViewer", "enable_selection", aViewer->isSelectionEnabled() ) );
74
75   myViewManager->setViewModel( aViewer );// custom view model, which extends SALOME_View interface
76
77   SUIT_ViewWindow* aViewWin = myViewManager->createViewWindow();
78   aViewer->setStaticTrihedronDisplayed( false );
79   Handle(AIS_Trihedron) aTrihedron =
80       HYDROGUI_AISTrihedron::createTrihedron( aResMgr->doubleValue( "3DViewer", "trihedron_size", aViewer->trihedronSize() ) );
81   Handle(AIS_InteractiveContext) anAISContext = aViewer->getAISContext();
82   if ( !anAISContext.IsNull() )
83   {
84     anAISContext->Display( aTrihedron );
85     anAISContext->Deactivate( aTrihedron );
86   }
87
88   viewBase->addWidget( aViewWin );
89
90   // Coordinates
91   connect( myViewManager, SIGNAL( mouseMove( SUIT_ViewWindow*, QMouseEvent* ) ),
92            this, SLOT( onMouseMove( SUIT_ViewWindow*, QMouseEvent* ) ) );
93
94   if ( aViewWin )
95   {
96     OCCViewer_ViewFrame* aViewFrame = dynamic_cast<OCCViewer_ViewFrame*>( aViewWin );
97     if ( aViewFrame && aViewFrame->getViewPort() )
98       aViewFrame->getViewPort()->installEventFilter( this );
99   }
100
101   myCoordLabel = new QLabel( viewMain );
102   viewBase->addWidget( myCoordLabel );
103 }
104
105 HYDROGUI_ViewerDlg::~HYDROGUI_ViewerDlg()
106 {
107   delete myViewManager;
108 }
109
110 bool HYDROGUI_ViewerDlg::event( QEvent* e )
111 {
112     if ( e->type() == QEvent::Polish )
113         addWidget( myCoordLabel->parentWidget(), 4 );
114
115     return HYDROGUI_InputPanel::event( e );
116 }
117
118 Handle(AIS_InteractiveContext) HYDROGUI_ViewerDlg::getAISContext()
119 {
120   OCCViewer_Viewer* aViewer = (OCCViewer_Viewer*)myViewManager->getViewModel();
121   return aViewer ? aViewer->getAISContext() : 0;
122 }
123
124 OCCViewer_Viewer* HYDROGUI_ViewerDlg::viewer() const
125 {
126     return ::qobject_cast<OCCViewer_Viewer*>( myViewManager->getViewModel() );
127 }
128
129 OCCViewer_ViewManager* HYDROGUI_ViewerDlg::viewManager() const
130 {
131     return myViewManager;
132 }
133
134 void HYDROGUI_ViewerDlg::onMouseMove( SUIT_ViewWindow* theViewWindow, QMouseEvent* theEvent )
135 {
136   OCCViewer_ViewWindow* anOCCViewWindow = 
137     dynamic_cast<OCCViewer_ViewWindow*>(theViewWindow);
138   if ( anOCCViewWindow && anOCCViewWindow->getViewPort() )
139   {
140     gp_Pnt aPnt = CurveCreator_Utils::ConvertClickToPoint( theEvent->x(), theEvent->y(), anOCCViewWindow->getViewPort()->getView() );
141
142     // Show the coordinates
143     QString aX = HYDROGUI_Tool::GetCoordinateString( aPnt.X(), true );
144     QString anY = HYDROGUI_Tool::GetCoordinateString( aPnt.Y(), true );
145     myCoordLabel->setText( tr("UZ_COORDINATES_INFO").arg( aX ).arg( anY ) );
146   }
147 }
148
149 bool HYDROGUI_ViewerDlg::eventFilter( QObject* theObj, QEvent* theEvent )
150 {
151   if ( theObj->inherits( "OCCViewer_ViewPort" ) )
152   {
153     if ( theEvent->type() == QEvent::Leave )
154       myCoordLabel->clear();
155
156     return false;
157   }
158
159   return HYDROGUI_InputPanel::eventFilter( theObj, theEvent );
160 }