diff --git a/src/urlfilterproxymodel.cpp b/src/urlfilterproxymodel.cpp index 9d9a6d8..f6934a5 100644 --- a/src/urlfilterproxymodel.cpp +++ b/src/urlfilterproxymodel.cpp @@ -1,69 +1,57 @@ /*************************************************************************** * * * Copyright 2019 Simon Schmeisser * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * * * ***************************************************************************/ #include "urlfilterproxymodel.h" #include "browsermanager.h" #include #include "urlmodel.h" using namespace AngelFish; UrlFilterProxyModel::UrlFilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) { setFilterCaseSensitivity(Qt::CaseInsensitive); + + connect(this, &UrlFilterProxyModel::sourceModelChanged, this, [this] { + sort(0, Qt::DescendingOrder); + }); } bool UrlFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { - QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); + const QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); return (sourceModel()->data(index, UrlModel::url).toString().contains(filterRegExp()) || sourceModel()->data(index, UrlModel::title).toString().contains(filterRegExp())); } bool UrlFilterProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const { auto leftDate = QDateTime::fromString( sourceModel()->data(source_left, UrlModel::lastVisited).toString(), Qt::ISODate); auto rightDate = QDateTime::fromString( sourceModel()->data(source_right, UrlModel::lastVisited).toString(), Qt::ISODate); return leftDate < rightDate; } - -void UrlFilterProxyModel::setSourceModel(QAbstractItemModel *sourceModel) -{ - if (QSortFilterProxyModel::sourceModel() != sourceModel) { - QSortFilterProxyModel::setSourceModel(sourceModel); - - sort(0, Qt::DescendingOrder); - - emit sourceModelChanged(); - } -} - -QAbstractItemModel *UrlFilterProxyModel::sourceModel() const -{ - return QSortFilterProxyModel::sourceModel(); -} diff --git a/src/urlfilterproxymodel.h b/src/urlfilterproxymodel.h index b0b1046..8bdd158 100644 --- a/src/urlfilterproxymodel.h +++ b/src/urlfilterproxymodel.h @@ -1,48 +1,42 @@ /*************************************************************************** * * * Copyright 2019 Simon Schmeisser * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * * * ***************************************************************************/ #ifndef URLFILTERPROXYMODEL_H #define URLFILTERPROXYMODEL_H #include #include class UrlFilterProxyModel : public QSortFilterProxyModel { Q_OBJECT - Q_PROPERTY(QAbstractItemModel *sourceModel READ sourceModel WRITE setSourceModel NOTIFY - sourceModelChanged) - public: UrlFilterProxyModel(QObject *parent = nullptr); bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override; - void setSourceModel(QAbstractItemModel *sourceModel) override; - QAbstractItemModel *sourceModel() const; - signals: void sourceModelChanged(); }; #endif // URLFILTERPROXYMODEL_H