From eed1223c1d81f0e4ab8128d05df79af509e23332 Mon Sep 17 00:00:00 2001 From: joshua Date: Sat, 4 Dec 2021 12:22:47 +0100 Subject: [PATCH] Minor improvement for function QETApp::customElementsDir() and QETApp::commonElementsDir() the path is set the first time the function is called. Each other call will immediately return the previously setted path instead of check again what path to return. --- sources/qetapp.cpp | 141 ++++++++++-------- sources/qetapp.h | 14 +- .../configpage/generalconfigurationpage.cpp | 6 +- 3 files changed, 93 insertions(+), 68 deletions(-) diff --git a/sources/qetapp.cpp b/sources/qetapp.cpp index 7c90c0a05..1a673f1bf 100644 --- a/sources/qetapp.cpp +++ b/sources/qetapp.cpp @@ -49,14 +49,17 @@ #endif #ifdef QET_ALLOW_OVERRIDE_CED_OPTION -QString QETApp::common_elements_dir = QString(); +QString QETApp::m_overrided_common_elements_dir = QString(); #endif + #ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION QString QETApp::common_tbt_dir_ = QString(); #endif + #ifdef QET_ALLOW_OVERRIDE_CD_OPTION QString QETApp::config_dir = QString(); #endif + QString QETApp::lang_dir = QString(); TitleBlockTemplatesFilesCollection *QETApp::m_common_tbt_collection; TitleBlockTemplatesFilesCollection *QETApp::m_custom_tbt_collection; @@ -66,8 +69,13 @@ uint QETApp::next_project_id = 0; RecentFiles *QETApp::m_projects_recent_files = nullptr; RecentFiles *QETApp::m_elements_recent_files = nullptr; TitleBlockTemplate *QETApp::default_titleblock_template_ = nullptr; -QString QETApp::m_user_common_elements_dir = QString(); -QString QETApp::m_user_custom_elements_dir = QString(); + +QString QETApp::m_common_element_dir = QString(); +bool QETApp::m_common_element_dir_is_set = false; + +QString QETApp::m_custom_element_dir = QString(); +bool QETApp::m_custom_element_dir_is_set = false; + QString QETApp::m_user_custom_tbt_dir = QString(); QETApp *QETApp::m_qetapp = nullptr; @@ -488,57 +496,62 @@ TitleBlockTemplatesCollection *QETApp::titleBlockTemplatesCollection( */ QString QETApp::commonElementsDir() { - if (m_user_common_elements_dir.isEmpty()) + if (m_common_element_dir_is_set) { + return m_common_element_dir; + } + else + { + m_common_element_dir_is_set = true; + + //Check if user define a custom path + //for the common collection QSettings settings; QString path = settings.value( - "elements-collections/common-collection-path", - "default").toString(); + "elements-collections/common-collection-path", + "default").toString(); if (path != "default" && !path.isEmpty()) { QDir dir(path); if (dir.exists()) { - m_user_common_elements_dir = path; - return m_user_common_elements_dir; + m_common_element_dir = path; + return m_common_element_dir; } } - else { - m_user_common_elements_dir = "default"; - } - } - else if (m_user_common_elements_dir != "default") { - return m_user_common_elements_dir; - } #ifdef QET_ALLOW_OVERRIDE_CED_OPTION - if (common_elements_dir != QString()) return(common_elements_dir); + if (m_overrided_common_elements_dir != QString()) { + m_common_element_dir = m_overrided_common_elements_dir; + return(m_common_element_dir); + } + m_common_element_dir = QCoreApplication::applicationDirPath() + "/elements"; + return m_common_element_dir; #endif #ifndef QET_COMMON_COLLECTION_PATH - /* in the absence of a compilation option, - * we use the elements folder, located next to the executable binary - * en l'absence d'option de compilation, - * on utilise le dossier elements, situe a cote du binaire executable - */ - return(QCoreApplication::applicationDirPath() + "/elements/"); + /* in the absence of a compilation option, + * we use the elements folder, located next to the executable binary + */ + m_common_element_dir = QCoreApplication::applicationDirPath() + "/elements"; + return m_common_element_dir; #else - #ifndef QET_COMMON_COLLECTION_PATH_RELATIVE_TO_BINARY_PATH +#ifndef QET_COMMON_COLLECTION_PATH_RELATIVE_TO_BINARY_PATH /* the compilation option represents a classic absolute * or relative path - * l'option de compilation represente un chemin absolu - * ou relatif classique */ - return(QUOTE(QET_COMMON_COLLECTION_PATH)); - #else + m_common_element_dir = QUOTE(QET_COMMON_COLLECTION_PATH); + return m_common_element_dir; +#else /* the compilation option represents a path - * relative to the folder containing the executable binary - * l'option de compilation represente un chemin - * relatif au dossier contenant le binaire executable - */ - return(QCoreApplication::applicationDirPath() - + "/" + QUOTE(QET_COMMON_COLLECTION_PATH)); - #endif + * relative to the folder containing the executable binary + */ + m_common_element_dir = QCoreApplication::applicationDirPath() + + "/" + + QUOTE(QET_COMMON_COLLECTION_PATH); + return m_common_element_dir; #endif +#endif + } } /** @@ -547,33 +560,34 @@ QString QETApp::commonElementsDir() */ QString QETApp::customElementsDir() { - if (m_user_custom_elements_dir.isEmpty()) + if (m_custom_element_dir_is_set) { + return m_custom_element_dir; + } + else + { + m_custom_element_dir_is_set = true; + QSettings settings; QString path = settings.value( - "elements-collections/custom-collection-path", - "default").toString(); + "elements-collections/custom-collection-path", + "default").toString(); if (path != "default" && !path.isEmpty()) { QDir dir(path); if (dir.exists()) - { - m_user_custom_elements_dir = path; - if(!m_user_custom_elements_dir.endsWith("/")) { - m_user_custom_elements_dir.append("/"); - } - return m_user_custom_elements_dir; + { + m_custom_element_dir = path; + if(!m_custom_element_dir.endsWith("/")) { + m_custom_element_dir.append("/"); + } + return m_custom_element_dir; } } - else { - m_user_custom_elements_dir = "default"; - } - } - else if (m_user_custom_elements_dir != "default") { - return m_user_custom_elements_dir; - } - return(configDir() + "elements/"); + m_custom_element_dir = configDir() + "elements/"; + return m_custom_element_dir; + } } /** @@ -601,14 +615,19 @@ QString QETApp::customElementsDirN() } /** - @brief QETApp::resetUserElementsDir - Reset the path of the user common and custom elements dir. - Use this function when the user path (common and/or custom) change. -*/ -void QETApp::resetUserElementsDir() + * @brief QETApp::resetCollectionsPath + * Reset the path of the user and common element collection + * and also the user titleblock path. + * Use this function when one of these three path change. + */ +void QETApp::resetCollectionsPath() { - m_user_common_elements_dir.clear(); - m_user_custom_elements_dir.clear(); + m_common_element_dir.clear(); + m_common_element_dir_is_set = false; + + m_custom_element_dir.clear(); + m_custom_element_dir_is_set = false; + m_user_custom_tbt_dir.clear(); } @@ -877,9 +896,9 @@ void QETApp::overrideCommonElementsDir(const QString &new_ced) { QFileInfo new_ced_info(new_ced); if (new_ced_info.isDir()) { - common_elements_dir = new_ced_info.absoluteFilePath(); - if (!common_elements_dir.endsWith("/")) - common_elements_dir += "/"; + m_overrided_common_elements_dir = new_ced_info.absoluteFilePath(); + if (!m_overrided_common_elements_dir.endsWith("/")) + m_overrided_common_elements_dir += "/"; } } #endif diff --git a/sources/qetapp.h b/sources/qetapp.h index 1a42cbd11..6ef57d71e 100644 --- a/sources/qetapp.h +++ b/sources/qetapp.h @@ -83,7 +83,7 @@ class QETApp : public QObject static QString customElementsDir(); static QString commonElementsDirN(); static QString customElementsDirN(); - static void resetUserElementsDir(); + static void resetCollectionsPath(); static QString commonTitleBlockTemplatesDir(); static QString customTitleBlockTemplatesDir(); static bool registerProject(QETProject *); @@ -108,7 +108,7 @@ class QETApp : public QObject @brief common_elements_dir Directory containing the common elements collection */ - static QString common_elements_dir; + static QString m_overrided_common_elements_dir; #endif #ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION @@ -208,9 +208,15 @@ class QETApp : public QObject static RecentFiles *m_projects_recent_files; static RecentFiles *m_elements_recent_files; static TitleBlockTemplate *default_titleblock_template_; - static QString m_user_common_elements_dir; - static QString m_user_custom_elements_dir; + + static QString m_common_element_dir; + static bool m_common_element_dir_is_set; + + static QString m_custom_element_dir; + static bool m_custom_element_dir_is_set; + static QString m_user_custom_tbt_dir; + public slots: void systray(QSystemTrayIcon::ActivationReason); diff --git a/sources/ui/configpage/generalconfigurationpage.cpp b/sources/ui/configpage/generalconfigurationpage.cpp index 0e0208cc4..7f5e63d18 100644 --- a/sources/ui/configpage/generalconfigurationpage.cpp +++ b/sources/ui/configpage/generalconfigurationpage.cpp @@ -197,7 +197,7 @@ void GeneralConfigurationPage::applyConf() settings.setValue("elements-collections/common-collection-path", "default"); } if (path != settings.value("elements-collections/common-collection-path").toString()) { - QETApp::resetUserElementsDir(); + QETApp::resetCollectionsPath(); } path = settings.value("elements-collections/custom-collection-path").toString(); @@ -212,7 +212,7 @@ void GeneralConfigurationPage::applyConf() settings.setValue("elements-collections/custom-collection-path", "default"); } if (path != settings.value("elements-collections/custom-collection-path").toString()) { - QETApp::resetUserElementsDir(); + QETApp::resetCollectionsPath(); } path = settings.value("elements-collections/custom-tbt-path").toString(); @@ -227,7 +227,7 @@ void GeneralConfigurationPage::applyConf() settings.setValue("elements-collections/custom-tbt-path", "default"); } if (path != settings.value("elements-collections/custom-tbt-path").toString()) { - QETApp::resetUserElementsDir(); + QETApp::resetCollectionsPath(); } }