Skip to content

Commit 8e92b28

Browse files
committed
Coding - improve QtTools documentation
1 parent ba63779 commit 8e92b28

9 files changed

Lines changed: 164 additions & 54 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ cmake_minimum_required (VERSION 3.13)
22

33
# common solution for multiple independent projects
44
project (occt-qt)
5-
5+
set_property (GLOBAL PROPERTY USE_FOLDERS ON)
6+
add_subdirectory (occt-qt-tools)
67
add_subdirectory (occt-qwidget)
78
add_subdirectory (occt-qopenglwidget)
89
add_subdirectory (occt-qtquick)

ReadMe.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
OCCT 3D Viewer setup within Qt
22
==============================
33

4-
This sample demonstrates how to use Open CASCADE Technology (OCCT) 3D Viewer within Qt5 GUI framework on Windows and Linux platforms.
4+
The samples in this repository demonstrate Open CASCADE Technology (OCCT) 3D Viewer setup
5+
within Qt GUI framework on Windows and Linux platforms.
56

67
![sample screenshot](/images/occt-qopenglwidget-sample-wnt.png)
78

@@ -10,7 +11,7 @@ This sample demonstrates how to use Open CASCADE Technology (OCCT) 3D Viewer wit
1011
Use *CMake* for building the project.
1112
See `CMakeLists.txt` in the folder defining building rules.
1213
Use `cmake-gui` or command-line interface for configuring project dependencies (Qt, OCCT).
13-
Building has been checked within OCCT 7.6.0.
14+
Building has been checked within OCCT 7.7.0.
1415

1516
Make sure to copy necessary dependencies (*OCCT*, *FreeType*, *FreeImage*, etc. - depending on *OCCT* building configuration)
1617
into sample folder or configuring environment (`PATH`, `LD_LIBRARY_PATH`).
@@ -19,19 +20,36 @@ into sample folder or configuring environment (`PATH`, `LD_LIBRARY_PATH`).
1920
*which could be used as alternative to CMake.*
2021
*Create `custom.pri` from `custom.pri.template` with filled in paths to OCCT.*
2122

23+
## OCCT Qt tools
24+
25+
This is a pseudo project providing source code for gluing between Qt and OCCT:
26+
- `OcctQtTools`
27+
- conversion between common Qt and OCCT structures like colors and strings;
28+
- Qt application setup for embedding 3D viewer;
29+
- Qt input events conversion into OCCT 3D Viewer events.
30+
- `OcctGlTools` - common tools (independent from Qt) for wrapping externally created OpenGL context to setup OCCT 3D Viewer.
31+
32+
Each Qt sample in the list below is defined independently
33+
so that it could be easily extracted to define your own application based on selected Qt module
34+
without bloating project with unnecessary code and dependencies.
35+
However, this leads to some code duplication in samples, which `OcctQtTools` tries to reduce.
36+
2237
## OCCT QWidget sample
2338

24-
Project within `occt-qwidget` subfolder shows initialization of OCCT 3D viewer using
25-
basic `QWidget` with own native window handle (`QWidget::setAttribute(Qt::WA_NativeWindow)` attribute).
39+
Project within `occt-qwidget` subfolder shows OCCT 3D viewer setup within
40+
basic `QWidget` from native window handle (`QWidget::setAttribute(Qt::WA_NativeWindow)` attribute).
2641

27-
Within this approach, OCCT will be responsible to initialize OpenGL / OpenGL ES context on its own.
42+
Within this approach, OCCT will be responsible to initialize OpenGL / OpenGL ES context on its own (Qt will be unaware of it).
2843
Unlike `QOpenGLWidget` sample, the widgets on top of `QWidget` holding 3D Viewer should have solid background color,
2944
as Qt will be unable to blend widgets together on its own.
3045

46+
* Notice: `QOpenGLWidget` (see next) is a preferred way for integrating OpenGL viewer into Qt Widgets application.
47+
* This `QWidget` sample demonstrates a working approach commonly used by applications based on Qt3/Qt4 and other GUI frameworks.
48+
3149
## OCCT QOpenGLWidget sample
3250

33-
Project within `occt-qopenglwidget` subfolder shows initialization of OCCT 3D viewer
34-
from OpenGL context created by `QOpenGLWidget` within Qt5 Widgets application.
51+
Project within `occt-qopenglwidget` subfolder shows OCCT 3D viewer setup
52+
from *OpenGL* context created by `QOpenGLWidget` within Qt Widgets application.
3553

