1 //=============================================================================
2 //  MusE Score
3 //  Linux Music Score Editor
4 //
5 //  Copyright (C) 2002-2011 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 __PALETTE_H__
21 #define __PALETTE_H__
22 
23 #include "palette/palettetree.h"
24 #include "ui_paletteProperties.h"
25 #include "libmscore/sym.h"
26 
27 namespace Ms {
28 
29 class Element;
30 class Sym;
31 class XmlWriter;
32 class XmlReader;
33 class Palette;
34 
35 //---------------------------------------------------------
36 //   PaletteProperties
37 //---------------------------------------------------------
38 
39 class PaletteProperties : public QDialog, private Ui::PaletteProperties {
40       Q_OBJECT
41 
42       Palette* palette;
43       virtual void accept();
44       virtual void hideEvent(QHideEvent*);
45    public:
46       PaletteProperties(Palette* p, QWidget* parent = 0);
47       };
48 
49 //---------------------------------------------------------
50 //    PaletteScrollArea
51 //---------------------------------------------------------
52 
53 class PaletteScrollArea : public QScrollArea {
54       Q_OBJECT
55       bool _restrictHeight;
56 
57       virtual void resizeEvent(QResizeEvent*);
58 
59    protected:
60       virtual void keyPressEvent(QKeyEvent* event) override;
61 
62    public:
63       PaletteScrollArea(Palette* w, QWidget* parent = 0);
restrictHeight()64       bool restrictHeight() const { return _restrictHeight; }
setRestrictHeight(bool val)65       void setRestrictHeight(bool val) { _restrictHeight = val; }
66       };
67 
68 //---------------------------------------------------------
69 //   Palette
70 //---------------------------------------------------------
71 
72 class Palette : public QWidget {
73       Q_OBJECT
74 
75       QString _name;
76       QList<PaletteCell*> cells;
77       QList<PaletteCell*> dragCells;  // used for filter & backup
78 
79       int hgrid;
80       int vgrid;
81       int currentIdx;
82       int pressedIndex = -1;
83       int dragIdx;
84       int selectedIdx;
85       QPoint dragStartPosition;
86 
87       qreal extraMag;
88       bool _drawGrid;
89       bool _selectable;
90       bool _disableElementsApply { false };
91       bool _useDoubleClickToActivate { false };
92       bool _readOnly;
93       bool _systemPalette;
94       qreal _yOffset;                // in spatium units of "gscore"
95       bool filterActive { false };   // bool if filter is active
96 
97       bool _moreElements;
98       bool _showContextMenu { true };
99       bool _isSymbolsPaletteInMasterPalette { false };
100 
101       virtual void paintEvent(QPaintEvent*) override;
102       virtual void mousePressEvent(QMouseEvent*) override;
103       void mouseReleaseEvent(QMouseEvent* event) override;
104       void mouseDoubleClickEvent(QMouseEvent*) override;
105       virtual void mouseMoveEvent(QMouseEvent*) override;
106       virtual void leaveEvent(QEvent*) override;
107       virtual bool event(QEvent*) override;
108       virtual void resizeEvent(QResizeEvent*) override;
109 
110       virtual void dragEnterEvent(QDragEnterEvent*) override;
111       virtual void dragMoveEvent(QDragMoveEvent*) override;
112       virtual void dropEvent(QDropEvent*) override;
113       virtual void contextMenuEvent(QContextMenuEvent*) override;
114 
115       int idx2(const QPoint&) const;
116       QRect idxRect(int) const;
117 
ccp()118       const QList<PaletteCell*>* ccp() const { return filterActive ? &dragCells : &cells; }
119       QPixmap pixmap(int cellIdx) const;
120 
121       void applyElementAtPosition(QPoint pos, Qt::KeyboardModifiers modifiers);
122 
123    private slots:
124       void actionToggled(bool val);
125 
126    signals:
127       void boxClicked(int);
128       void changed();
129       void displayMore(const QString& paletteName);
130 
131    public:
132       Palette(QWidget* parent = 0);
133       Palette(std::unique_ptr<PalettePanel>, QWidget* parent = nullptr);
134       virtual ~Palette();
135 
136       void nextPaletteElement();
137       void prevPaletteElement();
138       void applyPaletteElement();
139       static bool applyPaletteElement(Element* element, Qt::KeyboardModifiers modifiers = {});
140       PaletteCell* append(Element*, const QString& name, QString tag = QString(),
141          qreal mag = 1.0);
142       PaletteCell* add(int idx, Element*, const QString& name,
143          const QString tag = QString(), qreal mag = 1.0);
144 
emitChanged()145       void emitChanged()             { emit changed(); }
146       void setGrid(int, int);
147       Element* element(int idx);
setDrawGrid(bool val)148       void setDrawGrid(bool val)     { _drawGrid = val; }
drawGrid()149       bool drawGrid() const          { return _drawGrid; }
150       bool read(const QString& path); // TODO: remove/reuse PalettePanel code
151       void write(const QString& path); // TODO: remove/reuse PalettePanel code
152       void read(XmlReader&);
153       void write(XmlWriter&) const;
154       void clear();
setSelectable(bool val)155       void setSelectable(bool val)   { _selectable = val;  }
selectable()156       bool selectable() const        { return _selectable; }
getSelectedIdx()157       int getSelectedIdx() const     { return selectedIdx; }
setSelected(int idx)158       void setSelected(int idx)      { selectedIdx = idx;  }
readOnly()159       bool readOnly() const          { return _readOnly;   }
160       void setReadOnly(bool val);
disableElementsApply()161       bool disableElementsApply() const      { return _disableElementsApply; }
setDisableElementsApply(bool val)162       void setDisableElementsApply(bool val) { _disableElementsApply = val; }
163 
useDoubleClickToActivate()164       bool useDoubleClickToActivate() const { return _useDoubleClickToActivate; }
setUseDoubleClickToActivate(bool val)165       void setUseDoubleClickToActivate(bool val) { _useDoubleClickToActivate = val; }
166 
systemPalette()167       bool systemPalette() const     { return _systemPalette; }
168       void setSystemPalette(bool val);
169 
170       void setMag(qreal val);
mag()171       qreal mag() const              { return extraMag;    }
setYOffset(qreal val)172       void setYOffset(qreal val)     { _yOffset = val;     }
yOffset()173       qreal yOffset() const          { return _yOffset;    }
174       int columns() const;
175       int rows() const;
size()176       int size() const               { return filterActive ? dragCells.size() : cells.size(); }
cellAt(int index)177       PaletteCell* cellAt(int index) const { return ccp()->value(index); }
setCellReadOnly(int c,bool v)178       void setCellReadOnly(int c, bool v)  { cells[c]->readOnly = v;   }
name()179       QString name() const           { return _name;        }
setName(const QString & s)180       void setName(const QString& s) { _name = s;           }
gridWidth()181       int gridWidth() const          { return hgrid;        }
gridHeight()182       int gridHeight() const         { return vgrid;        }
moreElements()183       bool moreElements() const      { return _moreElements; }
184       void setMoreElements(bool val);
185       bool filter(const QString& text);
setShowContextMenu(bool val)186       void setShowContextMenu(bool val) { _showContextMenu = val; }
setIsSymbolsPaletteInMasterPalette(bool val)187       void setIsSymbolsPaletteInMasterPalette(bool val) { _isSymbolsPaletteInMasterPalette = val; }
188 
189       static qreal guiMag();
gridWidthM()190       int gridWidthM() const  { return hgrid * guiMag(); }
gridHeightM()191       int gridHeightM() const { return vgrid * guiMag(); }
192 
getCurrentIdx()193       int getCurrentIdx() { return currentIdx; }
setCurrentIdx(int i)194       void setCurrentIdx(int i) { currentIdx = i; }
isFilterActive()195       bool isFilterActive() { return filterActive == true; }
getDragCells()196       QList<PaletteCell*> getDragCells() { return dragCells; }
197       virtual int heightForWidth(int) const;
198       virtual QSize sizeHint() const;
199       int idx(const QPoint&) const;
200       };
201 
202 
203 } // namespace Ms
204 #endif
205