drumstick  2.10.0
C++MIDIlibrariesusingQtobjects,idioms,andstyle.
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 
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 
43 namespace drumstick { namespace widgets {
44 
45  Q_NAMESPACE
46 
56  class RawKbdHandler {
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 
125  };
126  Q_ENUM_NS(LabelVisibility)
127 
128 
135  };
136  Q_ENUM_NS(LabelAlteration)
137 
138 
145  };
146  Q_ENUM_NS(LabelOrientation)
147 
148 
151  enum LabelNaming {
155  };
156  Q_ENUM_NS(LabelNaming)
157 
158 
166  };
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 )
183  Q_PROPERTY( drumstick::widgets::LabelAlteration alterations READ labelAlterations WRITE setLabelAlterations )
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);
202  PianoHandler* getPianoHandler() const;
203  void setPianoHandler(PianoHandler* handler);
204 
205  PianoPalette getHighlightPalette() const;
206  void setHighlightPalette(const PianoPalette& p );
207  PianoPalette getBackgroundPalette() const;
208  void setBackgroundPalette(const PianoPalette& p );
209  PianoPalette getForegroundPalette() const;
210  void setForegroundPalette(const PianoPalette& p );
211 
212  bool showColorScale() const;
213  void setShowColorScale(const bool show);
214 
215  void useCustomNoteNames(const QStringList& names);
216  void useStandardNoteNames();
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);
230  void resetKeyPressedColor();
231  LabelVisibility showLabels() const;
232  void setShowLabels(const LabelVisibility show);
233  LabelAlteration labelAlterations() const;
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;
257  void setKeyboardMap(KeyboardMap* m);
258  KeyboardMap* getKeyboardMap();
259  void resetKeyboardMap();
260  void setRawKeyboardMap(KeyboardMap* m);
261  KeyboardMap* getRawKeyboardMap();
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 
326 }} // namespace drumstick::widgets
327 
328 #endif // PIANOKEYBD_H
The PianoHandler class callbacks.
Definition: pianokeybd.h:83
Show horizonal or vertical names depending on the size.
Definition: pianokeybd.h:144
Show custom names with flats.
Definition: pianokeybd.h:154
LabelOrientation
Labels Orientation.
Definition: pianokeybd.h:141
The PianoKeybd class.
Definition: pianokeybd.h:175
const int DEFAULTNUMBEROFKEYS
Default number of piano keys.
Definition: pianokeybd.h:115
const int DEFAULTBASEOCTAVE
Default base octave.
Definition: pianokeybd.h:114
LabelNaming
Labels Naming.
Definition: pianokeybd.h:151
Show standard names.
Definition: pianokeybd.h:152
virtual void noteOff(const int note, const int vel)=0
noteOff handles MIDI note off events
The PianoPalette class.
Definition: pianopalette.h:70
QHash< int, int > KeyboardMap
KeyboardMap.
Definition: pianokeybd.h:108
The RawKbdHandler class callbacks.
Definition: pianokeybd.h:56
Show always note names.
Definition: pianokeybd.h:124
Don&#39;t show octave numbers.
Definition: pianokeybd.h:162
Drumstick common.
Definition: alsaclient.cpp:71
LabelVisibility
Labels Visibility.
Definition: pianokeybd.h:120
Show sharps on black keys.
Definition: pianokeybd.h:132
Central C, MIDI note #60 is C5.
Definition: pianokeybd.h:165
Show only note C names.
Definition: pianokeybd.h:122
Don&#39;t show note names.
Definition: pianokeybd.h:121
virtual void noteOn(const int note, const int vel)=0
noteOn handles MIDI note on events
Show names when notes are activated.
Definition: pianokeybd.h:123
DRUMSTICK_WIDGETS_EXPORT KeyboardMap g_DefaultKeyMap
Global Key Map Variable.
Definition: pianokeybd.cpp:44
LabelAlteration
Labels for Alterations.
Definition: pianokeybd.h:131
Show flats on black keys.
Definition: pianokeybd.h:133
Central C, MIDI note #60 is C4.
Definition: pianokeybd.h:164
LabelCentralOctave
Labels Central Octave.
Definition: pianokeybd.h:161
Show custom names with sharps.
Definition: pianokeybd.h:153
const int DEFAULTSTARTINGKEY
Default starting key (A)
Definition: pianokeybd.h:113
Do not show names on black keys.
Definition: pianokeybd.h:134
Central C, MIDI note #60 is C3.
Definition: pianokeybd.h:163
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
DRUMSTICK_WIDGETS_EXPORT KeyboardMap g_DefaultRawKeyMap
Global Raw Key Map Variable.
Definition: pianokeybd.cpp:79
virtual bool handleKeyPressed(int keycode)=0
handleKeyPressed handles low level computer keyboard press events
The QEvent class is the base class of all event classes.
Piano Palette declarations.
virtual bool handleKeyReleased(int keycode)=0
handleKeyReleased handles low level computer keyboard reelase events