Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
keyboardgraphicsview.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 #ifndef KEYBOARDGRAPHICSVIEW_H
21 #define KEYBOARDGRAPHICSVIEW_H
22 
23 #include <QGraphicsView>
24 #include <QGraphicsScene>
25 #include <QGraphicsRectItem>
26 #include <QGraphicsPixmapItem>
27 #include "../core/system/log.h"
28 #include "../core/piano/keyboard.h"
29 #include "../core/messages/messagelistener.h"
30 
32 
47 class KeyboardGraphicsView : public QGraphicsView, public MessageListener
48 {
49  Q_OBJECT
50 public:
51  // drawing constants
52  static const qreal GLOBAL_SCALING;
53  static const qreal PEN_THIN_LINE;
54  static const qreal PEN_MEDIUM_LINE;
55  static const qreal KEY_WIDTH;
56  static const qreal KEY_HEIGHT;
57  static const qreal KEY_SPACING_TO_MARKERS;
58  static const qreal KEY_MARKERS_MARGIN;
59  static const qreal TOTAL_HEIGHT;
60  static const qreal KEY_BLACK_TO_WHITE_RATIO;
61  static const qreal KEY_WHITE_KEY_SIZE;
62 
63  // color constants
64  static const QColor KEY_PRELIMINARY_COLOR;
65  static const QColor KEY_SELECTED_RECORDED_COLOR;
66  static const QColor KEY_SELECTED_UNRECORDED_COLOR;
67  static const qreal KEY_FORCED_FACTOR;
68  static const QColor KEY_PRELIMINARY_AND_SELECTED_COLOR;
69 
70  static const int NO_KEY = -1;
71 
72 
73 private:
76 
77 public:
84  KeyboardGraphicsView(QWidget *parent);
86 
92  const QGraphicsScene &getScene() const {return mScene;}
93 
99  void setKeyboard(const Keyboard *kb);
100 
106  const Keyboard *getKeyboard() const {return mKeyboard;}
107 
113  int8_t getSelectedKeyIndex() const {return mSelectedKey;}
114 
115  const Key *getSelectedKey() const {if (mSelectedKey < 0) {return nullptr;} return &mKeyboard->at(mSelectedKey);}
116 
123 
129  int8_t getPreliminaryKey() const {return mPreliminaryKey;}
130 
138  void setSelection(int key, KeyState state, int preliminary);
139 
145  void centerOnKey(int8_t key);
146 
153 
164  void selectKey(int8_t key0, KeyState keyState, bool notifyMessageListeners = true);
165 
166  QRectF getVisibleContentsRect() const;
167 
168 protected:
169  virtual QSize sizeHint() const override final {return QSize(0, 200);}
170  virtual QSize minimumSizeHint() const override final {return QSize(0, 100);}
176  void showEvent(QShowEvent *event) override;
177 
183  void resizeEvent(QResizeEvent *event) override;
184 
185  virtual void scrollContentsBy(int dx, int dy) override final;
186 
194  void mousePressEvent(QMouseEvent *event) override;
195 
196  void mouseMoveEvent(QMouseEvent *event) override;
197 
204  void keyPressEvent(QKeyEvent *event) override;
205 
211  void handleMessage(MessagePtr m) override;
212 
219  int heightForWidth( int w ) const override;
220 
221 private:
227  void resetKey(int8_t key);
228 
236  void setKeyColor(int8_t key);
237 
243  void selectPreliminaryKey(int8_t key0);
244 
250  void redrawAllMarkers();
251 
259  void updateColorMarker(int8_t key0);
260 
267  QPixmap getPixmapForKeyRecordingState(bool recorded);
268 
278  QColor getKeyColor(piano::KeyColor color, bool selected, KeyState keyState, bool recorded);
279 
286  QRectF keyShape(int8_t key0);
287 
295  void changeTotalNumberOfKeys(int keys, int keyA);
296 
301  void fitAllInView();
302 
311  int getKeyAtPosition(const QPoint &pos);
312 
313 signals:
319  void selectionChanged(int8_t key);
320 
321 private slots:
326  void selectNext();
327 
332  void selectPrevious();
333 
338  void deselectKey(bool notifyListeners = true);
339 
340 private:
341  int mResizeBlocked = 0;
344 
346  QGraphicsScene mScene;
347 
349  std::vector<QGraphicsPixmapItem *> mMarkerPixmapItems;
350 
352  std::vector<QGraphicsRectItem *> mKeysGraphicsItems;
353 
354 
361  int8_t mCenterOnKey;
362 
364  int8_t mSelectedKey;
365 
368 
371 
374 
376  QList<AutoScaledToKeyboardGraphicsView*> mAutoScaledGraphicsViews;
377 };
378 
379 #endif // KEYBOARDGRAPHICSVIEW_H
void mouseMoveEvent(QMouseEvent *event) override
void redrawAllMarkers()
Force all markers to update its state.
std::shared_ptr< Message > MessagePtr
Global type of a shared message pointer.
Definition: message.h:98
const Key * getSelectedKey() const
KeyState mSelectedKeyState
The key state of the selected key.
void changeTotalNumberOfKeys(int keys, int keyA)
Function to change the total number of keys and the key number of A.
static const QColor KEY_SELECTED_UNRECORDED_COLOR
Color of the selected key if it is not recorded.
static const QColor KEY_SELECTED_RECORDED_COLOR
Color of the selected key if it is recorded.
int8_t getSelectedKeyIndex() const
Getter for mSelectedKey.
const Keyboard * getKeyboard() const
Getter for mKeyboard.
static const QColor KEY_PRELIMINARY_COLOR
Color of the preliminary key.
void setKeyColor(int8_t key)
Function to set the key color of key.
void handleMessage(MessagePtr m) override
Message handling.
Class describing the piano keyboard, holding a collection of keys.
Definition: keyboard.h:39
int mKeyNumberOfA
The keynumber of A of the given mKeyboard.
const Keyboard * mKeyboard
Pointer to the keyboard that is displayed.
static const int NO_KEY
No key selection constant.
virtual QSize sizeHint() const overridefinal
static const qreal KEY_BLACK_TO_WHITE_RATIO
Size ratio of black and white keys.
const Key & at(size_t i) const
Definition: keyboard.h:61
void mousePressEvent(QMouseEvent *event) override
The mouse press will select the klicked key.
virtual void scrollContentsBy(int dx, int dy) overridefinal
std::vector< QGraphicsRectItem * > mKeysGraphicsItems
List of all key items.
static const qreal KEY_WIDTH
Width of a single key.
static const qreal KEY_WHITE_KEY_SIZE
Size of a white key (a bit bigger than KEY_WIDTH)
static const qreal PEN_MEDIUM_LINE
Medium pen line width.
KeyboardGraphicsView(QWidget *parent)
Constructor.
void deselectKey(bool notifyListeners=true)
Slot to deselect a key.
Class describing a single piano key.
Definition: key.h:45
int getKeyAtPosition(const QPoint &pos)
Function to get the key index at the given position.
static const QColor KEY_PRELIMINARY_AND_SELECTED_COLOR
Color if the preliminary key equates the selected key.
QList< AutoScaledToKeyboardGraphicsView * > mAutoScaledGraphicsViews
List of other graphic views that are scaled along this keyboard view.
void selectKey(int8_t key0, KeyState keyState, bool notifyMessageListeners=true)
Function to select or deselect a key.
static const qreal KEY_FORCED_FACTOR
Color scaling if the key is forced.
void resizeEvent(QResizeEvent *event) override
Reimplemented to keep the complete scene rect in the view.
void selectionChanged(int8_t key)
Signal beeing emitted if the selected key changed.
void resetKey(int8_t key)
Function to reset the key and marker color to the unselected state.
std::vector< QGraphicsPixmapItem * > mMarkerPixmapItems
List of all marker items.
static const qreal PEN_THIN_LINE
Thin pen line width.
int8_t mSelectedKey
The selected key.
int8_t mPreliminaryKey
The preliminary key.
void setKeyboard(const Keyboard *kb)
Setter for mKeyboard.
KeyColor
The KeyColor enum.
Definition: pianodefines.h:51
void setSelection(int key, KeyState state, int preliminary)
Function to set the current selection.
int heightForWidth(int w) const override
Get the height for a given view width.
void centerOnKey(int8_t key)
This function will center the view on the given key.
int8_t mCenterOnKey
Index of the key to center on.
void addAutoScaledGraphicsView(AutoScaledToKeyboardGraphicsView *view)
Function to add a AutoScaledToKeyboardGraphicsView.
QRectF keyShape(int8_t key0)
Returns a QRectF as shape of the given key.
KeyState getSelectedKeyState() const
Getter for mSelectedKeyState.
static const qreal TOTAL_HEIGHT
Total height of the view.
void showEvent(QShowEvent *event) override
Reimplemented to keep the complete scene rect in the view.
void selectNext()
Slot that will select the next key.
static const qreal KEY_SPACING_TO_MARKERS
Space between keys and markers.
void keyPressEvent(QKeyEvent *event) override
Handler for key presses.
void updateColorMarker(int8_t key0)
Function to update a single color marker.
void selectPrevious()
Slot that will select the previous key.
const QGraphicsScene & getScene() const
Getter for mScene.
static const qreal KEY_MARKERS_MARGIN
Margin of a single marker.
void fitAllInView()
Fit the complete scene rect in the given view.
QGraphicsScene mScene
The QGraphicsScene in which the elements will be drawed.
void selectPreliminaryKey(int8_t key0)
Function to change the prelimary key stored in mPreliminaryKey.
Viewer for the keyboard.
An GraphicsViewAdaptorForQt keeping the same size as a KeyboardGraphicsView.
static const qreal GLOBAL_SCALING
A global scaling factor.
QPixmap getPixmapForKeyRecordingState(bool recorded)
Function to get the QPixmap for the given recording state.
QColor getKeyColor(piano::KeyColor color, bool selected, KeyState keyState, bool recorded)
Function to get the key color.
KeyState
The KeyState enum.
Definition: pianodefines.h:40
static const qreal KEY_HEIGHT
Height of a single key.
virtual QSize minimumSizeHint() const overridefinal
int8_t getPreliminaryKey() const
Getter for mPreliminaryKey.
QRectF getVisibleContentsRect() const