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:
Nikhil Tanwar 2023-10-01 12:06:12 +05:30 committed by Matthieu Gautier
parent d2a6098eca
commit f61635e7ca
8 changed files with 92 additions and 51 deletions

View File

@ -619,8 +619,12 @@ QStringList ContentManager::getDownloadIds()
return list; return list;
} }
void ContentManager::setCurrentLanguage(QStringList languageList) void ContentManager::setCurrentLanguage(FilterList langPairList)
{ {
QStringList languageList;
for (auto &langPair : langPairList) {
languageList.append(langPair.second);
}
languageList.sort(); languageList.sort();
for (auto &language : languageList) { for (auto &language : languageList) {
if (language.length() == 2) { if (language.length() == 2) {
@ -634,25 +638,33 @@ void ContentManager::setCurrentLanguage(QStringList languageList)
if (m_currentLanguage == newLanguage) if (m_currentLanguage == newLanguage)
return; return;
m_currentLanguage = newLanguage; m_currentLanguage = newLanguage;
KiwixApp::instance()->getSettingsManager()->setLanguage(languageList); KiwixApp::instance()->getSettingsManager()->setLanguage(langPairList);
emit(currentLangChanged()); emit(currentLangChanged());
emit(filterParamsChanged()); emit(filterParamsChanged());
} }
void ContentManager::setCurrentCategoryFilter(QStringList categoryList) void ContentManager::setCurrentCategoryFilter(FilterList categoryPairList)
{ {
QStringList categoryList;
for (auto &catPair : categoryPairList) {
categoryList.append(catPair.second);
}
categoryList.sort(); categoryList.sort();
if (m_categoryFilter == categoryList.join(",")) if (m_categoryFilter == categoryList.join(","))
return; return;
m_categoryFilter = categoryList.join(","); m_categoryFilter = categoryList.join(",");
KiwixApp::instance()->getSettingsManager()->setCategory(categoryList); KiwixApp::instance()->getSettingsManager()->setCategory(categoryPairList);
emit(filterParamsChanged()); 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; m_contentTypeFilters = contentTypeFilters;
KiwixApp::instance()->getSettingsManager()->setContentType(m_contentTypeFilters); KiwixApp::instance()->getSettingsManager()->setContentType(contentTypeFiltersPairList);
emit(filterParamsChanged()); emit(filterParamsChanged());
} }

View File

@ -19,15 +19,16 @@ class ContentManager : public QObject
public: public:
typedef QList<QPair<QString, QString>> LanguageList; typedef QList<QPair<QString, QString>> LanguageList;
typedef QList<QPair<QString, QString>> FilterList;
explicit ContentManager(Library* library, kiwix::Downloader *downloader, QObject *parent = nullptr); explicit ContentManager(Library* library, kiwix::Downloader *downloader, QObject *parent = nullptr);
virtual ~ContentManager() {} virtual ~ContentManager() {}
ContentManagerView* getView() { return mp_view; } ContentManagerView* getView() { return mp_view; }
void setLocal(bool local); void setLocal(bool local);
QStringList getDownloadIds(); QStringList getDownloadIds();
void setCurrentLanguage(QStringList languageList); void setCurrentLanguage(FilterList languageList);
void setCurrentCategoryFilter(QStringList category); void setCurrentCategoryFilter(FilterList category);
void setCurrentContentTypeFilter(QStringList contentTypeFilter); void setCurrentContentTypeFilter(FilterList contentTypeFilter);
bool isLocal() const { return m_local; } bool isLocal() const { return m_local; }
QStringList getCategories() const { return m_categories; } QStringList getCategories() const { return m_categories; }
LanguageList getLanguages() const { return m_languages; } LanguageList getLanguages() const { return m_languages; }

View File

@ -50,7 +50,7 @@ ContentManagerSide::ContentManagerSide(QWidget *parent) :
KiwixApp::instance()->getContentManager()->setSearch(searcher->text()); KiwixApp::instance()->getContentManager()->setSearch(searcher->text());
}); });
QList<QPair<QString, QString>> contentTypeList = { FilterList contentTypeList = {
{"_pictures:yes", gt("pictures")}, {"_pictures:yes", gt("pictures")},
{"_pictures:no", gt("no-pictures")}, {"_pictures:no", gt("no-pictures")},
{"_videos:yes", gt("videos")}, {"_videos:yes", gt("videos")},
@ -79,13 +79,13 @@ void ContentManagerSide::setContentManager(ContentManager *contentManager)
const auto checkedButton = mp_ui->buttonGroup->button(isLocal == CatalogButtonId::LOCAL); const auto checkedButton = mp_ui->buttonGroup->button(isLocal == CatalogButtonId::LOCAL);
checkedButton->setChecked(true); checkedButton->setChecked(true);
checkedButton->setStyleSheet("*{font-weight: bold}"); 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); mp_contentManager->setCurrentLanguage(values);
}); });
connect(mp_categories, &KiwixChoiceBox::choiceUpdated, this, [=](QStringList values) { connect(mp_categories, &KiwixChoiceBox::choiceUpdated, this, [=](FilterList values) {
mp_contentManager->setCurrentCategoryFilter(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); mp_contentManager->setCurrentContentTypeFilter(values);
}); });
} }

