File indexing completed on 2024-11-10 05:11:09

0001 /*
0002  * Copyright 2018 by Marco Martin <mart@kde.org>
0003  *
0004  * Licensed under the Apache License, Version 2.0 (the "License");
0005  * you may not use this file except in compliance with the License.
0006  * You may obtain a copy of the License at
0007  *
0008  *    http://www.apache.org/licenses/LICENSE-2.0
0009  *
0010  * Unless required by applicable law or agreed to in writing, software
0011  * distributed under the License is distributed on an "AS IS" BASIS,
0012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0013  * See the License for the specific language governing permissions and
0014  * limitations under the License.
0015  *
0016  */
0017 
0018 #pragma once
0019 
0020 #include <QAbstractListModel>
0021 #include <QHash>
0022 #include <QUrl>
0023 
0024 class QTimer;
0025 
0026 class AbstractDelegate;
0027 class DelegateLoader;
0028 
0029 class DelegatesModel : public QAbstractListModel
0030 {
0031     Q_OBJECT
0032     Q_PROPERTY(int currentIndex MEMBER m_currentIndex NOTIFY currentIndexChanged)
0033 
0034 public:
0035     enum Roles {
0036         DelegateUi = Qt::UserRole + 1
0037     };
0038 
0039     explicit DelegatesModel(QObject *parent = nullptr);
0040     virtual ~DelegatesModel();
0041 
0042     /**
0043      * Insert one or more delegates at position
0044      */
0045     void insertDelegateLoaders(int position, QList<DelegateLoader *> loaders);
0046 
0047     /**
0048      * clears the whole model
0049      */
0050     void clear();
0051 
0052     /**
0053      * @returns the complete delegate set
0054      */
0055     QList<AbstractDelegate *> delegates() const;
0056 
0057     bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
0058     bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
0059     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0060     QVariant data(const QModelIndex &index, int role = DelegateUi) const override;
0061     QHash<int, QByteArray> roleNames() const override;
0062 
0063 Q_SIGNALS:
0064     void currentIndexChanged();
0065 
0066 private:
0067     QList<DelegateLoader *> m_delegateLoaders;
0068     QList<DelegateLoader *> m_delegateLoadersToDelete;
0069     QTimer *m_deleteTimer;
0070     int m_currentIndex = 0;
0071 };