1 /***************************************************************************
2 **                                                                        **
3 **  Polyphone, a soundfont editor                                         **
4 **  Copyright (C) 2013-2020 Davy Triponney                                **
5 **                                                                        **
6 **  This program is free software: you can redistribute it and/or modify  **
7 **  it under the terms of the GNU General Public License as published by  **
8 **  the Free Software Foundation, either version 3 of the License, or     **
9 **  (at your option) any later version.                                   **
10 **                                                                        **
11 **  This program is distributed in the hope that it will be useful,       **
12 **  but WITHOUT ANY WARRANTY; without even the implied warranty of        **
13 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          **
14 **  GNU General Public License for more details.                          **
15 **                                                                        **
16 **  You should have received a copy of the GNU General Public License     **
17 **  along with this program. If not, see http://www.gnu.org/licenses/.    **
18 **                                                                        **
19 ****************************************************************************
20 **           Author: Davy Triponney                                       **
21 **  Website/Contact: https://www.polyphone-soundfonts.com                 **
22 **             Date: 01.01.2013                                           **
23 ***************************************************************************/
24 
25 #ifndef RECTANGLEITEM_H
26 #define RECTANGLEITEM_H
27 
28 #include <QGraphicsItem>
29 #include <QPen>
30 #include "basetypes.h"
31 
32 class GraphicsRectangleItem : public QGraphicsRectItem
33 {
34 public:
35     enum EditingMode
36     {
37         NONE,
38         MOVE_ALL,
39         MOVE_RIGHT,
40         MOVE_LEFT,
41         MOVE_TOP,
42         MOVE_BOTTOM
43     };
44 
45     GraphicsRectangleItem(EltID id, QGraphicsItem *parent = nullptr);
46 
getID()47     EltID getID() { return _id; }
48     EltID findBrother();
49     EditingMode setHover(bool isHovered, const QPoint &point = QPoint());
isHovered()50     bool isHovered() { return _editingMode != EditingMode::NONE; }
syncHover(bool isSync)51     static void syncHover(bool isSync) { s_synchronizeEditingMode = isSync; }
setSelected(bool isSelected)52     void setSelected(bool isSelected) { _isSelected = isSelected; }
isSelected()53     bool isSelected() { return _isSelected; }
54 
55     QRectF getRectF() const;
56     void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = nullptr);
57     bool contains(const QPointF &point) const;
58 
59     void computeNewRange(const QPointF &pointInit, const QPointF &pointFinal);
60     bool saveChanges(); // Return true if changes have been made
61 
62     bool operator==(const GraphicsRectangleItem& other) { return (_id == other._id); }
63     bool operator==(const EltID &id) { return _id == id; }
64 
currentMinKey()65     int currentMinKey() { return _minKey; }
currentMaxKey()66     int currentMaxKey() { return _maxKey; }
currentMinVel()67     int currentMinVel() { return _minVel; }
currentMaxVel()68     int currentMaxVel() { return _maxVel; }
69 
70 private:
71     QPen _penBorderThin;
72     QPen _penBorderFat;
73     QBrush _brushRectangle;
74     QBrush _brushRectangleSelected;
75 
76     EltID _id;
77     int _minKeyInit, _maxKeyInit, _minVelInit, _maxVelInit;
78     int _minKey, _maxKey, _minVel, _maxVel;
79     EditingMode _editingMode;
80     bool _isSelected;
81     static EditingMode s_globalEditingMode;
82     static bool s_synchronizeEditingMode;
83 
84     void initialize(EltID id);
85     EditingMode getEditingMode(const QPoint &point);
86     int limit(int value, int min, int max);
87 };
88 
89 #endif // RECTANGLEITEM_H
90