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 
33 namespace drumstick { namespace widgets {
34 
44  class RawKbdHandler {
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 
113  };
114 
122  };
123 
131  };
132 
136  enum LabelNaming {
140  };
141 
150  };
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);
189  PianoHandler* getPianoHandler() const;
190  void setPianoHandler(PianoHandler* handler);
191 
192  PianoPalette getHighlightPalette() const;
193  void setHighlightPalette(const PianoPalette& p );
194  PianoPalette getBackgroundPalette() const;
195  void setBackgroundPalette(const PianoPalette& p );
196  PianoPalette getForegroundPalette() const;
197  void setForegroundPalette(const PianoPalette& p );
198 
199  bool showColorScale() const;
200  void setShowColorScale(const bool show);
201 
202  void useCustomNoteNames(const QStringList& names);
203  void useStandardNoteNames();
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);
217  void resetKeyPressedColor();
218  LabelVisibility showLabels() const;
219  void setShowLabels(const LabelVisibility show);
220  LabelAlteration labelAlterations() const;
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;
244  void setKeyboardMap(KeyboardMap* m);
245  KeyboardMap* getKeyboardMap();
246  void resetKeyboardMap();
247  void setRawKeyboardMap(KeyboardMap* m);
248  KeyboardMap* getRawKeyboardMap();
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 
298 }} // namespace drumstick::widgets
299 
300 #endif // PIANOKEYBD_H
The PianoHandler class callbacks.
Definition: pianokeybd.h:71
Show horizonal or vertical names depending on the size.
Definition: pianokeybd.h:130
Show custom names with flats.
Definition: pianokeybd.h:139
LabelOrientation
Labels Orientation.
Definition: pianokeybd.h:127
The PianoKeybd class.
Definition: pianokeybd.h:158
const int DEFAULTNUMBEROFKEYS
Default number of piano keys.
Definition: pianokeybd.h:103
const int DEFAULTBASEOCTAVE
Default base octave.
Definition: pianokeybd.h:102
LabelNaming
Labels Naming.
Definition: pianokeybd.h:136
Show standard names.
Definition: pianokeybd.h:137
virtual void noteOff(const int note, const int vel)=0
noteOff handles MIDI note off events
The PianoPalette class.
Definition: pianopalette.h:59
QHash< int, int > KeyboardMap
KeyboardMap.
Definition: pianokeybd.h:96
The RawKbdHandler class callbacks.
Definition: pianokeybd.h:44
Drumstick visibility macros.
Show always note names.
Definition: pianokeybd.h:112
Don&#39;t show octave numbers.
Definition: pianokeybd.h:146
Drumstick common.
Definition: alsaclient.cpp:68
LabelVisibility
Labels Visibility.
Definition: pianokeybd.h:108
Show sharps on black keys.
Definition: pianokeybd.h:119
Central C, MIDI note #60 is C5.
Definition: pianokeybd.h:149
Show only note C names.
Definition: pianokeybd.h:110
Don&#39;t show note names.
Definition: pianokeybd.h:109
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:111
DRUMSTICK_EXPORT KeyboardMap g_DefaultKeyMap
Global Key Map Variable.
Definition: pianokeybd.cpp:39
LabelAlteration
Labels for Alterations.
Definition: pianokeybd.h:118
Show flats on black keys.
Definition: pianokeybd.h:120
Central C, MIDI note #60 is C4.
Definition: pianokeybd.h:148
LabelCentralOctave
Labels Central Octave.
Definition: pianokeybd.h:145
Show custom names with sharps.
Definition: pianokeybd.h:138
const int DEFAULTSTARTINGKEY
Default starting key (A)
Definition: pianokeybd.h:101
Do not show names on black keys.
Definition: pianokeybd.h:121
Central C, MIDI note #60 is C3.
Definition: pianokeybd.h:147
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
DRUMSTICK_EXPORT KeyboardMap g_DefaultRawKeyMap
Global Raw Key Map Variable.
Definition: pianokeybd.cpp:74
virtual bool handleKeyPressed(int keycode)=0
handleKeyPressed handles low level computer keyboard press events
Piano Palette declarations.
virtual bool handleKeyReleased(int keycode)=0
handleKeyReleased handles low level computer keyboard reelase events