Skip to content

Commit

Permalink
feat: Enable build with Qt6
Browse files Browse the repository at this point in the history
Enable build with Qt6 and compat Qt5.

Log: Enable build with Qt6.
  • Loading branch information
re2zero authored and deepin-bot[bot] committed Jan 9, 2025
1 parent 445f68b commit eab49bd
Show file tree
Hide file tree
Showing 40 changed files with 420 additions and 183 deletions.
5 changes: 5 additions & 0 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ Files: debian/*
Copyright: UnionTech Software Technology Co., Ltd.
License: CC0-1.0

# cmake
Files: cmake/*
Copyright: UnionTech Software Technology Co., Ltd.
License: CC0-1.0

# rpm
Files: rpm/*
Copyright: UnionTech Software Technology Co., Ltd.
Expand Down
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "sw_64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mieee")
endif ()

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake)
# 引入翻译生成
include(translation-generate)

# Find Qt version
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Core)
message(" >>> Found Qt version: ${QT_VERSION_MAJOR}")
set(QT_DESIRED_VERSION ${QT_VERSION_MAJOR})

if (QT_VERSION_MAJOR MATCHES 6)
set(DTK_VERSION_MAJOR 6)
set(KF_VERSION_MAJOR 6)
else()
set(DTK_VERSION_MAJOR "")
set(KF_VERSION_MAJOR 5)
endif()
message(" >>> Build with DTK: ${DTK_VERSION_MAJOR}")

# 设置软件版本
include_directories(${PROJECT_BINARY_DIR})
configure_file(${PROJECT_SOURCE_DIR}/src/environments.h.in ${PROJECT_BINARY_DIR}/environments.h @ONLY)
Expand Down
39 changes: 39 additions & 0 deletions cmake/translation-generate.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function(TRANSLATION_GENERATE QMS)
find_package(Qt${QT_VERSION_MAJOR}LinguistTools QUIET)

if (NOT Qt${QT_VERSION_MAJOR}_LRELEASE_EXECUTABLE)
set(QT_LRELEASE "/lib/qt${QT_VERSION_MAJOR}/bin/lrelease")
message(STATUS "NOT found lrelease, set QT_LRELEASE = ${QT_LRELEASE}")
else()
set(QT_LRELEASE "${Qt${QT_VERSION_MAJOR}_LRELEASE_EXECUTABLE}")
endif()

if(NOT ARGN)
message(SEND_ERROR "Error: TRANSLATION_GENERATE() called without any .ts path")
return()
endif()

# # 获取 translations 目录下的所有 .ts 文件
# file(GLOB_RECURSE TS_FILES "${ARGN}/*.ts")
# 获取指定目录下的所有 .ts 文件,不包括子目录
file(GLOB TS_FILES "${ARGN}/*.ts")

set(${QMS})
foreach(TSFIL ${TS_FILES})
get_filename_component(FIL_WE ${TSFIL} NAME_WE)
# get_filename_component(TS_DIR ${TSFIL} DIRECTORY)
set(QMFIL ${CMAKE_CURRENT_BINARY_DIR}/${FIL_WE}.qm)
list(APPEND ${QMS} ${QMFIL})
add_custom_command(
OUTPUT ${QMFIL}
# COMMAND ${Qt5_LUPDATE_EXECUTABLE} ${CMAKE_SOURCE_DIR} -ts ${TSFIL}
COMMAND ${QT_LRELEASE} ${TSFIL} -qm ${QMFIL}
DEPENDS ${TSFIL}
COMMENT "Running ${QT_LRELEASE} on ${TSFIL}"
VERBATIM
)
endforeach()

set_source_files_properties(${${QMS}} PROPERTIES GENERATED TRUE)
set(${QMS} ${${QMS}} PARENT_SCOPE)
endfunction()
21 changes: 13 additions & 8 deletions debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ Build-Depends:
cmake,
debhelper-compat (= 12),
pkg-config,
libqt5widgets5,
libdtkcore-dev,
libdtkwidget-dev,
libpolkit-qt5-1-dev,
libkf5syntaxhighlighting-dev,
libkf5codecs-dev,
qttools5-dev-tools,
qtbase5-private-dev,
qt6-5compat-dev | hello,
qt6-base-dev | qtbase5-dev,
qt6-tools-dev-tools | qttools5-dev-tools,
qt6-tools-dev | qttools5-dev,
qt6-base-private-dev | qtbase5-private-dev,
qt6-svg-dev | libqt5svg5-dev,
libdtk6widget-dev | libdtkwidget-dev,
libdtk6gui-dev | libdtkgui-dev,
libdtk6core-dev | libdtkcore-dev,
libdtk6declarative-dev | libdtkcore5-bin,
libpolkit-qt6-1-dev | libpolkit-qt5-1-dev,
libkf6syntaxhighlighting-dev | libkf5syntaxhighlighting-dev,
libkf6codecs-dev | libkf5codecs-dev,
libdframeworkdbus-dev,
libgtest-dev,
libgmock-dev,
Expand Down
20 changes: 18 additions & 2 deletions debian/rules
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,28 @@ PACK_VER = $(shell echo $(VERSION) | awk -F'[+_~-]' '{print $$1}')
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# 检测当前安装的Qt版本,优先使用Qt6,否则使用Qt5
define detect_qt_version
ifneq (,$(shell which qmake6 2>/dev/null))
QT_DIR="/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Qt6"
else
QT_DIR="/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Qt5"
endif
endef

# 调用检测Qt版本的命令
$(eval $(call detect_qt_version))

# hardcode this if want to force build with sepecific Qt version
# QT_DIR="/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/Qt5"

%:
dh $@
dh $@ --parallel

override_dh_auto_configure:
dh_auto_configure -- \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/usr \
-DCMAKE_SAFETYTEST_ARG="CMAKE_SAFETYTEST_ARG_OFF" \
-DVERSION=$(PACK_VER)
-DVERSION=$(PACK_VER) \
-DQT_DIR=$(QT_DIR)
72 changes: 44 additions & 28 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
if (NOT (${CMAKE_BUILD_TYPE} MATCHES "Debug"))
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
# generate qm files
execute_process(COMMAND bash "${CMAKE_SOURCE_DIR}/translate_generation.sh" WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
endif ()

# Sources files
Expand All @@ -20,20 +18,38 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/lib/include)

# Find the library
find_package(PkgConfig REQUIRED)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5DBus REQUIRED)
find_package(Qt5Concurrent REQUIRED)
find_package(Qt5PrintSupport REQUIRED)
find_package(Qt5Gui REQUIRED)
find_package(DtkWidget REQUIRED)
find_package(DtkCore REQUIRED)
find_package(KF5SyntaxHighlighting)
find_package(KF5Codecs)

set(qt_required_components Gui Widgets DBus Concurrent PrintSupport Svg Xml)
if (QT_DESIRED_VERSION MATCHES 6)
list(APPEND qt_required_components Core5Compat)
endif()
find_package(Qt${QT_DESIRED_VERSION} REQUIRED COMPONENTS ${qt_required_components})
find_package(Dtk${DTK_VERSION_MAJOR} REQUIRED COMPONENTS Widget Core)
find_package(KF${KF_VERSION_MAJOR}Codecs REQUIRED)
find_package(KF${KF_VERSION_MAJOR}SyntaxHighlighting REQUIRED)

set(LINK_LIBS
Qt${QT_VERSION_MAJOR}::Gui
Qt${QT_VERSION_MAJOR}::Widgets
Qt${QT_VERSION_MAJOR}::Xml
Qt${QT_VERSION_MAJOR}::Svg
Qt${QT_VERSION_MAJOR}::Concurrent
Qt${QT_VERSION_MAJOR}::PrintSupport
Qt${QT_VERSION_MAJOR}::DBus
Dtk${DTK_VERSION_MAJOR}::Widget
Dtk${DTK_VERSION_MAJOR}::Core
KF${KF_VERSION_MAJOR}::Codecs
KF${KF_VERSION_MAJOR}::SyntaxHighlighting
)

if (QT_DESIRED_VERSION MATCHES 6)
list(APPEND LINK_LIBS Qt${QT_DESIRED_VERSION}::Core5Compat)
else()
include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})
endif()

find_package(DFrameworkdbus REQUIRED)
find_package(Qt5Xml REQUIRED)
find_package(Qt5Svg REQUIRED)
find_package(ICU COMPONENTS i18n uc REQUIRED)
include_directories(${Qt5Gui_PRIVATE_INCLUDE_DIRS})

pkg_check_modules(chardet REQUIRED chardet)
include_directories(${chardet_INCLUDE_DIRS})
Expand All @@ -42,18 +58,9 @@ link_directories(${chardet_LIBRARY_DIRS})
# Tell CMake to create the executable
add_executable(deepin-editor ${SRCS} deepin-editor.qrc)
target_link_libraries(deepin-editor
${DtkWidget_LIBRARIES}
${DtkCore_LIBRARIES}
${Qt5Widgets_LIBRARIES}
${Qt5DBus_LIBRARIES}
${Qt5PrintSupport_LIBRARIES}
${Qt5Concurrent_LIBRARIES}
${LINK_LIBS}
${DFrameworkdbus_LIBRARIES}
${Qt5Xml_LIBRARIES}
${Qt5Svg_LIBRARIES}
${chardet_LIBRARY_DIRS}
KF5::Codecs
KF5::SyntaxHighlighting
ICU::i18n
ICU::uc
-lm
Expand All @@ -63,9 +70,14 @@ target_link_libraries(deepin-editor
)


# Install qm files
file(GLOB QM_FILES "${CMAKE_SOURCE_DIR}/translations/*.qm")
install(FILES ${QM_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/share/deepin-editor/translations/)
# translation files
TRANSLATION_GENERATE(QM_FILES ${CMAKE_SOURCE_DIR}/translations)
add_custom_target(${PROJECT_NAME}_qm_files DEPENDS ${QM_FILES})
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_qm_files)

# Install translations
install(FILES ${QM_FILES} DESTINATION share/${PROJECT_NAME}/translations)

install(FILES ${APP_ICONPATH} DESTINATION ${CMAKE_INSTALL_PREFIX}/share/icons/hicolor/scalable/apps/)
install(FILES ${CMAKE_SOURCE_DIR}/src/images/deepin-editor.svg DESTINATION ${CMAKE_INSTALL_PREFIX}/share/icons/hicolor/scalable/apps/)

Expand All @@ -92,7 +104,11 @@ install(DIRECTORY ${CMAKE_SOURCE_DIR}/src/themes DESTINATION ${CMAKE_INSTALL_PRE
set(DCONFIG_APPID org.deepin.editor)
file(GLOB DCONFIG_FILES "${CMAKE_SOURCE_DIR}/misc/configs/*.json")
if(DEFINED DSG_DATA_DIR)
dconfig_meta_files(APPID ${DCONFIG_APPID} FILES ${DCONFIG_FILES})
if (DTK_VERSION_MAJOR EQUAL "6")
dtk_add_config_meta_files(APPID ${DCONFIG_APPID} FILES ${DCONFIG_FILES})
else()
dconfig_meta_files(APPID ${DCONFIG_APPID} FILES ${DCONFIG_FILES})
endif()
message("-- DConfig is supported by DTK")
else()
install(FILES ${DCONFIG_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/share/dsg/configs/${DCONFIG_APPID})
Expand Down
8 changes: 8 additions & 0 deletions src/common/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,15 +565,23 @@ bool Settings::checkShortcutValid(const QString &Name, QString Key, QString &Rea
// 单键
if (Key.count("+") == 0) {
//F1-F12是允许的,这个正则不够精确,但是没关系。
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QRegExp regexp("^F[0-9]{1,2}$");
#else
QRegularExpression regexp("^F[0-9]{1,2}$");
#endif
if (!Key.contains(regexp)) {
Reason = tr("The shortcut %1 is invalid, please set another one.").arg(style);
bIsConflicts = false;
return false;
}
}
// 小键盘单键都不允许
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QRegExp regexpNum("^Num+.*");
#else
QRegularExpression regexpNum("^Num+.*");
#endif
if (Key.contains(regexpNum)) {
Reason = tr("The shortcut %1 is invalid, please set another one.").arg(style);
bIsConflicts = false;
Expand Down
2 changes: 1 addition & 1 deletion src/common/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public slots:
private:
void updateAllKeysWithKeymap(QString keymap);
void copyCustomizeKeysFromKeymap(QString keymap);
bool checkShortcutValid(const QString &Name, QString Key, QString &Reason, bool &bIsConflicts, QString defaultValue = QString::null);
bool checkShortcutValid(const QString &Name, QString Key, QString &Reason, bool &bIsConflicts, QString defaultValue = QString());
bool isShortcutConflict(const QString &Name, const QString &Key);
DDialog *createDialog(const QString &title, const QString &content, const bool &bIsConflicts);
void removeLockFiles();
Expand Down
19 changes: 11 additions & 8 deletions src/common/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ QSize Utils::getRenderSize(int fontSize, const QString &string)
int height = 0;

for (const QString &line : string.split("\n")) {
int lineWidth = fm.width(line);
int lineWidth = fm.horizontalAdvance(line);
int lineHeight = fm.height();

if (lineWidth > width) {
Expand Down Expand Up @@ -133,7 +133,7 @@ float Utils::codecConfidenceForData(const QTextCodec *codec, const QByteArray &d
break;
default:
// full-width character, emoji, 常用标点, 拉丁文补充1,天城文补充,CJK符号和标点符号(如:【】)
if ((ch.unicode() >= 0xff00 && ch <= 0xffef)
if ((ch.unicode() >= 0xff00 && ch.unicode() <= 0xffef)
|| (ch.unicode() >= 0x2600 && ch.unicode() <= 0x27ff)
|| (ch.unicode() >= 0x2000 && ch.unicode() <= 0x206f)
|| (ch.unicode() >= 0x80 && ch.unicode() <= 0xff)
Expand Down Expand Up @@ -255,7 +255,11 @@ QByteArray Utils::detectEncode(const QByteArray &data, const QString &fileName)
QTextStream stream(data);

pattern.setPatternOptions(QRegularExpression::DontCaptureOption | QRegularExpression::CaseInsensitiveOption);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
stream.setCodec("latin1");
#else
stream.setEncoding(QStringConverter::Latin1);
#endif

while (!stream.atEnd()) {
const QString &_data = stream.readLine();
Expand Down Expand Up @@ -587,7 +591,7 @@ bool Utils::isMimeTypeSupport(const QString &filepath)

bool Utils::isDraftFile(const QString &filepath)
{
QString draftDir = QDir(Utils::cleanPath(QStandardPaths::standardLocations(QStandardPaths::DataLocation)).first())
QString draftDir = QDir(Utils::cleanPath(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation)).first())
.filePath("blank-files");
draftDir = QDir::cleanPath(draftDir);
QString dir = QFileInfo(filepath).dir().absolutePath();
Expand All @@ -600,7 +604,7 @@ bool Utils::isDraftFile(const QString &filepath)
*/
bool Utils::isBackupFile(const QString &filepath)
{
QString backupDir = QDir(Utils::cleanPath(QStandardPaths::standardLocations(QStandardPaths::DataLocation)).first())
QString backupDir = QDir(Utils::cleanPath(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation)).first())
.filePath("backup-files");
QString dir = QFileInfo(filepath).dir().absolutePath();
return dir == backupDir;
Expand All @@ -623,7 +627,7 @@ QStringList Utils::cleanPath(const QStringList &filePaths)
*/
QString Utils::localDataPath()
{
auto dataPaths = Utils::cleanPath(QStandardPaths::standardLocations(QStandardPaths::DataLocation));
auto dataPaths = Utils::cleanPath(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation));
return dataPaths.isEmpty() ? QDir::homePath() + "/.local/share/deepin/deepin-editor/"
: dataPaths.first();
}
Expand Down Expand Up @@ -785,8 +789,7 @@ void Utils::killProcessByName(const char *pstrName)

