aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_bsplugins/src/BSPluginList/PluginGroupProxyModel.h
blob: c0f0f1cde449a135440d9c0cd11e932c3a6ecc9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef BSPLUGINLIST_PLUGINGROUPPROXYMODEL_H
#define BSPLUGINLIST_PLUGINGROUPPROXYMODEL_H

#include "TESData/FileInfo.h"

#include <imoinfo.h>

#include <boost/container/flat_map.hpp>

#include <QAbstractProxyModel>

#include <limits>
#include <memory>
#include <vector>

namespace BSPluginList
{

class PluginGroupProxyModel final : public QAbstractProxyModel
{
  Q_OBJECT

public:
  explicit PluginGroupProxyModel(MOBase::IOrganizer* organizer,
                                 QObject* parent = nullptr);

  void setSourceModel(QAbstractItemModel* sourceModel) override;

  bool hasChildren(const QModelIndex& parent = QModelIndex()) const override;
  int rowCount(const QModelIndex& parent = QModelIndex()) const override;
  int columnCount(const QModelIndex& parent = QModelIndex()) const override;

  QModelIndex index(int row, int column,
                    const QModelIndex& parent = QModelIndex()) const override;
  QModelIndex parent(const QModelIndex& index) const override;

  QModelIndex mapFromSource(const QModelIndex& sourceIndex) const override;
  QModelIndex mapToSource(const QModelIndex& proxyIndex) const override;

  Qt::ItemFlags flags(const QModelIndex& index) const override;
  QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
  bool setData(const QModelIndex& index, const QVariant& value,
               int role = Qt::EditRole) override;
  QModelIndex buddy(const QModelIndex& index) const override;
  QVariant headerData(int section, Qt::Orientation orientation,
                      int role = Qt::DisplayRole) const override;

  QMimeData* mimeData(const QModelIndexList& indexes) const override;
  bool canDropMimeData(const QMimeData* data, Qt::DropAction action, int row,
                       int column, const QModelIndex& parent) const override;
  bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column,
                    const QModelIndex& parent) override;

  void setDroppingBelowExpandedItem(bool value) { m_DroppingBelowExpandedItem = value; }

signals:
  void groupRenameRequested(const QModelIndex& index, const QString& name);

private slots:
  void onSourceDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight,
                           const QList<int>& roles = QList<int>());
  void onSourceLayoutChanged(
      const QList<QPersistentModelIndex>& parents = QList<QPersistentModelIndex>(),
      QAbstractItemModel::LayoutChangeHint hint =
          QAbstractItemModel::NoLayoutChangeHint);
  void onSourceModelReset();
  void onSourceRowsInserted(const QModelIndex& parent);
  void onSourceRowsRemoved(const QModelIndex& parent);

private:
  static constexpr std::size_t NO_ID = std::numeric_limits<std::size_t>::max();

  struct Group
  {
    QString name;
    std::vector<std::size_t> children;

    explicit Group(const QString& name) : name{name} {}
  };

  struct ProxyItem
  {
    int row;
    int sourceRow;
    std::size_t parentId;
    std::shared_ptr<Group> groupInfo;

    [[nodiscard]] bool isSourceItem() const { return sourceRow != -1; }
    [[nodiscard]] bool isGroup() const { return groupInfo != nullptr; }
    [[nodiscard]] bool isDivider() const
    {
      return groupInfo == nullptr && sourceRow == -1;
    }
  };

  [[nodiscard]] int mapLowerBoundToSourceRow(std::size_t id) const;
  [[nodiscard]] bool isAboveDivider(std::size_t id) const;
  [[nodiscard]] bool isBelowDivider(std::size_t id) const;

  void buildGroups();
  [[nodiscard]] std::size_t createItem(const QString& name, int row, int sourceRow,
                                       std::size_t parent, std::shared_ptr<Group> group,
                                       int repeat = 0);

  std::vector<ProxyItem> m_ProxyItems;
  boost::container::flat_multimap<QString, std::size_t> m_ItemMap;
  std::vector<std::size_t> m_TopLevel;
  std::vector<std::size_t> m_SourceMap;

  bool m_DroppingBelowExpandedItem;
  mutable std::vector<std::size_t> m_DraggingGroups;

  MOBase::IOrganizer* m_Organizer;
};

}  // namespace BSPluginList

#endif  // BSPLUGINLIST_PLUGINGROUPPROXYMODEL_H