7 #include "../paletteeditor/paletteeditor.h" 9 #include "../widgets/clearlineedit.h" 11 #include "../resources/resources.h" 13 #include "ui_qtappearanceoptionpage.h" 14 #include "ui_qtenvoptionpage.h" 15 #include "ui_qtlanguageoptionpage.h" 20 #include <QFileDialog> 21 #include <QFontDialog> 24 #include <QStringBuilder> 25 #include <QStyleFactory> 53 inline QtSettingsData::QtSettingsData()
54 : iconTheme(QIcon::themeName())
55 , localeName(defaultLocale.name())
57 , customPalette(false)
58 , customWidgetStyle(false)
59 , customStyleSheet(false)
60 , customIconTheme(false)
72 QtSettings::QtSettings()
82 QtSettings::~QtSettings()
89 bool QtSettings::hasCustomFont()
const 91 return m_d->customFont;
100 void QtSettings::restore(QSettings &settings)
102 settings.beginGroup(QStringLiteral(
"qt"));
103 m_d->font.fromString(settings.value(QStringLiteral(
"font")).toString());
104 m_d->customFont = settings.value(QStringLiteral(
"customfont"),
false).toBool();
105 m_d->palette = settings.value(QStringLiteral(
"palette")).value<QPalette>();
106 m_d->customPalette = settings.value(QStringLiteral(
"custompalette"),
false).toBool();
107 m_d->widgetStyle = settings.value(QStringLiteral(
"widgetstyle"), m_d->widgetStyle).toString();
108 m_d->customWidgetStyle = settings.value(QStringLiteral(
"customwidgetstyle"),
false).toBool();
109 m_d->styleSheetPath = settings.value(QStringLiteral(
"stylesheetpath"), m_d->styleSheetPath).toString();
110 m_d->customStyleSheet = settings.value(QStringLiteral(
"customstylesheet"),
false).toBool();
111 m_d->iconTheme = settings.value(QStringLiteral(
"icontheme"), m_d->iconTheme).toString();
112 m_d->customIconTheme = settings.value(QStringLiteral(
"customicontheme"),
false).toBool();
113 m_d->localeName = settings.value(QStringLiteral(
"locale"), m_d->localeName).toString();
114 m_d->customLocale = settings.value(QStringLiteral(
"customlocale"),
false).toBool();
115 m_d->additionalPluginDirectory = settings.value(QStringLiteral(
"plugindir")).toString();
116 m_d->additionalIconThemeSearchPath = settings.value(QStringLiteral(
"iconthemepath")).toString();
124 void QtSettings::save(QSettings &settings)
const 126 settings.beginGroup(QStringLiteral(
"qt"));
127 settings.setValue(QStringLiteral(
"font"), m_d->font.toString());
128 settings.setValue(QStringLiteral(
"customfont"), m_d->customFont);
129 settings.setValue(QStringLiteral(
"palette"), m_d->palette);
130 settings.setValue(QStringLiteral(
"custompalette"), m_d->customPalette);
131 settings.setValue(QStringLiteral(
"widgetstyle"), m_d->widgetStyle);
132 settings.setValue(QStringLiteral(
"customwidgetstyle"), m_d->customWidgetStyle);
133 settings.setValue(QStringLiteral(
"stylesheetpath"), m_d->styleSheetPath);
134 settings.setValue(QStringLiteral(
"customstylesheet"), m_d->customStyleSheet);
135 settings.setValue(QStringLiteral(
"icontheme"), m_d->iconTheme);
136 settings.setValue(QStringLiteral(
"customicontheme"), m_d->customIconTheme);
137 settings.setValue(QStringLiteral(
"locale"), m_d->localeName);
138 settings.setValue(QStringLiteral(
"customlocale"), m_d->customLocale);
139 settings.setValue(QStringLiteral(
"plugindir"), m_d->additionalPluginDirectory);
140 settings.setValue(QStringLiteral(
"iconthemepath"), m_d->additionalIconThemeSearchPath);
154 void QtSettings::apply()
158 if (m_d->customStyleSheet && !m_d->styleSheetPath.isEmpty()) {
159 QFile file(m_d->styleSheetPath);
160 if (!file.open(QFile::ReadOnly)) {
161 cerr <<
"Unable to open the specified stylesheet \"" << m_d->styleSheetPath.toLocal8Bit().data() <<
"\"." << endl;
163 styleSheet.append(file.readAll());
164 if (file.error() != QFile::NoError) {
165 cerr <<
"Unable to read the specified stylesheet \"" << m_d->styleSheetPath.toLocal8Bit().data() <<
"\"." << endl;
170 if (m_d->customFont) {
171 QGuiApplication::setFont(m_d->font);
173 if (m_d->customWidgetStyle) {
174 QApplication::setStyle(m_d->widgetStyle);
176 if (!styleSheet.isEmpty()) {
177 if (
auto *qapp = qobject_cast<QApplication *>(QApplication::instance())) {
178 qapp->setStyleSheet(styleSheet);
180 cerr <<
"Unable to apply the specified stylesheet \"" << m_d->styleSheetPath.toLocal8Bit().data()
181 <<
"\" because no QApplication has been instantiated." << endl;
184 if (m_d->customPalette) {
185 QGuiApplication::setPalette(m_d->palette);
187 if (m_d->customIconTheme) {
188 QIcon::setThemeName(m_d->iconTheme);
192 QLocale::setDefault(m_d->customLocale ? m_d->localeName : m_d->defaultLocale);
195 if (m_d->additionalPluginDirectory.isEmpty()) {
196 QCoreApplication::addLibraryPath(m_d->additionalPluginDirectory);
198 if (!m_d->additionalIconThemeSearchPath.isEmpty()) {
199 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << m_d->additionalIconThemeSearchPath);
212 OptionCategory *QtSettings::category()
214 auto *category =
new OptionCategory;
215 category->setDisplayName(QCoreApplication::translate(
"QtGui::QtOptionCategory",
"Qt"));
216 category->setIcon(QIcon::fromTheme(QStringLiteral(
"qtcreator"), QIcon(QStringLiteral(
":/qtutilities/icons/hicolor/48x48/apps/qtcreator.svg"))));
222 : QtAppearanceOptionPageBase(parentWidget)
223 , m_settings(settings)
224 , m_fontDialog(nullptr)
228 QtAppearanceOptionPage::~QtAppearanceOptionPage()
232 bool QtAppearanceOptionPage::apply()
234 m_settings.
font = ui()->fontComboBox->font();
235 m_settings.
customFont = !ui()->fontCheckBox->isChecked();
236 m_settings.
widgetStyle = ui()->widgetStyleComboBox->currentText();
238 m_settings.
styleSheetPath = ui()->styleSheetPathSelection->lineEdit()->text();
240 m_settings.
palette = ui()->paletteToolButton->palette();
241 m_settings.
customPalette = !ui()->paletteCheckBox->isChecked();
243 = ui()->iconThemeComboBox->currentIndex() != -1 ? ui()->iconThemeComboBox->currentData().toString() : ui()->iconThemeComboBox->currentText();
248 void QtAppearanceOptionPage::reset()
250 ui()->fontComboBox->setCurrentFont(m_settings.
font);
251 ui()->fontCheckBox->setChecked(!m_settings.
customFont);
252 ui()->widgetStyleComboBox->setCurrentText(
253 m_settings.
widgetStyle.isEmpty() ? (QApplication::style() ? QApplication::style()->objectName() : QString()) : m_settings.
widgetStyle);
255 ui()->styleSheetPathSelection->lineEdit()->setText(m_settings.
styleSheetPath);
257 ui()->paletteToolButton->setPalette(m_settings.
palette);
258 ui()->paletteCheckBox->setChecked(!m_settings.
customPalette);
259 int iconThemeIndex = ui()->iconThemeComboBox->findData(m_settings.
iconTheme);
260 if (iconThemeIndex != -1) {
261 ui()->iconThemeComboBox->setCurrentIndex(iconThemeIndex);
263 ui()->iconThemeComboBox->setCurrentText(m_settings.
iconTheme);
268 QWidget *QtAppearanceOptionPage::setupWidget()
271 auto *widget = QtAppearanceOptionPageBase::setupWidget();
274 ui()->widgetStyleComboBox->addItems(QStyleFactory::keys());
277 ui()->styleSheetPathSelection->provideCustomFileMode(QFileDialog::ExistingFile);
280 QObject::connect(ui()->fontPushButton, &QPushButton::clicked, [
this] {
282 m_fontDialog =
new QFontDialog(this->widget());
283 m_fontDialog->setCurrentFont(ui()->fontComboBox->font());
284 QObject::connect(m_fontDialog, &QFontDialog::fontSelected, ui()->fontComboBox, &QFontComboBox::setCurrentFont);
285 QObject::connect(ui()->fontComboBox, &QFontComboBox::currentFontChanged, m_fontDialog, &QFontDialog::setCurrentFont);
287 m_fontDialog->show();
291 QObject::connect(ui()->paletteToolButton, &QToolButton::clicked,
292 [
this] { ui()->paletteToolButton->setPalette(PaletteEditor::getPalette(this->widget(), ui()->paletteToolButton->palette())); });
295 const QStringList searchPaths = QIcon::themeSearchPaths() << QStringLiteral(
"/usr/share/icons/");
296 for (
const QString &searchPath : searchPaths) {
297 for (
const QString &iconTheme : QDir(searchPath).entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)) {
298 const int existingItemIndex = ui()->iconThemeComboBox->findData(iconTheme);
299 QFile indexFile(searchPath % QChar(
'/') % iconTheme % QStringLiteral(
"/index.theme"));
301 if (indexFile.open(QFile::ReadOnly) && !(index = indexFile.readAll()).isEmpty()) {
302 const int iconThemeSection = index.indexOf(
"[Icon Theme]");
303 const int nameStart = index.indexOf(
"Name=", iconThemeSection != -1 ? iconThemeSection : 0);
304 if (nameStart != -1) {
305 int nameLength = index.indexOf(
"\n", nameStart) - nameStart - 5;
306 if (nameLength > 0) {
307 QString displayName = index.mid(nameStart + 5, nameLength);
308 if (displayName != iconTheme) {
309 displayName += QChar(
' ') % QChar(
'(') % iconTheme % QChar(
')');
311 if (existingItemIndex != -1) {
312 ui()->iconThemeComboBox->setItemText(existingItemIndex, displayName);
314 ui()->iconThemeComboBox->addItem(displayName, iconTheme);
320 if (existingItemIndex == -1) {
321 ui()->iconThemeComboBox->addItem(iconTheme, iconTheme);
330 : QtLanguageOptionPageBase(parentWidget)
331 , m_settings(settings)
335 QtLanguageOptionPage::~QtLanguageOptionPage()
339 bool QtLanguageOptionPage::apply()
341 m_settings.
localeName = ui()->localeComboBox->currentText();
342 m_settings.
customLocale = !ui()->localeCheckBox->isChecked();
346 void QtLanguageOptionPage::reset()
348 ui()->localeComboBox->setCurrentText(m_settings.
localeName);
349 ui()->localeCheckBox->setChecked(!m_settings.
customLocale);
352 QWidget *QtLanguageOptionPage::setupWidget()
355 auto *widget = QtLanguageOptionPageBase::setupWidget();
358 auto *localeComboBox = ui()->localeComboBox;
359 for (
const QLocale &locale : QLocale::matchingLocales(QLocale::AnyLanguage, QLocale::AnyScript, QLocale::AnyCountry)) {
360 localeComboBox->addItem(locale.name());
363 auto *languageLabel = ui()->languageLabel;
364 QObject::connect(ui()->localeComboBox, &QComboBox::currentTextChanged, [languageLabel, localeComboBox] {
365 const QLocale selectedLocale(localeComboBox->currentText());
366 const QLocale currentLocale;
367 languageLabel->setText(QCoreApplication::translate(
"QtGui::QtLanguageOptionPage",
"recognized by Qt as") % QStringLiteral(
" <i>")
368 % currentLocale.languageToString(selectedLocale.language()) % QChar(
',') % QChar(
' ')
369 % currentLocale.countryToString(selectedLocale.country()) % QStringLiteral(
"</i>"));
375 : QtEnvOptionPageBase(parentWidget)
376 , m_settings(settings)
380 QtEnvOptionPage::~QtEnvOptionPage()
384 bool QtEnvOptionPage::apply()
392 void QtEnvOptionPage::reset()
QtEnvOptionPage(QtSettingsData &settings, QWidget *parentWidget=nullptr)
QtAppearanceOptionPage(QtSettingsData &settings, QWidget *parentWidget=nullptr)
QtLanguageOptionPage(QtSettingsData &settings, QWidget *parentWidget=nullptr)
QT_UTILITIES_EXPORT QString & additionalTranslationFilePath()
Allows to set an additional search path for translation files.
Provides common dialogs such as AboutDialog, EnterPasswordDialog and SettingsDialog.
QString additionalPluginDirectory
QString additionalIconThemeSearchPath
#define INSTANTIATE_UI_FILE_BASED_OPTION_PAGE(SomeClass)
Instantiates a class declared with BEGIN_DECLARE_UI_FILE_BASED_OPTION_PAGE in a convenient way...