strawberry-audio-player-win.../src/core/mergedproxymodel.h

120 lines
4.6 KiB
C
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* Strawberry 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 3 of the License, or
* (at your option) any later version.
*
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
2018-08-09 18:39:44 +02:00
*
2018-02-27 18:06:05 +01:00
*/
#ifndef MERGEDPROXYMODEL_H
#define MERGEDPROXYMODEL_H
#include "config.h"
#include <cstddef>
2018-02-27 18:06:05 +01:00
#include <QObject>
#include <QAbstractItemModel>
2018-02-27 18:06:05 +01:00
#include <QAbstractProxyModel>
#include <QMap>
2021-07-12 08:28:52 +02:00
#include <QHash>
#include <QVariant>
#include <QString>
#include <QStringList>
2018-02-27 18:06:05 +01:00
#include "core/scoped_ptr.h"
2020-02-08 03:40:30 +01:00
class QMimeData;
2021-01-26 16:48:04 +01:00
std::size_t hash_value(const QModelIndex &idx);
2018-02-27 18:06:05 +01:00
class MergedProxyModelPrivate;
class MergedProxyModel : public QAbstractProxyModel {
Q_OBJECT
public:
explicit MergedProxyModel(QObject *parent = nullptr);
2020-06-15 21:55:05 +02:00
~MergedProxyModel() override;
2018-02-27 18:06:05 +01:00
// Make another model appear as a child of the given item in the source model.
void AddSubModel(const QModelIndex &source_parent, QAbstractItemModel *submodel);
void RemoveSubModel(const QModelIndex &source_parent);
2018-02-27 18:06:05 +01:00
// Find the item in the source model that is the parent of the model containing proxy_index.
// If proxy_index is in the source model, then this just returns mapToSource(proxy_index).
QModelIndex FindSourceParent(const QModelIndex &proxy_index) const;
2018-02-27 18:06:05 +01:00
// QAbstractItemModel
2020-06-15 21:55:05 +02:00
QModelIndex index(int row, int column, const QModelIndex &parent) const override;
QModelIndex parent(const QModelIndex &child) const override;
int rowCount(const QModelIndex &parent) const override;
int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &proxy_index, int role = Qt::DisplayRole) const override;
bool hasChildren(const QModelIndex &parent) const override;
QMap<int, QVariant> itemData(const QModelIndex &proxy_index) const override;
2021-01-26 16:48:04 +01:00
Qt::ItemFlags flags(const QModelIndex &idx) const override;
bool setData(const QModelIndex &idx, const QVariant &value, int role) override;
2020-06-15 21:55:05 +02:00
QStringList mimeTypes() const override;
QMimeData *mimeData(const QModelIndexList &indexes) const override;
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
bool canFetchMore(const QModelIndex &parent) const override;
void fetchMore(const QModelIndex &parent) override;
2018-02-27 18:06:05 +01:00
// QAbstractProxyModel
// Note that these implementations of map{To,From}Source will not always give you an index in sourceModel(),
// you might get an index in one of the child models instead.
2020-06-15 21:55:05 +02:00
QModelIndex mapFromSource(const QModelIndex &source_index) const override;
QModelIndex mapToSource(const QModelIndex &proxy_index) const override;
void setSourceModel(QAbstractItemModel *source_model) override;
2018-02-27 18:06:05 +01:00
// Convenience functions that call map{To,From}Source multiple times.
QModelIndexList mapFromSource(const QModelIndexList &source_indexes) const;
QModelIndexList mapToSource(const QModelIndexList &proxy_indexes) const;
2018-02-27 18:06:05 +01:00
2020-06-15 21:55:05 +02:00
signals:
void SubModelReset(const QModelIndex root, QAbstractItemModel *model);
2018-02-27 18:06:05 +01:00
private slots:
void SourceModelReset();
void SubModelAboutToBeReset();
2021-01-26 16:48:04 +01:00
void SubModelResetSlot();
2018-02-27 18:06:05 +01:00
void RowsAboutToBeInserted(const QModelIndex &source_parent, int start, int end);
void RowsInserted(const QModelIndex &source_parent, int start, int end);
void RowsAboutToBeRemoved(const QModelIndex &source_parent, int start, int end);
void RowsRemoved(const QModelIndex &source_parent, int start, int end);
void DataChanged(const QModelIndex &top_left, const QModelIndex &bottom_right);
2018-02-27 18:06:05 +01:00
void LayoutAboutToBeChanged();
void LayoutChanged();
private:
QModelIndex GetActualSourceParent(const QModelIndex &source_parent, QAbstractItemModel *model) const;
QAbstractItemModel *GetModel(const QModelIndex &source_index) const;
2018-02-27 18:06:05 +01:00
void DeleteAllMappings();
bool IsKnownModel(const QAbstractItemModel *model) const;
2018-02-27 18:06:05 +01:00
2021-07-12 08:28:52 +02:00
QHash<QAbstractItemModel*, QPersistentModelIndex> merge_points_;
QAbstractItemModel *resetting_model_;
2018-02-27 18:06:05 +01:00
2021-07-12 08:28:52 +02:00
QHash<QAbstractItemModel*, QModelIndex> old_merge_points_;
2018-02-27 18:06:05 +01:00
ScopedPtr<MergedProxyModelPrivate> p_;
2018-02-27 18:06:05 +01:00
};
#endif // MERGEDPROXYMODEL_H