mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-22 11:37:56 -04:00
Save filter settings with their keys
Now, the filter settings (language, category and content type) are saved as: keys|values in QSettings.
This commit is contained in:
parent
d2a6098eca
commit
f61635e7ca
@ -619,8 +619,12 @@ QStringList ContentManager::getDownloadIds()
|
||||
return list;
|
||||
}
|
||||
|
||||
void ContentManager::setCurrentLanguage(QStringList languageList)
|
||||
void ContentManager::setCurrentLanguage(FilterList langPairList)
|
||||
{
|
||||
QStringList languageList;
|
||||
for (auto &langPair : langPairList) {
|
||||
languageList.append(langPair.second);
|
||||
}
|
||||
languageList.sort();
|
||||
for (auto &language : languageList) {
|
||||
if (language.length() == 2) {
|
||||
@ -634,25 +638,33 @@ void ContentManager::setCurrentLanguage(QStringList languageList)
|
||||
if (m_currentLanguage == newLanguage)
|
||||
return;
|
||||
m_currentLanguage = newLanguage;
|
||||
KiwixApp::instance()->getSettingsManager()->setLanguage(languageList);
|
||||
KiwixApp::instance()->getSettingsManager()->setLanguage(langPairList);
|
||||
emit(currentLangChanged());
|
||||
emit(filterParamsChanged());
|
||||
}
|
||||
|
||||
void ContentManager::setCurrentCategoryFilter(QStringList categoryList)
|
||||
void ContentManager::setCurrentCategoryFilter(FilterList categoryPairList)
|
||||
{
|
||||
QStringList categoryList;
|
||||
for (auto &catPair : categoryPairList) {
|
||||
categoryList.append(catPair.second);
|
||||
}
|
||||
categoryList.sort();
|
||||
if (m_categoryFilter == categoryList.join(","))
|
||||
return;
|
||||
m_categoryFilter = categoryList.join(",");
|
||||
KiwixApp::instance()->getSettingsManager()->setCategory(categoryList);
|
||||
KiwixApp::instance()->getSettingsManager()->setCategory(categoryPairList);
|
||||
emit(filterParamsChanged());
|
||||
}
|
||||
|
||||
void ContentManager::setCurrentContentTypeFilter(QStringList contentTypeFilters)
|
||||
void ContentManager::setCurrentContentTypeFilter(FilterList contentTypeFiltersPairList)
|
||||
{
|
||||
QStringList contentTypeFilters;
|
||||
for (auto &ctfPair : contentTypeFiltersPairList) {
|
||||
contentTypeFilters.append(ctfPair.second);
|
||||
}
|
||||
m_contentTypeFilters = contentTypeFilters;
|
||||
KiwixApp::instance()->getSettingsManager()->setContentType(m_contentTypeFilters);
|
||||
KiwixApp::instance()->getSettingsManager()->setContentType(contentTypeFiltersPairList);
|
||||
emit(filterParamsChanged());
|
||||
}
|
||||
|
||||
|
@ -19,15 +19,16 @@ class ContentManager : public QObject
|
||||
|
||||
public:
|
||||
typedef QList<QPair<QString, QString>> LanguageList;
|
||||
typedef QList<QPair<QString, QString>> FilterList;
|
||||
explicit ContentManager(Library* library, kiwix::Downloader *downloader, QObject *parent = nullptr);
|
||||
virtual ~ContentManager() {}
|
||||
|
||||
ContentManagerView* getView() { return mp_view; }
|
||||
void setLocal(bool local);
|
||||
QStringList getDownloadIds();
|
||||
void setCurrentLanguage(QStringList languageList);
|
||||
void setCurrentCategoryFilter(QStringList category);
|
||||
void setCurrentContentTypeFilter(QStringList contentTypeFilter);
|
||||
void setCurrentLanguage(FilterList languageList);
|
||||
void setCurrentCategoryFilter(FilterList category);
|
||||
void setCurrentContentTypeFilter(FilterList contentTypeFilter);
|
||||
bool isLocal() const { return m_local; }
|
||||
QStringList getCategories() const { return m_categories; }
|
||||
LanguageList getLanguages() const { return m_languages; }
|
||||
|
@ -50,7 +50,7 @@ ContentManagerSide::ContentManagerSide(QWidget *parent) :
|
||||
KiwixApp::instance()->getContentManager()->setSearch(searcher->text());
|
||||
});
|
||||
|
||||
QList<QPair<QString, QString>> contentTypeList = {
|
||||
FilterList contentTypeList = {
|
||||
{"_pictures:yes", gt("pictures")},
|
||||
{"_pictures:no", gt("no-pictures")},
|
||||
{"_videos:yes", gt("videos")},
|
||||
@ -79,13 +79,13 @@ void ContentManagerSide::setContentManager(ContentManager *contentManager)
|
||||
const auto checkedButton = mp_ui->buttonGroup->button(isLocal == CatalogButtonId::LOCAL);
|
||||
checkedButton->setChecked(true);
|
||||
checkedButton->setStyleSheet("*{font-weight: bold}");
|
||||
connect(mp_languages, &KiwixChoiceBox::choiceUpdated, this, [=](QStringList values) {
|
||||
connect(mp_languages, &KiwixChoiceBox::choiceUpdated, this, [=](FilterList values) {
|
||||
mp_contentManager->setCurrentLanguage(values);
|
||||
});
|
||||
connect(mp_categories, &KiwixChoiceBox::choiceUpdated, this, [=](QStringList values) {
|
||||
connect(mp_categories, &KiwixChoiceBox::choiceUpdated, this, [=](FilterList values) {
|
||||
mp_contentManager->setCurrentCategoryFilter(values);
|
||||
});
|
||||
connect(mp_contentType, &KiwixChoiceBox::choiceUpdated, this, [=](QStringList values) {
|
||||
connect(mp_contentType, &KiwixChoiceBox::choiceUpdated, this, [=](FilterList values) {
|
||||
mp_contentManager->setCurrentContentTypeFilter(values);
|
||||
});
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ class contentmanagerside;
|
||||
}
|
||||
|
||||
class KiwixChoiceBox;
|
||||
using FilterList = ContentManager::FilterList;
|
||||
|
||||
class ContentManagerSide : public QWidget
|
||||
{
|
||||
|
@ -226,7 +226,7 @@ QString beautifyString(QString word)
|
||||
return word;
|
||||
}
|
||||
|
||||
void KiwixChoiceBox::setSelections(QStringList selections, QStringList defaultSelection)
|
||||
void KiwixChoiceBox::setSelections(QStringList selections, SelectionList defaultSelection)
|
||||
{
|
||||
SelectionList sList;
|
||||
for (const auto &sel : selections) {
|
||||
@ -235,15 +235,8 @@ void KiwixChoiceBox::setSelections(QStringList selections, QStringList defaultSe
|
||||
setSelections(sList, defaultSelection);
|
||||
}
|
||||
|
||||
void KiwixChoiceBox::setSelections(SelectionList selections, QStringList defaultSelection)
|
||||
void KiwixChoiceBox::setSelections(SelectionList selections, SelectionList defaultSelection)
|
||||
{
|
||||
auto prevSelections = choiceSelector->selectedItems();
|
||||
for (auto prev : prevSelections) {
|
||||
QPair<QString, QString> prevPair = {prev->data(Qt::UserRole).toString(), prev->text()};
|
||||
if (!selections.contains(prevPair)) {
|
||||
selections.append(prevPair);
|
||||
}
|
||||
}
|
||||
clearSelections();
|
||||
choiceSelector->clear();
|
||||
for (const auto &selection: selections)
|
||||
@ -251,10 +244,20 @@ void KiwixChoiceBox::setSelections(SelectionList selections, QStringList default
|
||||
auto item = new KListWidgetItem(beautifyString(selection.second));
|
||||
item->setData(Qt::UserRole, selection.first);
|
||||
choiceSelector->addItem(item);
|
||||
if (defaultSelection.contains(selection.first)) {
|
||||
}
|
||||
|
||||
for (const auto &defSel : defaultSelection) {
|
||||
auto itemList = choiceSelector->findItems(defSel.first, Qt::MatchExactly);
|
||||
if (itemList.isEmpty()) {
|
||||
auto item = new KListWidgetItem(defSel.first);
|
||||
item->setData(Qt::UserRole, defSel.second);
|
||||
choiceSelector->addItem(item);
|
||||
addSelection(item, false);
|
||||
} else {
|
||||
addSelection(itemList[0], false);
|
||||
}
|
||||
}
|
||||
|
||||
if (choiceSelector->selectedItems().isEmpty())
|
||||
showPlaceholder();
|
||||
choiceSelector->setVisibleItems(choiceSelector->count());
|
||||
@ -277,11 +280,11 @@ void KiwixChoiceBox::setType(QString type)
|
||||
m_type = type;
|
||||
}
|
||||
|
||||
QStringList KiwixChoiceBox::getCurrentSelected()
|
||||
KiwixChoiceBox::SelectionList KiwixChoiceBox::getCurrentSelected()
|
||||
{
|
||||
QStringList selections;
|
||||
SelectionList selections;
|
||||
for (auto &item : choiceSelector->selectedItems()) {
|
||||
selections.append(item->data(Qt::UserRole).toString());
|
||||
selections.append({item->text(), item->data(Qt::UserRole).toString()});
|
||||
}
|
||||
return selections;
|
||||
}
|
||||
|
@ -29,8 +29,8 @@ class KiwixChoiceBox : public QWidget
|
||||
public:
|
||||
explicit KiwixChoiceBox(QWidget *parent = nullptr);
|
||||
void setType(QString type);
|
||||
void setSelections(SelectionList selections, QStringList defaultSelection);
|
||||
void setSelections(QStringList selections, QStringList defaultSelection);
|
||||
void setSelections(SelectionList selections, SelectionList defaultSelection);
|
||||
void setSelections(QStringList selections, SelectionList defaultSelection);
|
||||
~KiwixChoiceBox();
|
||||
void adjustSize();
|
||||
|
||||
@ -39,7 +39,7 @@ protected:
|
||||
void mousePressEvent(QMouseEvent *event) override;
|
||||
|
||||
signals:
|
||||
void choiceUpdated(QStringList);
|
||||
void choiceUpdated(SelectionList);
|
||||
void clicked();
|
||||
|
||||
private:
|
||||
@ -49,7 +49,7 @@ private:
|
||||
KiwixListWidget *choiceSelector;
|
||||
FlowLayout *currentChoicesLayout;
|
||||
KiwixLineEdit *searcher;
|
||||
QStringList getCurrentSelected();
|
||||
SelectionList getCurrentSelected();
|
||||
bool removeSelection(QListWidgetItem *item);
|
||||
void clearSelections();
|
||||
bool addSelection(QListWidgetItem *item, bool updateRequired = true);
|
||||
|
@ -98,23 +98,43 @@ void SettingsManager::setMoveToTrash(bool moveToTrash)
|
||||
emit(moveToTrashChanged(m_moveToTrash));
|
||||
}
|
||||
|
||||
void SettingsManager::setLanguage(QStringList langList)
|
||||
QList<QVariant> SettingsManager::flattenPair(FilterList pairList)
|
||||
{
|
||||
m_langList = langList;
|
||||
QList<QVariant> res;
|
||||
for (auto &pair : pairList) {
|
||||
res.push_back(pair.first+"|"+pair.second);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
SettingsManager::FilterList SettingsManager::deducePair(QList<QVariant> variantList)
|
||||
{
|
||||
FilterList pairList;
|
||||
for (auto &variant : variantList) {
|
||||
QString str = variant.toString();
|
||||
auto pairs = str.split('|');
|
||||
pairList.push_back({pairs[0], pairs[1]});
|
||||
}
|
||||
return pairList;
|
||||
}
|
||||
|
||||
void SettingsManager::setLanguage(FilterList langList)
|
||||
{
|
||||
m_langList = flattenPair(langList);
|
||||
setSettings("language", m_langList);
|
||||
emit(languageChanged(m_langList));
|
||||
}
|
||||
|
||||
void SettingsManager::setCategory(QStringList categoryList)
|
||||
void SettingsManager::setCategory(FilterList categoryList)
|
||||
{
|
||||
m_categoryList = categoryList;
|
||||
m_categoryList = flattenPair(categoryList);
|
||||
setSettings("category", m_categoryList);
|
||||
emit(categoryChanged(m_categoryList));
|
||||
}
|
||||
|
||||
void SettingsManager::setContentType(QStringList contentTypeList)
|
||||
void SettingsManager::setContentType(FilterList contentTypeList)
|
||||
{
|
||||
m_contentTypeList = contentTypeList;
|
||||
m_contentTypeList = flattenPair(contentTypeList);
|
||||
setSettings("contentType", m_contentTypeList);
|
||||
emit(contentTypeChanged(m_contentTypeList));
|
||||
}
|
||||
@ -127,7 +147,8 @@ void SettingsManager::initSettings()
|
||||
m_kiwixServerIpAddress = m_settings.value("localKiwixServer/ipAddress", QString("0.0.0.0")).toString();
|
||||
m_monitorDir = m_settings.value("monitor/dir", QString("")).toString();
|
||||
m_moveToTrash = m_settings.value("moveToTrash", true).toBool();
|
||||
m_langList = m_settings.value("language", QLocale::languageToString(QLocale().language())).toStringList();
|
||||
m_categoryList = m_settings.value("category", {}).toStringList();
|
||||
m_contentTypeList = m_settings.value("contentType", {}).toStringList();
|
||||
QVariant defaultLang = QVariant::fromValue(QLocale::languageToString(QLocale().language()) + '|' + QLocale().name().split("_").at(0));
|
||||
m_langList = m_settings.value("language", {defaultLang}).toList();
|
||||
m_categoryList = m_settings.value("category", {}).toList();
|
||||
m_contentTypeList = m_settings.value("contentType", {}).toList();
|
||||
}
|
||||
|
@ -13,6 +13,7 @@ class SettingsManager : public QObject
|
||||
Q_PROPERTY(QString downloadDir MEMBER m_downloadDir WRITE setDownloadDir NOTIFY downloadDirChanged)
|
||||
|
||||
public:
|
||||
typedef QList<QPair<QString, QString>> FilterList;
|
||||
explicit SettingsManager(QObject *parent = nullptr);
|
||||
virtual ~SettingsManager() {};
|
||||
|
||||
@ -28,9 +29,9 @@ public:
|
||||
QString getDownloadDir() const { return m_downloadDir; }
|
||||
QString getMonitorDir() const { return m_monitorDir; }
|
||||
bool getMoveToTrash() const { return m_moveToTrash; }
|
||||
QStringList getLanguageList() const { return m_langList; }
|
||||
QStringList getCategoryList() const { return m_categoryList; }
|
||||
QStringList getContentType() const { return m_contentTypeList; }
|
||||
FilterList getLanguageList() { return deducePair(m_langList); }
|
||||
FilterList getCategoryList() { return deducePair(m_categoryList); }
|
||||
FilterList getContentType() { return deducePair(m_contentTypeList); }
|
||||
|
||||
public slots:
|
||||
void setKiwixServerPort(int port);
|
||||
@ -39,11 +40,13 @@ public slots:
|
||||
void setDownloadDir(QString downloadDir);
|
||||
void setMonitorDir(QString monitorDir);
|
||||
void setMoveToTrash(bool moveToTrash);
|
||||
void setLanguage(QStringList langList);
|
||||
void setCategory(QStringList categoryList);
|
||||
void setContentType(QStringList contentTypeList);
|
||||
void setLanguage(FilterList langList);
|
||||
void setCategory(FilterList categoryList);
|
||||
void setContentType(FilterList contentTypeList);
|
||||
private:
|
||||
void initSettings();
|
||||
QList<QVariant> flattenPair(FilterList pairList);
|
||||
FilterList deducePair(QList<QVariant>);
|
||||
|
||||
signals:
|
||||
void portChanged(int port);
|
||||
@ -51,9 +54,9 @@ signals:
|
||||
void downloadDirChanged(QString downloadDir);
|
||||
void monitorDirChanged(QString monitorDir);
|
||||
void moveToTrashChanged(bool moveToTrash);
|
||||
void languageChanged(QStringList langList);
|
||||
void categoryChanged(QStringList categoryList);
|
||||
void contentTypeChanged(QStringList contentTypeList);
|
||||
void languageChanged(QList<QVariant> langList);
|
||||
void categoryChanged(QList<QVariant> categoryList);
|
||||
void contentTypeChanged(QList<QVariant> contentTypeList);
|
||||
|
||||
private:
|
||||
QSettings m_settings;
|
||||
@ -64,9 +67,9 @@ private:
|
||||
QString m_downloadDir;
|
||||
QString m_monitorDir;
|
||||
bool m_moveToTrash;
|
||||
QStringList m_langList;
|
||||
QStringList m_categoryList;
|
||||
QStringList m_contentTypeList;
|
||||
QList<QVariant> m_langList;
|
||||
QList<QVariant> m_categoryList;
|
||||
QList<QVariant> m_contentTypeList;
|
||||
};
|
||||
|
||||
#endif // SETTINGSMANAGER_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user