diff --git a/src/global/preferences.cpp b/src/global/preferences.cpp index bfdfbbc7..687a07c4 100644 --- a/src/global/preferences.cpp +++ b/src/global/preferences.cpp @@ -1,219 +1,215 @@ /*************************************************************************** * Copyright (C) 2004-2019 by Thomas Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, see . * ***************************************************************************/ #include "preferences.h" #include #ifdef HAVE_KF5 #include #include #include #include #else // HAVE_KF5 #define I18N_NOOP(text) QObject::tr(text) #define i18n(text) QObject::tr(text) #endif // HAVE_KF5 #ifdef HAVE_KF5 #include "notificationhub.h" #endif // HAVE_KF5 const QString Preferences::groupColor = QStringLiteral("Color Labels"); const QString Preferences::keyColorCodes = QStringLiteral("colorCodes"); const QStringList Preferences::defaultColorCodes {QStringLiteral("#cc3300"), QStringLiteral("#0033ff"), QStringLiteral("#009966"), QStringLiteral("#f0d000")}; const QString Preferences::keyColorLabels = QStringLiteral("colorLabels"); // FIXME // clazy warns: QString(const char*) being called [-Wclazy-qstring-uneeded-heap-allocations] // ... but using QStringLiteral may break the translation process? const QStringList Preferences::defaultColorLabels {I18N_NOOP("Important"), I18N_NOOP("Unread"), I18N_NOOP("Read"), I18N_NOOP("Watch")}; const QString Preferences::groupGeneral = QStringLiteral("General"); const QString Preferences::keyBackupScope = QStringLiteral("backupScope"); const Preferences::BackupScope Preferences::defaultBackupScope = LocalOnly; const QString Preferences::keyNumberOfBackups = QStringLiteral("numberOfBackups"); const int Preferences::defaultNumberOfBackups = 5; const QString Preferences::groupUserInterface = QStringLiteral("User Interface"); const QString Preferences::keyElementDoubleClickAction = QStringLiteral("elementDoubleClickAction"); const Preferences::ElementDoubleClickAction Preferences::defaultElementDoubleClickAction = ActionOpenEditor; const QString Preferences::keyEncoding = QStringLiteral("encoding"); const QString Preferences::defaultEncoding = QStringLiteral("LaTeX"); const QString Preferences::keyStringDelimiter = QStringLiteral("stringDelimiter"); const QString Preferences::defaultStringDelimiter = QStringLiteral("{}"); const QString Preferences::keyQuoteComment = QStringLiteral("quoteComment"); const Preferences::QuoteComment Preferences::defaultQuoteComment = qcNone; const QString Preferences::keyKeywordCasing = QStringLiteral("keywordCasing"); const KBibTeX::Casing Preferences::defaultKeywordCasing = KBibTeX::cLowerCase; const QString Preferences::keyProtectCasing = QStringLiteral("protectCasing"); const Qt::CheckState Preferences::defaultProtectCasing = Qt::PartiallyChecked; const QString Preferences::keyListSeparator = QStringLiteral("ListSeparator"); const QString Preferences::defaultListSeparator = QStringLiteral("; "); class Preferences::Private { private: Preferences *parent; public: #ifdef HAVE_KF5 KSharedConfigPtr config; KConfigWatcher::Ptr watcher; #endif // HAVE_KF5 Private(Preferences *_parent) : parent(_parent) { #ifdef HAVE_KF5 config = KSharedConfig::openConfig(QStringLiteral("kbibtexrc")); watcher = KConfigWatcher::create(config); dirtyFlagBibliographySystem = true; cachedBibliographySystem = defaultBibliographySystem; dirtyFlagPersonNameFormatting = true; cachedPersonNameFormatting = defaultPersonNameFormatting; #endif // HAVE_KF5 } inline bool validateValueForBibliographySystem(const int valueToBeChecked) { - for (QMap::ConstIterator it = Preferences::availableBibliographySystems().constBegin(); it != Preferences::availableBibliographySystems().constEnd(); ++it) - if (static_cast(it.key()) == valueToBeChecked) return true; + for (QVector>::ConstIterator it = Preferences::availableBibliographySystems.constBegin(); it != Preferences::availableBibliographySystems.constEnd(); ++it) + if (static_cast(it->first) == valueToBeChecked) return true; return false; } inline bool validateValueForPersonNameFormatting(const QString &valueToBeChecked) { return valueToBeChecked.contains(QStringLiteral("%f")) && valueToBeChecked.contains(QStringLiteral("%l")) && valueToBeChecked.contains(QStringLiteral("%s")); } #ifdef HAVE_KF5 #define getterSetter(type, typeInConfig, stem, notificationEventId, configGroupName) \ bool dirtyFlag##stem; \ type cached##stem; \ bool set##stem(const type &newValue) { \ dirtyFlag##stem = false; \ cached##stem = newValue; \ static KConfigGroup configGroup(config, QStringLiteral(configGroupName)); \ const typeInConfig valueFromConfig = configGroup.readEntry(QStringLiteral(#stem), static_cast(Preferences::default##stem)); \ const typeInConfig newValuePOD = static_cast(newValue); \ if (valueFromConfig == newValuePOD) return false; \ configGroup.writeEntry(QStringLiteral(#stem), newValuePOD, KConfig::Notify /** to catch changes via KConfigWatcher */); \ config->sync(); \ NotificationHub::publishEvent(notificationEventId); \ return true; \ } \ type get##stem() { \ if (dirtyFlag##stem) { \ config->reparseConfiguration(); static const KConfigGroup configGroup(config, QStringLiteral(configGroupName)); \ const typeInConfig valueFromConfiguration = configGroup.readEntry(QStringLiteral(#stem), static_cast(Preferences::default##stem)); \ if (validateValueFor##stem(valueFromConfiguration)) { \ cached##stem = static_cast(valueFromConfiguration); \ dirtyFlag##stem = false; \ } else { \ qWarning() << "Configuration file setting for" << #stem << "has an invalid value, using default as fallback"; \ set##stem(Preferences::default##stem); \ } \ } \ return cached##stem; \ } getterSetter(Preferences::BibliographySystem, int, BibliographySystem, NotificationHub::EventBibliographySystemChanged, "General") getterSetter(QString, QString, PersonNameFormatting, NotificationHub::EventConfigurationChanged, "General") #endif // HAVE_KF5 }; Preferences &Preferences::instance() { static Preferences singleton; return singleton; } Preferences::Preferences() : d(new Preferences::Private(this)) { #ifdef HAVE_KF5 QObject::connect(d->watcher.data(), &KConfigWatcher::configChanged, [this](const KConfigGroup & group, const QByteArrayList & names) { QSet eventsToPublish; #define eventCheck(type, stem, notificationEventId, configGroupName) \ if (group.name() == QStringLiteral(configGroupName) && names.contains(#stem)) { \ qDebug() << "Configuration setting"<<#stem<<"got changed by another Preferences instance"; \ d->dirtyFlag##stem = true; \ eventsToPublish.insert(notificationEventId); \ } eventCheck(Preferences::BibliographySystem, BibliographySystem, NotificationHub::EventBibliographySystemChanged, "General") eventCheck(QString, PersonNameFormatting, NotificationHub::EventConfigurationChanged, "General") for (const int eventId : eventsToPublish) NotificationHub::publishEvent(eventId); }); #endif // HAVE_KF5 } Preferences::~Preferences() { delete d; } const Preferences::BibliographySystem Preferences::defaultBibliographySystem = Preferences::BibTeX; Preferences::BibliographySystem Preferences::bibliographySystem() { #ifdef HAVE_KF5 return d->getBibliographySystem(); #else // HAVE_KF5 return defaultBibliographySystem; #endif // HAVE_KF5 } bool Preferences::setBibliographySystem(const Preferences::BibliographySystem bibliographySystem) { #ifdef HAVE_KF5 return d->setBibliographySystem(bibliographySystem); #else // HAVE_KF5 Q_UNUSED(bibliographySystem); return true; #endif // HAVE_KF5 } -const QMap Preferences::availableBibliographySystems() -{ - static const QMap result {{Preferences::BibTeX, i18n("BibTeX")}, {Preferences::BibLaTeX, i18n("BibLaTeX")}}; - return result; -} +const QVector> Preferences::availableBibliographySystems {{Preferences::BibTeX, i18n("BibTeX")}, {Preferences::BibLaTeX, i18n("BibLaTeX")}}; const QString Preferences::personNameFormatLastFirst = QStringLiteral("<%l><, %s><, %f>"); const QString Preferences::personNameFormatFirstLast = QStringLiteral("<%f ><%l>< %s>"); const QString Preferences::defaultPersonNameFormatting = Preferences::personNameFormatLastFirst; QString Preferences::personNameFormatting() { #ifdef HAVE_KF5 return d->getPersonNameFormatting(); #else // HAVE_KF5 return defaultPersonNameFormatting; #endif // HAVE_KF5 } bool Preferences::setPersonNameFormatting(const QString &personNameFormatting) { #ifdef HAVE_KF5 return d->setPersonNameFormatting(personNameFormatting); #else // HAVE_KF5 Q_UNUSED(personNameFormatting); #endif // HAVE_KF5 return true; } diff --git a/src/global/preferences.h b/src/global/preferences.h index 300962b8..31b41243 100644 --- a/src/global/preferences.h +++ b/src/global/preferences.h @@ -1,109 +1,109 @@ /*************************************************************************** * Copyright (C) 2004-2019 by Thomas Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, see . * ***************************************************************************/ #ifndef KBIBTEX_GLOBAL_PREFERENCES_H #define KBIBTEX_GLOBAL_PREFERENCES_H #include "kbibtex.h" /** @author Thomas Fischer */ class Preferences { public: static Preferences &instance(); ~Preferences(); protected: Preferences(); public: /// *** Bibliography system, as of now either BibTeX or BibLaTeX /// Bibliography system: either BibTeX or BibLaTeX enum BibliographySystem { BibTeX = 0, BibLaTeX = 1 }; /// Default bibliography system if nothing else is set or defined static const BibliographySystem defaultBibliographySystem; /// Retrieve current bibliography system BibliographySystem bibliographySystem(); /// Set bibliography system /// @return true if the set bibliography system is differed from the previous value, false if both were the same bool setBibliographySystem(const BibliographySystem bibliographySystem); /// Map of supported bibliography systems, should be the same as in enum BibliographySystem - static const QMap availableBibliographySystems(); + static const QVector> availableBibliographySystems; /// *** Name formatting like "Firstname Lastname", "Lastname, Firstname", or any other combination /// Predefined value for a person formatting, where last name comes before first name static const QString personNameFormatLastFirst; /// Predefined value for a person formatting, where first name comes before last name static const QString personNameFormatFirstLast; /// Default name formatting for a person if nothing else is set or defined static const QString defaultPersonNameFormatting; /// Retrieve current name formatting QString personNameFormatting(); /// Set name formatting /// @return true if the set formatting is differed from the previous value, false if both were the same bool setPersonNameFormatting(const QString &personNameFormatting); enum BackupScope { NoBackup, LocalOnly, BothLocalAndRemote }; enum ElementDoubleClickAction { ActionOpenEditor = 0, ActionViewDocument = 1 }; /** * Preferences for File objects */ enum QuoteComment { qcNone = 0, qcCommand = 1, qcPercentSign = 2 }; static const QString groupColor; static const QString keyColorCodes; static const QStringList defaultColorCodes; static const QString keyColorLabels; static const QStringList defaultColorLabels; static const QString groupGeneral; static const QString keyBackupScope; static const BackupScope defaultBackupScope; static const QString keyNumberOfBackups; static const int defaultNumberOfBackups; static const QString groupUserInterface; static const QString keyElementDoubleClickAction; static const ElementDoubleClickAction defaultElementDoubleClickAction; static const QString keyEncoding; static const QString defaultEncoding; static const QString keyStringDelimiter; static const QString defaultStringDelimiter; static const QString keyQuoteComment; static const QuoteComment defaultQuoteComment; static const QString keyKeywordCasing; static const KBibTeX::Casing defaultKeywordCasing; static const QString keyProtectCasing; static const Qt::CheckState defaultProtectCasing; static const QString keyListSeparator; static const QString defaultListSeparator; private: Q_DISABLE_COPY(Preferences) class Private; Private *const d; }; #endif // KBIBTEX_GLOBAL_PREFERENCES_H diff --git a/src/gui/preferences/settingsgeneralwidget.cpp b/src/gui/preferences/settingsgeneralwidget.cpp index 69a3e040..d2157e20 100644 --- a/src/gui/preferences/settingsgeneralwidget.cpp +++ b/src/gui/preferences/settingsgeneralwidget.cpp @@ -1,121 +1,121 @@ /*************************************************************************** * Copyright (C) 2004-2019 by Thomas Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, see . * ***************************************************************************/ #include "settingsgeneralwidget.h" #include #include #include #include #include #include "guihelper.h" #include "value.h" #include "preferences.h" class SettingsGeneralWidget::SettingsGeneralWidgetPrivate { private: SettingsGeneralWidget *p; KComboBox *comboBoxBibliographySystem; KComboBox *comboBoxPersonNameFormatting; const Person dummyPerson; public: SettingsGeneralWidgetPrivate(SettingsGeneralWidget *parent) : p(parent), dummyPerson(Person(i18n("John"), i18n("Doe"), i18n("Jr."))) { setupGUI(); } void loadState() { comboBoxBibliographySystem->setCurrentIndex(comboBoxBibliographySystem->findData(QVariant::fromValue(static_cast(Preferences::instance().bibliographySystem())))); int row = GUIHelper::selectValue(comboBoxPersonNameFormatting->model(), Person::transcribePersonName(&dummyPerson, Preferences::instance().personNameFormatting())); comboBoxPersonNameFormatting->setCurrentIndex(row); } void saveState() { Preferences::instance().setBibliographySystem(static_cast(comboBoxBibliographySystem->currentData().toInt())); Preferences::instance().setPersonNameFormatting(comboBoxPersonNameFormatting->itemData(comboBoxPersonNameFormatting->currentIndex()).toString()); } void resetToDefaults() { comboBoxBibliographySystem->setCurrentIndex(static_cast(Preferences::defaultBibliographySystem)); int row = GUIHelper::selectValue(comboBoxPersonNameFormatting->model(), Person::transcribePersonName(&dummyPerson, Preferences::defaultPersonNameFormatting)); comboBoxPersonNameFormatting->setCurrentIndex(row); } void setupGUI() { QFormLayout *layout = new QFormLayout(p); comboBoxBibliographySystem = new KComboBox(false, p); comboBoxBibliographySystem->setObjectName(QStringLiteral("comboBoxBibliographySystem")); - const QMap &availableBibliographySystems = Preferences::availableBibliographySystems(); - for (QMap::ConstIterator it = availableBibliographySystems.constBegin(), itEnd = availableBibliographySystems.constEnd(); it != itEnd; ++it) - comboBoxBibliographySystem->addItem(it.value(), QVariant::fromValue(static_cast(it.key()))); + + for (QVector>::ConstIterator it = Preferences::availableBibliographySystems.constBegin(); it != Preferences::availableBibliographySystems.constEnd(); ++it) + comboBoxBibliographySystem->addItem(it->second, QVariant::fromValue(static_cast(it->first))); layout->addRow(i18n("Bibliography System:"), comboBoxBibliographySystem); connect(comboBoxBibliographySystem, static_cast(&QComboBox::currentIndexChanged), p, &SettingsGeneralWidget::changed); comboBoxPersonNameFormatting = new KComboBox(false, p); layout->addRow(i18n("Person Names Formatting:"), comboBoxPersonNameFormatting); const QStringList formattingOptions {Preferences::personNameFormatFirstLast, Preferences::personNameFormatLastFirst}; for (const QString &formattingOption : formattingOptions) comboBoxPersonNameFormatting->addItem(Person::transcribePersonName(&dummyPerson, formattingOption), formattingOption); connect(comboBoxPersonNameFormatting, static_cast(&QComboBox::currentIndexChanged), p, &SettingsGeneralWidget::changed); } }; SettingsGeneralWidget::SettingsGeneralWidget(QWidget *parent) : SettingsAbstractWidget(parent), d(new SettingsGeneralWidgetPrivate(this)) { d->loadState(); } SettingsGeneralWidget::~SettingsGeneralWidget() { delete d; } QString SettingsGeneralWidget::label() const { return i18n("General"); } QIcon SettingsGeneralWidget::icon() const { return QIcon::fromTheme(QStringLiteral("kbibtex")); } void SettingsGeneralWidget::loadState() { d->loadState(); } void SettingsGeneralWidget::saveState() { d->saveState(); } void SettingsGeneralWidget::resetToDefaults() { d->resetToDefaults(); }