| 1 | /* |
| 2 | * configwidget.cpp |
| 3 | * |
| 4 | * SPDX-FileCopyrightText: 2004 Zack Rusin <zack@kde.org> |
| 5 | * SPDX-FileCopyrightText: 2020 Benjamin Port <benjamin.port@kde.org> |
| 6 | * |
| 7 | * SPDX-License-Identifier: LGPL-2.1-or-later |
| 8 | */ |
| 9 | #include "configview.h" |
| 10 | #include "ui_configui.h" |
| 11 | |
| 12 | #include "ui_debug.h" |
| 13 | |
| 14 | #include <QCheckBox> |
| 15 | #include <QLineEdit> |
| 16 | #include <QListWidget> |
| 17 | |
| 18 | using namespace Sonnet; |
| 19 | |
| 20 | class Sonnet::ConfigViewPrivate |
| 21 | { |
| 22 | public: |
| 23 | explicit ConfigViewPrivate(ConfigView *v); |
| 24 | Ui_SonnetConfigUI ui; |
| 25 | QWidget *wdg = nullptr; |
| 26 | QStringList ignoreList; |
| 27 | ConfigView *q; |
| 28 | void slotUpdateButton(const QString &text); |
| 29 | void slotSelectionChanged(); |
| 30 | void slotIgnoreWordAdded(); |
| 31 | void slotIgnoreWordRemoved(); |
| 32 | }; |
| 33 | |
| 34 | ConfigViewPrivate::ConfigViewPrivate(ConfigView *v) |
| 35 | { |
| 36 | q = v; |
| 37 | } |
| 38 | |
| 39 | void ConfigViewPrivate::slotUpdateButton(const QString &text) |
| 40 | { |
| 41 | ui.addButton->setEnabled(!text.isEmpty()); |
| 42 | } |
| 43 | |
| 44 | void ConfigViewPrivate::slotSelectionChanged() |
| 45 | { |
| 46 | ui.removeButton->setEnabled(!ui.ignoreListWidget->selectedItems().isEmpty()); |
| 47 | } |
| 48 | |
| 49 | void ConfigViewPrivate::slotIgnoreWordAdded() |
| 50 | { |
| 51 | QString newWord = ui.newIgnoreEdit->text(); |
| 52 | ui.newIgnoreEdit->clear(); |
| 53 | if (newWord.isEmpty() || ignoreList.contains(str: newWord)) { |
| 54 | return; |
| 55 | } |
| 56 | ignoreList.append(t: newWord); |
| 57 | |
| 58 | ui.ignoreListWidget->clear(); |
| 59 | ui.ignoreListWidget->addItems(labels: ignoreList); |
| 60 | |
| 61 | Q_EMIT q->configChanged(); |
| 62 | } |
| 63 | |
| 64 | void ConfigViewPrivate::slotIgnoreWordRemoved() |
| 65 | { |
| 66 | const QList<QListWidgetItem *> selectedItems = ui.ignoreListWidget->selectedItems(); |
| 67 | for (const QListWidgetItem *item : selectedItems) { |
| 68 | ignoreList.removeAll(t: item->text()); |
| 69 | } |
| 70 | |
| 71 | ui.ignoreListWidget->clear(); |
| 72 | ui.ignoreListWidget->addItems(labels: ignoreList); |
| 73 | |
| 74 | Q_EMIT q->configChanged(); |
| 75 | } |
| 76 | |
| 77 | ConfigView::ConfigView(QWidget *parent) |
| 78 | : QWidget(parent) |
| 79 | , d(new ConfigViewPrivate(this)) |
| 80 | { |
| 81 | auto *layout = new QVBoxLayout(this); |
| 82 | layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0); |
| 83 | layout->setObjectName(QStringLiteral("SonnetConfigUILayout" )); |
| 84 | d->wdg = new QWidget(this); |
| 85 | d->ui.setupUi(d->wdg); |
| 86 | d->ui.languageList->setProperty(name: "_breeze_force_frame" , value: true); |
| 87 | |
| 88 | for (int i = 0; i < d->ui.m_langCombo->count(); i++) { |
| 89 | const QString tag = d->ui.m_langCombo->itemData(index: i).toString(); |
| 90 | if (tag.isEmpty()) { // skip separator |
| 91 | continue; |
| 92 | } |
| 93 | auto *item = new QListWidgetItem(d->ui.m_langCombo->itemText(index: i), d->ui.languageList); |
| 94 | item->setData(role: Qt::UserRole, value: tag); |
| 95 | } |
| 96 | |
| 97 | d->ui.kcfg_backgroundCheckerEnabled->hide(); // hidden by default |
| 98 | |
| 99 | connect(sender: d->ui.addButton, signal: &QAbstractButton::clicked, context: this, slot: [this] { |
| 100 | d->slotIgnoreWordAdded(); |
| 101 | }); |
| 102 | connect(sender: d->ui.removeButton, signal: &QAbstractButton::clicked, context: this, slot: [this] { |
| 103 | d->slotIgnoreWordRemoved(); |
| 104 | }); |
| 105 | |
| 106 | layout->addWidget(d->wdg); |
| 107 | connect(sender: d->ui.newIgnoreEdit, signal: &QLineEdit::textChanged, context: this, slot: [this](const QString &text) { |
| 108 | d->slotUpdateButton(text); |
| 109 | }); |
| 110 | connect(sender: d->ui.ignoreListWidget, signal: &QListWidget::itemSelectionChanged, context: this, slot: [this] { |
| 111 | d->slotSelectionChanged(); |
| 112 | }); |
| 113 | d->ui.addButton->setEnabled(false); |
| 114 | d->ui.removeButton->setEnabled(false); |
| 115 | |
| 116 | connect(sender: d->ui.m_langCombo, signal: &DictionaryComboBox::dictionaryChanged, context: this, slot: &ConfigView::configChanged); |
| 117 | connect(sender: d->ui.languageList, signal: &QListWidget::itemChanged, context: this, slot: &ConfigView::configChanged); |
| 118 | |
| 119 | connect(sender: d->ui.kcfg_backgroundCheckerEnabled, signal: &QAbstractButton::clicked, context: this, slot: &ConfigView::configChanged); |
| 120 | connect(sender: d->ui.kcfg_skipUppercase, signal: &QAbstractButton::clicked, context: this, slot: &ConfigView::configChanged); |
| 121 | connect(sender: d->ui.kcfg_skipRunTogether, signal: &QAbstractButton::clicked, context: this, slot: &ConfigView::configChanged); |
| 122 | connect(sender: d->ui.kcfg_checkerEnabledByDefault, signal: &QAbstractButton::clicked, context: this, slot: &ConfigView::configChanged); |
| 123 | connect(sender: d->ui.kcfg_autodetectLanguage, signal: &QAbstractButton::clicked, context: this, slot: &ConfigView::configChanged); |
| 124 | } |
| 125 | |
| 126 | ConfigView::~ConfigView() = default; |
| 127 | |
| 128 | void ConfigView::setNoBackendFoundVisible(bool show) |
| 129 | { |
| 130 | d->ui.nobackendfound->setVisible(show); |
| 131 | } |
| 132 | |
| 133 | bool ConfigView::noBackendFoundVisible() const |
| 134 | { |
| 135 | return d->ui.nobackendfound->isVisible(); |
| 136 | } |
| 137 | |
| 138 | void ConfigView::setBackgroundCheckingButtonShown(bool b) |
| 139 | { |
| 140 | d->ui.kcfg_backgroundCheckerEnabled->setVisible(b); |
| 141 | } |
| 142 | |
| 143 | bool ConfigView::backgroundCheckingButtonShown() const |
| 144 | { |
| 145 | return !d->ui.kcfg_backgroundCheckerEnabled->isHidden(); |
| 146 | } |
| 147 | |
| 148 | void ConfigView::setLanguage(const QString &language) |
| 149 | { |
| 150 | d->ui.m_langCombo->setCurrentByDictionary(language); |
| 151 | } |
| 152 | |
| 153 | QString ConfigView::language() const |
| 154 | { |
| 155 | if (d->ui.m_langCombo->count()) { |
| 156 | return d->ui.m_langCombo->currentDictionary(); |
| 157 | } else { |
| 158 | return QString(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void ConfigView::setPreferredLanguages(const QStringList &preferredLanguages) |
| 163 | { |
| 164 | for (int i = 0; i < d->ui.languageList->count(); ++i) { |
| 165 | QListWidgetItem *item = d->ui.languageList->item(row: i); |
| 166 | QString tag = item->data(role: Qt::UserRole).toString(); |
| 167 | if (preferredLanguages.contains(str: tag)) { |
| 168 | item->setCheckState(Qt::Checked); |
| 169 | } else { |
| 170 | item->setCheckState(Qt::Unchecked); |
| 171 | } |
| 172 | } |
| 173 | Q_EMIT configChanged(); |
| 174 | } |
| 175 | |
| 176 | QStringList ConfigView::preferredLanguages() const |
| 177 | { |
| 178 | QStringList preferredLanguages; |
| 179 | for (int i = 0; i < d->ui.languageList->count(); i++) { |
| 180 | if (d->ui.languageList->item(row: i)->checkState() == Qt::Unchecked) { |
| 181 | continue; |
| 182 | } |
| 183 | preferredLanguages << d->ui.languageList->item(row: i)->data(role: Qt::UserRole).toString(); |
| 184 | } |
| 185 | return preferredLanguages; |
| 186 | } |
| 187 | |
| 188 | void ConfigView::setIgnoreList(const QStringList &ignoreList) |
| 189 | { |
| 190 | d->ignoreList = ignoreList; |
| 191 | d->ignoreList.sort(); |
| 192 | d->ui.ignoreListWidget->clear(); |
| 193 | d->ui.ignoreListWidget->addItems(labels: d->ignoreList); |
| 194 | Q_EMIT configChanged(); |
| 195 | } |
| 196 | |
| 197 | QStringList ConfigView::ignoreList() const |
| 198 | { |
| 199 | return d->ignoreList; |
| 200 | } |
| 201 | |
| 202 | #include "moc_configview.cpp" |
| 203 | |