QString Utils::getStringMD5Hash(const QString &input)
{
QByteArray byteArray;
byteArray.append(input);
QByteArray byteArray = input.toUtf8();;
QByteArray md5Path = QCryptographicHash::hash(byteArray, QCryptographicHash::Md5);

return md5Path.toHex();
Expand Down Expand Up @@ -914,7 +917,7 @@ QString Utils::lineFeed(const QString &text, int nWidth, const QFont &font, int

if (!strText.isEmpty()) {
for (int i = 1; i <= strText.size(); i++) {
if (fm.width(strText.left(i)) >= nWidth) {
if (fm.horizontalAdvance(strText.left(i)) >= nWidth) {
if (strListLine.size() + 1 == nElidedRow)
break;

Expand Down
1 change: 1 addition & 0 deletions src/common/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <QIcon>
#include <QDBusInterface>
#include <QDBusReply>
#include <QTextCodec>

#ifndef SAFE_DELETE
#define SAFE_DELETE(p) \
Expand Down
4 changes: 1 addition & 3 deletions src/controls/findbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@
#include "dimagebutton.h"
#include <QColor>
#include <DIconButton>
#include <DApplicationHelper>
#include <DGuiApplicationHelper>
#include <DFloatingWidget>
#include <QMouseEvent>
#include <qmouseeventtransition.h>
#include <QMouseEventTransition>

#include <DPalette>
#include <DAbstractDialog>
Expand Down
Loading

0 comments on commit eab49bd

Please sign in to comment.