1 /* TIATracker, (c) 2016 Andre "Kylearan" Wichmann.
2  * Website: https://bitbucket.org/kylearan/tiatracker
3  * Email: andre.wichmann@gmx.de
4  * See the file "license.txt" for information on usage and redistribution
5  * of this file.
6  */
7 
8 #ifndef GUIDEKEYBOARD_H
9 #define GUIDEKEYBOARD_H
10 
11 #include <QObject>
12 #include <QWidget>
13 
14 #include "tiasound/tiasound.h"
15 #include "tiasound/instrumentpitchguide.h"
16 
17 class GuideKeyboard : public QWidget
18 {
19     Q_OBJECT
20 public:
21     // Statistics about keys
22     static const int numOctaves = 7;
23     static const int numWhiteKeysPerOctave = 7;
24     static const int numKeysPerOctave = 12;
25     static const int numWhiteKeys = numOctaves*7;
26     static const int numKeys = numOctaves*numKeysPerOctave;
27     // GFX constants
28     static const int keyWidth = 20;
29     static const int keyHeight = 110;
30     static const int blackKeyWidth = keyWidth-4;
31     static const int blackKeyHeight = int(0.6*keyHeight);
32     static const int keyboardWidth = keyWidth*numWhiteKeys;
33     static const int keyboardHeight = keyHeight;
34 
35     // Key states and pitch info
36     struct KeyInformation {
37         int frequency;
38         TiaSound::Note note;
39         int off;
40         bool isEnabled;
41     };
42 
43     KeyInformation keyInfo[numKeys]{};
44 
45     explicit GuideKeyboard(QWidget *parent = 0);
46 
47     void setInstrumentPitchGuide(TiaSound::InstrumentPitchGuide *pitchGuide, int threshold);
48     void removeGuide();
49 
50 signals:
51 
52 public slots:
53 
54 protected:
55     void paintEvent(QPaintEvent *) Q_DECL_OVERRIDE;
56 
57     // color and pos data for keys of an octave
58     struct KeyGfxTrait {
59         bool isBlack;
60         int posIndex;   // with regards to white or black row
61     };
62     static const KeyGfxTrait octaveTraits[];
63 
64     void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
65 
66 private:
67     int offThreshold;
68 
69     // If a valid key is pressed with the mouse
70     bool isValidKeyPressed = false;
71     // Index of key that is currently pressed
72     int keyPressed = -1;
73 
74     // Key hints
75     static const int keyFontSize = 10;
76 
77     QFont keyFont{"Helvetica"};
78     int keyFontHeight;
79     int keyInfoRectHeight;
80 
81     bool usePitchGuide = false;
82 
83     /* Calc x-pos for a given white or black key (0..numKeys) */
84     int calcWhiteKeyXPos(int key);
85     int calcBlackKeyXPos(int key);
86 
87     /* Calc the key index for a white key for a given x coordinate */
88     int calcKeyIndexForWhiteKey(int xPos);
89 };
90 
91 #endif // GUIDEKEYBOARD_H
92