# HG changeset patch # User sbr # Date 1374916097 -7200 # Sat Jul 27 11:08:17 2013 +0200 # Node ID 1e3f895d48e4f2323d9170e68e4508c0dd76340a # Parent 1ae4021b468b0cf46c74ad867c578e4a3d8c34da -Feature: Allow to defines different sort orders depending of the used criteria. SetDefaultSortOrders() defines the default sort orders to use per sort criteria. If a default sort order is defined for the current sort criteria, UseDefaultSortOrder() set the current sort order to it. Three default sort order types are defined: - DSO_NONE: don't alter the current sort order; - DSO_ASCENDING: switch the current sort order to descending; - DSO_DESCENDING: switch the current sort order to descending. UseDefaultSortOrder() returns a boolean indicating if the current sort order has been changed or not. diff --git a/src/sortlist_type.h b/src/sortlist_type.h --- a/src/sortlist_type.h +++ b/src/sortlist_type.h @@ -52,8 +52,16 @@ public: typedef int CDECL SortFunction(const T*, const T*); ///< Signature of sort function. typedef bool CDECL FilterFunction(const T*, F); ///< Signature of filter function. + /** Types of sort orders to use by default. */ + enum DefaultSortOrder { + DSO_NONE, ///< Don't alter current sort order. + DSO_ASCENDING, ///< Switch to ascending order. + DSO_DESCENDING, ///< Switch to descending order. + }; + protected: SortFunction * const *sort_func_list; ///< the sort criteria functions + DefaultSortOrder * default_sort_orders; ///< the default sort orders per sort criteria. FilterFunction * const *filter_func_list; ///< the filter criteria functions SortListFlags flags; ///< used to control sorting/resorting/etc. uint8 sort_type; ///< what criteria to sort on @@ -244,6 +252,33 @@ public: } /** + * Sets the sort orders to use by default foreach sort criterias. + * @param default_sort_orders An array of \c DefaultSortOrder + */ + void SetDefaultSortOrders(DefaultSortOrder * default_sort_orders) + { + this->default_sort_orders = default_sort_orders; + } + + /** + * Sets the current sort order to the default one for the current sort criteria. + * Values cames from the array passed to \c SetDefaultSortOrders(). + * Do nothing when no default sort orders was defined. + * @return \c true if the sort order changed, \c false otherwise. + */ + bool UseDefaultSortOrder() + { + if (this->default_sort_orders == NULL) return false; + + DefaultSortOrder default_sort_order = this->default_sort_orders[this->sort_type]; + if (default_sort_order == DSO_NONE) return false; + if ((default_sort_order == DSO_DESCENDING) == this->IsDescSortOrder()) return false; + + this->ToggleSortOrder(); + return true; + } + + /** * Sort the list. * For the first sorting we use quick sort since it is * faster for irregular sorted data. After that we