drumstick 2.0.0
pianokeybd.h
Go to the documentation of this file.
1/*
2 Virtual Piano Widget for Qt5
3 Copyright (C) 2008-2020, 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 PIANOKEYBD_H
20#define PIANOKEYBD_H
21
22#include <QGraphicsView>
23#include <QScopedPointer>
24#include <QGraphicsScene>
25#include "macros.h"
26#include "pianopalette.h"
27
32
33namespace drumstick { namespace widgets {
34
45 public:
46 virtual ~RawKbdHandler() = default;
52 virtual bool handleKeyPressed(int keycode) = 0;
58 virtual bool handleKeyReleased(int keycode) = 0;
59 };
60
72 {
73 public:
74 virtual ~PianoHandler() = default;
80 virtual void noteOn( const int note, const int vel ) = 0;
86 virtual void noteOff( const int note, const int vel ) = 0;
87 };
88
96 typedef QHash<int, int> KeyboardMap;
97
98 extern DRUMSTICK_EXPORT KeyboardMap g_DefaultKeyMap;
99 extern DRUMSTICK_EXPORT KeyboardMap g_DefaultRawKeyMap;
100
101 const int DEFAULTSTARTINGKEY = 9;
102 const int DEFAULTBASEOCTAVE = 1;
103 const int DEFAULTNUMBEROFKEYS = 88;
104
114
123
132
141
151
158 class DRUMSTICK_EXPORT PianoKeybd : public QGraphicsView, public RawKbdHandler
159 {
160 Q_OBJECT
161 Q_PROPERTY( int baseOctave READ baseOctave WRITE setBaseOctave )
162 Q_PROPERTY( int numKeys READ numKeys WRITE setNumKeys )
163 Q_PROPERTY( int rotation READ getRotation WRITE setRotation )
164 Q_PROPERTY( QColor keyPressedColor READ getKeyPressedColor WRITE setKeyPressedColor )
165 Q_PROPERTY( LabelVisibility showLabels READ showLabels WRITE setShowLabels )
166 Q_PROPERTY( LabelAlteration alterations READ labelAlterations WRITE setLabelAlterations )
167 Q_PROPERTY( LabelOrientation labelOrientation READ labelOrientation WRITE setLabelOrientation )
168 Q_PROPERTY( LabelCentralOctave labelOctave READ labelOctave WRITE setLabelOctave )
169 Q_PROPERTY( int transpose READ getTranspose WRITE setTranspose )
170
171#ifndef Q_MOC_RUN
172 Q_CLASSINFO("Author", "Pedro Lopez-Cabanillas <plcl@users.sf.net>")
173 Q_CLASSINFO("URL", "https://sourceforge.net/projects/drumstick")
174 Q_CLASSINFO("Version", QT_STRINGIFY(VERSION))
175#endif
176
177 public:
178 explicit PianoKeybd(QWidget *parent = nullptr);
179 PianoKeybd(const int baseOctave, const int numKeys, const int startKey, QWidget *parent = nullptr);
180 virtual ~PianoKeybd();
181
182 Q_ENUM(LabelVisibility);
183 Q_ENUM(LabelAlteration);
184 Q_ENUM(LabelOrientation);
185 Q_ENUM(LabelNaming);
186 Q_ENUM(LabelCentralOctave);
187
188 void setFont(const QFont &font);
190 void setPianoHandler(PianoHandler* handler);
191
193 void setHighlightPalette(const PianoPalette& p );
195 void setBackgroundPalette(const PianoPalette& p );
197 void setForegroundPalette(const PianoPalette& p );
198
199 bool showColorScale() const;
200 void setShowColorScale(const bool show);
201
202 void useCustomNoteNames(const QStringList& names);
204 QStringList customNoteNames() const;
205 QStringList standardNoteNames() const;
206 void retranslate();
207
208 int baseOctave() const;
209 void setBaseOctave(const int baseOctave);
210 int numKeys() const;
211 int startKey() const;
212 void setNumKeys(const int numKeys, const int startKey = DEFAULTSTARTINGKEY);
213 int getRotation() const;
214 void setRotation(int r);
215 QColor getKeyPressedColor() const;
216 void setKeyPressedColor(const QColor& c);
218 LabelVisibility showLabels() const;
219 void setShowLabels(const LabelVisibility show);
221 void setLabelAlterations(const LabelAlteration use);
222 LabelOrientation labelOrientation() const;
223 void setLabelOrientation(const LabelOrientation orientation);
224 LabelCentralOctave labelOctave() const;
225 void setLabelOctave(const LabelCentralOctave octave);
226 int getTranspose() const;
227 void setTranspose(int t);
228 int getChannel() const;
229 void setChannel(const int c);
230 int getVelocity() const;
231 void setVelocity(const int v);
232
233 bool isKeyboardEnabled() const;
234 void setKeyboardEnabled( const bool enable );
235 bool isMouseEnabled() const;
236 void setMouseEnabled( const bool enable );
237 bool isTouchEnabled() const;
238 void setTouchEnabled( const bool enable );
239 bool velocityTint() const ;
240 void setVelocityTint( const bool enable );
241 void allKeysOff();
242
243 QSize sizeHint() const override;
246 void resetKeyboardMap();
249 void resetRawKeyboardMap();
250 bool getRawKeyboardMode() const;
251 void setRawKeyboardMode(const bool b);
252
253 void showNoteOn( const int note, QColor color, int vel = -1 );
254 void showNoteOn( const int note, int vel = -1 );
255 void showNoteOff( const int note, int vel = -1 );
256
257 // RawKbdHandler methods
258 bool handleKeyPressed(int keycode) override;
259 bool handleKeyReleased(int keycode) override;
260
261 signals:
269 void noteOn( int midiNote, int vel );
277 void noteOff( int midiNote, int vel );
283 void signalName( const QString& name );
284
285 protected:
286 void initialize();
287 void initDefaultMap();
288 void initScene(int base, int num, int ini, const QColor& c = QColor());
289 void resizeEvent(QResizeEvent *event) override;
290
291 private:
292 class PianoKeybdPrivate;
293 QScopedPointer<PianoKeybdPrivate> d;
294 };
295
297
298}} // namespace drumstick::widgets
299
300#endif // PIANOKEYBD_H
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
The PianoHandler class callbacks.
Definition pianokeybd.h:72
virtual void noteOff(const int note, const int vel)=0
noteOff handles MIDI note off events
virtual void noteOn(const int note, const int vel)=0
noteOn handles MIDI note on events
void initScene(int base, int num, int ini, const QColor &c=QColor())
Creates and initializes a new PianoScene instance and assigns it to this widget.
void allKeysOff()
Forces all active notes to silence.
void resetKeyPressedColor()
Assigns the default highlight palette colors and assigns it to the scene.
bool handleKeyReleased(int keycode) override
handleKeyReleased handles low level computer keyboard reelase events
void setNumKeys(const int numKeys, const int startKey=DEFAULTSTARTINGKEY)
This method changes the number of displayed keys and the starting key number, keeping the other setti...
PianoKeybd(QWidget *parent=nullptr)
Constructor.
void setBackgroundPalette(const PianoPalette &p)
Assigns the palette used to paint the keys' background.
void setRawKeyboardMode(const bool b)
Enables or disables the low level computer keyboard mode.
void setFont(const QFont &font)
Assigns a typographic font for drawing the note labels over the piano keys.
void initialize()
This method is called from the available constructors to initialize some widget attributes,...
QSize sizeHint() const override
Overrides QGraphicsView::sizeHint() providing a size value based on the piano scene.
void useStandardNoteNames()
Disables the custom note names usage as labels over the keys, and restores the standard note names in...
bool isKeyboardEnabled() const
Returns whether the computer keyboard is enabled.
void resetKeyboardMap()
Resets the low level computer keyboard note map to the default one.
void setKeyPressedColor(const QColor &c)
Assigns a single color for key highlight.
void setChannel(const int c)
Assigns the MIDI Channel (0-15).
PianoPalette getForegroundPalette() const
Returns the palette used to paint texts over the keys like the note names or custom labels.
void setHighlightPalette(const PianoPalette &p)
Assigns the palette used for highlighting the played keys.
void setShowColorScale(const bool show)
Enables or disables the color scale background palette.
void setPianoHandler(PianoHandler *handler)
Assigns a PianoHandler pointer for processing note events.
void setLabelOctave(const LabelCentralOctave octave)
Assigns the octave label policy.
bool showColorScale() const
Returns true if the color scale background palette is assigned and active.
void signalName(const QString &name)
signalName is emitted for each note created, and contains a string with the MIDI note number and the ...
void setKeyboardEnabled(const bool enable)
Enables or disables the computer keyboard note input.
KeyboardMap * getRawKeyboardMap()
Returns the low level computer keyboard note map.
PianoPalette getBackgroundPalette() const
Returns the palette used to paint the keys' background.
void setRawKeyboardMap(KeyboardMap *m)
Assigns the low level computer keyboard note map.
int getChannel() const
Returns the MIDI Channel (0-15).
void setBaseOctave(const int baseOctave)
Assigns the base octave number.
void setForegroundPalette(const PianoPalette &p)
Assigns the palette used to paint texts over the keys like the note names or custom labels.
void setVelocityTint(const bool enable)
Enables or disables the note MIDI velocity influencing the highlight color tint.
void setLabelAlterations(const LabelAlteration use)
Assigns the label alterations policy.
bool velocityTint() const
Returns whether the note MIDI velocity influences the highlight color tint.
QColor getKeyPressedColor() const
Returns the key highlight color.
void resizeEvent(QResizeEvent *event) override
This method overrides QGraphicsView::resizeEvent() to keep the aspect ratio of the keys scene when th...
void noteOff(int midiNote, int vel)
This signal is emitted for each Note Off MIDI event created using the computer keyboard,...
bool handleKeyPressed(int keycode) override
handleKeyPressed handles low level computer keyboard press events
void setKeyboardMap(KeyboardMap *m)
Assigns the computer keyboard note map.
void showNoteOff(const int note, int vel=-1)
Shows inactive one note key with the specified velocity.
void setShowLabels(const LabelVisibility show)
Assigns the label visibility policy.
LabelAlteration labelAlterations() const
Returns the label alterations policy.
PianoPalette getHighlightPalette() const
Returns the palette used for highlighting the played keys.
QStringList standardNoteNames() const
Returns the list of standard note names.
void noteOn(int midiNote, int vel)
This signal is emitted for each Note On MIDI event created using the computer keyboard,...
void resetRawKeyboardMap()
Resets the computer keyboard note map to the default one.
PianoHandler * getPianoHandler() const
Gets the PianoHandler pointer to the note receiver.
void setLabelOrientation(const LabelOrientation orientation)
Assigns the labels orientation policy.
bool isMouseEnabled() const
Returns whether the mouse note input is enabled.
bool getRawKeyboardMode() const
Returns the low level computer keyboard note map.
void showNoteOn(const int note, QColor color, int vel=-1)
Highlights one note key with the specified color and velocity.
void setRotation(int r)
Rotates the keyboard view an angle clockwise.
void retranslate()
Updates the standard names of notes according to the currently active program language translation.
int startKey() const
Returns the starting key note: C=0, A=9 and so on.
void setTouchEnabled(const bool enable)
Enables or disables the touch screen note input.
void useCustomNoteNames(const QStringList &names)
Assigns a list of custom text labels to be displayer over the keys.
void setMouseEnabled(const bool enable)
Enables or disables the mouse note input.
KeyboardMap * getKeyboardMap()
Returns the computer keyboard note map.
int getRotation() const
Returns the rotation angle in degrees, clockwise, of the piano view.
void setTranspose(int t)
Assigns the transpose amount in semitones.
QStringList customNoteNames() const
Returns the list of custom note names.
int getVelocity() const
Returns the MIDI note velocity.
void setVelocity(const int v)
Assigns the MIDI note velocity.
bool isTouchEnabled() const
Returns whether the touch screen note input is enabled.
int getTranspose() const
Returns the transpose amount in semitones.
The PianoPalette class.
The RawKbdHandler class callbacks.
Definition pianokeybd.h:44
virtual bool handleKeyReleased(int keycode)=0
handleKeyReleased handles low level computer keyboard reelase events
virtual bool handleKeyPressed(int keycode)=0
handleKeyPressed handles low level computer keyboard press events
LabelAlteration
Labels for Alterations.
Definition pianokeybd.h:118
DRUMSTICK_EXPORT KeyboardMap g_DefaultKeyMap
Global Key Map Variable.
LabelCentralOctave
Labels Central Octave.
Definition pianokeybd.h:145
LabelVisibility
Labels Visibility.
Definition pianokeybd.h:108
LabelNaming
Labels Naming.
Definition pianokeybd.h:136
const int DEFAULTNUMBEROFKEYS
Default number of piano keys.
Definition pianokeybd.h:103
LabelOrientation
Labels Orientation.
Definition pianokeybd.h:127
const int DEFAULTSTARTINGKEY
Default starting key (A).
Definition pianokeybd.h:101
QHash< int, int > KeyboardMap
KeyboardMap.
Definition pianokeybd.h:96
DRUMSTICK_EXPORT KeyboardMap g_DefaultRawKeyMap
Global Raw Key Map Variable.
const int DEFAULTBASEOCTAVE
Default base octave.
Definition pianokeybd.h:102
@ ShowSharps
Show sharps on black keys.
Definition pianokeybd.h:119
@ ShowNothing
Do not show names on black keys.
Definition pianokeybd.h:121
@ ShowFlats
Show flats on black keys.
Definition pianokeybd.h:120
@ OctaveC3
Central C, MIDI note #60 is C3.
Definition pianokeybd.h:147
@ OctaveNothing
Don't show octave numbers.
Definition pianokeybd.h:146
@ OctaveC5
Central C, MIDI note #60 is C5.
Definition pianokeybd.h:149
@ OctaveC4
Central C, MIDI note #60 is C4.
Definition pianokeybd.h:148
@ ShowAlways
Show always note names.
Definition pianokeybd.h:112
@ ShowMinimum
Show only note C names.
Definition pianokeybd.h:110
@ ShowActivated
Show names when notes are activated.
Definition pianokeybd.h:111
@ ShowNever
Don't show note names.
Definition pianokeybd.h:109
@ CustomNamesWithSharps
Show custom names with sharps.
Definition pianokeybd.h:138
@ StandardNames
Show standard names.
Definition pianokeybd.h:137
@ CustomNamesWithFlats
Show custom names with flats.
Definition pianokeybd.h:139
@ AutomaticOrientation
Show horizonal or vertical names depending on the size.
Definition pianokeybd.h:130
@ VerticalOrientation
Show vertical names.
Definition pianokeybd.h:129
@ HorizontalOrientation
Show horizontal names.
Definition pianokeybd.h:128
Drumstick visibility macros.
Drumstick Widgets library MIDI related widgets and functions.
Drumstick common.
Piano Palette declarations.