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