drumstick 2.0.0
pianokeybd.cpp
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#include <QApplication>
22
27
33
34namespace drumstick { namespace widgets {
35
40 {Qt::Key_Z, 12},
41 {Qt::Key_S, 13},
42 {Qt::Key_X, 14},
43 {Qt::Key_D, 15},
44 {Qt::Key_C, 16},
45 {Qt::Key_V, 17},
46 {Qt::Key_G, 18},
47 {Qt::Key_B, 19},
48 {Qt::Key_H, 20},
49 {Qt::Key_N, 21},
50 {Qt::Key_J, 22},
51 {Qt::Key_M, 23},
52 {Qt::Key_Q, 24},
53 {Qt::Key_2, 25},
54 {Qt::Key_W, 26},
55 {Qt::Key_3, 27},
56 {Qt::Key_E, 28},
57 {Qt::Key_R, 29},
58 {Qt::Key_5, 30},
59 {Qt::Key_T, 31},
60 {Qt::Key_6, 32},
61 {Qt::Key_Y, 33},
62 {Qt::Key_7, 34},
63 {Qt::Key_U, 35},
64 {Qt::Key_I, 36},
65 {Qt::Key_9, 37},
66 {Qt::Key_O, 38},
67 {Qt::Key_0, 39},
68 {Qt::Key_P, 40}
69};
70
75#if defined(Q_OS_LINUX)
76 {94, 11},
77 {52, 12},
78 {39, 13},
79 {53, 14},
80 {40, 15},
81 {54, 16},
82 {55, 17},
83 {42, 18},
84 {56, 19},
85 {43, 20},
86 {57, 21},
87 {44, 22},
88 {58, 23},
89 {59, 24},
90 {46, 25},
91 {60, 26},
92 {47, 27},
93 {61, 28},
94
95 {24, 29},
96 {11, 30},
97 {25, 31},
98 {12, 32},
99 {26, 33},
100 {13, 34},
101 {27, 35},
102 {28, 36},
103 {15, 37},
104 {29, 38},
105 {16, 39},
106 {30, 40},
107 {31, 41},
108 {18, 42},
109 {32, 43},
110 {19, 44},
111 {33, 45},
112 {20, 46},
113 {34, 47},
114 {35, 48}
115#endif
116
117#if defined(Q_OS_WIN)
118 {86, 11},
119 {44, 12},
120 {31, 13},
121 {45, 14},
122 {32, 15},
123 {46, 16},
124 {47, 17},
125 {34, 18},
126 {48, 19},
127 {35, 20},
128 {49, 21},
129 {36, 22},
130 {50, 23},
131 {51, 24},
132 {38, 25},
133 {52, 26},
134 {39, 27},
135 {53, 28},
136
137 {16, 29},
138 {3, 30},
139 {17, 31},
140 {4, 32},
141 {18, 33},
142 {5, 34},
143 {19, 35},
144 {20, 36},
145 {7, 37},
146 {21, 38},
147 {8, 39},
148 {22, 40},
149 {23, 41},
150 {10, 42},
151 {24, 43},
152 {11, 44},
153 {25, 45},
154 {12, 46},
155 {26, 47},
156 {27, 48}
157#endif
158
159#if defined(Q_OS_MAC)
160 {50, 11},
161 {6, 12},
162 {1, 13},
163 {7, 14},
164 {2, 15},
165 {8, 16},
166 {9, 17},
167 {5, 18},
168 {11, 19},
169 {4, 20},
170 {45, 21},
171 {38, 22},
172 {46, 23},
173 {43, 24},
174 {37, 25},
175 {47, 26},
176 {41, 27},
177 {44, 28},
178
179 {12, 29},
180 {19, 30},
181 {13, 31},
182 {20, 32},
183 {14, 33},
184 {21, 34},
185 {15, 35},
186 {17, 36},
187 {22, 37},
188 {16, 38},
189 {26, 39},
190 {32, 40},
191 {34, 41},
192 {25, 42},
193 {31, 43},
194 {29, 44},
195 {35, 45},
196 {27, 46},
197 {33, 47},
198 {30, 48}
199#endif
200};
201
202class PianoKeybd::PianoKeybdPrivate {
203public:
204 PianoKeybdPrivate(): m_rotation(0), m_scene(nullptr), m_rawMap(nullptr)
205 { }
206 ~PianoKeybdPrivate() = default;
207
208 int m_rotation;
209 PianoScene *m_scene;
210 KeyboardMap *m_rawMap;
211};
212
220PianoKeybd::PianoKeybd(QWidget *parent)
221 : QGraphicsView(parent), d(new PianoKeybdPrivate())
222{
223 initialize();
225}
226
235PianoKeybd::PianoKeybd(const int baseOctave, const int numKeys, const int startKey, QWidget *parent)
236 : QGraphicsView(parent), d(new PianoKeybdPrivate)
237{
238 initialize();
239 initScene(baseOctave, numKeys, startKey);
240}
241
246{
247 d->m_scene->setRawKeyboardMode(false);
248 setKeyboardMap(nullptr);
249}
250
259{
260 return d->m_scene->getPianoHandler();
261}
262
272{
273 d->m_scene->setPianoHandler(handler);
274}
275
281{
282 return d->m_scene->getHighlightPalette();
283}
284
292{
293 d->m_scene->setHighlightPalette(p);
294}
295
301{
302 return d->m_scene->getBackgroundPalette();
303}
304
310{
311 d->m_scene->setBackgroundPalette(p);
312}
313
320{
321 return d->m_scene->getForegroundPalette();
322}
323
330{
331 d->m_scene->setForegroundPalette(p);
332}
333
339{
340 return d->m_scene->showColorScale();
341}
342
347void PianoKeybd::setShowColorScale(const bool show)
348{
349 d->m_scene->setShowColorScale(show);
350}
351
361void PianoKeybd::useCustomNoteNames(const QStringList &names)
362{
363 d->m_scene->useCustomNoteNames(names);
364}
365
371{
372 d->m_scene->useStandardNoteNames();
373}
374
380{
381 return d->m_scene->customNoteNames();
382}
383
389{
390 return d->m_scene->standardNoteNames();
391}
392
399{
400 d->m_scene->retranslate();
401}
402
410void PianoKeybd::initScene(int base, int num, int strt, const QColor& c)
411{
412 d->m_scene = new PianoScene(base, num, strt, c, this);
413 d->m_scene->setKeyboardMap(&g_DefaultKeyMap);
414 connect(d->m_scene, &PianoScene::noteOn, this, &PianoKeybd::noteOn);
415 connect(d->m_scene, &PianoScene::noteOff, this, &PianoKeybd::noteOff);
416 connect(d->m_scene, &PianoScene::signalName, this, &PianoKeybd::signalName);
417 setScene(d->m_scene);
418}
419
425{
426 setAttribute(Qt::WA_AcceptTouchEvents);
427 setAttribute(Qt::WA_InputMethodEnabled, false);
428 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
429 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
430 setCacheMode(CacheBackground);
431 setViewportUpdateMode(MinimalViewportUpdate);
432 setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing|QPainter::SmoothPixmapTransform);
433 setOptimizationFlag(DontClipPainter, true);
434 setOptimizationFlag(DontSavePainterState, true);
435 setOptimizationFlag(DontAdjustForAntialiasing, true);
436 setBackgroundBrush(QApplication::palette().window());
438}
439
445void PianoKeybd::resizeEvent(QResizeEvent *event)
446{
447 QGraphicsView::resizeEvent(event);
448 fitInView(d->m_scene->sceneRect(), Qt::KeepAspectRatio);
449}
450
457void PianoKeybd::setNumKeys(const int numKeys, const int startKey)
458{
459 if ( numKeys != d->m_scene->numKeys() || startKey != d->m_scene->startKey() )
460 {
461 int baseOctave = d->m_scene->baseOctave();
462 QColor color = d->m_scene->getKeyPressedColor();
463 PianoHandler* handler = d->m_scene->getPianoHandler();
464 KeyboardMap* keyMap = d->m_scene->getKeyboardMap();
465 PianoPalette highlighPalette = d->m_scene->getHighlightPalette();
466 PianoPalette backgroundPalette = d->m_scene->getBackgroundPalette();
467 PianoPalette foregroundPalette = d->m_scene->getForegroundPalette();
468 bool keyboardEnabled = d->m_scene->isKeyboardEnabled();
469 bool mouseEnabled = d->m_scene->isMouseEnabled();
470 bool touchEnabled = d->m_scene->isTouchEnabled();
471 bool showScale = d->m_scene->showColorScale();
472 LabelVisibility showLabels = d->m_scene->showLabels();
473 LabelAlteration alteration = d->m_scene->alterations();
474 LabelCentralOctave octave = d->m_scene->getOctave();
475 LabelOrientation orientation = d->m_scene->getOrientation();
476 QStringList customNames = d->m_scene->customNoteNames();
477 delete d->m_scene;
478 initScene(baseOctave, numKeys, startKey, color);
479 d->m_scene->setPianoHandler(handler);
480 d->m_scene->setKeyboardMap(keyMap);
481 d->m_scene->setHighlightPalette(highlighPalette);
482 d->m_scene->setBackgroundPalette(backgroundPalette);
483 d->m_scene->setForegroundPalette(foregroundPalette);
484 d->m_scene->setKeyboardEnabled(keyboardEnabled);
485 d->m_scene->setMouseEnabled(mouseEnabled);
486 d->m_scene->setTouchEnabled(touchEnabled);
487 d->m_scene->setShowColorScale(showScale);
488 d->m_scene->setShowLabels(showLabels);
489 d->m_scene->setAlterations(alteration);
490 d->m_scene->setOctave(octave);
491 d->m_scene->setOrientation(orientation);
492 if (customNames.isEmpty()) {
493 d->m_scene->useStandardNoteNames();
494 } else {
495 d->m_scene->useCustomNoteNames(customNames);
496 }
497 fitInView(d->m_scene->sceneRect(), Qt::KeepAspectRatio);
498 }
499}
500
506{
507 if (r != d->m_rotation) {
508 d->m_rotation = r;
509 resetTransform();
510 rotate(d->m_rotation);
511 fitInView(d->m_scene->sceneRect(), Qt::KeepAspectRatio);
512 }
513}
514
520{
521 return mapFromScene(sceneRect()).boundingRect().size();
522}
523
524// RAWKBD_SUPPORT
526{
527 if (d->m_scene->isKeyboardEnabled() && d->m_rawMap != nullptr && d->m_rawMap->contains(keycode)) {
528 d->m_scene->keyOn(d->m_rawMap->value(keycode));
529 return true;
530 }
531 return false;
532}
533
535{
536 if (d->m_scene->isKeyboardEnabled() && d->m_rawMap != nullptr && d->m_rawMap->contains(keycode)) {
537 d->m_scene->keyOff(d->m_rawMap->value(keycode));
538 return true;
539 }
540 return false;
541}
542
549{
550 return d->m_scene->baseOctave();
551}
552
557void PianoKeybd::setBaseOctave(const int baseOctave)
558{
559 d->m_scene->setBaseOctave(baseOctave);
560}
561
568{
569 return d->m_scene->numKeys();
570}
571
577{
578 return d->m_scene->startKey();
579}
580
586{
587 return d->m_rotation;
588}
589
595{
596 return d->m_scene->getKeyPressedColor();
597}
598
606{
607 d->m_scene->setKeyPressedColor(c);
608}
609
614{
615 d->m_scene->resetKeyPressedColor();
616}
617
624{
625 return d->m_scene->showLabels();
626}
627
634{
635 d->m_scene->setShowLabels(show);
636}
637
644{
645 return d->m_scene->alterations();
646}
647
654{
655 d->m_scene->setAlterations(use);
656}
657
664{
665 return d->m_scene->getOrientation();
666}
667
674{
675 d->m_scene->setOrientation(orientation);
676}
677
684{
685 return d->m_scene->getOctave();
686}
687
694{
695 d->m_scene->setOctave(octave);
696}
697
704{
705 return d->m_scene->getTranspose();
706}
707
714{
715 d->m_scene->setTranspose(t);
716}
717
723{
724 return d->m_scene->getChannel();
725}
726
731void PianoKeybd::setChannel(const int c)
732{
733 d->m_scene->setChannel(c);
734}
735
742{
743 return d->m_scene->getVelocity();
744}
745
751void PianoKeybd::setVelocity(const int v)
752{
753 d->m_scene->setVelocity(v);
754}
755
761{
762 return d->m_scene->isKeyboardEnabled();
763}
764
769void PianoKeybd::setKeyboardEnabled(const bool enable)
770{
771 d->m_scene->setKeyboardEnabled(enable);
772}
773
779{
780 return d->m_scene->isMouseEnabled();
781}
782
787void PianoKeybd::setMouseEnabled(const bool enable)
788{
789 d->m_scene->setMouseEnabled(enable);
790}
791
797{
798 return d->m_scene->isTouchEnabled();
799}
800
805void PianoKeybd::setTouchEnabled(const bool enable)
806{
807 d->m_scene->setTouchEnabled(enable);
808}
809
815{
816 return d->m_scene->velocityTint();
817}
818
823void PianoKeybd::setVelocityTint(const bool enable)
824{
825 d->m_scene->setVelocityTint(enable);
826}
827
832{
833 d->m_scene->allKeysOff();
834}
835
841{
842 d->m_scene->setKeyboardMap(m);
843}
844
850{
851 return d->m_scene->getKeyboardMap();
852}
853
858{
859 d->m_rawMap = &g_DefaultRawKeyMap;
860}
861
867{
868 return d->m_scene->getRawKeyboardMode();
869}
870
876{
877 d->m_scene->setRawKeyboardMode(b);
878}
879
884{
885 d->m_scene->setKeyboardMap(&g_DefaultKeyMap);
886}
887
893{
894 d->m_rawMap = m;
895}
896
902{
903 return d->m_rawMap;
904}
905
912void PianoKeybd::showNoteOn(const int note, QColor color, int vel)
913{
914 d->m_scene->showNoteOn(note, color, vel);
915}
916
922void PianoKeybd::showNoteOn(const int note, int vel)
923{
924 d->m_scene->showNoteOn(note, vel);
925}
926
932void PianoKeybd::showNoteOff(const int note, int vel)
933{
934 d->m_scene->showNoteOff(note, vel);
935}
936
941void PianoKeybd::setFont(const QFont &font)
942{
943 QWidget::setFont(font);
944 d->m_scene->setFont(font);
945 d->m_scene->refreshLabels();
946}
947
948} // namespace widgets
949} // namespace drumstick
The QGraphicsView class provides a widget for displaying the contents of a QGraphicsScene.
The PianoHandler class callbacks.
Definition pianokeybd.h:72
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.
virtual ~PianoKeybd()
Destructor.
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.
int baseOctave() const
Returns the base octave number.
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 ...
int numKeys() const
Returns the total number of keys.
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.
LabelCentralOctave labelOctave() const
Returns the octave label policy.
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.
LabelOrientation labelOrientation() const
Returns the labels orientation 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.
LabelVisibility showLabels() const
Returns the label visibility policy.
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 PianoScene class is a QGraphicsScene composed by a number of graphics items: the piano keys.
Definition pianoscene.h:46
void noteOff(int n, int v)
This signal is emitted for each Note Off MIDI event created using the computer keyboard,...
void signalName(const QString &name)
signalName is emitted for each note created, and contains a string with the MIDI note number and the ...
void noteOn(int n, int v)
This signal is emitted for each Note On MIDI event created using the computer keyboard,...
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
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
Drumstick common.
Piano Keyboard Widget.
PianoScene class declaration.