drumstick 2.10.0
C++ MIDI libraries using Qt objects, idioms, and style.
pianokeybd.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 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
33#if defined(DRUMSTICK_STATIC)
34#define DRUMSTICK_WIDGETS_EXPORT
35#else
36#if defined(drumstick_widgets_EXPORTS)
37#define DRUMSTICK_WIDGETS_EXPORT Q_DECL_EXPORT
38#else
39#define DRUMSTICK_WIDGETS_EXPORT Q_DECL_IMPORT
40#endif
41#endif
42
43namespace drumstick { namespace widgets {
44
45 Q_NAMESPACE
46
57 public:
58 virtual ~RawKbdHandler() = default;
64 virtual bool handleKeyPressed(int keycode) = 0;
70 virtual bool handleKeyReleased(int keycode) = 0;
71 };
72
84 {
85 public:
86 virtual ~PianoHandler() = default;
92 virtual void noteOn( const int note, const int vel ) = 0;
98 virtual void noteOff( const int note, const int vel ) = 0;
99 };
100
108 typedef QHash<int, int> KeyboardMap;
109
110 extern DRUMSTICK_WIDGETS_EXPORT KeyboardMap g_DefaultKeyMap;
111 extern DRUMSTICK_WIDGETS_EXPORT KeyboardMap g_DefaultRawKeyMap;
112
113 const int DEFAULTSTARTINGKEY = 9;
114 const int DEFAULTBASEOCTAVE = 1;
115 const int DEFAULTNUMBEROFKEYS = 88;
116
126 Q_ENUM_NS(LabelVisibility)
127
128
136 Q_ENUM_NS(LabelAlteration)
137
138
146 Q_ENUM_NS(LabelOrientation)
147
148
156 Q_ENUM_NS(LabelNaming)
157
158
167 Q_ENUM_NS(LabelCentralOctave)
168
169
175 class DRUMSTICK_WIDGETS_EXPORT PianoKeybd : public QGraphicsView, public RawKbdHandler
176 {
177 Q_OBJECT
178 Q_PROPERTY( int baseOctave READ baseOctave WRITE setBaseOctave )
179 Q_PROPERTY( int numKeys READ numKeys WRITE setNumKeys )
180 Q_PROPERTY( int rotation READ getRotation WRITE setRotation )
181 Q_PROPERTY( QColor keyPressedColor READ getKeyPressedColor WRITE setKeyPressedColor )
182 Q_PROPERTY( drumstick::widgets::LabelVisibility showLabels READ showLabels WRITE setShowLabels )
184 Q_PROPERTY( drumstick::widgets::LabelOrientation labelOrientation READ labelOrientation WRITE setLabelOrientation )
185 Q_PROPERTY( drumstick::widgets::LabelCentralOctave labelOctave READ labelOctave WRITE setLabelOctave )
186 Q_PROPERTY( int transpose READ getTranspose WRITE setTranspose )
187 Q_PROPERTY( int startKey READ startKey WRITE setStartKey )
188 Q_PROPERTY( QFont labelFont READ font WRITE setFont )
189
190#ifndef Q_MOC_RUN
191 Q_CLASSINFO("Author", "Pedro Lopez-Cabanillas <plcl@users.sf.net>")
192 Q_CLASSINFO("URL", "https://sourceforge.net/projects/drumstick")
193 Q_CLASSINFO("Version", QT_STRINGIFY(VERSION))
194#endif
195
196 public:
197 explicit PianoKeybd(QWidget *parent = nullptr);
198 PianoKeybd(const int baseOctave, const int numKeys, const int startKey, QWidget *parent = nullptr);
199 virtual ~PianoKeybd();
200
201 void setFont(const QFont &font);
203 void setPianoHandler(PianoHandler* handler);
204
206 void setHighlightPalette(const PianoPalette& p );
208 void setBackgroundPalette(const PianoPalette& p );
210 void setForegroundPalette(const PianoPalette& p );
211
212 bool showColorScale() const;
213 void setShowColorScale(const bool show);
214
215 void useCustomNoteNames(const QStringList& names);
217 QStringList customNoteNames() const;
218 QStringList standardNoteNames() const;
219 void retranslate();
220
221 int baseOctave() const;
222 void setBaseOctave(const int baseOctave);
223 int numKeys() const;
224 int startKey() const;
225 void setNumKeys(const int numKeys, const int startKey = DEFAULTSTARTINGKEY);
226 int getRotation() const;
227 void setRotation(int r);
228 QColor getKeyPressedColor() const;
229 void setKeyPressedColor(const QColor& c);
231 LabelVisibility showLabels() const;
232 void setShowLabels(const LabelVisibility show);
234 void setLabelAlterations(const LabelAlteration use);
235 LabelOrientation labelOrientation() const;
236 void setLabelOrientation(const LabelOrientation orientation);
237 LabelCentralOctave labelOctave() const;
238 void setLabelOctave(const LabelCentralOctave octave);
239 int getTranspose() const;
240 void setTranspose(int t);
241 int getChannel() const;
242 void setChannel(const int c);
243 int getVelocity() const;
244 void setVelocity(const int v);
245
246 bool isKeyboardEnabled() const;
247 void setKeyboardEnabled( const bool enable );
248 bool isMouseEnabled() const;
249 void setMouseEnabled( const bool enable );
250 bool isTouchEnabled() const;
251 void setTouchEnabled( const bool enable );
252 bool velocityTint() const ;
253 void setVelocityTint( const bool enable );
254 void allKeysOff();
255
256 QSize sizeHint() const override;
259 void resetKeyboardMap();
262 void resetRawKeyboardMap();
263 bool getRawKeyboardMode() const;
264 void setRawKeyboardMode(const bool b);
265
266 void showNoteOn( const int note, QColor color, int vel = -1 );
267 void showNoteOn( const int note, int vel = -1 );
268 void showNoteOff( const int note, int vel = -1 );
269
270 // RawKbdHandler methods
271 bool handleKeyPressed(int keycode) override;
272 bool handleKeyReleased(int keycode) override;
273
274 void setKeyPicture(const bool natural, const QPixmap& pix);
275 QPixmap getKeyPicture(const bool natural);
276
277 void setUseKeyPictures(const bool enable);
278 bool getUseKeyPictures() const;
279
280 void setUsingNativeFilter(const bool newState);
281 bool isUsingNativeFilter() const;
282
283 void setOctaveSubscript(const bool enable);
284 bool octaveSubscript() const;
285
286 void setStartKey(const int startKey);
287
288 Q_SIGNALS:
296 void noteOn( int midiNote, int vel );
304 void noteOff( int midiNote, int vel );
310 void signalName( const QString& name );
311
312 protected:
313 void initialize();
314 void initDefaultMap();
315 void initScene(int base, int num, int ini, const QColor& c = QColor());
316 void resizeEvent(QResizeEvent *event) override;
317 bool viewportEvent(QEvent *ev) override;
318
319 private:
320 class PianoKeybdPrivate;
321 QScopedPointer<PianoKeybdPrivate> d;
322 };
323
325
326}} // namespace drumstick::widgets
327
328#endif // PIANOKEYBD_H
The QEvent class is the base class of all event classes.
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
The PianoHandler class callbacks.
Definition pianokeybd.h:84
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.
bool octaveSubscript() const
Returns whether the octave subscript designation is enabled.
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.
bool getUseKeyPictures() const
Returns whether pictures are used to paint the keys.
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.
bool isUsingNativeFilter() const
Returns whether the application is filtering native events.
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.
void setStartKey(const int startKey)
Sets the initial/starting note key.
void setKeyPicture(const bool natural, const QPixmap &pix)
Assigns a custom picture to the white or black keys that will be used as a texture to paint the keys.
bool velocityTint() const
Returns whether the note MIDI velocity influences the highlight color tint.
void setUsingNativeFilter(const bool newState)
Enables or disables the application level usage of a native event filter.
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.
QPixmap getKeyPicture(const bool natural)
Returns the custom picture used to paint the corresponding keys.
void retranslate()
Updates the standard names of notes according to the currently active program language translation.
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.
void setUseKeyPictures(const bool enable)
Enables or disables a picture to paint the keys.
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 viewportEvent(QEvent *ev) override
This method overrides QGraphicsView::viewportEvent() Only touchscreen events are processed here.
void setOctaveSubscript(const bool enable)
Enables or disables the octave subscript designation.
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:56
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:131
DRUMSTICK_WIDGETS_EXPORT KeyboardMap g_DefaultKeyMap
Global Key Map Variable.
LabelCentralOctave
Labels Central Octave.
Definition pianokeybd.h:161
LabelVisibility
Labels Visibility.
Definition pianokeybd.h:120
LabelNaming
Labels Naming.
Definition pianokeybd.h:151
const int DEFAULTNUMBEROFKEYS
Default number of piano keys.
Definition pianokeybd.h:115
LabelOrientation
Labels Orientation.
Definition pianokeybd.h:141
const int DEFAULTSTARTINGKEY
Default starting key (A)
Definition pianokeybd.h:113
QHash< int, int > KeyboardMap
KeyboardMap.
Definition pianokeybd.h:108
DRUMSTICK_WIDGETS_EXPORT KeyboardMap g_DefaultRawKeyMap
Global Raw Key Map Variable.
const int DEFAULTBASEOCTAVE
Default base octave.
Definition pianokeybd.h:114
@ ShowSharps
Show sharps on black keys.
Definition pianokeybd.h:132
@ ShowNothing
Do not show names on black keys.
Definition pianokeybd.h:134
@ ShowFlats
Show flats on black keys.
Definition pianokeybd.h:133
@ OctaveC3
Central C, MIDI note #60 is C3.
Definition pianokeybd.h:163
@ OctaveNothing
Don't show octave numbers.
Definition pianokeybd.h:162
@ OctaveC5
Central C, MIDI note #60 is C5.
Definition pianokeybd.h:165
@ OctaveC4
Central C, MIDI note #60 is C4.
Definition pianokeybd.h:164
@ ShowAlways
Show always note names.
Definition pianokeybd.h:124
@ ShowMinimum
Show only note C names.
Definition pianokeybd.h:122
@ ShowActivated
Show names when notes are activated.
Definition pianokeybd.h:123
@ ShowNever
Don't show note names.
Definition pianokeybd.h:121
@ CustomNamesWithSharps
Show custom names with sharps.
Definition pianokeybd.h:153
@ StandardNames
Show standard names.
Definition pianokeybd.h:152
@ CustomNamesWithFlats
Show custom names with flats.
Definition pianokeybd.h:154
@ AutomaticOrientation
Show horizonal or vertical names depending on the size.
Definition pianokeybd.h:144
@ VerticalOrientation
Show vertical names.
Definition pianokeybd.h:143
@ HorizontalOrientation
Show horizontal names.
Definition pianokeybd.h:142
Drumstick Widgets library MIDI related widgets and functions.
Drumstick common.
Piano Palette declarations.