MuseScore  3.4
Music composition and notation
palettemodel.h
Go to the documentation of this file.
1 //=============================================================================
2 // MuseScore
3 // Music Composition & Notation
4 //
5 // Copyright (C) 2019 Werner Schweer and others
6 //
7 // This program is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License version 2.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 //=============================================================================
19 
20 #ifndef __PALETTEMODEL_H__
21 #define __PALETTEMODEL_H__
22 
23 #include "palettetree.h"
24 
25 namespace Ms {
26 
27 class Selection;
28 
29 //---------------------------------------------------------
30 // PaletteCellFilter
32 //---------------------------------------------------------
33 
34 class PaletteCellFilter : public QObject {
35  Q_OBJECT
36 
38 
39  signals:
40  void filterChanged();
41 
42  protected:
43  virtual bool acceptCell(const PaletteCell&) const = 0;
44 
45  public:
46  PaletteCellFilter(QObject* parent = nullptr) : QObject(parent) {}
47 
48  bool accept(const PaletteCell&) const;
49 
51  void connectToModel(const QAbstractItemModel*);
52  };
53 
54 //---------------------------------------------------------
55 // VisibilityCellFilter
56 //---------------------------------------------------------
57 
60 
61  bool acceptCell(const PaletteCell& cell) const override { return cell.visible == acceptedValue; }
62 
63  public:
64  VisibilityCellFilter(bool acceptedVal, QObject* parent = nullptr)
65  : PaletteCellFilter(parent), acceptedValue(acceptedVal) {}
66  };
67 
68 //---------------------------------------------------------
69 // CustomizedCellFilter
70 //---------------------------------------------------------
71 
74 
75  bool acceptCell(const PaletteCell& cell) const override { return cell.custom == acceptedValue; }
76 
77  public:
78  CustomizedCellFilter(bool acceptedVal, QObject* parent = nullptr)
79  : PaletteCellFilter(parent), acceptedValue(acceptedVal) {}
80  };
81 
82 //---------------------------------------------------------
83 // PaletteTreeModel
84 //---------------------------------------------------------
85 
86 class PaletteTreeModel : public QAbstractItemModel {
87  Q_OBJECT
88 
89  public:
91  PaletteCellRole = Qt::UserRole,
101  CellActiveRole
102  };
104 
105  private:
106  std::unique_ptr<PaletteTree> _paletteTree;
107  bool _treeChanged = false;
108  bool _treeChangedSignalBlocked = false;
109 
110  std::vector<std::unique_ptr<PalettePanel>>& palettes() { return _paletteTree->palettes; }
111  const std::vector<std::unique_ptr<PalettePanel>>& palettes() const { return _paletteTree->palettes; }
112 
113  PalettePanel* iptrToPalettePanel(void* iptr, int* idx = nullptr);
114  const PalettePanel* iptrToPalettePanel(void* iptr, int* idx = nullptr) const { return const_cast<PaletteTreeModel*>(this)->iptrToPalettePanel(iptr, idx); }
115 
116  private slots:
117  void onDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight, const QVector<int>& roles);
118 
119  public slots:
120  void itemDataChanged(const QModelIndex& idx);
121  void setTreeChanged();
122  void setTreeUnchanged() { _treeChanged = false; }
123 
124  signals:
125  void treeChanged();
126 
127  public:
128  explicit PaletteTreeModel(std::unique_ptr<PaletteTree> tree, QObject* parent = nullptr);
129  explicit PaletteTreeModel(PaletteTree* tree, QObject* parent = nullptr)
130  : PaletteTreeModel(std::unique_ptr<PaletteTree>(tree), parent) {}
131 
132  bool blockTreeChanged(bool block);
133 
134  void setPaletteTree(std::unique_ptr<PaletteTree> tree);
135  const PaletteTree* paletteTree() const { return _paletteTree.get(); }
136 
137  bool paletteTreeChanged() const { return _treeChanged; }
138 
139  QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
140  QModelIndex parent(const QModelIndex& index) const override;
141 
142  int rowCount(const QModelIndex& parent = QModelIndex()) const override;
143  int columnCount(const QModelIndex& parent = QModelIndex()) const override;
144  QVariant data(const QModelIndex& index, int role) const override;
145  bool setData(const QModelIndex& index, const QVariant& value, int role) override;
146 // QVariant headerData(int section, Qt::Orientation orientation, int role) const override // TODO: headerData: palette name?
147 
148  QHash<int, QByteArray> roleNames() const override;
149 
150  Qt::ItemFlags flags(const QModelIndex& index) const override;
151  Qt::DropActions supportedDropActions() const override;
152 
153  QMimeData* mimeData(const QModelIndexList& indexes) const override;
154  QStringList mimeTypes() const override;
155 
156  bool canDropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent) const;
157  bool dropMimeData(const QMimeData* data, Qt::DropAction action, int row, int column, const QModelIndex& parent);
158 
159  QModelIndexList match(const QModelIndex& start, int role, const QVariant& value, int hits, Qt::MatchFlags flags) const override;
160  QModelIndex findPaletteCell(const PaletteCell& cell, const QModelIndex& parent) const;
161  PaletteCellFilter* getFilter(const QModelIndex&) const;
162 
163  bool moveRows(const QModelIndex& sourceParent, int sourceRow, int count, const QModelIndex& destinationParent, int destinationChild) override;
164  bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
165  bool insertRows(int row, int count, const QModelIndex& parent = QModelIndex()) override;
166 
167  const PalettePanel* findPalettePanel(const QModelIndex&) const;
168  PalettePanel* findPalettePanel(const QModelIndex& index);
169  PaletteCellConstPtr findCell(const QModelIndex&) const;
170  PaletteCellPtr findCell(const QModelIndex& index);
171  bool insertPalettePanel(std::unique_ptr<PalettePanel> pp, int row, const QModelIndex& parent = QModelIndex());
172 
173  void updateCellsState(const Selection&);
174  void retranslate();
175  };
176 
177 //---------------------------------------------------------
178 // FilterPaletteTreeModel
179 //---------------------------------------------------------
180 
181 class FilterPaletteTreeModel : public QSortFilterProxyModel {
182  Q_OBJECT
183 
185 
186  bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override;
187  bool filterAcceptsColumn(int /* sourceColumn */, const QModelIndex& /* sourceParent */) const override { return true; };
188 
189  private slots:
190  void invalidateFilter() { QSortFilterProxyModel::invalidateFilter(); }
191 
192  public:
193  FilterPaletteTreeModel(PaletteCellFilter* filter, PaletteTreeModel* model, QObject* parent = nullptr);
194  };
195 
196 //---------------------------------------------------------
197 // PaletteCellFilterProxyModel
198 //---------------------------------------------------------
199 
200 class PaletteCellFilterProxyModel : public QSortFilterProxyModel {
201  Q_OBJECT
202  public:
203  PaletteCellFilterProxyModel(QObject* parent = nullptr) : QSortFilterProxyModel(parent) {}
204 
205  bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const override;
206  };
207 
208 } // namespace Ms
209 
210 #endif
PaletteCellFilterProxyModel(QObject *parent=nullptr)
Definition: palettemodel.h:203
Definition: select.h:136
bool visible
Definition: palettetree.h:50
Definition: palettemodel.h:93
PaletteCellFilter * cellFilter
Definition: palettemodel.h:184
const std::vector< std::unique_ptr< PalettePanel > > & palettes() const
Definition: palettemodel.h:111
std::vector< std::unique_ptr< PalettePanel > > & palettes()
Definition: palettemodel.h:110
virtual bool acceptCell(const PaletteCell &) const =0
std::shared_ptr< const PaletteCell > PaletteCellConstPtr
Definition: palettetree.h:30
bool acceptedValue
Definition: palettemodel.h:59
Definition: palettetree.h:95
Definition: palettemodel.h:99
const PaletteTree * paletteTree() const
Definition: palettemodel.h:135
Definition: palettemodel.h:181
PaletteTreeModel(PaletteTree *tree, QObject *parent=nullptr)
Definition: palettemodel.h:129
Interface for filtering elements in a palette.
Definition: palettemodel.h:34
const PalettePanel * iptrToPalettePanel(void *iptr, int *idx=nullptr) const
Definition: palettemodel.h:114
CustomizedCellFilter(bool acceptedVal, QObject *parent=nullptr)
Definition: palettemodel.h:78
std::unique_ptr< PaletteTree > _paletteTree
Definition: palettemodel.h:103
Definition: palettemodel.h:98
Definition: palettemodel.h:94
Definition: palettemodel_list.h:31
Definition: palettemodel.h:100
Definition: palettemodel.h:92
PaletteCellFilter * chainedFilter
Definition: palettemodel.h:37
Definition: palettemodel.h:58
Definition: palettemodel.h:86
void addChainedFilter(PaletteCellFilter *)
Ownership over the added filter is passed to this filter.
Definition: palettemodel.cpp:903
VisibilityCellFilter(bool acceptedVal, QObject *parent=nullptr)
Definition: palettemodel.h:64
Definition: aeolus.cpp:26
bool custom
Definition: palettetree.h:51
Definition: palettemodel.h:72
PaletteCellFilter(QObject *parent=nullptr)
Definition: palettemodel.h:46
Definition: palettemodel.h:200
Definition: palettemodel.h:96
bool filterAcceptsColumn(int, const QModelIndex &) const override
Definition: palettemodel.h:187
bool acceptedValue
Definition: palettemodel.h:73
bool acceptCell(const PaletteCell &cell) const override
Definition: palettemodel.h:61
void connectToModel(const QAbstractItemModel *)
Definition: palettemodel.cpp:933
bool accept(const PaletteCell &) const
Definition: palettemodel.cpp:918
bool paletteTreeChanged() const
Definition: palettemodel.h:137
void setTreeUnchanged()
Definition: palettemodel.h:122
std::shared_ptr< PaletteCell > PaletteCellPtr
Definition: palettetree.h:29
PaletteTreeModelRoles
Definition: palettemodel.h:90
Definition: palettemodel.h:95
void invalidateFilter()
Definition: palettemodel.h:190
Definition: palettemodel.h:97
bool acceptCell(const PaletteCell &cell) const override
Definition: palettemodel.h:75
Definition: palettetree.h:222