View File

@ -12,6 +12,7 @@ class contentmanagerside;
} }
class KiwixChoiceBox; class KiwixChoiceBox;
using FilterList = ContentManager::FilterList;
class ContentManagerSide : public QWidget class ContentManagerSide : public QWidget
{ {

View File

@ -226,7 +226,7 @@ QString beautifyString(QString word)
return word; return word;
} }
void KiwixChoiceBox::setSelections(QStringList selections, QStringList defaultSelection) void KiwixChoiceBox::setSelections(QStringList selections, SelectionList defaultSelection)
{ {
SelectionList sList; SelectionList sList;
for (const auto &sel : selections) { for (const auto &sel : selections) {
@ -235,15 +235,8 @@ void KiwixChoiceBox::setSelections(QStringList selections, QStringList defaultSe
setSelections(sList, defaultSelection); 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(); clearSelections();
choiceSelector->clear(); choiceSelector->clear();
for (const auto &selection: selections) for (const auto &selection: selections)
@ -251,10 +244,20 @@ void KiwixChoiceBox::setSelections(SelectionList selections, QStringList default
auto item = new KListWidgetItem(beautifyString(selection.second)); auto item = new KListWidgetItem(beautifyString(selection.second));
item->setData(Qt::UserRole, selection.first); item->setData(Qt::UserRole, selection.first);
choiceSelector->addItem(item); 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); addSelection(item, false);
} else {
addSelection(itemList[0], false);
} }
} }
if (choiceSelector->selectedItems().isEmpty()) if (choiceSelector->selectedItems().isEmpty())
showPlaceholder(); showPlaceholder();
choiceSelector->setVisibleItems(choiceSelector->count()); choiceSelector->setVisibleItems(choiceSelector->count());
@ -277,11 +280,11 @@ void KiwixChoiceBox::setType(QString type)
m_type = type; m_type = type;
} }
QStringList KiwixChoiceBox::getCurrentSelected() KiwixChoiceBox::SelectionList KiwixChoiceBox::getCurrentSelected()
{ {
QStringList selections; SelectionList selections;
for (auto &item : choiceSelector->selectedItems()) { for (auto &item : choiceSelector->selectedItems()) {
selections.append(item->data(Qt::UserRole).toString()); selections.append({item->text(), item->data(Qt::UserRole).toString()});
} }
return selections; return selections;
} }

View File

@ -29,8 +29,8 @@ class KiwixChoiceBox : public QWidget
public: public:
explicit KiwixChoiceBox(QWidget *parent = nullptr); explicit KiwixChoiceBox(QWidget *parent = nullptr);
void setType(QString type); void setType(QString type);
void setSelections(SelectionList selections, QStringList defaultSelection); void setSelections(SelectionList selections, SelectionList defaultSelection);
void setSelections(QStringList selections, QStringList defaultSelection); void setSelections(QStringList selections, SelectionList defaultSelection);
~KiwixChoiceBox(); ~KiwixChoiceBox();
void adjustSize(); void adjustSize();
@ -39,7 +39,7 @@ protected:
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
signals: signals:
void choiceUpdated(QStringList); void choiceUpdated(SelectionList);
void clicked(); void clicked();
private: private:
@ -49,7 +49,7 @@ private:
KiwixListWidget *choiceSelector; KiwixListWidget *choiceSelector;
FlowLayout *currentChoicesLayout; FlowLayout *currentChoicesLayout;
KiwixLineEdit *searcher; KiwixLineEdit *searcher;
QStringList getCurrentSelected(); SelectionList getCurrentSelected();
bool removeSelection(QListWidgetItem *item); bool removeSelection(QListWidgetItem *item);
void clearSelections(); void clearSelections();
bool addSelection(QListWidgetItem *item, bool updateRequired = true); bool addSelection(QListWidgetItem *item, bool updateRequired = true);

View File

