7fd59977 |
1 | #include "Viewer.h" |
2 | #include "Timer.h" |
3 | |
4 | #include <QApplication.h> |
5 | #include <QCursor.h> |
6 | #include <QMessagebox.h> |
7 | |
8 | #include <QMouseEvent> |
9 | |
10 | #include <WNT_Window.hxx> |
11 | #include <Graphic3d_WNTGraphicDevice.hxx> |
12 | |
13 | #include <Voxel_Prs.hxx> |
14 | #include <AIS_ListOfInteractive.hxx> |
15 | #include <AIS_ListIteratorOfListOfInteractive.hxx> |
16 | |
17 | static Handle(Graphic3d_WNTGraphicDevice) device; |
18 | |
19 | Viewer::Viewer(QWidget* parent):QWidget(parent) |
20 | { |
21 | if(device.IsNull()) |
22cc9fe9 |
22 | { |
7fd59977 |
23 | device = new Graphic3d_WNTGraphicDevice(); |
22cc9fe9 |
24 | if (!device->GraphicDriver().IsNull()) |
25 | { |
26 | myGraphicDriver = Handle(OpenGl_GraphicDriver)::DownCast(device->GraphicDriver()); |
27 | } |
28 | } |
7fd59977 |
29 | |
22cc9fe9 |
30 | Handle(V3d_Viewer) aViewer = new V3d_Viewer(device, TCollection_ExtendedString("Visu3D").ToExtString(), "", |
7fd59977 |
31 | 1000, V3d_XposYnegZpos, |
32 | Quantity_NOC_GRAY30, V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT, |
33 | true, true, V3d_TEX_NONE); |
34 | aViewer->Init(); |
35 | |
36 | aViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK); |
37 | |
38 | myView = aViewer->CreateView(); |
39 | myIC = new AIS_InteractiveContext(aViewer); |
40 | myIC->SetDeviationCoefficient(1.e-3); |
41 | |
42 | int windowHandle = (int) winId(); |
43 | short hi, lo; |
44 | |
45 | lo = (short) windowHandle; |
46 | hi = (short) (windowHandle >> 16); |
47 | Handle(WNT_Window) hWnd = |
48 | new WNT_Window(Handle(Graphic3d_WNTGraphicDevice)::DownCast(aViewer->Device()), (int) hi, (int) lo); |
49 | |
50 | myView->SetWindow(hWnd); |
51 | if(!hWnd->IsMapped()) |
52 | hWnd->Map(); |
53 | |
54 | myView->MustBeResized(); |
55 | myView->SetSurfaceDetail(V3d_TEX_NONE); |
56 | myView->SetTransparency(Standard_True); |
57 | myView->SetSize(10000.0); |
58 | myView->SetZSize(10000.0); |
59 | myView->SetViewMappingDefault(); |
60 | |
61 | myZoom = false; |
62 | myPan = false; |
63 | myRotate = false; |
64 | setMouseTracking(true); |
65 | |
66 | setMinimumSize(400, 200); |
67 | |
68 | myView->ZBufferTriedronSetup(); |
69 | myView->TriedronDisplay(Aspect_TOTP_LEFT_LOWER, Quantity_NOC_BLACK, 0.1, V3d_ZBUFFER); |
70 | |
71 | mySelector.Init(myView); |
4d183a4b |
72 | |
73 | setBackgroundRole( QPalette::NoRole );//NoBackground ); |
74 | // set focus policy to threat QContextMenuEvent from keyboard |
75 | setFocusPolicy( Qt::StrongFocus ); |
76 | setAttribute( Qt::WA_PaintOnScreen ); |
77 | setAttribute( Qt::WA_NoSystemBackground ); |
7fd59977 |
78 | } |
79 | |
80 | Viewer::~Viewer() |
81 | { |
82 | |
83 | } |
84 | |
85 | void Viewer::paintEvent(QPaintEvent * pEvent) |
86 | { |
87 | if (!myView.IsNull()) |
88 | myView->Redraw(); |
89 | } |
90 | |
91 | |
4d183a4b |
92 | /*! |
93 | Get paint engine for the OpenGL viewer. [ virtual public ] |
94 | */ |
95 | QPaintEngine* Viewer::paintEngine() const |
96 | { |
97 | return 0; |
98 | } |
99 | |
7fd59977 |
100 | void Viewer::resizeEvent(QResizeEvent * e) |
101 | { |
102 | if (!myView.IsNull()) |
103 | { |
104 | myView->MustBeResized(); |
105 | } |
106 | } |
107 | |
108 | void Viewer::mousePressEvent(QMouseEvent * mpEvent) |
109 | { |
110 | // Memorize start point |
111 | myStartPnt.setX(mpEvent->x()); |
112 | myStartPnt.setY(mpEvent->y()); |
113 | |
114 | // Inform IC that the mouse cursor is at the point |
115 | myIC->MoveTo(myStartPnt.x(), myStartPnt.y(), myView); |
116 | |
117 | // In case of rotation, define the start rotation point |
118 | if ((mpEvent->modifiers() & Qt::ControlModifier) && (mpEvent->buttons() & Qt::RightButton)) |
119 | { |
120 | myView->StartRotation(myStartPnt.x(), myStartPnt.y()); |
121 | } |
122 | |
123 | // Start degenerate mode |
124 | setDegenerateMode(true); |
125 | |
126 | emit mousePressed(mpEvent->modifiers(), mpEvent->x(), mpEvent->y()); |
127 | } |
128 | |
129 | void Viewer::mouseMoveEvent(QMouseEvent * mmEvent) |
130 | { |
131 | QPoint currentPnt(mmEvent->x(), mmEvent->y()); |
132 | |
133 | if (mmEvent->modifiers() & Qt::ControlModifier) |
134 | { |
135 | if (mmEvent->buttons() & Qt::LeftButton) |
136 | { |
137 | myView->Zoom(myStartPnt.x(), myStartPnt.y(), currentPnt.x(), currentPnt.y()); |
138 | myStartPnt = currentPnt; |
139 | } |
140 | else if (mmEvent->buttons() & Qt::MidButton) |
141 | { |
142 | myView->Pan(currentPnt.x() - myStartPnt.x(), myStartPnt.y() - currentPnt.y()); |
143 | myStartPnt = currentPnt; |
144 | } |
145 | else if (mmEvent->buttons() & Qt::RightButton) |
146 | { |
147 | myView->Rotation(currentPnt.x(), currentPnt.y()); |
148 | } |
149 | } |
150 | else |
151 | { |
152 | myIC->MoveTo(currentPnt.x(), currentPnt.y(), myView); |
153 | } |
154 | |
155 | emit mouseMoved(mmEvent->modifiers(), currentPnt.x(), currentPnt.y()); |
156 | } |
157 | |
158 | void Viewer::mouseReleaseEvent(QMouseEvent * mrEvent) |
159 | { |
160 | if(mrEvent->button() == Qt::LeftButton) |
161 | { |
162 | if(!myZoom && !myPan && !myRotate) |
163 | { |
164 | if(mrEvent->modifiers() & Qt::ShiftModifier) |
165 | myIC->ShiftSelect(); |
166 | else |
167 | myIC->Select(); |
168 | |
169 | // Select a voxel |
170 | int ix = -1, iy = -1, iz = -1; |
171 | bool detected = mySelector.Detect(mrEvent->x(), mrEvent->y(), ix, iy, iz); |
172 | if (detected) |
173 | { |
174 | cout<<"("<<ix<<", "<<iy<<", "<<iz<<")"<<endl; |
175 | } |
176 | if (!myPrs.IsNull()) |
177 | myPrs->Highlight(ix, iy, iz); |
178 | } |
179 | } |
180 | else if(mrEvent->button() == Qt::RightButton) |
181 | { |
182 | // Popup menu: |
183 | |
184 | |
185 | } |
186 | |
187 | // Finish degenerate mode |
188 | setDegenerateMode(false); |
189 | |
190 | emit mouseReleased(mrEvent->modifiers(), mrEvent->x(), mrEvent->y()); |
191 | } |
192 | |
193 | void Viewer::mouseDoubleClickEvent(QMouseEvent * mdcEvent) |
194 | { |
195 | emit mouseDoubleClick(mdcEvent->modifiers(), mdcEvent->x(), mdcEvent->y()); |
196 | } |
197 | |
198 | void Viewer::setDegenerateMode(const bool on) |
199 | { |
200 | AIS_ListOfInteractive displayed; |
201 | myIC->DisplayedObjects(displayed); |
202 | AIS_ListIteratorOfListOfInteractive itri(displayed); |
203 | for (; itri.More(); itri.Next()) |
204 | { |
205 | if (itri.Value()->DynamicType() == STANDARD_TYPE(Voxel_Prs)) |
206 | { |
207 | Handle(Voxel_Prs) prs = Handle(Voxel_Prs)::DownCast(itri.Value()); |
208 | prs->SetDegenerateMode(on); |
209 | myView->Redraw(); |
210 | break; |
211 | } |
212 | } |
213 | } |