-
Notifications
You must be signed in to change notification settings - Fork 142
feat: migrate QML plugins to Qt6 resource system #2925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,84 @@ | ||
| include(CMakeParseArguments) | ||
|
|
||
| function(dcc_to_capitalized_string input_string output_variable) | ||
| if(NOT input_string STREQUAL "") | ||
| string(SUBSTRING ${input_string} 0 1 first_char) | ||
| string(SUBSTRING ${input_string} 1 -1 rest_chars) | ||
| string(TOUPPER ${first_char} capitalized_first_char) | ||
| set(result "${capitalized_first_char}${rest_chars}") | ||
| else() | ||
| set(result "") | ||
| endif() | ||
| set(${output_variable} ${result} PARENT_SCOPE) | ||
| endfunction() | ||
|
|
||
|
|
||
| macro(dcc_build_plugin) | ||
| set(oneValueArgs NAME TARGET QML_ROOT_DIR) | ||
| set(qml_root_dir ${CMAKE_CURRENT_SOURCE_DIR}/qml) | ||
| set(multiValueArgs QML_FILES RESOURCE_FILES) | ||
| set(QML_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/qml) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. QML_ROOT_DIR 这个不暴露出去吧,我们只处理默认的,不然下面跟QML_FILES一起组合使用很容易出问题, |
||
| find_package(Qt6 COMPONENTS Qml) | ||
| cmake_parse_arguments(_config "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) | ||
|
|
||
| if(NOT "${_config_NAME}" MATCHES "^[A-Za-z0-9]+$") | ||
| message(FATAL_ERROR "Error: The name '${_config_NAME}' contains invalid characters. Only alphanumeric characters are allowed.") | ||
| endif() | ||
|
|
||
| if (DEFINED _config_QML_ROOT_DIR) | ||
| set(qml_root_dir ${_config_QML_ROOT_DIR}) | ||
| set(QML_ROOT_DIR ${_config_QML_ROOT_DIR}) | ||
| endif() | ||
| file(GLOB_RECURSE qml_files ${qml_root_dir}/*) | ||
| add_custom_target(${_config_NAME}_qml ALL | ||
| SOURCES ${qml_files} | ||
| ) | ||
|
|
||
|
|
||
| if(NOT _config_QML_FILES) | ||
| file(GLOB_RECURSE _config_QML_FILES ${QML_ROOT_DIR}/*.qml ${QML_ROOT_DIR}/*.js) | ||
| endif() | ||
|
|
||
| if(NOT _config_RESOURCE_FILES) | ||
| file(GLOB_RECURSE _config_RESOURCE_FILES ${QML_ROOT_DIR}/*.dci ${QML_ROOT_DIR}/*.svg ${QML_ROOT_DIR}/*.png ${QML_ROOT_DIR}/*.jpg ${QML_ROOT_DIR}/*.webp) | ||
| endif() | ||
|
|
||
| set(plugin_dirs ${PROJECT_BINARY_DIR}/lib/${DDE_CONTROL_CENTER_PLUGIN_DIR}/${_config_NAME}/) | ||
| add_custom_command(TARGET ${_config_NAME}_qml POST_BUILD | ||
| COMMAND ${CMAKE_COMMAND} -E copy_directory ${qml_root_dir} ${plugin_dirs} | ||
| foreach(FILE_PATH ${_config_QML_FILES}) | ||
| file(RELATIVE_PATH FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR} ${FILE_PATH}) | ||
| list(APPEND QML_PATHS ${FILE_NAME}) | ||
|
|
||
| file(RELATIVE_PATH F_NAME ${QML_ROOT_DIR} ${FILE_PATH}) | ||
|
|
||
| if(${F_NAME} STREQUAL "main.qml") | ||
| dcc_to_capitalized_string(${_config_NAME} UPPERNAME) | ||
| message(FATAL_ERROR "Error: The filename '${F_NAME}' is deprecated. Please rename it to ${UPPERNAME}Main.qml. path:${FILE_PATH}") | ||
| endif() | ||
| if(NOT "${F_NAME}" MATCHES "^[A-Z]") | ||
| dcc_to_capitalized_string(${F_NAME} UPPERNAME) | ||
| message(FATAL_ERROR "Error: The filename '${F_NAME}' must start with an uppercase letter. Please rename it to ${UPPERNAME}. path:${FILE_PATH}") | ||
| endif() | ||
|
|
||
| set_source_files_properties(${FILE_NAME} PROPERTIES | ||
| QT_RESOURCE_ALIAS ${F_NAME} | ||
| ) | ||
| endforeach(FILE_PATH) | ||
|
|
||
| foreach(FILE_PATH ${_config_RESOURCE_FILES}) | ||
| file(RELATIVE_PATH FILE_NAME ${CMAKE_CURRENT_SOURCE_DIR} ${FILE_PATH}) | ||
| list(APPEND RESOURCE_PATHS ${FILE_NAME}) | ||
|
|
||
| file(RELATIVE_PATH F_NAME ${QML_ROOT_DIR} ${FILE_PATH}) | ||
| set_source_files_properties(${FILE_NAME} PROPERTIES | ||
| QT_RESOURCE_ALIAS ${F_NAME} | ||
| ) | ||
| endforeach(FILE_PATH) | ||
| qt_policy(SET QTP0001 NEW) | ||
| qt_policy(SET QTP0004 NEW) | ||
| qt_add_qml_module(${_config_NAME}_qml | ||
| PLUGIN_TARGET ${_config_NAME}_qml | ||
| URI ${_config_NAME} | ||
| VERSION 1.0 | ||
| QML_FILES ${QML_PATHS} | ||
| RESOURCES ${RESOURCE_PATHS} | ||
| OUTPUT_DIRECTORY ${plugin_dirs} | ||
| ) | ||
| install(TARGETS ${_config_NAME}_qml DESTINATION ${DDE_CONTROL_CENTER_PLUGIN_INSTALL_DIR}/${_config_NAME}) | ||
| install(FILES ${plugin_dirs}/qmldir DESTINATION ${DDE_CONTROL_CENTER_PLUGIN_INSTALL_DIR}/${_config_NAME}) | ||
|
|
||
| if (DEFINED _config_TARGET) | ||
| set_target_properties(${_config_TARGET} PROPERTIES | ||
|
|
@@ -33,7 +97,6 @@ function(dcc_install_plugin) | |
| install(TARGETS ${_config_TARGET} DESTINATION ${DDE_CONTROL_CENTER_PLUGIN_INSTALL_DIR}/${_config_NAME}) | ||
| endif() | ||
|
|
||
| install(DIRECTORY ${plugin_dirs} DESTINATION ${DDE_CONTROL_CENTER_PLUGIN_INSTALL_DIR}/${_config_NAME}) | ||
| endfunction() | ||
|
|
||
| function(dcc_handle_plugin_translation) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,20 +14,20 @@ | |
| #include <DIconTheme> | ||
|
|
||
| #include <QCoreApplication> | ||
| #include <QDBusConnection> | ||
| #include <QDBusPendingCall> | ||
| #include <QElapsedTimer> | ||
| #include <QFileInfo> | ||
| #include <QJsonArray> | ||
| #include <QJsonDocument> | ||
| #include <QJsonObject> | ||
| #include <QLoggingCategory> | ||
| #include <QQmlApplicationEngine> | ||
| #include <QQmlContext> | ||
| #include <QQuickWindow> | ||
| #include <QTimer> | ||
| #include <QTranslator> | ||
| #include <QWindow> | ||
| #include <QFileInfo> | ||
|
|
||
| DCORE_USE_NAMESPACE | ||
|
|
||
|
|
@@ -82,40 +82,23 @@ | |
|
|
||
| bool DccManager::installTranslator(const QString &name) | ||
| { | ||
| QTranslator *translator = new QTranslator(qApp); | ||
| if (translator->load(QLocale(), name, "_", TRANSLATE_READ_DIR)) { | ||
| qApp->installTranslator(translator); | ||
| #if 1 // 兼容旧版位置 | ||
| } else if (translator->load(QLocale(), name, "_", TRANSLATE_READ_DIR "/..")) { | ||
| qApp->installTranslator(translator); | ||
| #endif | ||
| } else { | ||
| delete translator; | ||
| qCWarning(dccLog()) << "install translator fail:" << name << ", dir:" << TRANSLATE_READ_DIR; | ||
| return false; | ||
| } | ||
| return true; | ||
| const QStringList translateDirs = { TRANSLATE_READ_DIR, | ||
| TRANSLATE_READ_DIR "/../v1.0", // 兼容旧版位置 | ||
| TRANSLATE_READ_DIR "/.." }; | ||
| return Dtk::Gui::DGuiApplicationHelper::loadTranslator(name, translateDirs, { QLocale() }); | ||
| } | ||
|
|
||
| void DccManager::init() | ||
| { | ||
| if (m_engine) | ||
| return; | ||
|
|
||
| QQmlEngine::setObjectOwnership(dccV25::DccApp::instance(), QQmlEngine::CppOwnership); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个可以直接在c++中用qtquick方式注册, |
||
| qmlRegisterSingletonInstance("org.deepin.dcc", 1, 0, "DccApp", dccV25::DccApp::instance()); | ||
|
|
||
| m_engine = new QQmlApplicationEngine(this); | ||
| auto paths = m_engine->importPathList(); | ||
| paths.prepend(DefaultModuleDirectory); | ||
| const auto runtimePluginDir = QCoreApplication::applicationDirPath() + "/../lib/"; | ||
| if (QFileInfo::exists(runtimePluginDir)) { | ||
| paths.prepend(runtimePluginDir); | ||
| } | ||
| qCInfo(dccLog()) << "Import paths:" << paths; | ||
| m_engine->setImportPathList(paths); | ||
| m_imageProvider = new DccImageProvider(); | ||
| m_engine->addImageProvider("DccImage", m_imageProvider); | ||
| QStringList dciPaths = Dtk::Gui::DIconTheme::dciThemeSearchPaths(); | ||
| dciPaths << QStringLiteral(DefaultModuleDirectory); | ||
| Dtk::Gui::DIconTheme::setDciThemeSearchPaths(dciPaths); | ||
| } | ||
|
|
||
| QQmlApplicationEngine *DccManager::engine() | ||
|
|
@@ -134,7 +117,7 @@ | |
| void DccManager::loadModules(bool async, const QStringList &dirs) | ||
| { | ||
| // onAddModule(m_rootModule); | ||
| m_plugins->loadModules(m_root, async, dirs); | ||
| m_plugins->loadModules(m_root, async, dirs, m_engine); | ||
| // showModule(m_rootModule); | ||
| } | ||
|
|
||
|
|
||
This file was deleted.
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这种我们要导出去么?如果只是控制中心内部使用的话,尽量不对外,