| 1 | #include "./qtsettings.h" |
| 2 | |
| 3 | #include "../misc/desktoputils.h" |
| 4 | #include "../resources/resources.h" |
| 5 | |
| 6 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 7 | #include "./optioncategory.h" |
| 8 | #include "./optioncategoryfiltermodel.h" |
| 9 | #include "./optioncategorymodel.h" |
| 10 | #include "./optionpage.h" |
| 11 | |
| 12 | #include "../paletteeditor/paletteeditor.h" |
| 13 | #include "../widgets/clearlineedit.h" |
| 14 | #endif |
| 15 | |
| 16 | #include "resources/config.h" |
| 17 | |
| 18 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 19 | #include "ui_qtappearanceoptionpage.h" |
| 20 | #include "ui_qtenvoptionpage.h" |
| 21 | #include "ui_qtlanguageoptionpage.h" |
| 22 | #endif |
| 23 | |
| 24 | #include <c++utilities/application/commandlineutils.h> |
| 25 | |
| 26 | #include <QDir> |
| 27 | #include <QFont> |
| 28 | #include <QGuiApplication> |
| 29 | #include <QIcon> |
| 30 | #include <QLocale> |
| 31 | #include <QOperatingSystemVersion> |
| 32 | #include <QPalette> |
| 33 | #include <QSettings> |
| 34 | #include <QStringBuilder> |
| 35 | #include <QVersionNumber> |
| 36 | |
| 37 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 38 | #include <QFileDialog> |
| 39 | #include <QFontDialog> |
| 40 | #include <QStyleFactory> |
| 41 | #endif |
| 42 | |
| 43 | #if defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 7, 0)) |
| 44 | #include <QOperatingSystemVersion> |
| 45 | #define QT_UTILITIES_USE_FUSION_ON_WINDOWS_11 |
| 46 | #endif |
| 47 | |
| 48 | #include <iostream> |
| 49 | #include <memory> |
| 50 | #include <optional> |
| 51 | |
| 52 | namespace QtUtilities { |
| 53 | |
| 54 | struct QtSettingsData { |
| 55 | QtSettingsData(); |
| 56 | |
| 57 | QFont font; |
| 58 | std::optional<QFont> initialFont; |
| 59 | QPalette palette; // the currently applied palette (only in use if customPalette is true, though) |
| 60 | QPalette selectedPalette; // the intermediately selected palette (chosen in palette editor but not yet applied) |
| 61 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 62 | QString widgetStyle; |
| 63 | QString initialWidgetStyle; |
| 64 | QString styleSheetPath; |
| 65 | #endif |
| 66 | QString iconTheme; |
| 67 | QString initialIconTheme; |
| 68 | QLocale defaultLocale; |
| 69 | QLocale previousLocale; |
| 70 | QString localeName; |
| 71 | QString previousPluginDirectory; |
| 72 | QString additionalPluginDirectory; |
| 73 | QString previousIconThemeSearchPath; |
| 74 | QString additionalIconThemeSearchPath; |
| 75 | bool customFont; |
| 76 | bool customPalette; |
| 77 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 78 | bool customWidgetStyle; |
| 79 | bool customStyleSheet; |
| 80 | #endif |
| 81 | bool customIconTheme; |
| 82 | bool customLocale; |
| 83 | bool isPaletteDark; |
| 84 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 85 | bool showNotices; |
| 86 | bool retranslatable; |
| 87 | #endif |
| 88 | }; |
| 89 | |
| 90 | inline QtSettingsData::QtSettingsData() |
| 91 | : iconTheme(QIcon::themeName()) |
| 92 | , initialIconTheme(iconTheme) |
| 93 | , localeName(defaultLocale.name()) |
| 94 | , customFont(false) |
| 95 | , customPalette(false) |
| 96 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 97 | , customWidgetStyle(false) |
| 98 | , customStyleSheet(false) |
| 99 | #endif |
| 100 | , customIconTheme(false) |
| 101 | , customLocale(false) |
| 102 | , isPaletteDark(false) |
| 103 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 104 | , showNotices(true) |
| 105 | , retranslatable(false) |
| 106 | #endif |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | /*! |
| 111 | * \brief Creates a new settings object. |
| 112 | * \remarks Settings are not restored automatically. Instead, some values (font, |
| 113 | * widget style, ...) are initialized |
| 114 | * from the current Qt configuration. These values are considered as |
| 115 | * system-default. |
| 116 | */ |
| 117 | QtSettings::QtSettings() |
| 118 | : m_d(std::make_unique<QtSettingsData>()) |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | /*! |
| 123 | * \brief Destroys the settings object. |
| 124 | * \remarks Unlike QSettings not explicitly saved settings are not saved |
| 125 | * automatically. |
| 126 | */ |
| 127 | QtSettings::~QtSettings() |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 132 | /*! |
| 133 | * \brief Disables notices on option pages that settings take only effect after restarting. |
| 134 | * \remarks |
| 135 | * - This function must be called before obtaining the option pages via category(). |
| 136 | * - Only call this function if the application actually re-applies these settings when the |
| 137 | * settings dialog is applied and when it is generally able to handle widget style and |
| 138 | * palette changes well. |
| 139 | * - The localization option page's notice is handled via setRetranslatable() and the notice |
| 140 | * for the environment page is still always shown (as those settings can never be applied |
| 141 | * at runtime). So this affects only the appearance page at this point. |
| 142 | */ |
| 143 | void QtSettings::disableNotices() |
| 144 | { |
| 145 | m_d->showNotices = false; |
| 146 | } |
| 147 | |
| 148 | /*! |
| 149 | * \brief Sets whether the application supports changing the locale settings at runtime. |
| 150 | * \remarks |
| 151 | * Set this to true if the application will retranslate its UI after the locale has changed. |
| 152 | * This requires the application to re-install translators and to re-invoke all ts() and |
| 153 | * translate() function calls. If set to true, the notice that the locale setting takes only |
| 154 | * effect after restarting is not shown anymore. |
| 155 | */ |
| 156 | void QtSettings::setRetranslatable(bool retranslatable) |
| 157 | { |
| 158 | m_d->retranslatable = retranslatable; |
| 159 | } |
| 160 | #endif |
| 161 | |
| 162 | /*! |
| 163 | * \brief Returns whether a custom font is set. |
| 164 | */ |
| 165 | bool QtSettings::hasCustomFont() const |
| 166 | { |
| 167 | return m_d->customFont; |
| 168 | } |
| 169 | |
| 170 | /*! |
| 171 | * \brief Restores the settings from the specified QSettings object. |
| 172 | * \remarks The restored values are not applied automatically (except |
| 173 | * translation path). |
| 174 | * \sa apply(), save() |
| 175 | */ |
| 176 | void QtSettings::restore(QSettings &settings) |
| 177 | { |
| 178 | settings.beginGroup(QStringLiteral("qt")); |
| 179 | m_d->font.fromString(settings.value(QStringLiteral("font")).toString()); |
| 180 | m_d->customFont = settings.value(QStringLiteral("customfont"), false).toBool(); |
| 181 | m_d->palette = settings.value(QStringLiteral("palette")).value<QPalette>(); |
| 182 | m_d->customPalette = settings.value(QStringLiteral("custompalette"), false).toBool(); |
| 183 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 184 | m_d->widgetStyle = settings.value(QStringLiteral("widgetstyle"), m_d->widgetStyle).toString(); |
| 185 | m_d->customWidgetStyle = settings.value(QStringLiteral("customwidgetstyle"), false).toBool(); |
| 186 | m_d->styleSheetPath = settings.value(QStringLiteral("stylesheetpath"), m_d->styleSheetPath).toString(); |
| 187 | m_d->customStyleSheet = settings.value(QStringLiteral("customstylesheet"), false).toBool(); |
| 188 | #endif |
| 189 | m_d->iconTheme = settings.value(QStringLiteral("icontheme"), m_d->iconTheme).toString(); |
| 190 | m_d->customIconTheme = settings.value(QStringLiteral("customicontheme"), false).toBool(); |
| 191 | m_d->localeName = settings.value(QStringLiteral("locale"), m_d->localeName).toString(); |
| 192 | m_d->customLocale = settings.value(QStringLiteral("customlocale"), false).toBool(); |
| 193 | m_d->additionalPluginDirectory = settings.value(QStringLiteral("plugindir")).toString(); |
| 194 | m_d->additionalIconThemeSearchPath = settings.value(QStringLiteral("iconthemepath")).toString(); |
| 195 | TranslationFiles::additionalTranslationFilePath() = settings.value(QStringLiteral("trpath")).toString(); |
| 196 | settings.endGroup(); |
| 197 | if (qEnvironmentVariableIntValue("QT_DEBUG_SETTINGS")) { |
| 198 | std::cerr << "Restored Qt settings values\n"; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | /*! |
| 203 | * \brief Saves the settings to the specified QSettings object. |
| 204 | */ |
| 205 | void QtSettings::save(QSettings &settings) const |
| 206 | { |
| 207 | settings.beginGroup(QStringLiteral("qt")); |
| 208 | settings.setValue(QStringLiteral("font"), QVariant(m_d->font.toString())); |
| 209 | settings.setValue(QStringLiteral("customfont"), m_d->customFont); |
| 210 | settings.setValue(QStringLiteral("palette"), QVariant(m_d->palette)); |
| 211 | settings.setValue(QStringLiteral("custompalette"), m_d->customPalette); |
| 212 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 213 | settings.setValue(QStringLiteral("widgetstyle"), m_d->widgetStyle); |
| 214 | settings.setValue(QStringLiteral("customwidgetstyle"), m_d->customWidgetStyle); |
| 215 | settings.setValue(QStringLiteral("stylesheetpath"), m_d->styleSheetPath); |
| 216 | settings.setValue(QStringLiteral("customstylesheet"), m_d->customStyleSheet); |
| 217 | #endif |
| 218 | settings.setValue(QStringLiteral("icontheme"), m_d->iconTheme); |
| 219 | settings.setValue(QStringLiteral("customicontheme"), m_d->customIconTheme); |
| 220 | settings.setValue(QStringLiteral("locale"), m_d->localeName); |
| 221 | settings.setValue(QStringLiteral("customlocale"), m_d->customLocale); |
| 222 | settings.setValue(QStringLiteral("plugindir"), m_d->additionalPluginDirectory); |
| 223 | settings.setValue(QStringLiteral("iconthemepath"), m_d->additionalIconThemeSearchPath); |
| 224 | settings.setValue(QStringLiteral("trpath"), QVariant(TranslationFiles::additionalTranslationFilePath())); |
| 225 | settings.endGroup(); |
| 226 | if (qEnvironmentVariableIntValue("QT_DEBUG_SETTINGS")) { |
| 227 | std::cerr << "Set Qt settings values\n"; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /*! |
| 232 | * \brief Returns the icon themes present in the specified \a searchPaths. |
| 233 | * \remarks The display name is the key and the actual icon theme name the value. |
| 234 | * This way the map is sorted correctly for display purposes. |
| 235 | */ |
| 236 | static QMap<QString, QString> scanIconThemes(const QStringList &searchPaths) |
| 237 | { |
| 238 | auto res = QMap<QString, QString>(); |
| 239 | for (const auto &searchPath : searchPaths) { |
| 240 | const auto dir = QDir(searchPath).entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name); |
| 241 | for (const auto &iconTheme : dir) { |
| 242 | auto indexFile = QFile(searchPath % QChar('/') % iconTheme % QStringLiteral("/index.theme")); |
| 243 | auto index = QByteArray(); |
| 244 | if (indexFile.open(QFile::ReadOnly) && !(index = indexFile.readAll()).isEmpty()) { |
| 245 | const auto iconThemeSection = index.indexOf("[Icon Theme]"); |
| 246 | const auto nameStart = index.indexOf("Name=", iconThemeSection != -1 ? iconThemeSection : 0); |
| 247 | if (nameStart != -1) { |
| 248 | auto nameLength = index.indexOf("\n", nameStart) - nameStart - 5; |
| 249 | if (nameLength > 0) { |
| 250 | auto displayName = QString::fromUtf8(index.mid(nameStart + 5, nameLength)); |
| 251 | if (displayName != iconTheme) { |
| 252 | displayName += QChar(' ') % QChar('(') % iconTheme % QChar(')'); |
| 253 | } |
| 254 | res[displayName] = iconTheme; |
| 255 | continue; |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | res[iconTheme] = iconTheme; |
| 260 | } |
| 261 | } |
| 262 | return res; |
| 263 | } |
| 264 | |
| 265 | /*! |
| 266 | * \brief Applies the current configuration. |
| 267 | * \remarks |
| 268 | * QApplication/QGuiApplication must be instantiated before calling this |
| 269 | * method. Hence it makes most sense to call this directly after instantiating |
| 270 | * QApplication/QGuiApplication. |
| 271 | * |
| 272 | * This function may be called multiple times. This supports restoring default |
| 273 | * settings (e.g. if use of a custom icon theme has been disabled again after it |
| 274 | * was enabled the default icon theme will be configured again). |
| 275 | */ |
| 276 | void QtSettings::apply() |
| 277 | { |
| 278 | // apply environment |
| 279 | if (m_d->additionalPluginDirectory != m_d->previousPluginDirectory) { |
| 280 | if (!m_d->previousPluginDirectory.isEmpty()) { |
| 281 | QCoreApplication::removeLibraryPath(m_d->previousPluginDirectory); |
| 282 | } |
| 283 | if (!m_d->additionalPluginDirectory.isEmpty()) { |
| 284 | QCoreApplication::addLibraryPath(m_d->additionalPluginDirectory); |
| 285 | } |
| 286 | m_d->previousPluginDirectory = m_d->additionalPluginDirectory; |
| 287 | } |
| 288 | if (m_d->additionalIconThemeSearchPath != m_d->previousIconThemeSearchPath) { |
| 289 | auto paths = QIcon::themeSearchPaths(); |
| 290 | if (!m_d->previousIconThemeSearchPath.isEmpty()) { |
| 291 | paths.removeAll(m_d->previousIconThemeSearchPath); |
| 292 | } |
| 293 | if (!m_d->additionalIconThemeSearchPath.isEmpty()) { |
| 294 | paths.append(m_d->additionalIconThemeSearchPath); |
| 295 | } |
| 296 | m_d->previousIconThemeSearchPath = m_d->additionalIconThemeSearchPath; |
| 297 | QIcon::setThemeSearchPaths(paths); |
| 298 | } |
| 299 | |
| 300 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 301 | // read style sheet |
| 302 | auto styleSheet = QString(); |
| 303 | if (m_d->customStyleSheet && !m_d->styleSheetPath.isEmpty()) { |
| 304 | auto file = QFile(m_d->styleSheetPath); |
| 305 | if (!file.open(QFile::ReadOnly)) { |
| 306 | std::cerr << "Unable to open the specified stylesheet \"" << m_d->styleSheetPath.toLocal8Bit().data() << "\"." << std::endl; |
| 307 | } |
| 308 | styleSheet.append(file.readAll()); |
| 309 | if (file.error() != QFile::NoError) { |
| 310 | std::cerr << "Unable to read the specified stylesheet \"" << m_d->styleSheetPath.toLocal8Bit().data() << "\"." << std::endl; |
| 311 | } |
| 312 | } |
| 313 | #endif |
| 314 | |
| 315 | // apply appearance |
| 316 | if (m_d->customFont) { |
| 317 | if (!m_d->initialFont.has_value()) { |
| 318 | m_d->initialFont = QGuiApplication::font(); |
| 319 | } |
| 320 | QGuiApplication::setFont(m_d->font); |
| 321 | } else if (m_d->initialFont.has_value()) { |
| 322 | QGuiApplication::setFont(m_d->initialFont.value()); |
| 323 | } |
| 324 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 325 | #ifdef QT_UTILITIES_USE_FUSION_ON_WINDOWS_11 |
| 326 | if (m_d->initialWidgetStyle.isEmpty()) { |
| 327 | // use Fusion on Windows 11 as the native style doesn't look good |
| 328 | // see https://bugreports.qt.io/browse/QTBUG-97668 |
| 329 | if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows11) { |
| 330 | m_d->initialWidgetStyle = QStringLiteral("Fusion"); |
| 331 | } |
| 332 | } |
| 333 | #endif |
| 334 | if (m_d->customWidgetStyle) { |
| 335 | #if QT_VERSION >= QT_VERSION_CHECK(6, 1, 0) |
| 336 | const auto *const currentStyle = QApplication::style(); |
| 337 | if (m_d->initialWidgetStyle.isEmpty() && currentStyle) { |
| 338 | m_d->initialWidgetStyle = currentStyle->name(); |
| 339 | } |
| 340 | #endif |
| 341 | QApplication::setStyle(m_d->widgetStyle); |
| 342 | } else if (!m_d->initialWidgetStyle.isEmpty()) { |
| 343 | QApplication::setStyle(m_d->initialWidgetStyle); |
| 344 | } |
| 345 | if (auto *const qapp = qobject_cast<QApplication *>(QApplication::instance())) { |
| 346 | qapp->setStyleSheet(styleSheet); |
| 347 | } else { |
| 348 | std::cerr << "Unable to apply the specified stylesheet \"" << m_d->styleSheetPath.toLocal8Bit().data() |
| 349 | << "\" because no QApplication has been instantiated." << std::endl; |
| 350 | } |
| 351 | #endif |
| 352 | if (m_d->customPalette) { |
| 353 | QGuiApplication::setPalette(m_d->palette); |
| 354 | } else { |
| 355 | QGuiApplication::setPalette(QPalette()); |
| 356 | } |
| 357 | m_d->isPaletteDark = QtUtilities::isPaletteDark(); |
| 358 | if (m_d->customIconTheme) { |
| 359 | QIcon::setThemeName(m_d->iconTheme); |
| 360 | } else if (!m_d->initialIconTheme.isEmpty()) { |
| 361 | if (m_d->iconTheme != m_d->initialIconTheme) { |
| 362 | // set the icon theme back to what it was before changing anything (not sure how to read the current system icon theme again) |
| 363 | QIcon::setThemeName(m_d->initialIconTheme); |
| 364 | } |
| 365 | } else { |
| 366 | // use bundled default icon theme matching the current palette |
| 367 | // notes: - It is ok that search paths specified via CLI arguments are not set here yet. When doing so one should also |
| 368 | // specify the desired icon theme explicitly. |
| 369 | // - The icon themes "default" and "default-dark" come from QtConfig.cmake which makes the first non-dark bundled |
| 370 | // icon theme available as "default" and the first dark icon theme available as "default-dark". An icon theme |
| 371 | // is considered dark if it ends with "-dark". |
| 372 | const auto bundledIconThemes = scanIconThemes(QStringList(QStringLiteral(":/icons"))); |
| 373 | if (m_d->isPaletteDark && bundledIconThemes.contains(QStringLiteral("default-dark"))) { |
| 374 | QIcon::setThemeName(QStringLiteral("default-dark")); |
| 375 | } else if (bundledIconThemes.contains(QStringLiteral("default"))) { |
| 376 | QIcon::setThemeName(QStringLiteral("default")); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // apply locale |
| 381 | m_d->previousLocale = QLocale(); |
| 382 | QLocale::setDefault(m_d->customLocale ? QLocale(m_d->localeName) : m_d->defaultLocale); |
| 383 | |
| 384 | // log some debug information on the first call if env variable set |
| 385 | static auto debugInfoLogged = false; |
| 386 | if (debugInfoLogged) { |
| 387 | return; |
| 388 | } |
| 389 | const auto debugLoggingEnabled = CppUtilities::isEnvVariableSet(PROJECT_VARNAME_UPPER "_LOG_QT_CONFIG"); |
| 390 | if (debugLoggingEnabled.has_value() && debugLoggingEnabled.value()) { |
| 391 | if (const auto os = QOperatingSystemVersion::current(); os.type() != static_cast<decltype(os.type())>(QOperatingSystemVersion::Unknown)) { |
| 392 | const auto version = QVersionNumber(os.majorVersion(), os.minorVersion(), os.microVersion()); |
| 393 | std::cerr << "OS name and version: " << os.name().toStdString() << ' ' << version.toString().toStdString() << '\n'; |
| 394 | } |
| 395 | std::cerr << "Qt version: " << qVersion() << '\n'; |
| 396 | std::cerr << "Qt platform (set QT_QPA_PLATFORM to override): " << QGuiApplication::platformName().toStdString() << '\n'; |
| 397 | std::cerr << "Qt locale: " << QLocale().name().toStdString() << '\n'; |
| 398 | std::cerr << "Qt library paths: " << QCoreApplication::libraryPaths().join(':').toStdString() << '\n'; |
| 399 | std::cerr << "Qt theme search paths: " << QIcon::themeSearchPaths().join(':').toStdString() << '\n'; |
| 400 | std::cerr << "Darkmode enabled: " << (QtUtilities::isDarkModeEnabled() ? "yes" : "no") << '\n'; |
| 401 | std::cerr << "Is Qt palette dark: " << (m_d->isPaletteDark ? "yes" : "no") << '\n'; |
| 402 | debugInfoLogged = true; |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | /*! |
| 407 | * \brief Re-applies default icon theme assuming the palette is dark or not depending on \a isPaletteDark. |
| 408 | * |
| 409 | * Re-assigns the appropriate default icon theme depending on the current palette. Call this function after the |
| 410 | * darkmode setting has changed without the current palette reflecting that but you want the darkmode setting take |
| 411 | * precedence over the palette (e.g. when using Qt Quick Material style under Android). If your application makes |
| 412 | * actually use of the palette and you want the palette take precedence than call \a reevaluatePaletteAndDefaultIconTheme() |
| 413 | * instead. |
| 414 | * |
| 415 | * Note that QtSettings::isPaletteDark() will return \a isPaletteDark after calling this function (even though the current |
| 416 | * palette is not actually reflecting that). |
| 417 | * |
| 418 | * \remarks |
| 419 | * - The default icon theme must have been assigned before using the apply() function. |
| 420 | * - This function has no effect if a custom icon theme is configured. |
| 421 | */ |
| 422 | void QtSettings::reapplyDefaultIconTheme(bool isPaletteDark) |
| 423 | { |
| 424 | if (isPaletteDark == m_d->isPaletteDark) { |
| 425 | return; // no need to do anything if there's no change |
| 426 | } |
| 427 | m_d->isPaletteDark = isPaletteDark; |
| 428 | if (auto iconTheme = QIcon::themeName(); iconTheme == QStringLiteral("default") || iconTheme == QStringLiteral("default-dark")) { |
| 429 | QIcon::setThemeName(m_d->isPaletteDark ? QStringLiteral("default-dark") : QStringLiteral("default")); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /*! |
| 434 | * \brief Re-evaluates whether the palette is dark and re-applies default icon theme. |
| 435 | * |
| 436 | * Re-assigns the appropriate default icon theme depending on the current palette. Call this function after the palette |
| 437 | * has changed. |
| 438 | * |
| 439 | * \remarks |
| 440 | * - The default icon theme must have been assigned before using the apply() function. |
| 441 | * - This function has no effect if a custom icon theme is configured. |
| 442 | */ |
| 443 | void QtSettings::reevaluatePaletteAndDefaultIconTheme() |
| 444 | { |
| 445 | reapplyDefaultIconTheme(QtUtilities::isPaletteDark()); |
| 446 | } |
| 447 | |
| 448 | /*! |
| 449 | * \brief Returns whether the palette is dark. |
| 450 | * \remarks |
| 451 | * Changes to the palette since the last call to apply() and reevaluatePaletteAndDefaultIconTheme() are not taken |
| 452 | * into account. |
| 453 | */ |
| 454 | bool QtSettings::isPaletteDark() |
| 455 | { |
| 456 | return m_d->isPaletteDark; |
| 457 | } |
| 458 | |
| 459 | /*! |
| 460 | * \brief Returns whether the last apply() call has changed the default locale. |
| 461 | */ |
| 462 | bool QtSettings::hasLocaleChanged() const |
| 463 | { |
| 464 | return m_d->previousLocale != QLocale(); |
| 465 | } |
| 466 | |
| 467 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 468 | /*! |
| 469 | * \brief Returns a new OptionCatecory containing all Qt related option pages. |
| 470 | * \remarks |
| 471 | * - The QtSettings instance does not keep the ownership over the returned |
| 472 | * category. |
| 473 | * - The pages of the returned category require the QtSettings instance which |
| 474 | * hence must be present as long as all pages are destroyed. |
| 475 | */ |
| 476 | OptionCategory *QtSettings::category() |
| 477 | { |
| 478 | auto *category = new OptionCategory; |
| 479 | category->setDisplayName(QCoreApplication::translate("QtGui::QtOptionCategory", "User interface")); |
| 480 | category->setIcon(QIcon::fromTheme(QStringLiteral("qtcreator"), QIcon(QStringLiteral(":/qtutilities/icons/hicolor/48x48/apps/qtcreator.svg")))); |
| 481 | category->assignPages({ new QtAppearanceOptionPage(*m_d), new QtLanguageOptionPage(*m_d), new QtEnvOptionPage(*m_d) }); |
| 482 | return category; |
| 483 | } |
| 484 | |
| 485 | QtAppearanceOptionPage::QtAppearanceOptionPage(QtSettingsData &settings, QWidget *parentWidget) |
| 486 | : QtAppearanceOptionPageBase(parentWidget) |
| 487 | , m_settings(settings) |
| 488 | , m_fontDialog(nullptr) |
| 489 | { |
| 490 | } |
| 491 | |
| 492 | QtAppearanceOptionPage::~QtAppearanceOptionPage() |
| 493 | { |
| 494 | } |
| 495 | |
| 496 | bool QtAppearanceOptionPage::apply() |
| 497 | { |
| 498 | m_settings.font = ui()->fontComboBox->currentFont(); |
| 499 | m_settings.customFont = !ui()->fontCheckBox->isChecked(); |
| 500 | m_settings.widgetStyle = ui()->widgetStyleComboBox->currentText(); |
| 501 | m_settings.customWidgetStyle = !ui()->widgetStyleCheckBox->isChecked(); |
| 502 | m_settings.styleSheetPath = ui()->styleSheetPathSelection->lineEdit()->text(); |
| 503 | m_settings.customStyleSheet = !ui()->styleSheetCheckBox->isChecked(); |
| 504 | m_settings.palette = m_settings.selectedPalette; |
| 505 | m_settings.customPalette = !ui()->paletteCheckBox->isChecked(); |
| 506 | m_settings.iconTheme |
| 507 | = ui()->iconThemeComboBox->currentIndex() != -1 ? ui()->iconThemeComboBox->currentData().toString() : ui()->iconThemeComboBox->currentText(); |
| 508 | m_settings.customIconTheme = !ui()->iconThemeCheckBox->isChecked(); |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | void QtAppearanceOptionPage::reset() |
| 513 | { |
| 514 | ui()->fontComboBox->setCurrentFont(m_settings.font); |
| 515 | ui()->fontCheckBox->setChecked(!m_settings.customFont); |
| 516 | ui()->widgetStyleComboBox->setCurrentText( |
| 517 | m_settings.widgetStyle.isEmpty() ? (QApplication::style() ? QApplication::style()->objectName() : QString()) : m_settings.widgetStyle); |
| 518 | ui()->widgetStyleCheckBox->setChecked(!m_settings.customWidgetStyle); |
| 519 | ui()->styleSheetPathSelection->lineEdit()->setText(m_settings.styleSheetPath); |
| 520 | ui()->styleSheetCheckBox->setChecked(!m_settings.customStyleSheet); |
| 521 | m_settings.selectedPalette = m_settings.palette; |
| 522 | ui()->paletteCheckBox->setChecked(!m_settings.customPalette); |
| 523 | int iconThemeIndex = ui()->iconThemeComboBox->findData(m_settings.iconTheme); |
| 524 | if (iconThemeIndex != -1) { |
| 525 | ui()->iconThemeComboBox->setCurrentIndex(iconThemeIndex); |
| 526 | } else { |
| 527 | ui()->iconThemeComboBox->setCurrentText(m_settings.iconTheme); |
| 528 | } |
| 529 | ui()->iconThemeCheckBox->setChecked(!m_settings.customIconTheme); |
| 530 | } |
| 531 | |
| 532 | QWidget *QtAppearanceOptionPage::setupWidget() |
| 533 | { |
| 534 | // call base implementation first, so ui() is available |
| 535 | auto *widget = QtAppearanceOptionPageBase::setupWidget(); |
| 536 | if (!m_settings.showNotices) { |
| 537 | ui()->label->hide(); |
| 538 | } |
| 539 | |
| 540 | // setup widget style selection |
| 541 | ui()->widgetStyleComboBox->addItems(QStyleFactory::keys()); |
| 542 | |
| 543 | // setup style sheet selection |
| 544 | ui()->styleSheetPathSelection->provideCustomFileMode(QFileDialog::ExistingFile); |
| 545 | |
| 546 | // setup font selection |
| 547 | QObject::connect(ui()->fontPushButton, &QPushButton::clicked, widget, [this] { |
| 548 | if (!m_fontDialog) { |
| 549 | m_fontDialog = new QFontDialog(this->widget()); |
| 550 | m_fontDialog->setCurrentFont(ui()->fontComboBox->font()); |
| 551 | QObject::connect(m_fontDialog, &QFontDialog::fontSelected, ui()->fontComboBox, &QFontComboBox::setCurrentFont); |
| 552 | QObject::connect(ui()->fontComboBox, &QFontComboBox::currentFontChanged, m_fontDialog, &QFontDialog::setCurrentFont); |
| 553 | } |
| 554 | m_fontDialog->show(); |
| 555 | }); |
| 556 | |
| 557 | // setup palette selection |
| 558 | QObject::connect(ui()->paletteToolButton, &QToolButton::clicked, ui()->paletteToolButton, |
| 559 | [this] { m_settings.selectedPalette = PaletteEditor::getPalette(this->widget(), m_settings.selectedPalette); }); |
| 560 | |
| 561 | // setup icon theme selection |
| 562 | const auto iconThemes = scanIconThemes(QIcon::themeSearchPaths() << QStringLiteral("/usr/share/icons/")); |
| 563 | auto *iconThemeComboBox = ui()->iconThemeComboBox; |
| 564 | for (auto i = iconThemes.begin(), end = iconThemes.end(); i != end; ++i) { |
| 565 | const auto &displayName = i.key(); |
| 566 | const auto &id = i.value(); |
| 567 | if (const auto existingItemIndex = iconThemeComboBox->findData(id); existingItemIndex != -1) { |
| 568 | iconThemeComboBox->setItemText(existingItemIndex, displayName); |
| 569 | } else { |
| 570 | iconThemeComboBox->addItem(displayName, id); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return widget; |
| 575 | } |
| 576 | |
| 577 | QtLanguageOptionPage::QtLanguageOptionPage(QtSettingsData &settings, QWidget *parentWidget) |
| 578 | : QtLanguageOptionPageBase(parentWidget) |
| 579 | , m_settings(settings) |
| 580 | { |
| 581 | } |
| 582 | |
| 583 | QtLanguageOptionPage::~QtLanguageOptionPage() |
| 584 | { |
| 585 | } |
| 586 | |
| 587 | bool QtLanguageOptionPage::apply() |
| 588 | { |
| 589 | m_settings.localeName = ui()->localeComboBox->currentText(); |
| 590 | m_settings.customLocale = !ui()->localeCheckBox->isChecked(); |
| 591 | return true; |
| 592 | } |
| 593 | |
| 594 | void QtLanguageOptionPage::reset() |
| 595 | { |
| 596 | ui()->localeComboBox->setCurrentText(m_settings.localeName); |
| 597 | ui()->localeCheckBox->setChecked(!m_settings.customLocale); |
| 598 | } |
| 599 | |
| 600 | QWidget *QtLanguageOptionPage::setupWidget() |
| 601 | { |
| 602 | // call base implementation first, so ui() is available |
| 603 | auto *widget = QtLanguageOptionPageBase::setupWidget(); |
| 604 | if (m_settings.retranslatable) { |
| 605 | ui()->label->hide(); |
| 606 | } |
| 607 | |
| 608 | // add all available locales to combo box |
| 609 | auto *localeComboBox = ui()->localeComboBox; |
| 610 | const auto locales = QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry); |
| 611 | for (const QLocale &locale : locales) { |
| 612 | localeComboBox->addItem(locale.name()); |
| 613 | } |
| 614 | |
| 615 | auto *languageLabel = ui()->languageLabel; |
| 616 | QObject::connect(ui()->localeComboBox, &QComboBox::currentTextChanged, languageLabel, [languageLabel, localeComboBox] { |
| 617 | const auto selectedLocale = QLocale(localeComboBox->currentText()); |
| 618 | const auto currentLocale = QLocale(); |
| 619 | const auto territory = |
| 620 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) |
| 621 | currentLocale.territoryToString(selectedLocale.territory()); |
| 622 | #else |
| 623 | currentLocale.countryToString(selectedLocale.country()); |
| 624 | #endif |
| 625 | languageLabel->setText(QCoreApplication::translate("QtGui::QtLanguageOptionPage", "recognized by Qt as") % QStringLiteral(" <i>") |
| 626 | % currentLocale.languageToString(selectedLocale.language()) % QChar(',') % QChar(' ') % territory % QStringLiteral("</i>")); |
| 627 | }); |
| 628 | return widget; |
| 629 | } |
| 630 | |
| 631 | QtEnvOptionPage::QtEnvOptionPage(QtSettingsData &settings, QWidget *parentWidget) |
| 632 | : QtEnvOptionPageBase(parentWidget) |
| 633 | , m_settings(settings) |
| 634 | { |
| 635 | } |
| 636 | |
| 637 | QtEnvOptionPage::~QtEnvOptionPage() |
| 638 | { |
| 639 | } |
| 640 | |
| 641 | bool QtEnvOptionPage::apply() |
| 642 | { |
| 643 | m_settings.additionalPluginDirectory = ui()->pluginPathSelection->lineEdit()->text(); |
| 644 | m_settings.additionalIconThemeSearchPath = ui()->iconThemeSearchPathSelection->lineEdit()->text(); |
| 645 | TranslationFiles::additionalTranslationFilePath() = ui()->translationPathSelection->lineEdit()->text(); |
| 646 | return true; |
| 647 | } |
| 648 | |
| 649 | void QtEnvOptionPage::reset() |
| 650 | { |
| 651 | ui()->pluginPathSelection->lineEdit()->setText(m_settings.additionalPluginDirectory); |
| 652 | ui()->iconThemeSearchPathSelection->lineEdit()->setText(m_settings.additionalIconThemeSearchPath); |
| 653 | ui()->translationPathSelection->lineEdit()->setText(TranslationFiles::additionalTranslationFilePath()); |
| 654 | } |
| 655 | |
| 656 | QWidget *QtEnvOptionPage::setupWidget() |
| 657 | { |
| 658 | // call base implementation first, so ui() is available |
| 659 | return QtEnvOptionPageBase::setupWidget(); |
| 660 | } |
| 661 | #endif |
| 662 | |
| 663 | /*! |
| 664 | * \brief Returns a handle to the internal data. |
| 665 | * \remarks |
| 666 | * This is an opaque data structure. It can be used to construct option pages |
| 667 | * like QtLanguageOptionPage. |
| 668 | */ |
| 669 | QtSettings::operator QtSettingsData &() const |
| 670 | { |
| 671 | return *m_d.get(); |
| 672 | } |
| 673 | |
| 674 | } // namespace QtUtilities |
| 675 | |
| 676 | #if defined(QT_UTILITIES_GUI_QTWIDGETS) |
| 677 | INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(QtAppearanceOptionPage) |
| 678 | INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(QtLanguageOptionPage) |
| 679 | INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(QtEnvOptionPage) |
| 680 | #endif |