11// Copyright (c) 2025 Kirill Gavrilov
22
3- #include " OcctQOpenGLWidgetViewer .h"
3+ #include " OcctQMainWindowSample .h"
44
55#include " ../occt-qt-tools/OcctQtTools.h"
66
77#include < Standard_WarningsDisable.hxx>
88#include < QApplication>
9- #include < QSurfaceFormat>
10-
11- #include < QAction>
12- #include < QLabel>
13- #include < QMainWindow>
14- #include < QMenuBar>
15- #include < QMessageBox>
16- #include < QVBoxLayout>
17- #include < QPushButton>
18- #include < QSlider>
199#include < Standard_WarningsRestore.hxx>
2010
2111#include < Standard_Version.hxx>
2212
23- // ! Main application window.
24- class MyMainWindow : public QMainWindow
25- {
26- OcctQOpenGLWidgetViewer* myViewer = nullptr ;
27-
28- public:
29- // ! Window constructor.
30- MyMainWindow ()
31- {
32- // 3D Viewer widget as a central widget
33- myViewer = new OcctQOpenGLWidgetViewer ();
34- setCentralWidget (myViewer);
35-
36- // menu bar
37- createMenuBar ();
38-
39- // some controls on top of 3D Viewer
40- createLayoutOverViewer ();
41- }
42-
43- private:
44- // ! Define menu bar with Quit item.
45- void createMenuBar ()
46- {
47- QMenuBar* aMenuBar = new QMenuBar ();
48- QMenu* aMenuWindow = aMenuBar->addMenu (" &File" );
49- #if (OCC_VERSION_HEX >= 0x070700)
50- {
51- QAction* anActionSplit = new QAction (aMenuWindow);
52- anActionSplit->setText (" Split Views" );
53- aMenuWindow->addAction (anActionSplit);
54- connect (anActionSplit, &QAction::triggered, [this ]() { splitSubviews (); });
55- }
56- #endif
57- {
58- QAction* anActionQuit = new QAction (aMenuWindow);
59- anActionQuit->setText (" Quit" );
60- aMenuWindow->addAction (anActionQuit);
61- connect (anActionQuit, &QAction::triggered, [this ]() { close (); });
62- }
63- setMenuBar (aMenuBar);
64- }
65-
66- // ! Define controls over 3D viewer.
67- void createLayoutOverViewer ()
68- {
69- QVBoxLayout* aLayout = new QVBoxLayout (myViewer);
70- aLayout->setDirection (QBoxLayout::BottomToTop);
71- aLayout->setAlignment (Qt::AlignBottom);
72- {
73- // button displaying message window with OpenGL info
74- QPushButton* aQuitBtn = new QPushButton (" About" );
75- aLayout->addWidget (aQuitBtn);
76- connect (aQuitBtn, &QPushButton::clicked, [this ]() {
77- QMessageBox::information (0 ,
78- " About Sample" ,
79- QString () + " OCCT 3D Viewer sample embedded into Qt Widgets.\n\n "
80- + " Open CASCADE Technology v." OCC_VERSION_STRING_EXT " \n "
81- + " Qt v." QT_VERSION_STR " \n\n " + " OpenGL info:\n " + myViewer->getGlInfo ());
82- });
83- }
84- {
85- // slider changing viewer background color
86-
87- // the widgets on top of OCCT 3D Viewer (implemented as QOpenGLWidget) might have transparent background
88- QWidget* aSliderBox = new QWidget ();
89- aSliderBox->setStyleSheet (" QWidget { background-color: rgba(0, 0, 0, 0); }" );
90-
91- QHBoxLayout* aSliderLayout = new QHBoxLayout (aSliderBox);
92- {
93- QLabel* aSliderLabel = new QLabel (" Background" );
94- aSliderLabel->setStyleSheet (" QLabel { background-color: rgba(0, 0, 0, 0); color: white; }" );
95- aSliderLabel->setGeometry (50 , 50 , 50 , 50 );
96- aSliderLabel->adjustSize ();
97- aSliderLayout->addWidget (aSliderLabel);
98- }
99- {
100- QSlider* aSlider = new QSlider (Qt::Horizontal);
101- aSlider->setRange (0 , 255 );
102- aSlider->setSingleStep (1 );
103- aSlider->setPageStep (15 );
104- aSlider->setTickInterval (15 );
105- aSlider->setTickPosition (QSlider::TicksRight);
106- aSlider->setValue (0 );
107- aSliderLayout->addWidget (aSlider);
108- connect (aSlider, &QSlider::valueChanged, [this ](int theValue) {
109- const float aVal = theValue / 255 .0f ;
110- const Quantity_Color aColor (aVal, aVal, aVal, Quantity_TOC_sRGB);
111- #if (OCC_VERSION_HEX >= 0x070700)
112- for (const Handle (V3d_View)& aSubviewIter : myViewer->View ()->Subviews ())
113- {
114- aSubviewIter->SetBgGradientColors (aColor, Quantity_NOC_BLACK, Aspect_GradientFillMethod_Elliptical);
115- aSubviewIter->Invalidate ();
116- }
117- #endif
118- // myViewer->View()->SetBackgroundColor(aColor);
119- myViewer->View ()->SetBgGradientColors (aColor, Quantity_NOC_BLACK, Aspect_GradientFillMethod_Elliptical);
120- myViewer->View ()->Invalidate ();
121- myViewer->update ();
122- });
123- }
124- aLayout->addWidget (aSliderBox);
125- }
126- }
127-
128- #if (OCC_VERSION_HEX >= 0x070700)
129- // ! Advanced method splitting 3D Viewer into sub-views.
130- void splitSubviews ()
131- {
132- if (!myViewer->View ()->Subviews ().IsEmpty ())
133- {
134- // remove subviews
135- myViewer->View ()->View ()->SetSubviewComposer (false );
136- NCollection_Sequence<Handle (V3d_View)> aSubviews = myViewer->View ()->Subviews ();
137- for (const Handle (V3d_View)& aSubviewIter : aSubviews)
138- aSubviewIter->Remove ();
139-
140- myViewer->OnSubviewChanged (myViewer->Context (), nullptr , myViewer->View ());
141- }
142- else
143- {
144- // create two subviews splitting window horizontally
145- myViewer->View ()->View ()->SetSubviewComposer (true );
146-
147- Handle (V3d_View) aSubView1 = new V3d_View (myViewer->Viewer ());
148- aSubView1->SetImmediateUpdate (false );
149- aSubView1->SetWindow (myViewer->View (),
150- Graphic3d_Vec2d (0.5 , 1.0 ),
151- Aspect_TOTP_LEFT_UPPER,
152- Graphic3d_Vec2d (0.0 , 0.0 ));
153-
154- Handle (V3d_View) aSubView2 = new V3d_View (myViewer->Viewer ());
155- aSubView2->SetImmediateUpdate (false );
156- aSubView2->SetWindow (myViewer->View (),
157- Graphic3d_Vec2d (0.5 , 1.0 ),
158- Aspect_TOTP_LEFT_UPPER,
159- Graphic3d_Vec2d (0.5 , 0.0 ));
160-
161- myViewer->OnSubviewChanged (myViewer->Context (), nullptr , aSubView1);
162- }
163- myViewer->View ()->Invalidate ();
164- myViewer->update ();
165- }
166- #endif
167- };
168-
16913int main (int theNbArgs, char ** theArgVec)
17014{
17115 // before creating QApplication: define platform plugin to load (e.g. xcb on Linux)
@@ -177,7 +21,7 @@ int main(int theNbArgs, char** theArgVec)
17721 QCoreApplication::setOrganizationName (" OpenCASCADE" );
17822 QCoreApplication::setApplicationVersion (OCC_VERSION_STRING_EXT);
17923
180- MyMainWindow aMainWindow;
24+ OcctQMainWindowSample aMainWindow;
18125 aMainWindow.resize (aMainWindow.sizeHint ());
18226 aMainWindow.show ();
18327 return aQApp.exec ();
0 commit comments