MuseScore  3.4
Music composition and notation
paletteworkspace.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 __PALETTEWORKSPACE_H__
21 #define __PALETTEWORKSPACE_H__
22 
23 #include "palettemodel.h"
24 
25 namespace Ms {
26 
27 class AbstractPaletteController;
28 class PaletteWorkspace;
29 
30 //---------------------------------------------------------
31 // PaletteElementEditor
32 //---------------------------------------------------------
33 
34 class PaletteElementEditor : public QObject {
35  Q_OBJECT
36 
38  QPersistentModelIndex _paletteIndex;
40 
41  Q_PROPERTY(bool valid READ valid CONSTANT)
42  Q_PROPERTY(QString actionName READ actionName CONSTANT) // TODO: make NOTIFY instead of CONSTANT for retranslations
43 
44  private slots:
45  void onElementAdded(const Element*);
46 
47  public:
48  PaletteElementEditor(QObject* parent = nullptr) : QObject(parent) {}
49  PaletteElementEditor(AbstractPaletteController* controller, QPersistentModelIndex paletteIndex, PalettePanel::Type type, QObject* parent = nullptr)
50  : QObject(parent), _controller(controller), _paletteIndex(paletteIndex), _type(type) {}
51 
52  bool valid() const;
53  QString actionName() const;
54 
55  Q_INVOKABLE void open();
56  };
57 
58 //---------------------------------------------------------
59 // AbstractPaletteController
60 //---------------------------------------------------------
61 
62 class AbstractPaletteController : public QObject {
63  Q_OBJECT
64 
66  Q_PROPERTY(bool canDropElements READ canDropElements CONSTANT)
67 
68  virtual bool canDropElements() const { return false; }
69 
70  public:
71  enum class RemoveAction {
72  NoAction,
73  Hide,
74  DeletePermanently,
75  AutoAction
76  };
77 
78  AbstractPaletteController(QObject* parent = nullptr) : QObject(parent) {}
79 
80  Q_INVOKABLE virtual Qt::DropAction dropAction(const QVariantMap& mimeData, Qt::DropAction proposedAction, const QModelIndex& parent, bool internal) const
81  {
82  Q_UNUSED(mimeData); Q_UNUSED(proposedAction); Q_UNUSED(parent); Q_UNUSED(internal);
83  return Qt::IgnoreAction;
84  }
85 
86  Q_INVOKABLE virtual bool move(const QModelIndex& sourceParent, int sourceRow, const QModelIndex& destinationParent, int destinationChild) = 0;
87  Q_INVOKABLE virtual bool insert(const QModelIndex& parent, int row, const QVariantMap& mimeData, Qt::DropAction action) = 0;
88  Q_INVOKABLE virtual bool insertNewItem(const QModelIndex& parent, int row) = 0;
89  Q_INVOKABLE virtual void remove(const QModelIndex&) = 0;
90  Q_INVOKABLE virtual void removeSelection(const QModelIndexList&, const QModelIndex& parent) = 0;
91 
92  Q_INVOKABLE virtual bool canEdit(const QModelIndex&) const { return false; }
93 
94  Q_INVOKABLE virtual void editPaletteProperties(const QModelIndex& index) { Q_UNUSED(index); }
95  Q_INVOKABLE virtual void editCellProperties(const QModelIndex& index) { Q_UNUSED(index); }
96 
97  Q_INVOKABLE virtual bool applyPaletteElement(const QModelIndex& index, Qt::KeyboardModifiers modifiers) { Q_UNUSED(index); Q_UNUSED(modifiers); return false; }
98 
99  Q_INVOKABLE Ms::PaletteElementEditor* elementEditor(const QModelIndex& index);
100  };
101 
102 //---------------------------------------------------------
103 // UserPaletteController
104 //---------------------------------------------------------
105 
107  Q_OBJECT
108 
109  QAbstractItemModel* _model;
111 
112  bool _visible = true;
113  bool _custom = false;
114  bool _filterCustom = false;
115 
116  bool _userEditable = true;
117 
118  bool canDropElements() const override { return _userEditable; }
119 
120  void showHideOrDeleteDialog(const QString& question, std::function<void(RemoveAction)> resultHandler) const;
121  void queryRemove(const QModelIndexList&, int customCount);
122 
126  CustomPaletteHideDeleteConfirmation
127  };
128 
129  void remove(const QModelIndexList&, AbstractPaletteController::RemoveAction);
130 
131  protected:
132  QAbstractItemModel* model() { return _model; }
133  const QAbstractItemModel* model() const { return _model; }
134 
135  public:
136  UserPaletteController(QAbstractItemModel* m, PaletteTreeModel* userPalette, QObject* parent = nullptr)
137  : AbstractPaletteController(parent), _model(m), _userPalette(userPalette) {}
138 
139  bool visible() const { return _visible; }
140  void setVisible(bool val) { _visible = val; }
141  bool custom() const { return _custom; }
142  void setCustom(bool val) { _custom = val; _filterCustom = true; }
143 
144  Qt::DropAction dropAction(const QVariantMap& mimeData, Qt::DropAction proposedAction, const QModelIndex& parent, bool internal) const override;
145 
146  bool move(const QModelIndex& sourceParent, int sourceRow, const QModelIndex& destinationParent, int destinationChild) override;
147  bool insert(const QModelIndex& parent, int row, const QVariantMap& mimeData, Qt::DropAction action) override;
148  bool insertNewItem(const QModelIndex& parent, int row) override;
149  void remove(const QModelIndex& index) override;
150  void removeSelection(const QModelIndexList&, const QModelIndex& parent) override;
151 
152  void editPaletteProperties(const QModelIndex& index) override;
153  void editCellProperties(const QModelIndex& index) override;
154 
155  bool userEditable() const { return _userEditable; }
156  void setUserEditable(bool val) { _userEditable = val; }
157 
158  bool canEdit(const QModelIndex&) const override;
159 
160  bool applyPaletteElement(const QModelIndex& index, Qt::KeyboardModifiers modifiers) override;
161  };
162 
163 //---------------------------------------------------------
164 // PaletteWorkspace
165 //---------------------------------------------------------
166 
167 class PaletteWorkspace : public QObject {
168  Q_OBJECT
169 
172  PaletteTreeModel* defaultPalette; // palette used by "Reset palette" action
173 
174  QAbstractItemModel* mainPalette = nullptr;
175 // PaletteTreeModel* poolPalette; ///< masterPalette entries not yet added to mainPalette
176  FilterPaletteTreeModel* customPoolPalette = nullptr;
177 
178  UserPaletteController* mainPaletteController = nullptr;
179 // PaletteController* masterPaletteController;
180  UserPaletteController* customElementsPaletteController = nullptr;
181 
182  Q_PROPERTY(QAbstractItemModel* mainPaletteModel READ mainPaletteModel CONSTANT)
183  Q_PROPERTY(Ms::AbstractPaletteController* mainPaletteController READ getMainPaletteController CONSTANT)
184 
185  Q_PROPERTY(Ms::FilterPaletteTreeModel* customElementsPaletteModel READ customElementsPaletteModel CONSTANT)
186  Q_PROPERTY(Ms::AbstractPaletteController* customElementsPaletteController READ getCustomElementsPaletteController CONSTANT)
187 
188  QAbstractItemModel* mainPaletteModel();
189  AbstractPaletteController* getMainPaletteController();
190 
191  FilterPaletteTreeModel* customElementsPaletteModel();
192  AbstractPaletteController* getCustomElementsPaletteController();
193 
195  CustomRole = Qt::UserRole + 1,
196  PaletteIndexRole
197  };
198 
199  signals:
200  void userPaletteChanged();
201 
202  public:
203  explicit PaletteWorkspace(PaletteTreeModel* user, PaletteTreeModel* master = nullptr, QObject* parent = nullptr);
204 
205  Q_INVOKABLE QModelIndex poolPaletteIndex(const QModelIndex& index, Ms::FilterPaletteTreeModel* poolPalette);
206  Q_INVOKABLE QModelIndex customElementsPaletteIndex(const QModelIndex& index);
207 
208  Q_INVOKABLE Ms::FilterPaletteTreeModel* poolPaletteModel(const QModelIndex& index);
209  Q_INVOKABLE Ms::AbstractPaletteController* poolPaletteController(Ms::FilterPaletteTreeModel*, const QModelIndex& rootIndex);
210 
211  PaletteTreeModel* userPaletteModel() { return userPalette; }
212 
213  Q_INVOKABLE QAbstractItemModel* availableExtraPalettesModel();
214  Q_INVOKABLE bool addPalette(const QPersistentModelIndex&);
215  Q_INVOKABLE bool removeCustomPalette(const QPersistentModelIndex&);
216 
217  Q_INVOKABLE bool resetPalette(const QModelIndex&);
218 
219  Q_INVOKABLE bool savePalette(const QModelIndex&);
220  Q_INVOKABLE bool loadPalette(const QModelIndex&);
221 
222  bool paletteChanged() const { return userPalette->paletteTreeChanged(); }
223 
224  void setUserPaletteTree(std::unique_ptr<PaletteTree> tree);
225  void setDefaultPaletteTree(std::unique_ptr<PaletteTree> tree);
226  void write(XmlWriter&) const;
227  bool read(XmlReader&);
228 
229  void updateCellsState(const Selection& sel) { userPalette->updateCellsState(sel); }
230  void retranslate() { userPalette->retranslate(); masterPalette->retranslate(); defaultPalette->retranslate(); }
231  };
232 
233 } // namespace Ms
234 
235 #endif
Definition: paletteworkspace.h:167
Definition: select.h:136
PalettesModelRoles
Definition: paletteworkspace.h:194
void updateCellsState(const Selection &)
Definition: palettemodel.cpp:795
bool canDropElements() const override
Definition: paletteworkspace.h:118
void retranslate()
Definition: paletteworkspace.h:230
Definition: xml.h:67
QPersistentModelIndex _paletteIndex
Definition: paletteworkspace.h:38
bool custom() const
Definition: paletteworkspace.h:141
PaletteTreeModel * masterPalette
Definition: paletteworkspace.h:171
PaletteTreeModel * defaultPalette
Definition: paletteworkspace.h:172
Definition: paletteworkspace.h:124
Base class of score layout elements.
Definition: element.h:158
Definition: palettemodel.h:181
PaletteTreeModel * _userPalette
Definition: paletteworkspace.h:110
Type
Definition: palettetree.h:98
PaletteElementEditor(AbstractPaletteController *controller, QPersistentModelIndex paletteIndex, PalettePanel::Type type, QObject *parent=nullptr)
Definition: paletteworkspace.h:49
bool userEditable() const
Definition: paletteworkspace.h:155
virtual Q_INVOKABLE void editCellProperties(const QModelIndex &index)
Definition: paletteworkspace.h:95
Definition: paletteworkspace.h:34
QAbstractItemModel * _model
Definition: paletteworkspace.h:109
AbstractPaletteController * _controller
Definition: paletteworkspace.h:37
Definition: paletteworkspace.h:62
void setCustom(bool val)
Definition: paletteworkspace.h:142
RemoveActionConfirmationType
Definition: paletteworkspace.h:123
PaletteTreeModel * userPaletteModel()
Definition: paletteworkspace.h:211
QString actionName() const
Definition: palettemodel.h:86
Definition: aeolus.cpp:26
AbstractPaletteController(QObject *parent=nullptr)
Definition: paletteworkspace.h:78
UserPaletteController(QAbstractItemModel *m, PaletteTreeModel *userPalette, QObject *parent=nullptr)
Definition: paletteworkspace.h:136
Definition: xml.h:218
bool visible() const
Definition: paletteworkspace.h:139
bool paletteChanged() const
Definition: paletteworkspace.h:222
PaletteTreeModel * userPalette
Definition: paletteworkspace.h:170
void setVisible(bool val)
Definition: paletteworkspace.h:140
QAbstractItemModel * model()
Definition: paletteworkspace.h:132
virtual Q_INVOKABLE Qt::DropAction dropAction(const QVariantMap &mimeData, Qt::DropAction proposedAction, const QModelIndex &parent, bool internal) const
Definition: paletteworkspace.h:80
void setUserEditable(bool val)
Definition: paletteworkspace.h:156
virtual Q_INVOKABLE void editPaletteProperties(const QModelIndex &index)
Definition: paletteworkspace.h:94
virtual Q_INVOKABLE bool applyPaletteElement(const QModelIndex &index, Qt::KeyboardModifiers modifiers)
Definition: paletteworkspace.h:97
virtual Q_INVOKABLE bool canEdit(const QModelIndex &) const
Definition: paletteworkspace.h:92
bool paletteTreeChanged() const
Definition: palettemodel.h:137
void retranslate()
Definition: palettemodel.cpp:839
Q_INVOKABLE void open()
Definition: paletteworkspace.cpp:91
PalettePanel::Type _type
Definition: paletteworkspace.h:39
void updateCellsState(const Selection &sel)
Definition: paletteworkspace.h:229
Definition: paletteworkspace.h:106
void onElementAdded(const Element *)
Definition: paletteworkspace.cpp:75
RemoveAction
Definition: paletteworkspace.h:71
const QAbstractItemModel * model() const
Definition: paletteworkspace.h:133