File indexing completed on 2024-07-14 03:42:03

0001 /*
0002     This file is part of Kiten, a KDE Japanese Reference Tool...
0003     SPDX-FileCopyrightText: 2006 Joseph Kerian <jkerian@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "configsortingpage.h"
0009 
0010 #include <QCheckBox>
0011 #include <QListWidget>
0012 #include <QStringList>
0013 
0014 #include "dictionarymanager.h"
0015 
0016 #include "kitenconfig.h"
0017 
0018 ConfigSortingPage::ConfigSortingPage(QWidget *parent, KitenConfigSkeleton *config, Qt::WindowFlags f)
0019     : QWidget(parent, f)
0020     , _config(config)
0021 {
0022     setupUi(this);
0023     _dictNames = DictionaryManager::listDictFileTypes();
0024 
0025     // Setup the relationship between the checkbox and the dictionary sortlist
0026     connect(kcfg_dictionary_enable, &QCheckBox::clicked, dictionary_order, &KActionSelector::setEnabled);
0027     dictionary_order->setEnabled(_config->dictionary_enable() == QLatin1String("true"));
0028 
0029     _fields.append(QStringLiteral("Word/Kanji"));
0030     _fields.append(QStringLiteral("Meaning"));
0031     _fields.append(QStringLiteral("Reading"));
0032     QList<QString> fieldListMap = DictionaryManager::generateExtendedFieldsList().keys();
0033     _fields += fieldListMap;
0034 
0035     // Make the connections to alert the main dialog when things change
0036     connect(dictionary_order, &KActionSelector::added, this, &ConfigSortingPage::widgetChanged);
0037     connect(dictionary_order, &KActionSelector::removed, this, &ConfigSortingPage::widgetChanged);
0038     connect(dictionary_order, &KActionSelector::movedUp, this, &ConfigSortingPage::widgetChanged);
0039     connect(dictionary_order, &KActionSelector::movedDown, this, &ConfigSortingPage::widgetChanged);
0040 
0041     connect(field_order, &KActionSelector::added, this, &ConfigSortingPage::widgetChanged);
0042     connect(field_order, &KActionSelector::removed, this, &ConfigSortingPage::widgetChanged);
0043     connect(field_order, &KActionSelector::movedUp, this, &ConfigSortingPage::widgetChanged);
0044     connect(field_order, &KActionSelector::movedDown, this, &ConfigSortingPage::widgetChanged);
0045 }
0046 
0047 // Read from preferences and set widgets
0048 void ConfigSortingPage::updateWidgets()
0049 {
0050     QStringList selectedDicts = _config->dictionary_sortlist();
0051     QStringList selectedFields = _config->field_sortlist();
0052 
0053     QStringList availDicts = _dictNames;
0054     QStringList availFields = _fields;
0055 
0056     for (const QString &dict : selectedDicts) {
0057         availDicts.removeAll(dict);
0058     }
0059 
0060     for (const QString &field : selectedFields) {
0061         availDicts.removeAll(field);
0062     }
0063 
0064     dictionary_order->availableListWidget()->clear();
0065     dictionary_order->availableListWidget()->addItems(availDicts);
0066     dictionary_order->selectedListWidget()->clear();
0067     dictionary_order->selectedListWidget()->addItems(selectedDicts);
0068 
0069     field_order->availableListWidget()->clear();
0070     field_order->availableListWidget()->addItems(availFields);
0071     field_order->selectedListWidget()->clear();
0072     field_order->selectedListWidget()->addItems(selectedFields);
0073 }
0074 
0075 void ConfigSortingPage::updateSettings()
0076 {
0077     QStringList list;
0078     for (int i = 0; i < dictionary_order->selectedListWidget()->count(); i++) {
0079         list.append(dictionary_order->selectedListWidget()->item(i)->text());
0080     }
0081     _config->setDictionary_sortlist(list);
0082 
0083     list.clear();
0084     for (int i = 0; i < field_order->selectedListWidget()->count(); i++) {
0085         list.append(field_order->selectedListWidget()->item(i)->text());
0086     }
0087     _config->setField_sortlist(list);
0088 }
0089 
0090 void ConfigSortingPage::updateWidgetsDefault()
0091 {
0092     // No Default?
0093 }
0094 
0095 bool ConfigSortingPage::isDefault()
0096 {
0097     return true;
0098 }
0099 
0100 bool ConfigSortingPage::hasChanged()
0101 {
0102     return false;
0103 }
0104 
0105 #include "moc_configsortingpage.cpp"