File indexing completed on 2024-04-28 15:14:02
0001 /*************************************************************************** 0002 File : SettingsGeneralPage.cpp 0003 Project : LabPlot 0004 Description : general settings page 0005 -------------------------------------------------------------------- 0006 Copyright : (C) 2008-2020 Alexander Semke (alexander.semke@web.de) 0007 Copyright : (C) 2020 Stefan Gerlach (stefan.gerlach@uni.kn) 0008 0009 ***************************************************************************/ 0010 0011 /*************************************************************************** 0012 * * 0013 * This program is free software; you can redistribute it and/or modify * 0014 * it under the terms of the GNU General Public License as published by * 0015 * the Free Software Foundation; either version 2 of the License, or * 0016 * (at your option) any later version. * 0017 * * 0018 * This program is distributed in the hope that it will be useful, * 0019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0021 * GNU General Public License for more details. * 0022 * * 0023 * You should have received a copy of the GNU General Public License * 0024 * along with this program; if not, write to the Free Software * 0025 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 0026 * Boston, MA 02110-1301 USA * 0027 * * 0028 ***************************************************************************/ 0029 0030 #include "SettingsGeneralPage.h" 0031 #include "backend/lib/macros.h" 0032 0033 #include <KI18n/KLocalizedString> 0034 #include <KConfigGroup> 0035 #include <KSharedConfig> 0036 0037 /** 0038 * \brief Page for the 'General' settings of the Labplot settings dialog. 0039 */ 0040 SettingsGeneralPage::SettingsGeneralPage(QWidget* parent) : SettingsPage(parent) { 0041 ui.setupUi(this); 0042 ui.sbAutoSaveInterval->setSuffix(i18n("min.")); 0043 retranslateUi(); 0044 0045 connect(ui.cbLoadOnStart, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsGeneralPage::changed); 0046 connect(ui.cbTitleBar, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsGeneralPage::changed); 0047 connect(ui.cbInterface, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsGeneralPage::interfaceChanged); 0048 connect(ui.cbMdiVisibility, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsGeneralPage::changed); 0049 connect(ui.cbTabPosition, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsGeneralPage::changed); 0050 connect(ui.cbUnits, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsGeneralPage::changed); 0051 connect(ui.cbDecimalSeparator, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &SettingsGeneralPage::changed); 0052 connect(ui.chkAutoSave, &QCheckBox::stateChanged, this, &SettingsGeneralPage::autoSaveChanged); 0053 0054 loadSettings(); 0055 interfaceChanged(ui.cbInterface->currentIndex()); 0056 autoSaveChanged(ui.chkAutoSave->checkState()); 0057 } 0058 0059 /* returns decimal separator (as SettingsGeneralPage::DecimalSeparator) of given locale (default: system setting) */ 0060 SettingsGeneralPage::DecimalSeparator SettingsGeneralPage::decimalSeparator(QLocale locale) { 0061 DEBUG(Q_FUNC_INFO << ", LOCALE: " << STDSTRING(locale.name()) << ", " << locale.language()) 0062 QChar decimalPoint{locale.decimalPoint()}; 0063 DEBUG(Q_FUNC_INFO << ", SEPARATING CHAR: " << STDSTRING(QString(decimalPoint)) ) 0064 if (decimalPoint == QChar('.')) 0065 return DecimalSeparator::Dot; 0066 else if (decimalPoint == QChar(',')) 0067 return DecimalSeparator::Comma; 0068 0069 return DecimalSeparator::Arabic; 0070 } 0071 0072 QLocale::Language SettingsGeneralPage::decimalSeparatorLocale() const { 0073 int currentIndex = ui.cbDecimalSeparator->currentIndex(); 0074 DEBUG(Q_FUNC_INFO << ", SYSTEM LOCALE: " << STDSTRING(QLocale().name()) << ':' << QLocale().language()) 0075 DEBUG(Q_FUNC_INFO << ", SYSTEM SEPARATING CHAR: " << STDSTRING(QString(QLocale().decimalPoint())) ) 0076 0077 QChar groupSeparator{QLocale().groupSeparator()}; 0078 switch (currentIndex) { 0079 case static_cast<int>(DecimalSeparator::Dot): 0080 if (groupSeparator == QLocale(QLocale::Language::Zarma).groupSeparator()) // \u00a0 0081 return QLocale::Language::Zarma; // . \u00a0 0082 else if (groupSeparator == QLocale(QLocale::Language::SwissGerman).groupSeparator()) // \u2019 0083 return QLocale::Language::SwissGerman; // . \u2019 0084 else 0085 return QLocale::Language::C; // . , 0086 case static_cast<int>(DecimalSeparator::Comma): 0087 if (groupSeparator == QLocale(QLocale::Language::French).groupSeparator()) // \u00a0 0088 return QLocale::Language::French; // , \u00a0 0089 else if (groupSeparator == QLocale(QLocale::Language::Walser).groupSeparator()) // \u2019 0090 return QLocale::Language::Walser; // , \u2019 0091 else 0092 return QLocale::Language::German; // , . 0093 case static_cast<int>(DecimalSeparator::Arabic): 0094 return QLocale::Language::Arabic; // \u066b \u066c 0095 default: // automatic 0096 return QLocale::Language::AnyLanguage; 0097 } 0098 } 0099 0100 void SettingsGeneralPage::applySettings() { 0101 DEBUG(Q_FUNC_INFO) 0102 if (!m_changed) 0103 return; 0104 0105 KConfigGroup group = KSharedConfig::openConfig()->group(QLatin1String("Settings_General")); 0106 group.writeEntry(QLatin1String("LoadOnStart"), ui.cbLoadOnStart->currentIndex()); 0107 group.writeEntry(QLatin1String("TitleBar"), ui.cbTitleBar->currentIndex()); 0108 group.writeEntry(QLatin1String("ViewMode"), ui.cbInterface->currentIndex()); 0109 group.writeEntry(QLatin1String("TabPosition"), ui.cbTabPosition->currentIndex()); 0110 group.writeEntry(QLatin1String("MdiWindowVisibility"), ui.cbMdiVisibility->currentIndex()); 0111 group.writeEntry(QLatin1String("Units"), ui.cbUnits->currentIndex()); 0112 if (ui.cbDecimalSeparator->currentIndex() == static_cast<int>(DecimalSeparator::Automatic)) // needed to overwrite previous setting 0113 group.writeEntry(QLatin1String("DecimalSeparatorLocale"), static_cast<int>(QLocale::Language::AnyLanguage)); 0114 else 0115 group.writeEntry(QLatin1String("DecimalSeparatorLocale"), static_cast<int>(decimalSeparatorLocale())); 0116 group.writeEntry(QLatin1String("AutoSave"), ui.chkAutoSave->isChecked()); 0117 group.writeEntry(QLatin1String("AutoSaveInterval"), ui.sbAutoSaveInterval->value()); 0118 } 0119 0120 void SettingsGeneralPage::restoreDefaults() { 0121 ui.cbLoadOnStart->setCurrentIndex(0); 0122 ui.cbTitleBar->setCurrentIndex(0); 0123 ui.cbInterface->setCurrentIndex(0); 0124 ui.cbTabPosition->setCurrentIndex(0); 0125 ui.cbMdiVisibility->setCurrentIndex(0); 0126 ui.cbUnits->setCurrentIndex(0); 0127 ui.cbDecimalSeparator->setCurrentIndex(static_cast<int>(DecimalSeparator::Automatic)); 0128 ui.chkAutoSave->setChecked(false); 0129 ui.sbAutoSaveInterval->setValue(0); 0130 ui.sbAutoSaveInterval->setValue(5); 0131 } 0132 0133 void SettingsGeneralPage::loadSettings() { 0134 const KConfigGroup group = KSharedConfig::openConfig()->group(QLatin1String("Settings_General")); 0135 ui.cbLoadOnStart->setCurrentIndex(group.readEntry(QLatin1String("LoadOnStart"), 0)); 0136 ui.cbTitleBar->setCurrentIndex(group.readEntry(QLatin1String("TitleBar"), 0)); 0137 ui.cbInterface->setCurrentIndex(group.readEntry(QLatin1String("ViewMode"), 0)); 0138 ui.cbTabPosition->setCurrentIndex(group.readEntry(QLatin1String("TabPosition"), 0)); 0139 ui.cbMdiVisibility->setCurrentIndex(group.readEntry(QLatin1String("MdiWindowVisibility"), 0)); 0140 ui.cbUnits->setCurrentIndex(group.readEntry(QLatin1String("Units"), 0)); 0141 QLocale locale(static_cast<QLocale::Language>(group.readEntry( QLatin1String("DecimalSeparatorLocale"), static_cast<int>(QLocale::Language::AnyLanguage) )) ); 0142 if (locale.language() == QLocale::Language::AnyLanguage) // no or default setting 0143 ui.cbDecimalSeparator->setCurrentIndex( static_cast<int>(DecimalSeparator::Automatic) ); 0144 else 0145 ui.cbDecimalSeparator->setCurrentIndex( static_cast<int>(decimalSeparator(locale)) ); 0146 ui.chkAutoSave->setChecked(group.readEntry<bool>(QLatin1String("AutoSave"), false)); 0147 ui.sbAutoSaveInterval->setValue(group.readEntry(QLatin1String("AutoSaveInterval"), 0)); 0148 } 0149 0150 void SettingsGeneralPage::retranslateUi() { 0151 ui.cbLoadOnStart->clear(); 0152 ui.cbLoadOnStart->addItem(i18n("Do nothing")); 0153 ui.cbLoadOnStart->addItem(i18n("Create new empty project")); 0154 ui.cbLoadOnStart->addItem(i18n("Create new project with worksheet")); 0155 ui.cbLoadOnStart->addItem(i18n("Load last used project")); 0156 // ui.cbLoadOnStart->addItem(i18n("Show Welcome Screen")); 0157 0158 ui.cbTitleBar->clear(); 0159 ui.cbTitleBar->addItem(i18n("Show File Path")); 0160 ui.cbTitleBar->addItem(i18n("Show File Name")); 0161 ui.cbTitleBar->addItem(i18n("Show Project Name")); 0162 0163 ui.cbInterface->clear(); 0164 ui.cbInterface->addItem(i18n("Sub-window view")); 0165 ui.cbInterface->addItem(i18n("Tabbed view")); 0166 0167 ui.cbMdiVisibility->clear(); 0168 ui.cbMdiVisibility->addItem(i18n("Show windows of the current folder only")); 0169 ui.cbMdiVisibility->addItem(i18n("Show windows of the current folder and its subfolders only")); 0170 ui.cbMdiVisibility->addItem(i18n("Show all windows")); 0171 0172 ui.cbTabPosition->clear(); 0173 ui.cbTabPosition->addItem(i18n("Top")); 0174 ui.cbTabPosition->addItem(i18n("Bottom")); 0175 ui.cbTabPosition->addItem(i18n("Left")); 0176 ui.cbTabPosition->addItem(i18n("Right")); 0177 0178 ui.cbUnits->addItem(i18n("Metric")); 0179 ui.cbUnits->addItem(i18n("Imperial")); 0180 0181 ui.cbDecimalSeparator->addItem(i18n("Dot (.)")); 0182 ui.cbDecimalSeparator->addItem(i18n("Comma (,)")); 0183 ui.cbDecimalSeparator->addItem(i18n("Arabic (٫)")); 0184 ui.cbDecimalSeparator->addItem(i18n("Automatic")); 0185 } 0186 0187 void SettingsGeneralPage::changed() { 0188 m_changed = true; 0189 emit settingsChanged(); 0190 } 0191 0192 void SettingsGeneralPage::interfaceChanged(int index) { 0193 bool tabbedView = (index == 1); 0194 ui.lTabPosition->setVisible(tabbedView); 0195 ui.cbTabPosition->setVisible(tabbedView); 0196 ui.lMdiVisibility->setVisible(!tabbedView); 0197 ui.cbMdiVisibility->setVisible(!tabbedView); 0198 changed(); 0199 } 0200 0201 void SettingsGeneralPage::autoSaveChanged(int state) { 0202 const bool visible = (state == Qt::Checked); 0203 ui.lAutoSaveInterval->setVisible(visible); 0204 ui.sbAutoSaveInterval->setVisible(visible); 0205 changed(); 0206 }