0031570: Samples - add Qt samples similar to standard MFC samples
[occt.git] / samples / qt / OCCTOverview / src / DocumentCommon.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of the examples of the Open CASCADE Technology software library.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
21
22 #include "DocumentCommon.h"
23
24 #include "ApplicationCommon.h"
25 #include "Transparency.h"
26
27 #include <Standard_WarningsDisable.hxx>
28 #include <QApplication>
29 #include <QColor>
30 #include <QColorDialog>
31 #include <QStatusBar>
32 #include <Standard_WarningsRestore.hxx>
33
34 #include <AIS_InteractiveObject.hxx>
35 #include <Aspect_DisplayConnection.hxx>
36 #include <Graphic3d_NameOfMaterial.hxx>
37 #include <OpenGl_GraphicDriver.hxx>
38 #include <OSD_Environment.hxx>
39
40 #include <TCollection_AsciiString.hxx>
41
42 // =======================================================================
43 // function : Viewer
44 // purpose  :
45 // =======================================================================
46 Handle(V3d_Viewer) DocumentCommon::Viewer(const Standard_ExtString,
47                                           const Standard_CString,
48                                           const Standard_Real theViewSize,
49                                           const V3d_TypeOfOrientation theViewProj,
50                                           const Standard_Boolean theComputedMode,
51                                           const Standard_Boolean theDefaultComputedMode)
52 {
53   static Handle(OpenGl_GraphicDriver) aGraphicDriver;
54   if (aGraphicDriver.IsNull())
55   {
56     Handle(Aspect_DisplayConnection) aDisplayConnection;
57 #if !defined(_WIN32) && !defined(__WIN32__) && (!defined(__APPLE__) || defined(MACOSX_USE_GLX))
58     aDisplayConnection = new Aspect_DisplayConnection(OSD_Environment("DISPLAY").Value());
59 #endif
60     aGraphicDriver = new OpenGl_GraphicDriver(aDisplayConnection);
61   }
62
63   Handle(V3d_Viewer) aViewer = new V3d_Viewer(aGraphicDriver);
64   aViewer->SetDefaultViewSize(theViewSize);
65   aViewer->SetDefaultViewProj(theViewProj);
66   aViewer->SetComputedMode(theComputedMode);
67   aViewer->SetDefaultComputedMode(theDefaultComputedMode);
68   return aViewer;
69 }
70
71 DocumentCommon::DocumentCommon(ApplicationCommonWindow* theApp)
72 : QObject (theApp),
73   myContextIsEmpty(true)
74 {
75   TCollection_ExtendedString a3DName("Visu3D");
76
77   myViewer = Viewer(a3DName.ToExtString(), "", 1000.0, V3d_XposYnegZpos, Standard_True, Standard_True);
78
79   myViewer->SetDefaultLights();
80   myViewer->SetLightOn();
81
82   myContext = new AIS_InteractiveContext(myViewer);
83 }
84
85 void DocumentCommon::SetObjects (const NCollection_Vector<Handle(AIS_InteractiveObject)>& theObjects,
86                                  Standard_Boolean theDisplayShaded)
87 {
88   myContext->RemoveAll(Standard_False);
89   myContextIsEmpty = theObjects.IsEmpty();
90   for (const Handle(AIS_InteractiveObject) anObject : theObjects)
91   {
92     if (!theDisplayShaded)
93     {
94       myContext->Display(anObject, Standard_False);
95     }
96     else
97     {
98       myContext->Display(anObject, AIS_Shaded, 0, Standard_False);
99     }
100   }
101   myViewer->Redraw();
102 }
103
104 void DocumentCommon::Clear()
105 {
106   myContext->RemoveAll(Standard_True);
107   myContextIsEmpty = true;
108 }