forked from STOCD/OSCR-UI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
__pycache__ | ||
build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
cmake_minimum_required(VERSION 3.29) | ||
set(NAME "OSCR") | ||
project(${NAME}) | ||
|
||
add_subdirectory(shim) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
find_package(Python COMPONENTS Interpreter Development) | ||
find_package(Qt6 REQUIRED COMPONENTS Core Widgets) | ||
|
||
qt_add_executable(${NAME} main.cpp) | ||
|
||
target_link_libraries(${NAME} PUBLIC Python::Python) | ||
target_link_libraries(${NAME} PUBLIC Qt6::Core) | ||
target_link_libraries(${NAME} PUBLIC Qt6::Widgets) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#include <exception> | ||
#include <iostream> | ||
|
||
#include <Python.h> | ||
#include <QApplication> | ||
#include <QMainWindow> | ||
|
||
// nix-shell -p cmake gdb qt6.full | ||
|
||
class PythonQt { | ||
public: | ||
PythonQt() { | ||
const char *fname = "main.py"; | ||
PyObject *obj = Py_BuildValue("s", fname); | ||
FILE *fp = _Py_fopen_obj(obj, "rb"); | ||
if (NULL == fp) { | ||
throw std::runtime_error("Failed to open main.py"); | ||
} | ||
|
||
int ret = PyRun_SimpleFile(fp, fname); | ||
fclose(fp); | ||
Py_Finalize(); | ||
if (ret) { | ||
PyErr_Print(); | ||
throw std::runtime_error("Failed to run main.py"); | ||
} | ||
} | ||
}; | ||
|
||
static int init_python(int argc, char **argv) { | ||
const char *executable = "venv/bin/python"; | ||
const char *home = "venv"; | ||
size_t size; | ||
PyConfig config; | ||
PyStatus status; | ||
|
||
PyConfig_InitIsolatedConfig(&config); | ||
|
||
status = PyConfig_SetBytesString(&config, &config.executable, executable); | ||
if (PyStatus_Exception(status)) { | ||
throw std::runtime_error("Failed to set executable venv"); | ||
} | ||
|
||
status = PyConfig_SetBytesString(&config, &config.home, home); | ||
if (PyStatus_Exception(status)) { | ||
throw std::runtime_error("Failed to set executable venv"); | ||
} | ||
|
||
Py_InitializeFromConfig(&config); | ||
PyConfig_Clear(&config); | ||
return 0; | ||
} | ||
|
||
static int init_qt(int argc, char **argv) { | ||
QApplication app(argc, argv); | ||
QMainWindow win; | ||
win.setObjectName("OSCR"); | ||
|
||
PythonQt code; | ||
app.exec(); | ||
|
||
return 0; | ||
} | ||
|
||
static int run(int argc, char **argv) { | ||
int ret = 0; | ||
|
||
ret = init_python(argc, argv); | ||
if (ret) { | ||
throw std::runtime_error("Failed to initialize python"); | ||
} | ||
|
||
ret = init_qt(argc, argv); | ||
if (ret) { | ||
throw std::runtime_error("Failed to initialize qt"); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
int ret = 0; | ||
try { | ||
ret = run(argc, argv); | ||
} catch (std::exception &e) { | ||
std::cout << e.what() << std::endl; | ||
} catch (...) { | ||
std::cout << "Failed to parse exception" << std::endl; | ||
} | ||
return ret; | ||
} |