1 /*****************************************************************************
2 * Copyright (C) 2004-2018 by Thomas Fischer <fischer@unix-ag.uni-kl.de> *
3 * *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see <https://www.gnu.org/licenses/>. *
17 *****************************************************************************/
18
19 #include "filesettingswidget.h"
20
21 #include <QCheckBox>
22 #include <QFormLayout>
23
24 #include <KComboBox>
25 #include <KLocalizedString>
26
27 #include "preferences.h"
28 #include "italictextitemmodel.h"
29 #include "textencoder.h"
30 #include "file.h"
31 #include "guihelper.h"
32
33 #define createDelimiterString(a, b) (QString(QStringLiteral("%1%2%3")).arg(a).arg(QChar(8230)).arg(b))
34
35 FileSettingsWidget::FileSettingsWidget(QWidget *parent)
36 : QWidget(parent), dummyPerson(Person(i18n("John"), i18n("Doe"), i18n("Jr."))), m_file(nullptr)
37 {
38 setupGUI();
39 }
40
41 void FileSettingsWidget::resetToLoadedProperties()
42 {
43 loadProperties(m_file);
44 }
45
46 void FileSettingsWidget::loadProperties(File *file)
47 {
48 m_file = file;
49 if (m_file == nullptr) return; /// Nothing to do
50
51 if (file->hasProperty(File::Encoding)) {
52 m_comboBoxEncodings->blockSignals(true);
53 const QString encoding = file->property(File::Encoding).toString();
54 const int row = GUIHelper::selectValue(m_comboBoxEncodings->model(), encoding);
55 m_comboBoxEncodings->setCurrentIndex(row);
56 m_comboBoxEncodings->blockSignals(false);
57 }
58 if (file->hasProperty(File::StringDelimiter)) {
59 m_comboBoxStringDelimiters->blockSignals(true);
60 const QString stringDelimiter = file->property(File::StringDelimiter).toString();
61 const int row = GUIHelper::selectValue(m_comboBoxStringDelimiters->model(), createDelimiterString(stringDelimiter[0], stringDelimiter[1]));
62 m_comboBoxStringDelimiters->setCurrentIndex(row);
63 m_comboBoxStringDelimiters->blockSignals(false);
64 }
65 if (file->hasProperty(File::QuoteComment)) {
66 m_comboBoxQuoteComment->blockSignals(true);
67 const Preferences::QuoteComment quoteComment = static_cast<Preferences::QuoteComment>(file->property(File::QuoteComment).toInt());
68 m_comboBoxQuoteComment->setCurrentIndex(static_cast<int>(quoteComment));
69 m_comboBoxQuoteComment->blockSignals(false);
70 }
71 if (file->hasProperty(File::KeywordCasing)) {
72 m_comboBoxKeywordCasing->blockSignals(true);
73 const KBibTeX::Casing keywordCasing = static_cast<KBibTeX::Casing>(file->property(File::KeywordCasing).toInt());
74 m_comboBoxKeywordCasing->setCurrentIndex(static_cast<int>(keywordCasing));
75 m_comboBoxKeywordCasing->blockSignals(false);
76 }
77 if (file->hasProperty(File::ProtectCasing)) {
78 m_checkBoxProtectCasing->blockSignals(true);
79 m_checkBoxProtectCasing->setCheckState(static_cast<Qt::CheckState>(file->property(File::ProtectCasing).toInt()));
80 m_checkBoxProtectCasing->blockSignals(false);
81 }
82 if (file->hasProperty(File::NameFormatting)) {
83 m_comboBoxPersonNameFormatting->blockSignals(true);
84 const int row = GUIHelper::selectValue(m_comboBoxPersonNameFormatting->model(), file->property(File::NameFormatting).toString(), ItalicTextItemModel::IdentifierRole);
85 m_comboBoxPersonNameFormatting->setCurrentIndex(row);
86 m_comboBoxPersonNameFormatting->blockSignals(false);
87 }
88 if (file->hasProperty(File::ListSeparator)) {
89 m_comboBoxListSeparator->blockSignals(true);
90 m_comboBoxListSeparator->setCurrentIndex(m_comboBoxListSeparator->findData(file->property(File::ListSeparator)));
91 m_comboBoxListSeparator->blockSignals(false);
92 }
93 }
94
Private(Preferences * _parent)95 void FileSettingsWidget::applyProperties()
96 {
97 saveProperties(m_file);
98 }
99
100 void FileSettingsWidget::saveProperties(File *file)
101 {
102 m_file = file;
103 if (m_file == nullptr) return;
104
105 file->setProperty(File::Encoding, m_comboBoxEncodings->currentText());
106 const QString stringDelimiter = m_comboBoxStringDelimiters->currentText();
107 file->setProperty(File::StringDelimiter, QString(stringDelimiter[0]) + stringDelimiter[stringDelimiter.length() - 1]);
108 const Preferences::QuoteComment quoteComment = static_cast<Preferences::QuoteComment>(m_comboBoxQuoteComment->currentIndex());
instance()109 file->setProperty(File::QuoteComment, static_cast<int>(quoteComment));
110 const KBibTeX::Casing keywordCasing = static_cast<KBibTeX::Casing>(m_comboBoxKeywordCasing->currentIndex());
111 file->setProperty(File::KeywordCasing, static_cast<int>(keywordCasing));
112 file->setProperty(File::ProtectCasing, static_cast<int>(m_checkBoxProtectCasing->checkState()));
113 file->setProperty(File::NameFormatting, m_comboBoxPersonNameFormatting->itemData(m_comboBoxPersonNameFormatting->currentIndex(), ItalicTextItemModel::IdentifierRole));
114 file->setProperty(File::ListSeparator, m_comboBoxListSeparator->itemData(m_comboBoxListSeparator->currentIndex()).toString());
Preferences()115 }
116
117 void FileSettingsWidget::resetToDefaults()
118 {
119 if (m_file != nullptr) {
120 m_file->setPropertiesToDefault();
121 loadProperties(m_file);
122 }
123 }
124
125 void FileSettingsWidget::setupGUI()
126 {
127 QFormLayout *layout = new QFormLayout(this);
128
129 m_comboBoxEncodings = new KComboBox(false, this);
130 m_comboBoxEncodings->setObjectName(QStringLiteral("comboBoxEncodings"));
131 layout->addRow(i18n("Encoding:"), m_comboBoxEncodings);
132 m_comboBoxEncodings->addItem(QStringLiteral("LaTeX"));
133 m_comboBoxEncodings->insertSeparator(1);
134 m_comboBoxEncodings->addItems(TextEncoder::encodings);
135 connect(m_comboBoxEncodings, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &FileSettingsWidget::widgetsChanged);
~Preferences()136
137 m_comboBoxStringDelimiters = new KComboBox(false, this);
138 m_comboBoxStringDelimiters->setObjectName(QStringLiteral("comboBoxStringDelimiters"));
139 layout->addRow(i18n("String Delimiters:"), m_comboBoxStringDelimiters);
140 m_comboBoxStringDelimiters->addItem(createDelimiterString('"', '"'));
bibliographySystem()141 m_comboBoxStringDelimiters->addItem(createDelimiterString('{', '}'));
142 m_comboBoxStringDelimiters->addItem(createDelimiterString('(', ')'));
143 connect(m_comboBoxStringDelimiters, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &FileSettingsWidget::widgetsChanged);
144
145 m_comboBoxQuoteComment = new KComboBox(false, this);
146 layout->addRow(i18n("Comment Quoting:"), m_comboBoxQuoteComment);
147 m_comboBoxQuoteComment->addItem(i18nc("Comment Quoting", "None"));
148 m_comboBoxQuoteComment->addItem(i18nc("Comment Quoting", "@comment{%1}", QChar(8230)));
149 m_comboBoxQuoteComment->addItem(i18nc("Comment Quoting", "%{%1}", QChar(8230)));
150 connect(m_comboBoxQuoteComment, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &FileSettingsWidget::widgetsChanged);
151
152 m_comboBoxKeywordCasing = new KComboBox(false, this);
153 layout->addRow(i18n("Keyword Casing:"), m_comboBoxKeywordCasing);
154 m_comboBoxKeywordCasing->addItem(i18nc("Keyword Casing", "lowercase"));
155 m_comboBoxKeywordCasing->addItem(i18nc("Keyword Casing", "Initial capital"));
156 m_comboBoxKeywordCasing->addItem(i18nc("Keyword Casing", "UpperCamelCase"));
157 m_comboBoxKeywordCasing->addItem(i18nc("Keyword Casing", "lowerCamelCase"));
158 m_comboBoxKeywordCasing->addItem(i18nc("Keyword Casing", "UPPERCASE"));
159 connect(m_comboBoxKeywordCasing, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &FileSettingsWidget::widgetsChanged);
160
161 m_checkBoxProtectCasing = new QCheckBox(i18n("Protect Titles"), this);
setBibliographySystem(const Preferences::BibliographySystem bibliographySystem)162 m_checkBoxProtectCasing->setTristate(true);
163 layout->addRow(i18n("Protect Casing?"), m_checkBoxProtectCasing);
164 connect(m_checkBoxProtectCasing, &QCheckBox::stateChanged, this, &FileSettingsWidget::widgetsChanged);
165
166 m_comboBoxPersonNameFormatting = new KComboBox(false, this);
167 m_comboBoxPersonNameFormatting->setObjectName(QStringLiteral("comboBoxPersonNameFormatting"));
168 layout->addRow(i18n("Person Names Formatting:"), m_comboBoxPersonNameFormatting);
169 connect(m_comboBoxPersonNameFormatting, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &FileSettingsWidget::widgetsChanged);
170
171 ItalicTextItemModel *itim = new ItalicTextItemModel(this);
172 itim->addItem(i18n("Use global settings"), QString(QString()));
173 itim->addItem(Person::transcribePersonName(&dummyPerson, Preferences::personNameFormatFirstLast), Preferences::personNameFormatFirstLast);
174 itim->addItem(Person::transcribePersonName(&dummyPerson, Preferences::personNameFormatLastFirst), Preferences::personNameFormatLastFirst);
175 m_comboBoxPersonNameFormatting->setModel(itim);
176
177 m_comboBoxListSeparator = new KComboBox(false, this);
178 layout->addRow(i18n("List Separator"), m_comboBoxListSeparator);
179 connect(m_comboBoxListSeparator, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &FileSettingsWidget::widgetsChanged);
availableBibliographySystems()180 m_comboBoxListSeparator->addItem(QStringLiteral(";"), QVariant::fromValue<QString>(QStringLiteral("; ")));
181 m_comboBoxListSeparator->addItem(QStringLiteral(","), QVariant::fromValue<QString>(QStringLiteral(", ")));
182 }
183