File indexing completed on 2024-05-12 04:58:06

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2014  David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #ifndef HISTORYMODEL_H
0019 #define HISTORYMODEL_H
0020 
0021 #include <QAbstractItemModel>
0022 #include <QSortFilterProxyModel>
0023 
0024 #include "qzcommon.h"
0025 #include "history.h"
0026 
0027 class QTimer;
0028 
0029 class History;
0030 class HistoryItem;
0031 
0032 class FALKON_EXPORT HistoryModel : public QAbstractItemModel
0033 {
0034     Q_OBJECT
0035 public:
0036     enum Roles {
0037         IdRole = Qt::UserRole + 1,
0038         TitleRole = Qt::UserRole + 2,
0039         UrlRole = Qt::UserRole + 3,
0040         UrlStringRole = Qt::UserRole + 4,
0041         IconRole = Qt::UserRole + 5,
0042         IsTopLevelRole = Qt::UserRole + 7,
0043         TimestampStartRole = Qt::UserRole + 8,
0044         TimestampEndRole = Qt::UserRole + 9,
0045         MaxRole = TimestampEndRole
0046     };
0047 
0048     explicit HistoryModel(History* history);
0049 
0050     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0051     QVariant data(const QModelIndex &index, int role) const override;
0052     bool setData(const QModelIndex &index, const QVariant &value, int role) override;
0053 
0054     QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override;
0055     QModelIndex parent(const QModelIndex &child) const override;
0056     Qt::ItemFlags flags(const QModelIndex &index) const override;
0057 
0058     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0059     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0060 
0061     bool canFetchMore(const QModelIndex &parent) const override;
0062     void fetchMore(const QModelIndex &parent) override;
0063 
0064     bool hasChildren(const QModelIndex &parent) const override;
0065 
0066     HistoryItem* itemFromIndex(const QModelIndex &index) const;
0067 
0068     void removeTopLevelIndexes(const QList<QPersistentModelIndex> &indexes);
0069 
0070 private Q_SLOTS:
0071     void resetHistory();
0072 
0073     void historyEntryAdded(const HistoryEntry &entry);
0074     void historyEntryDeleted(const HistoryEntry &entry);
0075     void historyEntryEdited(const HistoryEntry &before, const HistoryEntry &after);
0076 
0077 private:
0078     HistoryItem* findHistoryItem(const HistoryEntry &entry);
0079     void checkEmptyParentItem(HistoryItem* item);
0080     void init();
0081 
0082     HistoryItem* m_rootItem;
0083     HistoryItem* m_todayItem;
0084     History* m_history;
0085 };
0086 
0087 class FALKON_EXPORT HistoryFilterModel : public QSortFilterProxyModel
0088 {
0089     Q_OBJECT
0090 public:
0091     explicit HistoryFilterModel(QAbstractItemModel* parent);
0092     bool isPatternEmpty() const;
0093 
0094 public Q_SLOTS:
0095     void setFilterFixedString(const QString &pattern);
0096 
0097 Q_SIGNALS:
0098     void expandAllItems();
0099     void collapseAllItems();
0100 
0101 protected:
0102     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
0103 
0104 private Q_SLOTS:
0105     void startFiltering();
0106 
0107 private:
0108     QString m_pattern;
0109     QTimer* m_filterTimer;
0110 };
0111 
0112 #endif // HISTORYMODEL_H