instead of storing the current indexes in the model, we will store the current objects in order to correctly check whether notifications should be sent

This commit is contained in:
Roman Pudashkin 2022-04-27 10:11:01 +02:00 committed by Casper Jeukendrup
parent 55f1a7a2d8
commit ef30af1bf9
No known key found for this signature in database
GPG key ID: 6C571BEF59E722DD
2 changed files with 24 additions and 46 deletions

View file

@ -79,17 +79,17 @@ QStringList TemplatesModel::categoriesTitles() const
int TemplatesModel::currentCategoryIndex() const
{
return m_currentCategoryIndex;
return m_visibleCategoriesTitles.indexOf(m_currentCategory);
}
int TemplatesModel::currentTemplateIndex() const
{
return m_currentTemplateIndex;
return m_visibleTemplates.indexOf(m_currentTemplate);
}
QString TemplatesModel::currentTemplatePath() const
{
return currentTemplate().meta.filePath.toQString();
return m_currentTemplate.meta.filePath.toQString();
}
QStringList TemplatesModel::templatesTitles() const
@ -105,7 +105,7 @@ QStringList TemplatesModel::templatesTitles() const
void TemplatesModel::setCurrentCategoryIndex(int index)
{
doSetCurrentCategoryIndex(index);
setCurrentCategory(m_visibleCategoriesTitles.value(index));
updateTemplatesByCurrentCategory();
}
@ -118,16 +118,16 @@ void TemplatesModel::setVisibleTemplates(const Templates& templates)
m_visibleTemplates = templates;
emit templatesChanged();
doSetCurrentTemplateIndex(0);
setCurrentTemplate(templates.value(0));
}
void TemplatesModel::doSetCurrentTemplateIndex(int index)
void TemplatesModel::setCurrentTemplate(const Template& templ)
{
if (m_currentTemplateIndex == index) {
if (m_currentTemplate == templ) {
return;
}
m_currentTemplateIndex = index;
m_currentTemplate = templ;
emit currentTemplateChanged();
}
@ -137,28 +137,25 @@ void TemplatesModel::setVisibleCategories(const QStringList& titles)
return;
}
QString currentCategory = m_visibleCategoriesTitles.value(m_currentCategoryIndex, QString());
QString currentCategory = titles.value(0);
if (m_saveCurrentCategory) {
m_saveCurrentCategory = false;
currentCategory = m_currentCategory;
}
m_visibleCategoriesTitles = titles;
emit categoriesChanged();
int currentCategoryIndex = 0;
if (m_saveCurrentCategory) {
currentCategoryIndex = std::max(titles.indexOf(currentCategory), currentCategoryIndex);
m_saveCurrentCategory = false;
}
doSetCurrentCategoryIndex(currentCategoryIndex);
setCurrentCategory(currentCategory);
}
void TemplatesModel::doSetCurrentCategoryIndex(int index)
void TemplatesModel::setCurrentCategory(const QString& category)
{
if (m_currentCategoryIndex == index) {
if (m_currentCategory == category) {
return;
}
m_currentCategoryIndex = index;
m_currentCategory = category;
emit currentCategoryChanged();
}
@ -171,11 +168,10 @@ void TemplatesModel::updateTemplatesByCurrentCategory()
return;
}
QString currentCategoryTitle = titles[m_currentCategoryIndex];
Templates newVisibleTemplates;
for (const Template& templ: m_allTemplates) {
if (templ.categoryTitle == currentCategoryTitle) {
if (templ.categoryTitle == m_currentCategory) {
newVisibleTemplates << templ;
}
}
@ -185,16 +181,10 @@ void TemplatesModel::updateTemplatesByCurrentCategory()
void TemplatesModel::setCurrentTemplateIndex(int index)
{
if (m_currentTemplateIndex == index) {
return;
}
doSetCurrentTemplateIndex(index);
setCurrentTemplate(m_visibleTemplates.value(index));
if (isSearching()) {
const QString& currentCategory = currentTemplate().categoryTitle;
int newCategoryIndex = m_visibleCategoriesTitles.indexOf(currentCategory);
doSetCurrentCategoryIndex(newCategoryIndex);
setCurrentCategory(m_currentTemplate.categoryTitle);
}
}
@ -252,13 +242,3 @@ bool TemplatesModel::isSearching() const
{
return !m_searchText.isEmpty();
}
const Template& TemplatesModel::currentTemplate() const
{
if (m_currentTemplateIndex < 0 || m_currentTemplateIndex >= m_visibleTemplates.size()) {
static Template dummy;
return dummy;
}
return m_visibleTemplates[m_currentTemplateIndex];
}

View file

@ -72,8 +72,8 @@ private:
void setVisibleTemplates(const Templates& templates);
void setVisibleCategories(const QStringList& titles);
void doSetCurrentTemplateIndex(int index);
void doSetCurrentCategoryIndex(int index);
void setCurrentTemplate(const Template& templ);
void setCurrentCategory(const QString& category);
void updateTemplatesByCurrentCategory();
void updateTemplatesAndCategoriesBySearch();
@ -82,16 +82,14 @@ private:
bool isSearching() const;
const Template& currentTemplate() const;
Templates m_allTemplates;
Templates m_visibleTemplates;
QStringList m_visibleCategoriesTitles;
QString m_searchText;
int m_currentCategoryIndex = -1;
int m_currentTemplateIndex = -1;
QString m_currentCategory;
Template m_currentTemplate;
bool m_saveCurrentCategory = false;
};