@ -98,23 +98,43 @@ void SettingsManager::setMoveToTrash(bool moveToTrash)
emit(moveToTrashChanged(m_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); setSettings("language", m_langList);
emit(languageChanged(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); setSettings("category", m_categoryList);
emit(categoryChanged(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); setSettings("contentType", m_contentTypeList);
emit(contentTypeChanged(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_kiwixServerIpAddress = m_settings.value("localKiwixServer/ipAddress", QString("0.0.0.0")).toString();
m_monitorDir = m_settings.value("monitor/dir", QString("")).toString(); m_monitorDir = m_settings.value("monitor/dir", QString("")).toString();
m_moveToTrash = m_settings.value("moveToTrash", true).toBool(); m_moveToTrash = m_settings.value("moveToTrash", true).toBool();
m_langList = m_settings.value("language", QLocale::languageToString(QLocale().language())).toStringList(); QVariant defaultLang = QVariant::fromValue(QLocale::languageToString(QLocale().language()) + '|' + QLocale().name().split("_").at(0));
m_categoryList = m_settings.value("category", {}).toStringList(); m_langList = m_settings.value("language", {defaultLang}).toList();
m_contentTypeList = m_settings.value("contentType", {}).toStringList(); m_categoryList = m_settings.value("category", {}).toList();
m_contentTypeList = m_settings.value("contentType", {}).toList();
} }

View File

@ -13,6 +13,7 @@ class SettingsManager : public QObject
Q_PROPERTY(QString downloadDir MEMBER m_downloadDir WRITE setDownloadDir NOTIFY downloadDirChanged) Q_PROPERTY(QString downloadDir MEMBER m_downloadDir WRITE setDownloadDir NOTIFY downloadDirChanged)
public: public:
typedef QList<QPair<QString, QString>> FilterList;
explicit SettingsManager(QObject *parent = nullptr); explicit SettingsManager(QObject *parent = nullptr);
virtual ~SettingsManager() {}; virtual ~SettingsManager() {};
@ -28,9 +29,9 @@ public:
QString getDownloadDir() const { return m_downloadDir; } QString getDownloadDir() const { return m_downloadDir; }
QString getMonitorDir() const { return m_monitorDir; } QString getMonitorDir() const { return m_monitorDir; }
bool getMoveToTrash() const { return m_moveToTrash; } bool getMoveToTrash() const { return m_moveToTrash; }
QStringList getLanguageList() const { return m_langList; } FilterList getLanguageList() { return deducePair(m_langList); }
QStringList getCategoryList() const { return m_categoryList; } FilterList getCategoryList() { return deducePair(m_categoryList); }
QStringList getContentType() const { return m_contentTypeList; } FilterList getContentType() { return deducePair(m_contentTypeList); }
public slots: public slots:
void setKiwixServerPort(int port); void setKiwixServerPort(int port);
@ -39,11 +40,13 @@ public slots:
void setDownloadDir(QString downloadDir); void setDownloadDir(QString downloadDir);
void setMonitorDir(QString monitorDir); void setMonitorDir(QString monitorDir);
void setMoveToTrash(bool moveToTrash); void setMoveToTrash(bool moveToTrash);
void setLanguage(QStringList langList); void setLanguage(FilterList langList);
void setCategory(QStringList categoryList); void setCategory(FilterList categoryList);
void setContentType(QStringList contentTypeList); void setContentType(FilterList contentTypeList);
private: private:
void initSettings(); void initSettings();
QList<QVariant> flattenPair(FilterList pairList);
FilterList deducePair(QList<QVariant>);
signals: signals:
void portChanged(int port); void portChanged(int port);
@ -51,9 +54,9 @@ signals:
void downloadDirChanged(QString downloadDir); void downloadDirChanged(QString downloadDir);
void monitorDirChanged(QString monitorDir); void monitorDirChanged(QString monitorDir);
void moveToTrashChanged(bool moveToTrash); void moveToTrashChanged(bool moveToTrash);
void languageChanged(QStringList langList); void languageChanged(QList<QVariant> langList);
void categoryChanged(QStringList categoryList); void categoryChanged(QList<QVariant> categoryList);
void contentTypeChanged(QStringList contentTypeList); void contentTypeChanged(QList<QVariant> contentTypeList);
private: private:
QSettings m_settings; QSettings m_settings;
@ -64,9 +67,9 @@ private:
QString m_downloadDir; QString m_downloadDir;
QString m_monitorDir; QString m_monitorDir;
bool m_moveToTrash; bool m_moveToTrash;
QStringList m_langList; QList<QVariant> m_langList;
QStringList m_categoryList; QList<QVariant> m_categoryList;
QStringList m_contentTypeList; QList<QVariant> m_contentTypeList;
}; };
#endif // SETTINGSMANAGER_H #endif // SETTINGSMANAGER_H