Skip to content

Commit d95d641

Browse files
authored
Merge pull request #54 from solvcon/project/ustmesh
Merge from project/ustmesh
2 parents 68681c7 + 030a6df commit d95d641

File tree

5 files changed

+274
-86
lines changed

5 files changed

+274
-86
lines changed

src/viewer/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ if(NOT DEFINED INSTALL_VIEWERDIR)
1515
endif()
1616

1717
find_package(Qt6 COMPONENTS Core)
18+
find_package(Qt6 COMPONENTS Widgets)
1819
find_package(Qt6 COMPONENTS Gui)
1920
find_package(Qt6 COMPONENTS 3DCore)
2021
find_package(Qt6 COMPONENTS 3DRender)
@@ -23,6 +24,7 @@ find_package(Qt6 COMPONENTS 3DExtras)
2324

2425
qt_add_executable(
2526
viewer
27+
modmesh/viewer/RPythonText.cpp
2628
main.cpp
2729
)
2830

@@ -39,6 +41,8 @@ set_target_properties(
3941

4042
target_link_libraries(
4143
viewer PUBLIC
44+
pybind11::embed
45+
Qt::Widgets
4246
Qt::3DCore
4347
Qt::3DExtras
4448
Qt::3DInput

src/viewer/main.cpp

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@
2626
* POSSIBILITY OF SUCH DAMAGE.
2727
*/
2828

29+
#include <modmesh/python/python.hpp> // Must be the first include.
2930
#include <modmesh/modmesh.hpp>
3031
#include <modmesh/viewer/viewer.hpp>
32+
#include <modmesh/viewer/RPythonText.hpp>
3133

32-
#include <QGuiApplication>
34+
#include <pybind11/embed.h>
3335

34-
#include <qt3dwindow.h>
36+
#include <QApplication>
37+
#include <QMainWindow>
3538

3639
std::shared_ptr<modmesh::StaticMesh2d> make_3triangles()
3740
{
@@ -71,47 +74,61 @@ std::shared_ptr<modmesh::StaticMesh2d> make_3triangles()
7174
int main(int argc, char ** argv)
7275
{
7376
/*
74-
* TODO: Sequence of application startup:
75-
* 1. Parsing arguments and parameters.
77+
* Sequence of application startup:
78+
* 1. (Todo) Parsing arguments and parameters.
7679
* 2. Initialize application globals.
7780
* 3. Initialize GUI globals.
7881
* 4. Set up GUI windowing.
7982
*/
8083

8184
using namespace modmesh;
85+
namespace py = pybind11;
8286

83-
// Start application with GUI.
84-
QGuiApplication app(argc, argv);
87+
// Start the interpreter.
88+
py::scoped_interpreter interpreter_guard{};
8589

86-
// Create and set up main window.
87-
Qt3DExtras::Qt3DWindow window;
90+
// Instantiate the application object.
91+
QApplication app(argc, argv);
92+
QMainWindow main_window;
8893

94+
// Load the Python extension module.
95+
std::cerr << "Loading modmesh._modmesh ... ";
96+
bool load_failure = false;
97+
try
8998
{
90-
// Set up the camera.
91-
Qt3DRender::QCamera * camera = window.camera();
92-
camera->lens()->setPerspectiveProjection(45.0f, 16.0f / 9.0f, 0.1f, 1000.0f);
93-
camera->setPosition(QVector3D(0, 0, 40.0f));
94-
camera->setViewCenter(QVector3D(0, 0, 0));
99+
py::module_::import("modmesh._modmesh");
95100
}
96-
97-
// Create and set up the root scene.
98-
RScene scene;
99-
101+
catch (const py::error_already_set & e)
102+
{
103+
if (std::string::npos == std::string(e.what()).find("ModuleNotFoundError"))
104+
{
105+
throw;
106+
}
107+
else
108+
{
109+
std::cerr << "fails" << std::endl;
110+
load_failure = true;
111+
}
112+
}
113+
if (!load_failure)
100114
{
101-
// Set up the camera control.
102-
auto * control = scene.camera_controller();
103-
control->setCamera(window.camera());
104-
control->setLinearSpeed(50.0f);
105-
control->setLookSpeed(180.0f);
106-
107-
// Set the mesh to the scene.
108-
RStaticMesh<2> rmh(make_3triangles());
109-
rmh->setParent(scene.ptr());
115+
std::cerr << "succeeds" << std::endl;
110116
}
111117

112-
// Show the window.
113-
window.setRootEntity(scene.ptr());
114-
window.show();
118+
// Create and set up main 3D view.
119+
auto * vwidget = new R3DWidget();
120+
new RStaticMesh<2>(make_3triangles(), vwidget->scene());
121+
122+
auto * pydock = new RPythonText(QString("Python"), &main_window);
123+
pydock->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::RightDockWidgetArea);
124+
main_window.addDockWidget(Qt::RightDockWidgetArea, pydock);
125+
126+
// Set up window.
127+
main_window.setCentralWidget(vwidget);
128+
main_window.resize(800, 400);
129+
vwidget->resize(400, 400);
130+
main_window.show();
115131

132+
// Run the application.
116133
return app.exec();
117134
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2022, Yung-Yu Chen <[email protected]>
3+
*
4+
* Redistribution and use in source and binary forms, with or without
5+
* modification, are permitted provided that the following conditions are met:
6+
*
7+
* - Redistributions of source code must retain the above copyright notice,
8+
* this list of conditions and the following disclaimer.
9+
* - Redistributions in binary form must reproduce the above copyright notice,
10+
* this list of conditions and the following disclaimer in the documentation
11+
* and/or other materials provided with the distribution.
12+
* - Neither the name of the copyright holder nor the names of its contributors
13+
* may be used to endorse or promote products derived from this software
14+
* without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*/
28+
29+
#include "RPythonText.hpp"
30+
31+
namespace modmesh
32+
{
33+
34+
RPythonText::RPythonText(
35+
QString const & title,
36+
QWidget * parent,
37+
Qt::WindowFlags flags)
38+
: QDockWidget(title, parent, flags)
39+
{
40+
setUp();
41+
}
42+
43+
void RPythonText::setUp()
44+
{
45+
m_text = new QTextEdit();
46+
m_text->setFont(QFont("Courier New"));
47+
m_run = new QPushButton(QString("run"));
48+
49+
m_layout = new QVBoxLayout();
50+
m_layout->addWidget(m_text);
51+
m_layout->addWidget(m_run);
52+
m_widget = new QWidget();
53+
m_widget->setLayout(m_layout);
54+
55+
setWidget(m_widget);
56+
57+
connect(m_run, &QPushButton::clicked, this, &RPythonText::runPythonCode);
58+
}
59+
60+
void RPythonText::runPythonCode()
61+
{
62+
namespace py = pybind11;
63+
64+
std::string code = m_text->toPlainText().toStdString();
65+
try
66+
{
67+
py::exec(code);
68+
}
69+
catch (const py::error_already_set & e)
70+
{
71+
std::cerr << e.what() << std::endl;
72+
}
73+
}
74+
75+
} /* end namespace modmesh */
76+
77+
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#pragma once
2+
3+
/*
4+
* Copyright (c) 2022, Yung-Yu Chen <[email protected]>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* - Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* - Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* - Neither the name of the copyright holder nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <modmesh/python/python.hpp> // Must be the first include.
32+
#include <pybind11/embed.h>
33+
34+
#include <Qt>
35+
#include <QDockWidget>
36+
37+
#include <QTextEdit>
38+
#include <QPushButton>
39+
#include <QVBoxLayout>
40+
41+
namespace modmesh
42+
{
43+
44+
class RPythonText
45+
: public QDockWidget
46+
{
47+
48+
public:
49+
50+
RPythonText(
51+
QString const & title = "Python",
52+
QWidget * parent = nullptr,
53+
Qt::WindowFlags flags = Qt::WindowFlags());
54+
55+
private:
56+
57+
void setUp();
58+
59+
void runPythonCode();
60+
61+
QTextEdit * m_text;
62+
QPushButton * m_run;
63+
QVBoxLayout * m_layout;
64+
QWidget * m_widget;
65+
66+
}; /* end class RPythonText */
67+
68+
} /* end namespace modmesh */
69+
70+
// vim: set ff=unix fenc=utf8 et sw=4 ts=4 sts=4:

0 commit comments

Comments
 (0)