From 9d1fcbaca3e2774e5c605c1df4228c41a1a7dd55 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sat, 2 Apr 2016 23:48:58 -0400 Subject: [PATCH] Added category filter to SelectProductDialog. --- glabels/SelectProductDialog.cpp | 96 ++++++++++++++- glabels/SelectProductDialog.h | 13 ++ glabels/Settings.cpp | 47 +++++++- glabels/Settings.h | 7 ++ glabels/TemplatePicker.cpp | 22 +++- glabels/TemplatePicker.h | 4 +- glabels/ui/SelectProductDialog.ui | 193 ++++++++++++++++++------------ 7 files changed, 297 insertions(+), 85 deletions(-) diff --git a/glabels/SelectProductDialog.cpp b/glabels/SelectProductDialog.cpp index b13d5c5..67f0c31 100644 --- a/glabels/SelectProductDialog.cpp +++ b/glabels/SelectProductDialog.cpp @@ -39,13 +39,30 @@ SelectProductDialog::SelectProductDialog( QWidget *parent ) pageSizeUsCheck->setChecked( Settings::searchUsPaperSizes() ); pageSizeOtherCheck->setChecked( Settings::searchOtherPaperSizes() ); + anyCategoryCheck->setChecked( Settings::searchAllCategories() ); + mCategoryIdList = Settings::searchCategoryList(); + + QList categories = glabels::Db::categories(); + foreach ( glabels::Category *category, categories ) + { + QCheckBox* check = new QCheckBox( category->name() ); + check->setChecked( mCategoryIdList.contains( category->id() ) || anyCategoryCheck->isChecked() ); + categoriesLayout->addWidget( check ); + + mCheckToCategoryMap[check] = category->id(); + + connect( check, SIGNAL(clicked()), this, SLOT(onCategoryCheckClicked()) ); + } + QList tmplates = glabels::Db::templates(); templatePicker->setTemplates( tmplates ); templatePicker->applyFilter( searchEntry->text(), pageSizeIsoCheck->isChecked(), pageSizeUsCheck->isChecked(), - pageSizeOtherCheck->isChecked() ); + pageSizeOtherCheck->isChecked(), + anyCategoryCheck->isChecked(), + mCategoryIdList ); } /// @@ -72,7 +89,9 @@ void SelectProductDialog::onSearchEntryTextChanged() templatePicker->applyFilter( searchEntry->text(), pageSizeIsoCheck->isChecked(), pageSizeUsCheck->isChecked(), - pageSizeOtherCheck->isChecked() ); + pageSizeOtherCheck->isChecked(), + anyCategoryCheck->isChecked(), + mCategoryIdList ); } @@ -86,7 +105,7 @@ void SelectProductDialog::onSearchClearButtonClicked() /// -/// Page Size Check Toggled Slot +/// Page Size Check Clicked Slot /// void SelectProductDialog::onPageSizeCheckClicked() { @@ -97,7 +116,56 @@ void SelectProductDialog::onPageSizeCheckClicked() templatePicker->applyFilter( searchEntry->text(), pageSizeIsoCheck->isChecked(), pageSizeUsCheck->isChecked(), - pageSizeOtherCheck->isChecked() ); + pageSizeOtherCheck->isChecked(), + anyCategoryCheck->isChecked(), + mCategoryIdList ); +} + + +/// +/// Any category Check Clicked Slot +/// +void SelectProductDialog::onAnyCategoryCheckClicked() +{ + if ( anyCategoryCheck->isChecked() ) + { + foreach( QCheckBox* check, mCheckToCategoryMap.keys() ) + { + check->setChecked( true ); + } + } + + loadCategoryList(); + + templatePicker->applyFilter( searchEntry->text(), + pageSizeIsoCheck->isChecked(), + pageSizeUsCheck->isChecked(), + pageSizeOtherCheck->isChecked(), + anyCategoryCheck->isChecked(), + mCategoryIdList ); +} + + +/// +/// Category Check Clicked Slot +/// +void SelectProductDialog::onCategoryCheckClicked() +{ + bool allFlag = true; + foreach( QCheckBox* check, mCheckToCategoryMap.keys() ) + { + allFlag = allFlag && check->isChecked(); + } + anyCategoryCheck->setChecked( allFlag ); + + loadCategoryList(); + + templatePicker->applyFilter( searchEntry->text(), + pageSizeIsoCheck->isChecked(), + pageSizeUsCheck->isChecked(), + pageSizeOtherCheck->isChecked(), + anyCategoryCheck->isChecked(), + mCategoryIdList ); } @@ -129,3 +197,23 @@ void SelectProductDialog::timerEvent( QTimerEvent *event ) mTimer.stop(); close(); } + + +/// +/// Load category list +/// +void SelectProductDialog::loadCategoryList() +{ + mCategoryIdList.clear(); + + foreach( QCheckBox* check, mCheckToCategoryMap.keys() ) + { + if ( check->isChecked() ) + { + mCategoryIdList.append( mCheckToCategoryMap[check] ); + } + } + + Settings::setSearchAllCategories( anyCategoryCheck->isChecked() ); + Settings::setSearchCategoryList( mCategoryIdList ); +} diff --git a/glabels/SelectProductDialog.h b/glabels/SelectProductDialog.h index c056da0..bd2acec 100644 --- a/glabels/SelectProductDialog.h +++ b/glabels/SelectProductDialog.h @@ -53,6 +53,8 @@ private slots: void onSearchEntryTextChanged(); void onSearchClearButtonClicked(); void onPageSizeCheckClicked(); + void onAnyCategoryCheckClicked(); + void onCategoryCheckClicked(); void onTemplatePickerSelectionChanged(); void onCancelButtonClicked(); @@ -64,11 +66,22 @@ protected: void timerEvent(QTimerEvent *event); + ///////////////////////////////// + // Private methods + ///////////////////////////////// +private: + void loadCategoryList(); + + ///////////////////////////////// // Private data ///////////////////////////////// private: QBasicTimer mTimer; + + QMap mCheckToCategoryMap; + QStringList mCategoryIdList; + bool mCanceled; }; diff --git a/glabels/Settings.cpp b/glabels/Settings.cpp index a6dbb90..957a29c 100644 --- a/glabels/Settings.cpp +++ b/glabels/Settings.cpp @@ -22,6 +22,7 @@ #include #include +#include Settings* Settings::mInstance = 0; @@ -108,7 +109,6 @@ bool Settings::searchIsoPaperSizes() void Settings::setSearchIsoPaperSizes( bool searchIsoPaperSizes ) { - mInstance->beginGroup( "Search" ); mInstance->setValue( "isoPaperSizes", searchIsoPaperSizes ); mInstance->endGroup(); @@ -143,7 +143,6 @@ bool Settings::searchUsPaperSizes() void Settings::setSearchUsPaperSizes( bool searchUsPaperSizes ) { - mInstance->beginGroup( "Search" ); mInstance->setValue( "usPaperSizes", searchUsPaperSizes ); mInstance->endGroup(); @@ -167,7 +166,6 @@ bool Settings::searchOtherPaperSizes() void Settings::setSearchOtherPaperSizes( bool searchOtherPaperSizes ) { - mInstance->beginGroup( "Search" ); mInstance->setValue( "otherPaperSizes", searchOtherPaperSizes ); mInstance->endGroup(); @@ -176,3 +174,46 @@ void Settings::setSearchOtherPaperSizes( bool searchOtherPaperSizes ) } +bool Settings::searchAllCategories() +{ + // Guess at a suitable default + bool defaultValue = true; + + mInstance->beginGroup( "Search" ); + bool returnValue = mInstance->value( "allCategories", defaultValue ).toBool(); + mInstance->endGroup(); + + return returnValue; +} + + +void Settings::setSearchAllCategories( bool searchAllCategories ) +{ + mInstance->beginGroup( "Search" ); + mInstance->setValue( "allCategories", searchAllCategories ); + mInstance->endGroup(); + + emit mInstance->changed(); +} + + +QStringList Settings::searchCategoryList() +{ + QStringList defaultList; + + mInstance->beginGroup( "Search" ); + QStringList returnList = mInstance->value( "categoryList", defaultList ).toStringList(); + mInstance->endGroup(); + + return returnList; +} + + +void Settings::setSearchCategoryList( const QStringList& searchCategoryList ) +{ + mInstance->beginGroup( "Search" ); + mInstance->setValue( "categoryList", searchCategoryList ); + mInstance->endGroup(); + + emit mInstance->changed(); +} diff --git a/glabels/Settings.h b/glabels/Settings.h index 17f5b37..25fd79e 100644 --- a/glabels/Settings.h +++ b/glabels/Settings.h @@ -24,6 +24,7 @@ #include #include "libglabels/Distance.h" +#include /// @@ -71,6 +72,12 @@ public: static bool searchOtherPaperSizes(); static void setSearchOtherPaperSizes( bool searchOtherPaperSizes ); + static bool searchAllCategories(); + static void setSearchAllCategories( bool searchAllCategories ); + + static QStringList searchCategoryList(); + static void setSearchCategoryList( const QStringList& searchCategoryList ); + private: static Settings* mInstance; diff --git a/glabels/TemplatePicker.cpp b/glabels/TemplatePicker.cpp index f722273..b8d537f 100644 --- a/glabels/TemplatePicker.cpp +++ b/glabels/TemplatePicker.cpp @@ -55,18 +55,36 @@ void TemplatePicker::setTemplates( const QList &tmplates ) /// Apply Filter to Narrow Template Choices /// void TemplatePicker::applyFilter( const QString &searchString, - bool isoMask, bool usMask, bool otherMask ) + bool isoMask, bool usMask, bool otherMask, + bool anyCategory, const QStringList& categoryIds ) { foreach ( QListWidgetItem *item, findItems( "*", Qt::MatchWildcard ) ) { TemplatePickerItem *tItem = dynamic_cast(item); + bool nameMask = tItem->tmplate()->name().contains( searchString, Qt::CaseInsensitive ); + bool sizeMask = (isoMask && tItem->tmplate()->isSizeIso()) || (usMask && tItem->tmplate()->isSizeUs()) || (otherMask && tItem->tmplate()->isSizeOther()); - if ( tItem->tmplate()->name().contains( searchString, Qt::CaseInsensitive ) && sizeMask ) + bool categoryMask; + if ( anyCategory ) + { + categoryMask = true; + } + else + { + categoryMask = false; + foreach ( QString id, categoryIds ) + { + categoryMask = categoryMask || tItem->tmplate()->hasCategory( id ); + } + } + + + if ( nameMask && sizeMask && categoryMask ) { item->setHidden( false ); } diff --git a/glabels/TemplatePicker.h b/glabels/TemplatePicker.h index b379b30..029119a 100644 --- a/glabels/TemplatePicker.h +++ b/glabels/TemplatePicker.h @@ -53,7 +53,9 @@ public: ///////////////////////////////// // Methods ///////////////////////////////// - void applyFilter( const QString &searchString, bool isoMask, bool usMask, bool otherMask ); + void applyFilter( const QString &searchString, + bool isoMask, bool usMask, bool otherMask, + bool anyCategory, const QStringList& categoryIds ); const glabels::Template *selectedTemplate(); diff --git a/glabels/ui/SelectProductDialog.ui b/glabels/ui/SelectProductDialog.ui index 4bc2f85..d124788 100644 --- a/glabels/ui/SelectProductDialog.ui +++ b/glabels/ui/SelectProductDialog.ui @@ -85,7 +85,7 @@ false - Page size + Filter by paper size false @@ -118,6 +118,32 @@ + + + + Filter by category + + + + + + + + + 50 + false + + + + All categories + + + + + + + + @@ -182,82 +208,18 @@ - searchEntry - textChanged(QString) - SelectProductDialog - onSearchEntryTextChanged() - - - 69 - 48 - - - 2 - 53 - - - - - searchClearButton - clicked() - SelectProductDialog - onSearchClearButtonClicked() - - - 177 - 52 - - - 190 - 7 - - - - - templatePicker - itemSelectionChanged() - SelectProductDialog - onTemplatePickerSelectionChanged() - - - 232 - 550 - - - 244 - 697 - - - - - cancelButton - clicked() - SelectProductDialog - onCancelButtonClicked() - - - 864 - 648 - - - 689 - 704 - - - - - pageSizeIsoCheck + pageSizeOtherCheck clicked() SelectProductDialog onPageSizeCheckClicked() - 40 - 112 + 35 + 169 - 2 - 116 + 8 + 168 @@ -278,18 +240,98 @@ - pageSizeOtherCheck + templatePicker + itemSelectionChanged() + SelectProductDialog + onTemplatePickerSelectionChanged() + + + 232 + 550 + + + 244 + 697 + + + + + pageSizeIsoCheck clicked() SelectProductDialog onPageSizeCheckClicked() - 35 - 169 + 40 + 112 - 8 - 168 + 2 + 116 + + + + + cancelButton + clicked() + SelectProductDialog + onCancelButtonClicked() + + + 955 + 648 + + + 689 + 704 + + + + + searchClearButton + clicked() + SelectProductDialog + onSearchClearButtonClicked() + + + 177 + 52 + + + 190 + 7 + + + + + searchEntry + textChanged(QString) + SelectProductDialog + onSearchEntryTextChanged() + + + 69 + 48 + + + 2 + 53 + + + + + anyCategoryCheck + clicked() + SelectProductDialog + onAnyCategoryCheckClicked() + + + 31 + 234 + + + -7 + 240 @@ -302,5 +344,6 @@ onCancelButtonClicked() onSelectButtonClicked() onPageSizeCheckClicked() + onAnyCategoryCheckClicked()