7 #include "../paletteeditor/paletteeditor.h" 9 #include "../widgets/clearlineedit.h" 11 #include "../resources/resources.h" 13 #include "ui_qtappearanceoptionpage.h" 14 #include "ui_qtlanguageoptionpage.h" 15 #include "ui_qtenvoptionpage.h" 19 #include <QStyleFactory> 20 #include <QFontDialog> 21 #include <QFileDialog> 23 #include <QStringBuilder> 54 inline QtSettingsData::QtSettingsData() :
57 customWidgetStyle(false),
58 customStyleSheet(false),
59 iconTheme(QIcon::themeName()),
60 customIconTheme(false),
61 localeName(defaultLocale.name()),
70 QtSettings::QtSettings() :
78 QtSettings::~QtSettings()
84 bool QtSettings::hasCustomFont()
const 86 return m_d->customFont;
94 void QtSettings::restore(QSettings &settings)
96 settings.beginGroup(QStringLiteral(
"qt"));
97 m_d->font.fromString(settings.value(QStringLiteral(
"font")).toString());
98 m_d->customFont = settings.value(QStringLiteral(
"customfont"),
false).toBool();
99 m_d->palette = settings.value(QStringLiteral(
"palette")).value<QPalette>();
100 m_d->customPalette = settings.value(QStringLiteral(
"custompalette"),
false).toBool();
101 m_d->widgetStyle = settings.value(QStringLiteral(
"widgetstyle"), m_d->widgetStyle).toString();
102 m_d->customWidgetStyle = settings.value(QStringLiteral(
"customwidgetstyle"),
false).toBool();
103 m_d->styleSheetPath = settings.value(QStringLiteral(
"stylesheetpath"), m_d->styleSheetPath).toString();
104 m_d->customStyleSheet = settings.value(QStringLiteral(
"customstylesheet"),
false).toBool();
105 m_d->iconTheme = settings.value(QStringLiteral(
"icontheme"), m_d->iconTheme).toString();
106 m_d->customIconTheme = settings.value(QStringLiteral(
"customicontheme"),
false).toBool();
107 m_d->localeName = settings.value(QStringLiteral(
"locale"), m_d->localeName).toString();
108 m_d->customLocale = settings.value(QStringLiteral(
"customlocale"),
false).toBool();
109 m_d->additionalPluginDirectory = settings.value(QStringLiteral(
"plugindir")).toString();
110 m_d->additionalIconThemeSearchPath = settings.value(QStringLiteral(
"iconthemepath")).toString();
118 void QtSettings::save(QSettings &settings)
const 120 settings.beginGroup(QStringLiteral(
"qt"));
121 settings.setValue(QStringLiteral(
"font"), m_d->font.toString());
122 settings.setValue(QStringLiteral(
"customfont"), m_d->customFont);
123 settings.setValue(QStringLiteral(
"palette"), m_d->palette);
124 settings.setValue(QStringLiteral(
"custompalette"), m_d->customPalette);
125 settings.setValue(QStringLiteral(
"widgetstyle"), m_d->widgetStyle);
126 settings.setValue(QStringLiteral(
"customwidgetstyle"), m_d->customWidgetStyle);
127 settings.setValue(QStringLiteral(
"stylesheetpath"), m_d->styleSheetPath);
128 settings.setValue(QStringLiteral(
"customstylesheet"), m_d->customStyleSheet);
129 settings.setValue(QStringLiteral(
"icontheme"), m_d->iconTheme);
130 settings.setValue(QStringLiteral(
"customicontheme"), m_d->customIconTheme);
131 settings.setValue(QStringLiteral(
"locale"), m_d->localeName);
132 settings.setValue(QStringLiteral(
"customlocale"), m_d->customLocale);
133 settings.setValue(QStringLiteral(
"plugindir"), m_d->additionalPluginDirectory);
134 settings.setValue(QStringLiteral(
"iconthemepath"), m_d->additionalIconThemeSearchPath);
146 void QtSettings::apply()
150 if(m_d->customStyleSheet && !m_d->styleSheetPath.isEmpty()) {
151 QFile file(m_d->styleSheetPath);
152 if(!file.open(QFile::ReadOnly)) {
153 cerr <<
"Unable to open the specified stylesheet \"" << m_d->styleSheetPath.toLocal8Bit().data() <<
"\"." << endl;
155 styleSheet.append(file.readAll());
156 if(file.error() != QFile::NoError) {
157 cerr <<
"Unable to read the specified stylesheet \"" << m_d->styleSheetPath.toLocal8Bit().data() <<
"\"." << endl;
162 if(m_d->customFont) {
163 QGuiApplication::setFont(m_d->font);
165 if(m_d->customWidgetStyle) {
166 QApplication::setStyle(m_d->widgetStyle);
168 if(!styleSheet.isEmpty()) {
169 if(
auto *qapp = qobject_cast<QApplication *>(QApplication::instance())) {
170 qapp->setStyleSheet(styleSheet);
172 cerr <<
"Unable to apply the specified stylesheet \"" << m_d->styleSheetPath.toLocal8Bit().data() <<
"\" because no QApplication has been instantiated." << endl;
175 if(m_d->customPalette) {
176 QGuiApplication::setPalette(m_d->palette);
178 if(m_d->customIconTheme) {
179 QIcon::setThemeName(m_d->iconTheme);
183 QLocale::setDefault(m_d->customLocale ? m_d->localeName : m_d->defaultLocale);
186 if(m_d->additionalPluginDirectory.isEmpty()) {
187 QCoreApplication::addLibraryPath(m_d->additionalPluginDirectory);
189 if(!m_d->additionalIconThemeSearchPath.isEmpty()) {
190 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << m_d->additionalIconThemeSearchPath);
204 category->
setDisplayName(QCoreApplication::translate(
"QtGui::QtOptionCategory",
"Qt"));
205 category->setIcon(QIcon::fromTheme(QStringLiteral(
"qtcreator"), QIcon(QStringLiteral(
":/qtutilities/icons/hicolor/48x48/apps/qtcreator.svg"))));
206 category->assignPages(QList<OptionPage *>()
214 QtAppearanceOptionPageBase(parentWidget),
215 m_settings(settings),
216 m_fontDialog(nullptr)
219 QtAppearanceOptionPage::~QtAppearanceOptionPage()
222 bool QtAppearanceOptionPage::apply()
225 m_settings.
font = ui()->fontComboBox->font();
226 m_settings.
customFont = !ui()->fontCheckBox->isChecked();
227 m_settings.
widgetStyle = ui()->widgetStyleComboBox->currentText();
229 m_settings.
styleSheetPath = ui()->styleSheetPathSelection->lineEdit()->text();
231 m_settings.
palette = ui()->paletteToolButton->palette();
232 m_settings.
customPalette = !ui()->paletteCheckBox->isChecked();
233 m_settings.
iconTheme = ui()->iconThemeComboBox->currentIndex() != -1 ? ui()->iconThemeComboBox->currentData().toString() : ui()->iconThemeComboBox->currentText();
239 void QtAppearanceOptionPage::reset()
242 ui()->fontComboBox->setCurrentFont(m_settings.
font);
243 ui()->fontCheckBox->setChecked(!m_settings.
customFont);
244 ui()->widgetStyleComboBox->setCurrentText(m_settings.
widgetStyle.isEmpty() ? (QApplication::style() ? QApplication::style()->objectName() : QString()) : m_settings.
widgetStyle);
246 ui()->styleSheetPathSelection->lineEdit()->setText(m_settings.
styleSheetPath);
248 ui()->paletteToolButton->setPalette(m_settings.
palette);
249 ui()->paletteCheckBox->setChecked(!m_settings.
customPalette);
250 int iconThemeIndex = ui()->iconThemeComboBox->findData(m_settings.
iconTheme);
251 if(iconThemeIndex != -1) {
252 ui()->iconThemeComboBox->setCurrentIndex(iconThemeIndex);
254 ui()->iconThemeComboBox->setCurrentText(m_settings.
iconTheme);
260 QWidget *QtAppearanceOptionPage::setupWidget()
263 auto *widget = QtAppearanceOptionPageBase::setupWidget();
266 ui()->widgetStyleComboBox->addItems(QStyleFactory::keys());
269 ui()->styleSheetPathSelection->provideCustomFileMode(QFileDialog::ExistingFile);
272 QObject::connect(ui()->fontPushButton, &QPushButton::clicked, [
this] {
274 m_fontDialog =
new QFontDialog(this->widget());
275 m_fontDialog->setCurrentFont(ui()->fontComboBox->font());
276 QObject::connect(m_fontDialog, &QFontDialog::fontSelected, ui()->fontComboBox, &QFontComboBox::setCurrentFont);
277 QObject::connect(ui()->fontComboBox, &QFontComboBox::currentFontChanged, m_fontDialog, &QFontDialog::setCurrentFont);
279 m_fontDialog->show();
283 QObject::connect(ui()->paletteToolButton, &QToolButton::clicked, [
this] {
288 const QStringList searchPaths = QIcon::themeSearchPaths() << QStringLiteral(
"/usr/share/icons/");
289 for(
const QString &searchPath : searchPaths) {
290 for(
const QString &iconTheme : QDir(searchPath).entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name)) {
291 const int existingItemIndex = ui()->iconThemeComboBox->findData(iconTheme);
292 QFile indexFile(searchPath % QChar(
'/') % iconTheme % QStringLiteral(
"/index.theme"));
294 if(indexFile.open(QFile::ReadOnly) && !(index = indexFile.readAll()).isEmpty()) {
295 const int iconThemeSection = index.indexOf(
"[Icon Theme]");
296 const int nameStart = index.indexOf(
"Name=", iconThemeSection != -1 ? iconThemeSection : 0);
297 if(nameStart != -1) {
298 int nameLength = index.indexOf(
"\n", nameStart) - nameStart - 5;
300 QString displayName = index.mid(nameStart + 5, nameLength);
301 if(displayName != iconTheme) {
302 displayName += QChar(
' ') % QChar(
'(') % iconTheme % QChar(
')');
304 if(existingItemIndex != -1) {
305 ui()->iconThemeComboBox->setItemText(existingItemIndex, displayName);
307 ui()->iconThemeComboBox->addItem(displayName, iconTheme);
313 if(existingItemIndex == -1) {
314 ui()->iconThemeComboBox->addItem(iconTheme, iconTheme);
323 QtLanguageOptionPageBase(parentWidget),
327 QtLanguageOptionPage::~QtLanguageOptionPage()
330 bool QtLanguageOptionPage::apply()
333 m_settings.
localeName = ui()->localeComboBox->currentText();
334 m_settings.
customLocale = !ui()->localeCheckBox->isChecked();
339 void QtLanguageOptionPage::reset()
342 ui()->localeComboBox->setCurrentText(m_settings.
localeName);
343 ui()->localeCheckBox->setChecked(!m_settings.
customLocale);
347 QWidget *QtLanguageOptionPage::setupWidget()
350 auto *widget = QtLanguageOptionPageBase::setupWidget();
353 auto *localeComboBox = ui()->localeComboBox;
354 for(
const QLocale &locale : QLocale::matchingLocales(
355 QLocale::AnyLanguage,
357 QLocale::AnyCountry)) {
358 localeComboBox->addItem(locale.name());
361 auto *languageLabel = ui()->languageLabel;
362 QObject::connect(ui()->localeComboBox, &QComboBox::currentTextChanged, [languageLabel, localeComboBox] {
363 const QLocale selectedLocale(localeComboBox->currentText());
364 const QLocale currentLocale;
365 languageLabel->setText(QCoreApplication::translate(
"QtGui::QtLanguageOptionPage",
"recognized by Qt as")
366 % QStringLiteral(
" <i>")
367 % currentLocale.languageToString(selectedLocale.language())
368 % QChar(
',') % QChar(
' ')
369 % currentLocale.countryToString(selectedLocale.country())
370 % QStringLiteral(
"</i>"));
376 QtEnvOptionPageBase(parentWidget),
380 QtEnvOptionPage::~QtEnvOptionPage()
383 bool QtEnvOptionPage::apply()
393 void QtEnvOptionPage::reset()
QtEnvOptionPage(QtSettingsData &settings, QWidget *parentWidget=nullptr)
QtAppearanceOptionPage(QtSettingsData &settings, QWidget *parentWidget=nullptr)
The OptionCategory class wraps associated option pages.
QtLanguageOptionPage(QtSettingsData &settings, QWidget *parentWidget=nullptr)
QT_UTILITIES_EXPORT QString & additionalTranslationFilePath()
Allows to set an additional search path for translation files.
static QPalette getPalette(QWidget *parent, const QPalette &init=QPalette(), const QPalette &parentPal=QPalette(), int *result=nullptr)
void setDisplayName(const QString &displayName)
Sets the display name of the category.
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...