From 165e95c0d54e4ba978bcba50a0f7b43200648309 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 1 Jul 2020 16:23:51 +0800 Subject: [PATCH] add QT5 support --- CMakeLists.txt | 23 +++++++++- inc/hgl/QT.h | 10 +++++ inc/hgl/platform/QT5.h | 28 ++++++++++++ inc/hgl/platform/QT5Application.h | 33 ++++++++++++++ inc/hgl/type/QTString.h | 72 +++++++++++++++++++++++++++++++ src/QT/CMakeLists.txt | 11 +++++ src/QT/QT5GuiApplication.cpp | 18 ++++++++ src/QT/QT5UnixUTF8.cpp | 20 +++++++++ src/QT/QT5WinUCS2.cpp | 34 +++++++++++++++ 9 files changed, 248 insertions(+), 1 deletion(-) create mode 100644 inc/hgl/QT.h create mode 100644 inc/hgl/platform/QT5.h create mode 100644 inc/hgl/platform/QT5Application.h create mode 100644 inc/hgl/type/QTString.h create mode 100644 src/QT/CMakeLists.txt create mode 100644 src/QT/QT5GuiApplication.cpp create mode 100644 src/QT/QT5UnixUTF8.cpp create mode 100644 src/QT/QT5WinUCS2.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b0ae79..6efd9af 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,4 +5,25 @@ project(CMPlatform) include(path_config.cmake) CMPlatformSetup(${CMAKE_CURRENT_SOURCE_DIR}) -add_subdirectory(${CMPLATFORM_ROOT_SOURCE_PATH}) \ No newline at end of file +OPTION(BUILD_QT5_SUPPORT_LIB OFF) + +IF(BUILD_QT5_SUPPORT_LIB) + + add_definitions("-DHGL_QT5") + + set(QT_MIN_VERSION "5.14.0") + find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS Core) + + include_directories(${Qt5Core_INCLUDES}) + add_definitions(${Qt5Core_DEFINITIONS}) + + IF(WIN32) + SET(HGL_QT_MAIN_SOURCE ${CMPLATFORM_ROOT_SOURCE_PATH}/src/QT/QT5WinUCS2.cpp) + ELSE() + SET(HGL_QT_MAIN_SOURCE ${CMPLATFORM_ROOT_SOURCE_PATH}/src/QT/QT5UnixUTF8.cpp) + ENDIF() + + SET(HGL_QT_LIB CM.QT5Support) +ENDIF() + +add_subdirectory(${CMPLATFORM_ROOT_SOURCE_PATH}) diff --git a/inc/hgl/QT.h b/inc/hgl/QT.h new file mode 100644 index 0000000..d0e5a9a --- /dev/null +++ b/inc/hgl/QT.h @@ -0,0 +1,10 @@ +#ifndef HGL_QT_INCLUDE +#define HGL_QT_INCLUDE + +#include +#include + +#define QTConnect(obj,event,class_pointer,slot_func) QObject::connect(obj,SIGNAL(event()),class_pointer,SLOT(slot_func())) + +#endif//HGL_QT_INCLUDE + diff --git a/inc/hgl/platform/QT5.h b/inc/hgl/platform/QT5.h new file mode 100644 index 0000000..4aee4d7 --- /dev/null +++ b/inc/hgl/platform/QT5.h @@ -0,0 +1,28 @@ +#ifndef HGL_QT5_MAIN_INCLUDE +#define HGL_QT5_MAIN_INCLUDE + +#include +#include + +#define HGL_QT_MAIN(sii_name,app_name,arg_name) extern "C" int QT5AppMain(hgl::ConsoleSystemInitInfo &sii_name,hgl::QT5GuiApplication &app_name,const hgl::StringList &arg_name) + +#define QTConnect(obj,event,class_pointer,slot_func) connect(obj,SIGNAL(event()),class_pointer,SLOT(slot_func())) + +#define HGL_QT_MAIN_FUNC() HGL_QT_MAIN(sii,app,args) + +#define HGL_QT_APPLICATION(name,code,start) using namespace hgl; \ + \ + HGL_QT_MAIN_FUNC()\ + { \ + sii.info.ProjectName=name; \ + sii.info.ProjectCode=code; \ + \ + if(!app.Init(&sii)) \ + return(-1); \ + \ + app.SetStart(start); \ + \ + return app.Run(); \ + } + +#endif//HGL_QT5_MAIN_INCLUDE diff --git a/inc/hgl/platform/QT5Application.h b/inc/hgl/platform/QT5Application.h new file mode 100644 index 0000000..17a6cdc --- /dev/null +++ b/inc/hgl/platform/QT5Application.h @@ -0,0 +1,33 @@ +#ifndef HGL_QT5_APPLICATION_INCLUDE +#define HGL_QT5_APPLICATION_INCLUDE + +#include + +class QApplication; +#include + +namespace hgl +{ + class QT5GuiApplication:public ConsoleApplication + { + QApplication *qt_app; + + public: + + QT5GuiApplication(QApplication *); + ~QT5GuiApplication(); + + int exec(); + };//class QT5GuiApplication +}//namespace hgl + +/** +* 控制台程序总入口函数 +* @param sii 系统初始化信息类,需由开发者补充填写一定的信息 +* @param app 应用程序整体控制管理类 +* @param args 由命令行或其它程序传来的参数列表 +* @return 返回值,将会回传给操作系统 +*/ +extern "C" int QT5AppMain(hgl::ConsoleSystemInitInfo &sii,hgl::QT5GuiApplication &app,const hgl::StringList &args); + +#endif//HGL_QT5_APPLICATION_INCLUDE diff --git a/inc/hgl/type/QTString.h b/inc/hgl/type/QTString.h new file mode 100644 index 0000000..395f881 --- /dev/null +++ b/inc/hgl/type/QTString.h @@ -0,0 +1,72 @@ +#ifndef HGL_QT_STRING_INCLUDE +#define HGL_QT_STRING_INCLUDE + +#include +#include +#include + +#if HGL_OS == HGL_OS_Windows + inline QString toQString(const hgl::WideString &ws) + { + return QString::fromWCharArray(ws.c_str(),ws.Length()); + } + + inline QString toQString(const wchar_t *str,int size){return QString::fromWCharArray(str,size);} + + inline hgl::UTF8String ToUTF8String(const QString &str) + { + QByteArray u8str=str.toUtf8(); + + return hgl::UTF8String(u8str.data(),u8str.size()); + } + + inline hgl::WideString ToOSString(const QString &str) + { + //qt有toWCharArray(),但是不能预估wchar_t的长度,所以暂时使用先UTF8,再转utf16的方式 + + QByteArray u8str=str.toUtf8(); + + return hgl::to_u16(u8str.data(),u8str.size()); + } +#else + template QString WCharToQString(const wchar_t *,int); + + template<> inline QString WCharToQString<2>(const wchar_t *str,int size){return QString::fromUtf16((ushort *)str,size);} + template<> inline QString WCharToQString<4>(const wchar_t *str,int size){return QString::fromUcs4((uint *)str,size);} + + inline QString toQString(const wchar_t *str,int size) + { + return WCharToQString(str,size); + } + + inline QString toQString(const u16char *str,int size){return QString::fromUtf16((ushort *)str,size);} + inline QString toQString(const char32_t *str,int size){return QString::fromUcs4((uint *)str,size);} + + inline QString toQString(const hgl::UTF16String &str) + { + return QString::fromUtf16((ushort *)str.c_str(), str.Length()); + } + + inline hgl::UTF8String ToUTF8String(const QString &str) + { + QByteArray u8str=str.toUtf8(); + + return hgl::UTF8String(u8str.data(),u8str.size()); + } + + inline hgl::UTF8String ToOSString(const QString &str) + { + return ToUTF8String(str); + } +#endif//HGL_OS == HGL_OS_Windows + +inline QString toQString(const hgl::UTF8String &str) +{ + return QString::fromUtf8(str.c_str(),str.Length()); +} + +// inline QString toQString(const hgl::UTF32String &str) +// { +// return QString::fromUcs4((uint *)str.c_str(),str.Length()); +// } +#endif//HGL_QT_STRING_INCLUDE diff --git a/src/QT/CMakeLists.txt b/src/QT/CMakeLists.txt new file mode 100644 index 0000000..8a46061 --- /dev/null +++ b/src/QT/CMakeLists.txt @@ -0,0 +1,11 @@ +IF(BUILD_QT5_SUPPORT_LIB) + find_package(Qt5 ${QT_MIN_VERSION} CONFIG REQUIRED COMPONENTS Gui Widgets) + + IF(WIN32) + add_library(CM.QT5Support STATIC QT5GuiApplication.cpp) + ELSE() + add_library(CM.QT5Support SHARED QT5GuiApplication.cpp) + ENDIF() + + target_link_libraries(CM.QT5Support Qt5::Core Qt5::Gui Qt5::Widgets) +ENDIF(BUILD_QT5_SUPPORT_LIB) diff --git a/src/QT/QT5GuiApplication.cpp b/src/QT/QT5GuiApplication.cpp new file mode 100644 index 0000000..c68803c --- /dev/null +++ b/src/QT/QT5GuiApplication.cpp @@ -0,0 +1,18 @@ +#include +#include + +namespace hgl +{ + QT5GuiApplication::QT5GuiApplication(QApplication *qa):qt_app(qa) + { + } + + QT5GuiApplication::~QT5GuiApplication() + { + } + + int QT5GuiApplication::exec() + { + return qt_app->exec(); + } +}//namespace hgl diff --git a/src/QT/QT5UnixUTF8.cpp b/src/QT/QT5UnixUTF8.cpp new file mode 100644 index 0000000..7aa8666 --- /dev/null +++ b/src/QT/QT5UnixUTF8.cpp @@ -0,0 +1,20 @@ +#include +#include +#include + +using namespace hgl; + +int main(int argc,char **argv) +{ + QApplication qt_app(argc,argv); + + StringList sl; + + for(int i=0;i +#include +#include +#include + +extern "C" int WINAPI wWinMain(HINSTANCE,HINSTANCE,wchar_t *cmd_line,int) +{ + wchar_t **w_argv; + char **argv; + int argc; + + w_argv = CommandLineToArgvW(cmd_line, &argc); + + hgl::StringList sl; + hgl::StringList sl8; + + argv = new char *[argc]; + + for (int i = 0; i < argc; i++) + { + sl.Add(w_argv[i]); + sl8.Add(hgl::to_u8(w_argv[i])); + + argv[i] = sl8[i].c_str(); + } + + QApplication qt_app(argc,argv); + + hgl::ConsoleSystemInitInfo sii; + hgl::QT5GuiApplication app(&qt_app); + + delete[] argv; + return QT5AppMain(sii, app, sl); +}