Skip to content

Commit eb15acf

Browse files
committed
Coding - move out Window sample from main.cpp to deducated file
1 parent 6a75084 commit eb15acf

9 files changed

Lines changed: 407 additions & 320 deletions

occt-qopenglwidget/CMakeLists.txt

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

occt-qopenglwidget/main.cpp

Lines changed: 2 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -1,171 +1,15 @@
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-
16913
int 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

Comments
 (0)