drumstick  2.10.0
C++MIDIlibrariesusingQtobjects,idioms,andstyle.
pianoscene.h
Go to the documentation of this file.
1 /*
2  Virtual Piano Widget for Qt
3  Copyright (C) 2008-2024, Pedro Lopez-Cabanillas <plcl@users.sf.net>
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 3 of the License, or
8  (at your option) any later version.
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 along
16  with this program; If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef PIANOSCENE_H_
20 #define PIANOSCENE_H_
21 
22 #include <QGraphicsScene>
23 #include <QScopedPointer>
24 #include <QHash>
25 #include <QTouchEvent>
26 #include <drumstick/pianokeybd.h>
27 #include <drumstick/pianopalette.h>
28 #include "pianokey.h"
29 #include "keylabel.h"
30 
36 namespace drumstick { namespace widgets {
37 
46 class PianoScene : public QGraphicsScene
47 {
48  Q_OBJECT
49 public:
50  PianoScene(const int baseOctave,
51  const int numKeys,
52  const int startKey,
53  const QColor &keyPressedColor = QColor(),
54  QObject *parent = nullptr);
55  ~PianoScene();
56 
57  QSize sizeHint() const;
58  void setKeyboardMap(KeyboardMap *map);
59  KeyboardMap *getKeyboardMap() const;
61  void setPianoHandler(PianoHandler *handler);
62 
64  void setHighlightPalette(const PianoPalette &p);
66  void setBackgroundPalette(const PianoPalette &p);
68  void setForegroundPalette(const PianoPalette &p);
69 
70  bool showColorScale() const;
71  void setShowColorScale(const bool show);
72 
73  QColor getKeyPressedColor() const;
74  void setKeyPressedColor(const QColor &color);
75  void resetKeyPressedColor();
76 
77  int getMinNote() const;
78  void setMinNote(const int note);
79  int getMaxNote() const;
80  void setMaxNote(const int note);
81  int getTranspose() const;
82  void setTranspose(const int transpose);
83 
85  void setShowLabels(const LabelVisibility show);
87  void setAlterations(const LabelAlteration use);
89  void setOctave(const LabelCentralOctave octave);
90  LabelOrientation getOrientation() const;
91  void setOrientation(const LabelOrientation orientation);
92 
93  bool isKeyboardEnabled() const;
94  void setKeyboardEnabled(const bool enable);
95  bool isMouseEnabled() const;
96  void setMouseEnabled(const bool enable);
97  bool isTouchEnabled() const;
98  void setTouchEnabled(const bool enable);
99  bool velocityTint() const;
100  void setVelocityTint(const bool enable);
101  bool isOctaveStart(const int note);
102 
103  void showNoteOn(const int note, QColor color, int vel = -1);
104  void showNoteOn(const int note, int vel = -1);
105  void showNoteOff(const int note, int vel = -1);
106  int baseOctave() const;
107  void setBaseOctave(const int base);
108  int numKeys() const;
109  int startKey() const;
110  void allKeysOff();
111  void keyOn(const int note);
112  void keyOff(const int note);
113  bool getRawKeyboardMode() const;
114  void setRawKeyboardMode(const bool b);
115  void useCustomNoteNames(const QStringList &names);
116  void useStandardNoteNames();
117  QStringList customNoteNames() const;
118  QStringList standardNoteNames() const;
119  int getVelocity();
120  void setVelocity(const int velocity);
121  int getChannel() const;
122  void setChannel(const int channel);
123  void retranslate();
124  void refreshLabels();
125  void hideOrShowKeys();
126  void refreshKeys();
127 
128  void setKeyPicture(const bool natural, const QPixmap &pix);
129  QPixmap getKeyPicture(const bool natural);
130 
131  void setUseKeyPictures(const bool enable);
132  bool getUseKeyPictures() const;
133 
134  void saveData(QByteArray &ba);
135  void loadData(QByteArray &ba);
136 
137  bool touchScreenEvent(QTouchEvent *touchEvent);
138 
139  void setUsingNativeFilter(const bool newState);
140  bool isUsingNativeFilter() const;
141 
142  void setOctaveSubscript(const bool enable);
143  bool octaveSubscript() const;
144 
145 Q_SIGNALS:
153  void noteOn(int n, int v);
161  void noteOff(int n, int v);
167  void signalName(const QString &name);
168 
169 protected:
170  void showKeyOn(PianoKey *key, QColor color, int vel);
171  void showKeyOn(PianoKey *key, int vel);
172  void showKeyOff(PianoKey *key, int vel);
173  void displayKeyOn(PianoKey *key);
174  void keyOn(PianoKey *key);
175  void keyOff(PianoKey *key);
176  void keyOn(PianoKey *key, qreal pressure);
177  void keyOff(PianoKey *key, qreal pressure);
178  PianoKey *getKeyForPos(const QPointF &p) const;
179  PianoKey *getPianoKey(const int key) const;
180  QString noteName(PianoKey *key);
181  void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
182  void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
183  void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override;
184  void keyPressEvent(QKeyEvent *keyEvent) override;
185  void keyReleaseEvent(QKeyEvent *keyEvent) override;
186  bool event(QEvent *event) override;
187 
188  void triggerNoteOn(const int note, const int vel);
189  void triggerNoteOff(const int note, const int vel);
190  int getNoteFromKey(const int key) const;
191  void setHighlightColorFromPolicy(PianoKey *key, const int vel);
192 
193 private:
194  class PianoScenePrivate;
195  QScopedPointer<PianoScenePrivate> d;
196  };
197 
200 }} // namespace drumstick::widgets
201 
202 #endif /*PIANOSCENE_H_*/
void showNoteOff(const int note, int vel=-1)
Displays deactivated the corresponding key for a given MIDI note, with MIDI velocity.
Definition: pianoscene.cpp:470
The PianoHandler class callbacks.
Definition: pianokeybd.h:83
bool isMouseEnabled() const
Returns whether the computer keyboard note generation is enabled.
bool touchScreenEvent(QTouchEvent *touchEvent)
Process touch screen events, called by the view.
QSize sizeHint() const
Returns the calculated size of the scene.
Definition: pianoscene.cpp:313
The QGraphicsScene class provides a surface for managing a large number of 2D graphical items...
The PianoScene class is a QGraphicsScene composed by a number of graphics items: the piano keys...
Definition: pianoscene.h:46
void triggerNoteOn(const int note, const int vel)
Performs a Note On MIDI event for the given MIDI note number and velocity.
Definition: pianoscene.cpp:492
bool velocityTint() const
Returns whether the velocity parameter of note events is used to influence the highlight key colors...
LabelOrientation
Labels Orientation.
Definition: pianokeybd.h:141
PianoPalette getBackgroundPalette()
Returns the background palette.
void setHighlightPalette(const PianoPalette &p)
Assigns the active highlight palette.
void showKeyOff(PianoKey *key, int vel)
Displays as deactivated a key.
Definition: pianoscene.cpp:423
void setShowColorScale(const bool show)
Enables or disables the color scale key background mode.
void allKeysOff()
Deactivates all keys.
Definition: pianoscene.cpp:807
void refreshLabels()
Refresh the visibility and other attributes of the labels shown over the piano keys.
Definition: pianoscene.cpp:962
void keyOn(const int note)
Produces a MIDI Note On event and highlights the corresponding key for the given MIDI note number...
Definition: pianoscene.cpp:607
void keyPressEvent(QKeyEvent *keyEvent) override
This event handler, for event keyEvent, is reimplemented to receive keypress events.
Definition: pianoscene.cpp:743
void setVelocityTint(const bool enable)
Enables or disables the velocity parameter of note events to influence the highlight key colors...
The PianoPalette class.
Definition: pianopalette.h:70
QHash< int, int > KeyboardMap
KeyboardMap.
Definition: pianokeybd.h:108
void signalName(const QString &name)
signalName is emitted for each note created, and contains a string with the MIDI note number and the ...
void setKeyPressedColor(const QColor &color)
Assigns a single color for key highlight.
Definition: pianoscene.cpp:820
void useStandardNoteNames()
Assigns the standard note names, clearing the list of custom note names.
KeyboardMap * getKeyboardMap() const
Returns the computer keyboard note map.
Definition: pianoscene.cpp:331
void setOrientation(const LabelOrientation orientation)
Assigns the label orientation policy.
int getMaxNote() const
Returns the maximum MIDI note number that will be displayed.
Definition: pianoscene.cpp:881
The QObject class is the base class of all Qt objects.
int numKeys() const
Returns the number of keys that will be displayed.
Definition: pianoscene.cpp:924
void setPianoHandler(PianoHandler *handler)
Assigns a PianoHandler pointer for processing note events.
Definition: pianoscene.cpp:356
void retranslate()
Retranslates the standard note names.
void showKeyOn(PianoKey *key, QColor color, int vel)
Displays highlighted the activated key with the supplied color and note velocity. ...
Definition: pianoscene.cpp:395
LabelVisibility showLabels() const
Returns the label visibility policy (display note names over the piano keys).
bool octaveSubscript() const
Returns whether the subscript octave designation is enabled.
void setMaxNote(const int note)
Assigns the maximum MIDI note number that will be displayed.
Definition: pianoscene.cpp:890
void setMouseEnabled(const bool enable)
Enables or disables the mouse note generation.
Declaration of the PianoKey class.
PianoHandler * getPianoHandler() const
Gets the PianoHandler pointer to the note receiver.
Definition: pianoscene.cpp:343
Drumstick common.
Definition: alsaclient.cpp:71
LabelVisibility
Labels Visibility.
Definition: pianokeybd.h:120
void setKeyboardMap(KeyboardMap *map)
Assigns the computer keyboard note map.
Definition: pianoscene.cpp:322
bool isTouchEnabled() const
Returns whether the touch screen note generation is enabled.
void displayKeyOn(PianoKey *key)
Displays the note label over a highligted key.
Definition: pianoscene.cpp:374
void refreshKeys()
Refresh the background colors of all the piano keys.
Definition: pianoscene.cpp:982
void triggerNoteOff(const int note, const int vel)
Performs a Note Off MIDI event for the given MIDI note number and velocity.
Definition: pianoscene.cpp:511
int getVelocity()
Returns the MIDI note velocity parameter that is assigned to the MIDI OUT notes.
PianoKey * getPianoKey(const int key) const
Returns the piano key object corresponding to the given computer keyboard key.
Definition: pianoscene.cpp:731
void keyReleaseEvent(QKeyEvent *keyEvent) override
This event handler, for event keyEvent, is reimplemented to receive key release events.
Definition: pianoscene.cpp:770
void setMinNote(const int note)
Assigns the minimum MIDI note number that will be displayed.
Definition: pianoscene.cpp:869
int startKey() const
Returns the first key number that will be displayed.
Definition: pianoscene.cpp:933
void setTranspose(const int transpose)
Assigns the transpose amount in semitones.
Declaration of the KeyLabel class.
int getNoteFromKey(const int key) const
Returns the note number for the given computer keyboard key code.
Definition: pianoscene.cpp:714
void setOctaveSubscript(const bool enable)
Enables or disables the subscript octave designation.
QColor getKeyPressedColor() const
Returns the single highlight palette color.
int getChannel() const
Returns the MIDI channel that is assigned to the output events, or used to filter the input events (u...
PianoKey * getKeyForPos(const QPointF &p) const
Returns the piano key for the given scene point coordenates.
Definition: pianoscene.cpp:641
bool isUsingNativeFilter() const
Returns whether the application is filtering native events.
bool event(QEvent *event) override
This method overrides QGraphicsScene::event().
Definition: pianoscene.cpp:799
void useCustomNoteNames(const QStringList &names)
Assigns the list of custom note names, and enables this mode.
int getMinNote() const
Returns the minimum MIDI note number that will be displayed.
Definition: pianoscene.cpp:848
void setVelocity(const int velocity)
Assigns the MIDI note velocity parameter that is assigned to the MIDI OUT notes.
QStringList customNoteNames() const
Returns the custom note names list.
void noteOn(int n, int v)
This signal is emitted for each Note On MIDI event created using the computer keyboard, mouse or touch screen.
void setShowLabels(const LabelVisibility show)
Assigns the label visibility policy to the piano keys.
void noteOff(int n, int v)
This signal is emitted for each Note Off MIDI event created using the computer keyboard, mouse or touch screen.
void setKeyboardEnabled(const bool enable)
Enables or disables the computer keyboard note generation.
LabelAlteration
Labels for Alterations.
Definition: pianokeybd.h:131
QStringList standardNoteNames() const
Returns the standard note names list.
void resetKeyPressedColor()
Assigns the default highlight palette colors and assigns it to the scene.
Definition: pianoscene.cpp:835
QString noteName(PianoKey *key)
Returns the note name string that will be displayed over a given piano key.
Definition: pianoscene.cpp:953
void showNoteOn(const int note, QColor color, int vel=-1)
Displays highlighted the corresponding key for a given MIDI note, with a color and MIDI velocity...
Definition: pianoscene.cpp:443
void keyOff(const int note)
Produces a MIDI Note Off event and deactivates the corresponding key for the given MIDI note number...
Definition: pianoscene.cpp:619
bool isOctaveStart(const int note)
Returns whether the given note number is a octave startup note.
Definition: pianoscene.cpp:943
LabelCentralOctave
Labels Central Octave.
Definition: pianokeybd.h:161
void setForegroundPalette(const PianoPalette &p)
Assigns the active foreground palette.
void setHighlightColorFromPolicy(PianoKey *key, const int vel)
Assigns to the given key the highlight color from the active highlight palette and the given MIDI vel...
Definition: pianoscene.cpp:529
void hideOrShowKeys()
Hides or shows keys.
Definition: pianoscene.cpp:856
void setBaseOctave(const int base)
Assigns the octave base number.
Definition: pianoscene.cpp:911
Piano Keyboard Widget.
PianoScene(const int baseOctave, const int numKeys, const int startKey, const QColor &keyPressedColor=QColor(), QObject *parent=nullptr)
Constructor.
Definition: pianoscene.cpp:253
PianoPalette getForegroundPalette()
Returns the active foreground palette.
LabelAlteration alterations() const
Returns the alterations name policy.
void setRawKeyboardMode(const bool b)
Assigns the low level computer keyboard mode.
void setUsingNativeFilter(const bool newState)
Enables or disables the application level usage of a native event filter.
bool showColorScale() const
Returns whether the color scale mode is enabled.
int baseOctave() const
Returns the base octave number.
Definition: pianoscene.cpp:483
void mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent) override
This event handler, for event mouseEvent, is reimplemented to receive mouse press events for the scen...
Definition: pianoscene.cpp:679
LabelCentralOctave getOctave() const
Returns the central octave name policy.
bool getRawKeyboardMode() const
Returns whether the low level computer keyboard mode is enabled.
Definition: pianoscene.cpp:631
void setBackgroundPalette(const PianoPalette &p)
Assigns the active background palette.
The QEvent class is the base class of all event classes.
void setAlterations(const LabelAlteration use)
Assigns the alterations name policy.
Piano Palette declarations.
void setChannel(const int channel)
Assigns the MIDI channel that is included into the output events, or used to filter the input events ...
void mouseReleaseEvent(QGraphicsSceneMouseEvent *mouseEvent) override
This event handler, for event mouseEvent, is reimplemented to receive mouse release events for the sc...
Definition: pianoscene.cpp:696
void setTouchEnabled(const bool enable)
Enables or disables the touch screen note generation.
int getTranspose() const
Returns the transpose amount in semitones.
Definition: pianoscene.cpp:902
PianoPalette getHighlightPalette()
Returns the palette used for highlighting the played keys.
Definition: pianoscene.cpp:365
void mouseMoveEvent(QGraphicsSceneMouseEvent *mouseEvent) override
This event handler, for event mouseEvent, is reimplemented to receive mouse move events for the scene...
Definition: pianoscene.cpp:657