3654
The approach with `QOpenGLWidget` requires careful gluing layer for Qt and OCCT 3D Viewer to share common OpenGL context.
3755
Unlike `QWidget` sample, the widgets on top of `QOpenGLWidget` holding 3D Viewer might have semitransparent background color,
@@ -41,8 +59,8 @@ as Qt will be able to blend widgets together on its own.
4159

4260
## OCCT QtQuick/QML sample
4361

44-
Project within `occt-qtquick` subfolder shows initialization of OCCT 3D viewer
45-
from OpenGL context created by `QQuickFramebufferObject` within Qt5 QtQuick/QML application.
62+
Project within `occt-qtquick` subfolder shows OCCT 3D viewer setup
63+
from *OpenGL* context created by `QQuickFramebufferObject` within Qt5 QtQuick/QML application.
4664

4765
The approach with `QQuickFramebufferObject` requires careful gluing layer for Qt and OCCT 3D Viewer to share common OpenGL context.
4866

occt-qopenglwidget/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ add_executable (${PROJECT_NAME}
8989
OcctQOpenGLWidgetViewer.h
9090
OcctQOpenGLWidgetViewer.cpp
9191
)
92+
set_target_properties (${PROJECT_NAME} PROPERTIES FOLDER "Qt Widgets")
9293
target_link_libraries (${PROJECT_NAME} PRIVATE ${QT_VERSION}::${QT_COMPONENT_OPENGLWIDGETS} ${OpenCASCADE_LIBS} ${OPENGL_LIBRARIES})
9394

9495
# auxiliary development environment

occt-qopenglwidget/occt-qopenglwidget-sample.pro

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ CONFIG += console
55

66
# source code of the sample
77
HEADERS = OcctQOpenGLWidgetViewer.h \
8-
OcctGlTools.h
8+
../occt-qt-tools/OcctQtTools.h
9+
../occt-qt-tools/OcctGlTools.h
910
SOURCES = main.cpp \
10-
OcctQOpenGLWidgetViewer.cpp \
11-
OcctGlTools.cpp
12-
OTHER_FILES = LICENSE.md\
13-
ReadMe.md \
14-
custom.pri.template
11+
OcctQOpenGLWidgetViewer.cpp \
12+
../occt-qt-tools/OcctQtTools.cpp \
13+
../occt-qt-tools/OcctGlTools.cpp
14+
OTHER_FILES = ../LICENSE.md\
15+
../ReadMe.md \
16+
custom.pri.template
1517

1618
# target configuration
1719
MY_TARGET_ABI = $${QMAKE_TARGET.arch}

occt-qt-tools/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
cmake_minimum_required (VERSION 3.13)
2+
3+
# dummy project - just to collect files in folder
4+
project (occt-qt-tools)
5+
add_custom_target (${PROJECT_NAME} SOURCES
6+
OcctQtTools.h
7+
OcctQtTools.cpp
8+
OcctGlTools.h
9+
OcctGlTools.cpp
10+
../ReadMe.md
11+
)
12+
set_target_properties (${PROJECT_NAME} PROPERTIES FOLDER "Tools")

occt-qt-tools/OcctQtTools.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,56 @@ QColor OcctQtTools::qtColorFromOcct(const Quantity_Color& theColor)
3030
return QColor((int)Round(anRgb.r() * 255.0), (int)Round(anRgb.g() * 255.0), (int)Round(anRgb.b() * 255.0));
3131
}
3232

33+
// ================================================================
34+
// Function : qtColorToOcctRgba
35+
// ================================================================
36+
Quantity_ColorRGBA OcctQtTools::qtColorToOcctRgba(const QColor& theColor)
37+
{
38+
return Quantity_ColorRGBA(OcctQtTools::qtColorToOcct(theColor), theColor.alpha() / 255.0f);
39+
}
40+
41+
// ================================================================
42+
// Function : qtColorFromOcctRgba
43+
// ================================================================
44+
QColor OcctQtTools::qtColorFromOcctRgba(const Quantity_ColorRGBA& theColor)
45+
{
46+
QColor aColor = OcctQtTools::qtColorFromOcct(theColor.GetRGB());
47+
aColor.setAlpha((int)Round(theColor.Alpha() * 255.0));
48+
return aColor;
49+
}
50+
51+
// ================================================================
52+
// Function : qtStringToOcct
53+
// ================================================================
54+
TCollection_AsciiString OcctQtTools::qtStringToOcct(const QString& theText)
55+
{
56+
return TCollection_AsciiString(theText.toUtf8().data());
57+
}
58+
59+
// ================================================================
60+
// Function : qtStringFromOcct
61+
// ================================================================
62+
QString OcctQtTools::qtStringFromOcct(const TCollection_AsciiString& theText)
63+
{
64+
return QString::fromUtf8(theText.ToCString());
65+
}
66+
67+
// ================================================================
68+
// Function : qtStringToOcctExt
69+
// ================================================================
70+
TCollection_ExtendedString OcctQtTools::qtStringToOcctExt(const QString& theText)
71+
{
72+
return TCollection_ExtendedString(theText.toUtf8().data(), true);
73+
}
74+
75+
// ================================================================
76+
// Function : qtStringFromOcctExt
77+
// ================================================================
78+
QString OcctQtTools::qtStringFromOcctExt(const TCollection_ExtendedString& theText)
79+
{
80+
return QString::fromUtf16(theText.ToExtString());
81+
}
82+
3383
// ================================================================
3484
// Function : qtGlPlatformSetup
3585
// ================================================================

