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.
This commit is contained in:
joshua
2021-12-04 12:22:47 +01:00
parent 94f66553ac
commit eed1223c1d
3 changed files with 93 additions and 68 deletions

View File

@@ -49,14 +49,17 @@
#endif #endif
#ifdef QET_ALLOW_OVERRIDE_CED_OPTION #ifdef QET_ALLOW_OVERRIDE_CED_OPTION
QString QETApp::common_elements_dir = QString(); QString QETApp::m_overrided_common_elements_dir = QString();
#endif #endif
#ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION #ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION
QString QETApp::common_tbt_dir_ = QString(); QString QETApp::common_tbt_dir_ = QString();
#endif #endif
#ifdef QET_ALLOW_OVERRIDE_CD_OPTION #ifdef QET_ALLOW_OVERRIDE_CD_OPTION
QString QETApp::config_dir = QString(); QString QETApp::config_dir = QString();
#endif #endif
QString QETApp::lang_dir = QString(); QString QETApp::lang_dir = QString();
TitleBlockTemplatesFilesCollection *QETApp::m_common_tbt_collection; TitleBlockTemplatesFilesCollection *QETApp::m_common_tbt_collection;
TitleBlockTemplatesFilesCollection *QETApp::m_custom_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_projects_recent_files = nullptr;
RecentFiles *QETApp::m_elements_recent_files = nullptr; RecentFiles *QETApp::m_elements_recent_files = nullptr;
TitleBlockTemplate *QETApp::default_titleblock_template_ = 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(); QString QETApp::m_user_custom_tbt_dir = QString();
QETApp *QETApp::m_qetapp = nullptr; QETApp *QETApp::m_qetapp = nullptr;
@@ -488,8 +496,16 @@ TitleBlockTemplatesCollection *QETApp::titleBlockTemplatesCollection(
*/ */
QString QETApp::commonElementsDir() 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; QSettings settings;
QString path = settings.value( QString path = settings.value(
"elements-collections/common-collection-path", "elements-collections/common-collection-path",
@@ -499,46 +515,43 @@ QString QETApp::commonElementsDir()
QDir dir(path); QDir dir(path);
if (dir.exists()) if (dir.exists())
{ {
m_user_common_elements_dir = path; m_common_element_dir = path;
return m_user_common_elements_dir; 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 #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 #endif
#ifndef QET_COMMON_COLLECTION_PATH #ifndef QET_COMMON_COLLECTION_PATH
/* in the absence of a compilation option, /* in the absence of a compilation option,
* we use the elements folder, located next to the executable binary * 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/"); m_common_element_dir = QCoreApplication::applicationDirPath() + "/elements";
return m_common_element_dir;
#else #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 /* the compilation option represents a classic absolute
* or relative path * or relative path
* l'option de compilation represente un chemin absolu
* ou relatif classique
*/ */
return(QUOTE(QET_COMMON_COLLECTION_PATH)); m_common_element_dir = QUOTE(QET_COMMON_COLLECTION_PATH);
#else return m_common_element_dir;
#else
/* the compilation option represents a path /* the compilation option represents a path
* relative to the folder containing the executable binary * 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() m_common_element_dir = QCoreApplication::applicationDirPath()
+ "/" + QUOTE(QET_COMMON_COLLECTION_PATH)); + "/"
#endif + QUOTE(QET_COMMON_COLLECTION_PATH);
return m_common_element_dir;
#endif #endif
#endif
}
} }
/** /**
@@ -547,8 +560,14 @@ QString QETApp::commonElementsDir()
*/ */
QString QETApp::customElementsDir() 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; QSettings settings;
QString path = settings.value( QString path = settings.value(
"elements-collections/custom-collection-path", "elements-collections/custom-collection-path",
@@ -558,22 +577,17 @@ QString QETApp::customElementsDir()
QDir dir(path); QDir dir(path);
if (dir.exists()) if (dir.exists())
{ {
m_user_custom_elements_dir = path; m_custom_element_dir = path;
if(!m_user_custom_elements_dir.endsWith("/")) { if(!m_custom_element_dir.endsWith("/")) {
m_user_custom_elements_dir.append("/"); m_custom_element_dir.append("/");
} }
return m_user_custom_elements_dir; 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 * @brief QETApp::resetCollectionsPath
Reset the path of the user common and custom elements dir. * Reset the path of the user and common element collection
Use this function when the user path (common and/or custom) change. * and also the user titleblock path.
*/ * Use this function when one of these three path change.
void QETApp::resetUserElementsDir() */
void QETApp::resetCollectionsPath()
{ {
m_user_common_elements_dir.clear(); m_common_element_dir.clear();
m_user_custom_elements_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(); m_user_custom_tbt_dir.clear();
} }
@@ -877,9 +896,9 @@ void QETApp::overrideCommonElementsDir(const QString &new_ced) {
QFileInfo new_ced_info(new_ced); QFileInfo new_ced_info(new_ced);
if (new_ced_info.isDir()) if (new_ced_info.isDir())
{ {
common_elements_dir = new_ced_info.absoluteFilePath(); m_overrided_common_elements_dir = new_ced_info.absoluteFilePath();
if (!common_elements_dir.endsWith("/")) if (!m_overrided_common_elements_dir.endsWith("/"))
common_elements_dir += "/"; m_overrided_common_elements_dir += "/";
} }
} }
#endif #endif

View File

@@ -83,7 +83,7 @@ class QETApp : public QObject
static QString customElementsDir(); static QString customElementsDir();
static QString commonElementsDirN(); static QString commonElementsDirN();
static QString customElementsDirN(); static QString customElementsDirN();
static void resetUserElementsDir(); static void resetCollectionsPath();
static QString commonTitleBlockTemplatesDir(); static QString commonTitleBlockTemplatesDir();
static QString customTitleBlockTemplatesDir(); static QString customTitleBlockTemplatesDir();
static bool registerProject(QETProject *); static bool registerProject(QETProject *);
@@ -108,7 +108,7 @@ class QETApp : public QObject
@brief common_elements_dir @brief common_elements_dir
Directory containing the common elements collection Directory containing the common elements collection
*/ */
static QString common_elements_dir; static QString m_overrided_common_elements_dir;
#endif #endif
#ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION #ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION
@@ -208,10 +208,16 @@ class QETApp : public QObject
static RecentFiles *m_projects_recent_files; static RecentFiles *m_projects_recent_files;
static RecentFiles *m_elements_recent_files; static RecentFiles *m_elements_recent_files;
static TitleBlockTemplate *default_titleblock_template_; 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; static QString m_user_custom_tbt_dir;
public slots: public slots:
void systray(QSystemTrayIcon::ActivationReason); void systray(QSystemTrayIcon::ActivationReason);
void reduceEveryEditor(); void reduceEveryEditor();

View File

@@ -197,7 +197,7 @@ void GeneralConfigurationPage::applyConf()
settings.setValue("elements-collections/common-collection-path", "default"); settings.setValue("elements-collections/common-collection-path", "default");
} }
if (path != settings.value("elements-collections/common-collection-path").toString()) { if (path != settings.value("elements-collections/common-collection-path").toString()) {
QETApp::resetUserElementsDir(); QETApp::resetCollectionsPath();
} }
path = settings.value("elements-collections/custom-collection-path").toString(); path = settings.value("elements-collections/custom-collection-path").toString();
@@ -212,7 +212,7 @@ void GeneralConfigurationPage::applyConf()
settings.setValue("elements-collections/custom-collection-path", "default"); settings.setValue("elements-collections/custom-collection-path", "default");
} }
if (path != settings.value("elements-collections/custom-collection-path").toString()) { if (path != settings.value("elements-collections/custom-collection-path").toString()) {
QETApp::resetUserElementsDir(); QETApp::resetCollectionsPath();
} }
path = settings.value("elements-collections/custom-tbt-path").toString(); path = settings.value("elements-collections/custom-tbt-path").toString();
@@ -227,7 +227,7 @@ void GeneralConfigurationPage::applyConf()
settings.setValue("elements-collections/custom-tbt-path", "default"); settings.setValue("elements-collections/custom-tbt-path", "default");
} }
if (path != settings.value("elements-collections/custom-tbt-path").toString()) { if (path != settings.value("elements-collections/custom-tbt-path").toString()) {
QETApp::resetUserElementsDir(); QETApp::resetCollectionsPath();
} }
} }