]> SALOME platform Git repositories - modules/shaper.git/blob - src/NewGeom/NewGeom_SalomeViewer.cpp
Salome HOME
Issue #236: Provide filtering of selected objects for extrusion
[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   connect(aMgr, SIGNAL(lastViewClosed(SUIT_ViewManager*)), this, SIGNAL(lastViewClosed()));
63   connect(aMgr, SIGNAL(tryCloseView(SUIT_ViewManager*)), this, SIGNAL(tryCloseView()));
64   connect(aMgr, SIGNAL(deleteView(SUIT_ViewManager*)), this, SIGNAL(deleteView()));
65   connect(aMgr, SIGNAL(viewCreated(SUIT_ViewManager*)), this, SIGNAL(viewCreated()));
66   connect(aMgr, SIGNAL(activated(SUIT_ViewManager*)), this, SIGNAL(activated()));
67
68   connect(aMgr, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), this,
69           SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
70   connect(aMgr, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), this,
71           SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
72   connect(aMgr, SIGNAL(mouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)), this,
73           SLOT(onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent*)));
74   connect(aMgr, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), this,
75           SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
76   connect(aMgr, SIGNAL(keyPress(SUIT_ViewWindow*, QKeyEvent*)), this,
77           SLOT(onKeyPress(SUIT_ViewWindow*, QKeyEvent*)));
78   connect(aMgr, SIGNAL(keyRelease(SUIT_ViewWindow*, QKeyEvent*)), this,
79           SLOT(onKeyRelease(SUIT_ViewWindow*, QKeyEvent*)));
80
81   connect(aViewer, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
82 }
83
84 //**********************************************
85 void NewGeom_SalomeViewer::onMousePress(SUIT_ViewWindow*, QMouseEvent* theEvent)
86 {
87   emit mousePress(theEvent);
88 }
89
90 //**********************************************
91 void NewGeom_SalomeViewer::onMouseRelease(SUIT_ViewWindow*, QMouseEvent* theEvent)
92 {
93   emit mouseRelease(theEvent);
94   //if ((theEvent->button() == Qt::RightButton) && 
95   //  (theEvent->modifiers() == Qt::NoModifier)) {
96   //  QContextMenuEvent aEvent(QContextMenuEvent::Mouse, theEvent->pos(), theEvent->globalPos());
97   //  emit contextMenuRequested(&aEvent);
98   //}
99 }
100
101 //**********************************************
102 void NewGeom_SalomeViewer::onMouseDoubleClick(SUIT_ViewWindow*, QMouseEvent* theEvent)
103 {
104   emit mouseDoubleClick(theEvent);
105 }
106
107 //**********************************************
108 void NewGeom_SalomeViewer::onMouseMove(SUIT_ViewWindow*, QMouseEvent* theEvent)
109 {
110   emit mouseMove(theEvent);
111 }
112
113 //**********************************************
114 void NewGeom_SalomeViewer::onKeyPress(SUIT_ViewWindow*, QKeyEvent* theEvent)
115 {
116   emit keyPress(theEvent);
117 }
118
119 //**********************************************
120 void NewGeom_SalomeViewer::onKeyRelease(SUIT_ViewWindow*, QKeyEvent* theEvent)
121 {
122   emit keyRelease(theEvent);
123 }
124
125 //**********************************************
126 void NewGeom_SalomeViewer::enableSelection(bool isEnabled)
127 {
128   mySelector->viewer()->enableSelection(isEnabled);
129   // there is a fix for a black-colored window 
130   // the viewer rubber band is valid if the values delta is less than 1
131   // TODO: remove this row after moving to SALOME 7.5
132   mySelector->viewer()->setInteractionStyle(isEnabled ? SUIT_ViewModel::STANDARD
133                                                       : SUIT_ViewModel::KEY_FREE);
134 }
135
136 //**********************************************
137 bool NewGeom_SalomeViewer::isSelectionEnabled() const
138 {
139   return mySelector->viewer()->isSelectionEnabled();
140 }
141
142 //**********************************************
143 void NewGeom_SalomeViewer::enableMultiselection(bool isEnable)
144 {
145   mySelector->viewer()->enableMultiselection(isEnable);
146 }
147
148 //**********************************************
149 bool NewGeom_SalomeViewer::isMultiSelectionEnabled() const
150 {
151   return mySelector->viewer()->isMultiSelectionEnabled();
152 }
153
154 //**********************************************
155 void NewGeom_SalomeViewer::fitAll()
156 {
157   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
158   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
159   if (aVFrame) {
160     aVFrame->onFitAll();
161   }
162 }
163
164 //**********************************************
165 void NewGeom_SalomeViewer::setViewProjection(double theX, double theY, double theZ)
166 {
167   SUIT_ViewManager* aMgr = mySelector->viewer()->getViewManager();
168   OCCViewer_ViewFrame* aVFrame = dynamic_cast<OCCViewer_ViewFrame*>(aMgr->getActiveView());
169   if (aVFrame) {
170     Handle(V3d_View) aView3d = aVFrame->getViewPort()->getView();
171     if (!aView3d.IsNull()) {
172       aView3d->SetProj(theX, theY, theZ);
173       aView3d->FitAll(0.01, true, true);
174       aView3d->SetZSize(0.);
175     }
176   }
177 }
178
179 //***************************************
180 void NewGeom_SalomeViewer::addSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
181 {
182   Handle(AIS_InteractiveContext) aContext = AISContext();
183   if (!aContext.IsNull()) {
184     aContext->AddFilter(theFilter);
185   }
186 }
187
188 //***************************************
189 void NewGeom_SalomeViewer::removeSelectionFilter(const Handle(SelectMgr_Filter)& theFilter)
190 {
191   Handle(AIS_InteractiveContext) aContext = AISContext();
192   if (!aContext.IsNull()) {
193     aContext->RemoveFilter(theFilter);
194   }
195 }
196
197 //***************************************
198 void NewGeom_SalomeViewer::clearSelectionFilters()
199 {
200   Handle(AIS_InteractiveContext) aContext = AISContext();
201   if (!aContext.IsNull()) {
202     aContext->RemoveFilters();
203   }
204 }