Salome HOME
ed2764138491bdf65be5d0c1aea6684fa8d65b57
[modules/shaper.git] / src / NewGeom / NewGeom_SalomeViewer.cpp
1 #include "NewGeom_SalomeViewer.h"
2 #include "NewGeom_OCCSelector.h"
3
4 #include <OCCViewer_ViewWindow.h>
5 #include <OCCViewer_ViewPort3d.h>
6 #include <OCCViewer_ViewFrame.h>
7
8 #include <SUIT_ViewManager.h>
9
10 #include <QMouseEvent>
11 #include <QContextMenuEvent>
12
13 NewGeom_SalomeViewer::NewGeom_SalomeViewer(QObject* theParent)
14     : ModuleBase_IViewer(theParent),
15       mySelector(0)
16 {
17 }
18
19 //**********************************************
20 Handle(AIS_InteractiveContext) NewGeom_SalomeViewer::AISContext() const
21 {
22   if (mySelector && mySelector->viewer())
23     return mySelector->viewer()->getAISContext();
24   Handle(AIS_InteractiveContext) aNull;
25   return aNull;
26 }
27
28 //**********************************************
29 Handle(V3d_Viewer) NewGeom_SalomeViewer::v3dViewer() const
30 {
31   return mySelector->viewer()->getViewer3d();
32 }
33
34 //**********************************************
35 Handle(V3d_View) NewGeom_SalomeViewer::activeView() const
36 {
37   OCCViewer_Viewer* aViewer = mySelector->viewer();
38   SUIT_ViewManager* aMgr = aViewer->getViewManager();
39   OCCViewer_ViewWindow* aWnd = static_cast<OCCViewer_ViewWindow*>(aMgr->getActiveView());
40   return aWnd->getViewPort()->getView();
41 }
42
43 //**********************************************
44 void NewGeom_SalomeViewer::setSelector(NewGeom_OCCSelector* theSel)
45 {
46   if (mySelector) {
47     if (mySelector == theSel)
48       return;
49     else {
50       mySelector->viewer()->getViewManager()->disconnect(this);
51       OCCViewer_Viewer* aViewer = mySelector->viewer();
52       if (aViewer)
53         aViewer->disconnect(this);
54     }
55   }
56   mySelector = theSel;
57   if (!mySelector)
58     return;
59   OCCViewer_Viewer* aViewer = mySelector->viewer();
60   SUIT_ViewManager* aMgr = aViewer->getViewManager();
61
62   // TODO: Provide ModuleBase_IViewWindow interface
63   //connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
64   //connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewManager*)), this, SIGNAL(tryCloseView()));
65   //connect(aMgr, SIGNAL(deleteView(SUIT_ViewManager*)), this, SIGNAL(deleteView()));
66   //connect(aMgr, SIGNAL(viewCreated(SUIT_ViewManager*)), this, SIGNAL(viewCreated()));
67   //connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)), this, SIGNAL(activated()));
68
69   //connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
70   //        SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
71   //connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
72   //        SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
73   //connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
74   //        SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
75   //connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
76   //        SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
77   //connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
78   //        SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
79   //connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
80   //        SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
81
82   //connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
83 }
84
85 //**********************************************
86 //void NewGeom_SalomeViewer::onMousePress(SUIT_ViewWindow*, QMouseEvent* theEvent)
87 //{
88 //  emit mousePress(theEvent);
89 //}
90 //
91 ////**********************************************
92 //void NewGeom_SalomeViewer::onMouseRelease(SUIT_ViewWindow*, QMouseEvent* theEvent)
93 //{
94 //  emit mouseRelease(theEvent);
95 //}
96 //
97 ////**********************************************
98 //void NewGeom_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent* theEvent)
99 //{
100 //  emit mouseDoubleClick(theEvent);
101 //}
102 //
103 ////**********************************************
104 //void NewGeom_SalomeViewer::onMouseMove(SUIT_ViewWindow*, QMouseEvent* theEvent)
105 //{
106 //  emit mouseMove(theEvent);
107 //}
108 //
109 ////**********************************************
110 //void NewGeom_SalomeViewer::onKeyPress(SUIT_ViewWindow*, QKeyEvent* theEvent)
111 //{
112 //  emit keyPress(theEvent);
113 //}
114 //
115 ////**********************************************
116 //void NewGeom_SalomeViewer::onKeyRelease(SUIT_ViewWindow*, QKeyEvent* theEvent)
117 //{
118 //  emit keyRelease(theEvent);
119 //}
120
121 //**********************************************
122 void NewGeom_SalomeViewer::enableSelection(bool isEnabled)
123 {
124   mySelector->viewer()->enableSelection(isEnabled);
125   // there is a fix for a black-colored window 
126   // the viewer rubber band is valid if the values delta is less than 1
127   // TODO: remove this row after moving to SALOME 7.5
128   mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
129                                                       : SUIT_ViewModel::KEY_FREE);
130 }
131
132 //**********************************************
133 bool NewGeom_SalomeViewer::isSelectionEnabled() const
134 {
135   return mySelector->viewer()->isSelectionEnabled();
136 }
137
138 //**********************************************
139 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
140 {
141   mySelector->viewer()->enableMultiselection(isEnable);
142 }
143
144 //**********************************************
145 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
146 {
147   return mySelector->viewer()->isMultiSelectionEnabled();
148 }
149
150 //**********************************************
151 void NewGeom_SalomeViewer::fitAll()
152 {
153   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
154   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
155   if (aVFrame) {
156     aVFrame->onFitAll();
157   }
158 }
159
160 //**********************************************
161 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ)
162 {
163   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
164   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
165   if (aVFrame) {
166     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
167     if (!aView3d.IsNull()) {
168       aView3d->SetProj(theX, theY, theZ);
169       aView3d->FitAll(0.01, true, true);
170       aView3d->SetZSize(0.);
171     }
172   }
173 }
174
175 //***************************************
176 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
177 {
178   Handle(AIS_InteractiveContext) aContext = AISContext();
179   if (!aContext.IsNull()) {
180     aContext->AddFilter(theFilter);
181   }
182 }
183
184 //***************************************
185 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
186 {
187   Handle(AIS_InteractiveContext) aContext = AISContext();
188   if (!aContext.IsNull()) {
189     aContext->RemoveFilter(theFilter);
190   }
191 }
192
193 //***************************************
194 void NewGeom_SalomeViewer::clearSelectionFilters()
195 {
196   Handle(AIS_InteractiveContext) aContext = AISContext();
197   if (!aContext.IsNull()) {
198     aContext->RemoveFilters();
199   }
200 }