Skip to content

Commit e943272

Browse files
committed
🧪 test(Wrapper): Wrapper for sip and shiboken
1 parent a4adf87 commit e943272

17 files changed

+1052
-0
lines changed

.gitignore

+760
Large diffs are not rendered by default.

Test/WigglyWidget/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(WigglyWidget LANGUAGES CXX)
4+
5+
add_subdirectory(LibWigglyWidget)
6+
add_subdirectory(TestWigglyWidget)
7+
add_subdirectory(PyQtWrapper)
8+
add_subdirectory(PySideWrapper)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(LibWigglyWidget LANGUAGES CXX)
4+
5+
set(CMAKE_AUTOUIC ON)
6+
set(CMAKE_AUTOMOC ON)
7+
set(CMAKE_AUTORCC ON)
8+
set(CMAKE_CXX_STANDARD 17)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
11+
# 配置安装目录
12+
set(CMAKE_INSTALL_DIR "${CMAKE_SOURCE_DIR}/dist")
13+
14+
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
15+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
16+
17+
add_library(LibWigglyWidget SHARED WigglyWidget_global.h wigglywidget.cpp
18+
wigglywidget.h)
19+
20+
target_link_libraries(LibWigglyWidget PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)
21+
22+
target_compile_definitions(LibWigglyWidget PRIVATE LIBWIGGLYWIDGET_LIBRARY)
23+
24+
# 配置生成的文件安装目录
25+
install(
26+
TARGETS ${PROJECT_NAME}
27+
RUNTIME DESTINATION "${CMAKE_INSTALL_DIR}/bin"
28+
LIBRARY DESTINATION "${CMAKE_INSTALL_DIR}/lib"
29+
ARCHIVE DESTINATION "${CMAKE_INSTALL_DIR}/lib")
30+
31+
install(FILES wigglywidget.h DESTINATION "${CMAKE_INSTALL_DIR}/include")
32+
33+
message(
34+
NOTICE
35+
"****${Qt5Core_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS}"
36+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef WIGGLYWIDGET_GLOBAL_H
2+
#define WIGGLYWIDGET_GLOBAL_H
3+
4+
#include <QtCore/qglobal.h>
5+
6+
#if defined(WIGGLYWIDGET_LIBRARY)
7+
#define WIGGLYWIDGET_EXPORT Q_DECL_EXPORT
8+
#else
9+
#define WIGGLYWIDGET_EXPORT Q_DECL_IMPORT
10+
#endif
11+
12+
#endif // WIGGLYWIDGET_GLOBAL_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include "wigglywidget.h"
2+
3+
#include <QFontMetrics>
4+
#include <QPainter>
5+
#include <QTimerEvent>
6+
7+
WigglyWidget::WigglyWidget(QWidget *parent) : QWidget(parent), step(0) {
8+
setBackgroundRole(QPalette::Midlight);
9+
setAutoFillBackground(true);
10+
11+
QFont newFont = font();
12+
newFont.setPointSize(newFont.pointSize() + 20);
13+
setFont(newFont);
14+
15+
timer.start(60, this);
16+
}
17+
18+
void WigglyWidget::setText(const QString &newText) { text = newText; }
19+
20+
void WigglyWidget::paintEvent(QPaintEvent * /* event */)
21+
22+
{
23+
static constexpr int sineTable[16] = {0, 38, 71, 92, 100, 92, 71, 38,
24+
0, -38, -71, -92, -100, -92, -71, -38};
25+
26+
QFontMetrics metrics(font());
27+
int x = (width() - metrics.horizontalAdvance(text)) / 2;
28+
int y = (height() + metrics.ascent() - metrics.descent()) / 2;
29+
QColor color;
30+
31+
QPainter painter(this);
32+
33+
for (int i = 0; i < text.size(); ++i) {
34+
int index = (step + i) % 16;
35+
color.setHsv((15 - index) * 16, 255, 191);
36+
painter.setPen(color);
37+
painter.drawText(x, y - ((sineTable[index] * metrics.height()) / 400),
38+
QString(text[i]));
39+
x += metrics.horizontalAdvance(text[i]);
40+
}
41+
}
42+
43+
void WigglyWidget::timerEvent(QTimerEvent *event) {
44+
if (event->timerId() == timer.timerId()) {
45+
++step;
46+
update();
47+
} else {
48+
QWidget::timerEvent(event);
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef WIGGLYWIDGET_H
2+
#define WIGGLYWIDGET_H
3+
4+
#include <QBasicTimer>
5+
#include <QWidget>
6+
7+
#include "WigglyWidget_global.h"
8+
9+
class WIGGLYWIDGET_EXPORT WigglyWidget : public QWidget {
10+
Q_OBJECT
11+
12+
public:
13+
WigglyWidget(QWidget *parent = nullptr);
14+
15+
public slots:
16+
void setText(const QString &newText);
17+
18+
protected:
19+
virtual void paintEvent(QPaintEvent *event) override;
20+
virtual void timerEvent(QTimerEvent *event) override;
21+
22+
private:
23+
QBasicTimer timer;
24+
QString text;
25+
int step;
26+
};
27+
28+
#endif // WIGGLYWIDGET_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(PyQtWrapper)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.14)
2+
3+
project(PySideWrapper)
4+
5+
set(BINDINGS_HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/bindings.h")
6+
set(BINDINGS_TYPESYSTEM_FILE "${CMAKE_CURRENT_SOURCE_DIR}/bindings.xml")
7+
set(BINDINGS_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dist")
8+
set(BINDINGS_INCLUDE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../dist/include/")
9+
10+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/bindings.txt.in"
11+
"${CMAKE_CURRENT_SOURCE_DIR}/bindings.txt")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
Created on 2024/04/26
6+
@author: Irony
7+
@site: https://pyqt.site | https://github.com/PyQt5
8+
9+
@file: TestWigglyWidget.py
10+
@description:
11+
"""
12+
13+
import os
14+
import sys
15+
16+
17+
from PySide2.QtWidgets import QApplication, QLineEdit, QVBoxLayout, QWidget
18+
from PySide2.WigglyWidget import WigglyWidget
19+
20+
class TestWigglyWidget(QWidget):
21+
def __init__(self, *args, **kwargs):
22+
super(TestWigglyWidget, self).__init__(*args, **kwargs)
23+
self._layout = QVBoxLayout(self)
24+
self._lineEdit = QLineEdit(self)
25+
self._wigglyWidget = WigglyWidget(self)
26+
self._layout.addWidget(self._lineEdit)
27+
self._layout.addWidget(self._wigglyWidget)
28+
29+
self._lineEdit.textChanged.connect(self._wigglyWidget.setText)
30+
self._lineEdit.setText("pyqt.site")
31+
32+
33+
if __name__ == "__main__":
34+
import cgitb
35+
import sys
36+
37+
cgitb.enable(format="text")
38+
app = QApplication(sys.argv)
39+
w = TestWigglyWidget()
40+
w.show()
41+
sys.exit(app.exec_())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#ifndef BINDINGS_H
2+
#define BINDINGS_H
3+
#include "wigglywidget.h"
4+
#endif // BINDINGS_H
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[generator-project]
2+
generator-set = shiboken
3+
header-file = ${BINDINGS_HEADER_FILE}
4+
typesystem-file = ${BINDINGS_TYPESYSTEM_FILE}
5+
output-directory = ${BINDINGS_OUTPUT_DIR}
6+
include-path = ${BINDINGS_INCLUDE_PATH}
7+
framework-include-paths =
8+
typesystem-paths = ${BINDINGS_TYPESYSTEM_PATH}
9+
10+
enable-parent-ctor-heuristic
11+
enable-pyside-extensions
12+
enable-return-value-heuristic
13+
use-isnull-as-nb_nonzero
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<typesystem package="PySide2">
3+
<load-typesystem name="typesystem_widgets.xml" generate="no"/>
4+
<object-type name="WigglyWidget"/>
5+
</typesystem>

Test/WigglyWidget/PySideWrapper/generated.bat

Whitespace-only changes.

Test/WigglyWidget/PySideWrapper/generated.sh

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
PySide2
2+
3+
https://download.qt.io/official_releases/QtForPython/shiboken2-generator/shiboken2_generator-5.15.2.1-5.15.2-cp35.cp36.cp37.cp38.cp39.cp310-none-win_amd64.whl; platform_machine == "x86_64" and platform_system == "Windows"
4+
5+
https://download.qt.io/official_releases/QtForPython/shiboken2-generator/shiboken2_generator-5.15.2.1-5.15.2-cp35.cp36.cp37.cp38.cp39.cp310-none-win32.whl; platform_machine == "i686" and platform_system == "Windows"
6+
7+
https://download.qt.io/official_releases/QtForPython/shiboken2-generator/shiboken2_generator-5.15.2.1-5.15.2-cp35.cp36.cp37.cp38.cp39.cp310-abi3-manylinux1_x86_64.whl; platform_machine == "x86_64" and platform_system == "Linux"
8+
9+
https://download.qt.io/official_releases/QtForPython/shiboken2-generator/shiboken2_generator-5.15.2.1-5.15.2-cp35.cp36.cp37.cp38.cp39.cp310-abi3-macosx_10_13_intel.whl; platform_machine == "x86_64" and platform_system == "Darwin"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
3+
project(
4+
TestWigglyWidget
5+
VERSION 0.1
6+
LANGUAGES CXX)
7+
8+
set(CMAKE_AUTOUIC ON)
9+
set(CMAKE_AUTOMOC ON)
10+
set(CMAKE_AUTORCC ON)
11+
12+
set(CMAKE_CXX_STANDARD 17)
13+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
14+
15+
# 配置安装目录
16+
set(CMAKE_INSTALL_DIR "${CMAKE_SOURCE_DIR}/dist")
17+
18+
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
19+
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)
20+
21+
include_directories("${CMAKE_CURRENT_SOURCE_DIR}/../LibWigglyWidget")
22+
23+
set(PROJECT_SOURCES main.cpp)
24+
25+
if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
26+
qt_add_executable(${PROJECT_NAME} MANUAL_FINALIZATION ${PROJECT_SOURCES})
27+
# Define target properties for Android with Qt 6 as: set_property(TARGET
28+
# TestWigglyWidget APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
29+
# ${CMAKE_CURRENT_SOURCE_DIR}/android) For more information, see
30+
# https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
31+
else()
32+
if(ANDROID)
33+
add_library(${PROJECT_NAME} SHARED ${PROJECT_SOURCES})
34+
# Define properties for Android with Qt 5 after find_package() calls as:
35+
# set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
36+
else()
37+
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})
38+
endif()
39+
endif()
40+
41+
target_link_libraries(${PROJECT_NAME} PRIVATE Qt${QT_VERSION_MAJOR}::Widgets
42+
LibWigglyWidget)
43+
44+
# Qt for iOS sets MACOSX_BUNDLE_GUI_IDENTIFIER automatically since Qt 6.1. If
45+
# you are developing for iOS or macOS you should consider setting an explicit,
46+
# fixed bundle identifier manually though.
47+
if(${QT_VERSION} VERSION_LESS 6.1.0)
48+
set(BUNDLE_ID_OPTION MACOSX_BUNDLE_GUI_IDENTIFIER
49+
com.example.TestWigglyWidget)
50+
endif()
51+
set_target_properties(
52+
${PROJECT_NAME}
53+
PROPERTIES ${BUNDLE_ID_OPTION} MACOSX_BUNDLE_BUNDLE_VERSION
54+
${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING
55+
${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} MACOSX_BUNDLE
56+
TRUE WIN32_EXECUTABLE
57+
TRUE)
58+
59+
if(QT_VERSION_MAJOR EQUAL 6)
60+
qt_finalize_executable(${PROJECT_NAME})
61+
endif()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <QApplication>
2+
3+
#include "wigglywidget.h"
4+
5+
int main(int argc, char *argv[]) {
6+
QApplication a(argc, argv);
7+
WigglyWidget w;
8+
w.setText("pyqt.site");
9+
w.show();
10+
return a.exec();
11+
}

0 commit comments

Comments
 (0)