0026741: Problem with building samples and demo
[occt.git] / samples / qt / VoxelDemo / src / Viewer.cpp
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
12 #include <Voxel_Prs.hxx>
13 #include <AIS_ListOfInteractive.hxx>
14 #include <AIS_ListIteratorOfListOfInteractive.hxx>
15 #include <Aspect_DisplayConnection.hxx>
16 #include <OpenGl_GraphicDriver.hxx>
17 #include <V3d_DirectionalLight.hxx>
18 #include <V3d_AmbientLight.hxx>
19
20 static Handle(Graphic3d_GraphicDriver) Viewer_aGraphicDriver;
21
22 Viewer::Viewer(QWidget* parent):QWidget(parent)
23 {
24     if (myGraphicDriver.IsNull())
25     {
26       if (Viewer_aGraphicDriver.IsNull())
27       {
28         Handle(Aspect_DisplayConnection) aDisplayConnection;
29         Viewer_aGraphicDriver = new OpenGl_GraphicDriver (aDisplayConnection);
30       }
31       myGraphicDriver = Handle(OpenGl_GraphicDriver)::DownCast(Viewer_aGraphicDriver);
32     }
33
34         Handle(V3d_Viewer) aViewer = new V3d_Viewer(myGraphicDriver, TCollection_ExtendedString("Visu3D").ToExtString(), "",
35                                                 1000, V3d_XposYnegZpos,
36                                                 Quantity_NOC_GRAY30, V3d_ZBUFFER, V3d_GOURAUD, V3d_WAIT,
37                                                 true, true, V3d_TEX_NONE);
38
39         aViewer->SetDefaultBackgroundColor(Quantity_NOC_BLACK);
40
41     myView = aViewer->CreateView();
42     myIC = new AIS_InteractiveContext(aViewer);
43     myIC->SetDeviationCoefficient(1.e-3);
44
45     Aspect_Handle aWindowHandle = (Aspect_Handle )winId();
46     Handle(WNT_Window) hWnd = new WNT_Window (aWindowHandle);
47
48     myView->SetWindow(hWnd);
49     if(!hWnd->IsMapped())
50         hWnd->Map();
51
52     myView->MustBeResized();
53     myView->SetSurfaceDetail(V3d_TEX_NONE);
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);
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 );
75
76     aViewer->SetLightOn(new V3d_DirectionalLight(aViewer, V3d_XnegYnegZneg, Quantity_NOC_WHITE, Standard_True));
77     aViewer->SetLightOn(new V3d_AmbientLight(aViewer, Quantity_NOC_WHITE));
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
92 /*!
93   Get paint engine for the OpenGL viewer. [ virtual public ]
94 */
95 QPaintEngine* Viewer::paintEngine() const
96 {
97   return 0;
98 }
99
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         Handle(Voxel_Prs) prs = Handle(Voxel_Prs)::DownCast(itri.Value());
206         if (!prs.IsNull())
207         {
208             prs->SetDegenerateMode(on);
209             myView->Redraw();
210             break;
211         }
212     }
213 }