+/******************************************************************************************
+ * FilteredNickModel
+ ******************************************************************************************/
+
+FilteredNickModel::FilteredNickModel(QObject *parent) : QSortFilterProxyModel(parent) {
+ setDynamicSortFilter(true);
+ setSortCaseSensitivity(Qt::CaseInsensitive);
+ setSortRole(NickModel::SortKeyRole);
+
+}
+
+FilteredNickModel::~FilteredNickModel() {
+
+}
+
+void FilteredNickModel::setSourceModel(QAbstractItemModel *model) {
+ QSortFilterProxyModel::setSourceModel(model);
+ connect(model, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SLOT(sourceRowsInserted(const QModelIndex &, int, int)));
+ connect(model, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SLOT(sourceRowsRemoved(const QModelIndex &, int, int)));
+}
+
+// Hide empty categories
+bool FilteredNickModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
+ if(!source_parent.isValid()) {
+ QModelIndex index = sourceModel()->index(source_row, 0);
+ return sourceModel()->rowCount(index);
+ }
+ return true;
+}
+
+void FilteredNickModel::sourceRowsInserted(const QModelIndex &index, int start, int end) {
+ if(!index.isValid()) return;
+ if(sourceModel()->rowCount(index) <= end - start + 1) {
+ // category no longer empty
+ invalidateFilter();
+ }
+}
+
+void FilteredNickModel::sourceRowsRemoved(const QModelIndex &index, int, int) {
+ if(!index.isValid()) return;
+ if(sourceModel()->rowCount(index) == 0) {
+ // category is now empty!
+ invalidateFilter();
+ }
+}