occt-qt-tools/OcctQtTools.h

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#define _OcctQtTools_HeaderFile
55

66
#include <Aspect_WindowInputListener.hxx>
7-
#include <Quantity_Color.hxx>
7+
#include <Quantity_ColorRGBA.hxx>
88

99
#include <Standard_WarningsDisable.hxx>
1010
#include <QColor>
@@ -16,55 +16,79 @@ class OpenGl_Caps;
1616
class V3d_View;
1717

1818
//! Auxiliary tools between Qt and OCCT definitions.
19-
namespace OcctQtTools
19+
class OcctQtTools
2020
{
21-
//! Perform global Qt platform setup - to be called before QApplication creation.
22-
//! Defines platform plugin to load (e.g. xcb on Linux)
23-
//! and graphic driver (e.g. desktop OpenGL with desired profile/surface).
24-
void qtGlPlatformSetup();
21+
public: //! @name Qt application pre-setup for OCCT 3D Viewer integration
2522

26-
//! Return default Qt surface format for GL context.
27-
QSurfaceFormat qtGlSurfaceFormat(QSurfaceFormat::OpenGLContextProfile theProfile = QSurfaceFormat::NoProfile,
28-
bool theToDebug = false);
23+
//! Perform global Qt platform setup - to be called before QApplication creation.
24+
//! Defines platform plugin to load (e.g. xcb on Linux)
25+
//! and graphic driver (e.g. desktop OpenGL with desired profile/surface).
26+
static void qtGlPlatformSetup();
2927

30-
//! Fill in OCCT GL caps from Qt surface format.
31-
void qtGlCapsFromSurfaceFormat(OpenGl_Caps& theCaps, const QSurfaceFormat& theFormat);
28+
//! Define default Qt surface format for GL context.
29+
static QSurfaceFormat qtGlSurfaceFormat(QSurfaceFormat::OpenGLContextProfile theProfile = QSurfaceFormat::NoProfile,
30+
bool theToDebug = false);
3231

33-
//! Map QColor into Quantity_Color.
34-
Quantity_Color qtColorToOcct(const QColor& theColor);
32+
//! Fill in OCCT GL caps from Qt surface format.
33+
static void qtGlCapsFromSurfaceFormat(OpenGl_Caps& theCaps, const QSurfaceFormat& theFormat);
3534

36-
//! Map Quantity_Color into QColor.
37-
QColor qtColorFromOcct(const Quantity_Color& theColor);
35+
public: //! @name common conversion tools
3836

39-
//! Map Qt buttons bitmask to virtual keys.
40-
Aspect_VKeyMouse qtMouseButtons2VKeys(Qt::MouseButtons theButtons);
37+
//! Map QColor into Quantity_Color.
38+
static Quantity_Color qtColorToOcct(const QColor& theColor);
4139

42-
//! Map Qt mouse modifiers bitmask to virtual keys.
43-
Aspect_VKeyFlags qtMouseModifiers2VKeys(Qt::KeyboardModifiers theModifiers);
40+
//! Map Quantity_Color into QColor.
41+
static QColor qtColorFromOcct(const Quantity_Color& theColor);
4442

45-
//! Map Qt key to virtual key.
46-
Aspect_VKey qtKey2VKey(int theKey);
43+
//! Map QColor into Quantity_ColorRGBA.
44+
static Quantity_ColorRGBA qtColorToOcctRgba(const QColor& theColor);
4745

48-
//! Queue Qt mouse hover event to OCCT listener.
49-
bool qtHandleHoverEvent(Aspect_WindowInputListener& theListener,
50-
const Handle(V3d_View)& theView,
51-
const QHoverEvent* theEvent);
46+
//! Map Quantity_ColorRGBA into QColor.
47+
static QColor qtColorFromOcctRgba(const Quantity_ColorRGBA& theColor);
5248

53-
//! Queue Qt mouse event to OCCT listener.
54-
bool qtHandleMouseEvent(Aspect_WindowInputListener& theListener,
55-
const Handle(V3d_View)& theView,
56-
const QMouseEvent* theEvent);
49+
//! Map QString into TCollection_AsciiString (UTF-8).
50+
static TCollection_AsciiString qtStringToOcct(const QString& theText);
5751

58-
//! Queue Qt mouse wheel event to OCCT listener.
59-
bool qtHandleWheelEvent(Aspect_WindowInputListener& theListener,
60-
const Handle(V3d_View)& theView,
61-
const QWheelEvent* theEvent);
52+
//! Map TCollection_AsciiString (UTF-8) into QString.
53+
static QString qtStringFromOcct(const TCollection_AsciiString& theText);
6254

63-
//! Queue Qt touch event to OCCT listener.
64-
bool qtHandleTouchEvent(Aspect_WindowInputListener& theListener,
65-
const Handle(V3d_View)& theView,
66-
const QTouchEvent* theEvent);
55+
//! Map QString into TCollection_ExtendedString (UTF-16).
56+
static TCollection_ExtendedString qtStringToOcctExt(const QString& theText);
6757

68-
} // namespace OcctQtTools
58+
//! Map TCollection_ExtendedString (UTF-16) into QString.
59+
static QString qtStringFromOcctExt(const TCollection_ExtendedString& theText);
60+
61+
public: //! @name methods for wrapping Qt input events into Aspect_WindowInputListener events
62+
63+
//! Queue Qt mouse hover event to OCCT listener.
64+
static bool qtHandleHoverEvent(Aspect_WindowInputListener& theListener,
65+
const Handle(V3d_View)& theView,
66+
const QHoverEvent* theEvent);
67+
68+
//! Queue Qt mouse event to OCCT listener.
69+
static bool qtHandleMouseEvent(Aspect_WindowInputListener& theListener,
70+
const Handle(V3d_View)& theView,
71+
const QMouseEvent* theEvent);
72+
73+
//! Queue Qt mouse wheel event to OCCT listener.
74+
static bool qtHandleWheelEvent(Aspect_WindowInputListener& theListener,
75+
const Handle(V3d_View)& theView,
76+
const QWheelEvent* theEvent);
77+
78+
//! Queue Qt touch event to OCCT listener.
79+
static bool qtHandleTouchEvent(Aspect_WindowInputListener& theListener,
80+
const Handle(V3d_View)& theView,
81+
const QTouchEvent* theEvent);
82+
83+
//! Map Qt buttons bitmask to virtual keys.
84+
static Aspect_VKeyMouse qtMouseButtons2VKeys(Qt::MouseButtons theButtons);
85+
86+
//! Map Qt mouse modifiers bitmask to virtual keys.
87+
static Aspect_VKeyFlags qtMouseModifiers2VKeys(Qt::KeyboardModifiers theModifiers);
88+
89+
//! Map Qt key to virtual key.
90+
static Aspect_VKey qtKey2VKey(int theKey);
91+
92+
};
6993

7094
#endif // _OcctQtTools_HeaderFile

occt-qtquick/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ add_executable (${PROJECT_NAME}
9292
OcctQQuickFramebufferViewer.cpp
9393
occt-qtquick.qrc
9494
)
95+
set_target_properties (${PROJECT_NAME} PROPERTIES FOLDER "Qt Quick")
9596
target_link_libraries (${PROJECT_NAME} PRIVATE
9697
${Qt_LIBS}
9798
${OpenCASCADE_LIBS}

occt-qwidget/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ add_executable (${PROJECT_NAME}
8383
OcctQWidgetViewer.h
8484
OcctQWidgetViewer.cpp
8585
)
86+
set_target_properties (${PROJECT_NAME} PROPERTIES FOLDER "Qt Widgets")
8687
target_link_libraries (${PROJECT_NAME} PRIVATE ${QT_VERSION}::Widgets ${OpenCASCADE_LIBS} ${OPENGL_LIBRARIES})
8788

8889
# auxiliary development environment

0 commit comments

Comments
 (0)