File indexing completed on 2024-05-12 04:38:54

0001 /*
0002     SPDX-FileCopyrightText: 2010 Aleix Pol <aleixpol@kde.org>
0003 
0004     Split into separate class
0005     SPDX-FileCopyrightText: 2011 Andrey Batyiev <batyiev@gmail.com>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KDEVPLATFORM_VCSFILECHANGESMODEL_H
0011 #define KDEVPLATFORM_VCSFILECHANGESMODEL_H
0012 
0013 #include <QSortFilterProxyModel>
0014 #include <QStandardItemModel>
0015 
0016 #include <vcs/vcsstatusinfo.h>
0017 
0018 #include <vcs/vcsexport.h>
0019 
0020 class QUrl;
0021 
0022 namespace KDevelop
0023 {
0024 class VcsStatusInfo;
0025 class VcsFileChangesModelPrivate;
0026 
0027 class KDEVPLATFORMVCS_EXPORT VcsFileChangesSortProxyModel : public QSortFilterProxyModel
0028 {
0029     Q_OBJECT
0030 
0031 public:
0032     explicit VcsFileChangesSortProxyModel(QObject* parent = nullptr);
0033 
0034     bool lessThan(const QModelIndex& rLeft, const QModelIndex& rRight) const override;
0035 };
0036 
0037 /**
0038  * This class holds and represents information about changes in files.
0039  * Also it is possible to provide tree like models by inheriting this class, see protected members.
0040  * All stuff should be pulled in by @p updateState.
0041  */
0042 class KDEVPLATFORMVCS_EXPORT VcsFileChangesModel : public QStandardItemModel
0043 {
0044     Q_OBJECT
0045 
0046 public:
0047     enum ItemRoles { VcsStatusInfoRole = Qt::UserRole+1, UrlRole, StateRole, LastItemRole };
0048     enum Column { PathColumn = 0, StatusColumn = 1 };
0049 
0050     /**
0051      * Constructor for class.
0052      * @param isCheckable if true, model will show checkboxes on items.
0053      */
0054     explicit VcsFileChangesModel(QObject *parent = nullptr, bool isCheckable = false);
0055     ~VcsFileChangesModel() override;
0056 
0057     QVariant data(const QModelIndex &index, int role) const override;
0058 
0059     /**
0060      * Returns list of currently checked urls.
0061      */
0062     QList<QUrl> checkedUrls() const {
0063         return checkedUrls(invisibleRootItem());
0064     }
0065 
0066     /**
0067      * Returns urls of all files
0068      * */
0069     QList<QUrl> urls() const {
0070         return urls(invisibleRootItem());
0071     }
0072     
0073     /**
0074      * Set the checked urls
0075      * */
0076     void setCheckedUrls(const QList<QUrl>& urls) const {
0077         checkUrls(invisibleRootItem(), urls);
0078     }
0079 
0080     /** 
0081      * Returns the item for the specified url
0082      * */
0083     QStandardItem* itemForUrl(const QUrl &url) const {
0084         return fileItemForUrl(invisibleRootItem(), url);
0085     }
0086 
0087     /**
0088      * Changes the check-state of all files to the given state
0089      * */
0090     void setAllChecked(bool checked);
0091 
0092     void setIsCheckbable(bool checkable);
0093     bool isCheckable() const;
0094 
0095     bool removeUrl(const QUrl& url);
0096 
0097 public Q_SLOTS:
0098     /**
0099      * Used to post update of status of some file. Any status except UpToDate
0100      * and Unknown will update (or add) item representation.
0101      */
0102     void updateState(const KDevelop::VcsStatusInfo &status) {
0103         updateState(invisibleRootItem(), status);
0104     }
0105 
0106 protected:
0107     /**
0108      * Post update of status of some file.
0109      * @return changed row or -1 if row is deleted
0110      */
0111     int updateState(QStandardItem *parent, const KDevelop::VcsStatusInfo &status);
0112 
0113     /**
0114      * Returns list of currently checked urls.
0115      */
0116     QList<QUrl> checkedUrls(QStandardItem *parent) const;
0117     
0118     /**
0119      * Checks the given urls, unchecks all others.
0120      * */
0121     void checkUrls(QStandardItem *parent, const QList<QUrl>& urls) const;
0122     
0123     /**
0124      * Returns all urls
0125      * */
0126     QList<QUrl> urls(QStandardItem *parent) const;
0127 
0128     /**
0129      * Returns item for particular url.
0130      */
0131     QStandardItem* fileItemForUrl(QStandardItem *parent, const QUrl &url) const;
0132 
0133 private:
0134     const QScopedPointer<class VcsFileChangesModelPrivate> d_ptr;
0135     Q_DECLARE_PRIVATE(VcsFileChangesModel)
0136 };
0137 }
0138 
0139 Q_DECLARE_METATYPE(KDevelop::VcsStatusInfo::State)
0140 
0141 #endif // KDEVPLATFORM_VCSFILECHANGESMODEL_H