21 #include <QFontDatabase>
22 #include <QMouseEvent>
25 #include <QLinearGradient>
27 #include <QGraphicsTextItem>
28 #include <QGraphicsSimpleTextItem>
66 : QGraphicsView(parent),
68 mKeysGraphicsItems(0),
78 setWhatsThis(tr(
"This window displays the keyboard. Each key has a small indicator that will display the current recording state of this key."));
81 setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
85 new QShortcut(QKeySequence(Qt::Key_Left),
this, SLOT(
selectPrevious()));
86 new QShortcut(QKeySequence(Qt::Key_Right),
this, SLOT(
selectNext()));
87 new QShortcut(QKeySequence(Qt::Key_Escape),
this, SLOT(
deselectKey()));
89 setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
92 QSizePolicy sp(sizePolicy());
93 sp.setHeightForWidth(
true);
98 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
99 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
100 QScroller::grabGesture(
this, QScroller::LeftMouseButtonGesture);
128 QGraphicsView::showEvent(event);
136 bool wGrH = width() / height() >
mScene.width() /
mScene.height();
137 QSizePolicy sp(sizePolicy());
138 sp.setHeightForWidth(wGrH);
145 QGraphicsView::resizeEvent(event);
151 QGraphicsView::scrollContentsBy(dx, dy);
155 view->scene()->setSceneRect(visibleRect);
156 view->fitInView(visibleRect, Qt::IgnoreAspectRatio);
161 if (
mScene.sceneRect().width() == 0) {
168 QGraphicsView::mousePressEvent(event);
172 bool notifyListeners =
true;
185 QGraphicsView::mouseMoveEvent(event);
187 if (event->buttons() & Qt::LeftButton) {
196 QGraphicsView::keyPressEvent(event);
198 if (event->key() == Qt::Key_Return) {
204 switch (m->getType()) {
206 auto mksc(std::static_pointer_cast<MessageKeySelectionChanged>(m));
207 selectKey(mksc->getKeyNumber(), mksc->getKeyState(),
false);
209 scaledViews->highlightKey(mksc->getKeyNumber());
213 auto mpk(std::static_pointer_cast<MessagePreliminaryKey>(m));
218 auto mpf(std::static_pointer_cast<MessageProjectFile>(m));
219 mKeyboard = &mpf->getPiano().getKeyboard();
220 changeTotalNumberOfKeys(mpf->getPiano().getKeyboard().getNumberOfKeys(), mpf->getPiano().getKeyboard().getKeyNumberOfA4());
232 auto mkdc(std::static_pointer_cast<MessageKeyDataChanged>(m));
319 if (notifyMessageListeners) {
320 MessageHandler::send<MessageKeySelectionChanged>(key0,
mKeyboard->
getKeyPtr(key0), keyState);
374 return QPixmap(
":/media/images/status_unchecked.png");
376 return QPixmap(
":/media/images/status_checked.png");
395 return QColor(Qt::white);
397 return QColor(Qt::black).darker(350);
401 double frac = 12.0 / 7.0;
434 QLinearGradient shadowGradient(0, 0, 0,
KEY_HEIGHT);
435 shadowGradient.setSpread(QGradient::PadSpread);
436 shadowGradient.setColorAt(0.9, Qt::black);
437 shadowGradient.setColorAt(1, Qt::transparent);
444 keyFont.setPixelSize(12);
451 borderpen.setCosmetic(
true);
459 pixmapItem->setTransformationMode(Qt::SmoothTransformation);
467 textColor = Qt::darkGray;
473 textColor = Qt::black;
475 textColor = Qt::lightGray;
483 QPointF pos(shape.topLeft());
484 shape.setTopLeft(QPointF(0, 0));
488 item->setBrush(QBrush(keyColor));
491 item->setPen(borderpen);
511 Qt::AspectRatioMode arm = Qt::KeepAspectRatioByExpanding;
512 fitInView(
mScene.sceneRect(), arm);
518 QGraphicsRectItem *selectedItem(
nullptr);
520 QList<QGraphicsItem *> itemList(items(pos));
521 for (QGraphicsItem *item : itemList) {
522 if (item->type() == QGraphicsRectItem::Type) {
523 if (!selectedItem || item->zValue() > selectedItem->zValue()) {
525 selectedItem =
dynamic_cast<QGraphicsRectItem*
>(item);
529 if (!selectedItem) {
return -1;}
556 QPointF topLeft = mapToScene(0, 0);
558 QPointF bottomRight = mapToScene(width(), height());
561 return QRectF(topLeft, bottomRight);
bool isRecorded() const
Get recorded flag.
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.
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.
called when the recording was cleared
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.
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.
static const qreal KEY_BLACK_TO_WHITE_RATIO
Size ratio of black and white keys.
const Key & at(size_t i) const
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)
Message that a change was made with the current project file.
static const qreal PEN_MEDIUM_LINE
Medium pen line width.
KeyboardGraphicsView(QWidget *parent)
Constructor.
void deselectKey(bool notifyListeners=true)
Slot to deselect a key.
int getKeyAtPosition(const QPoint &pos)
Function to get the key index at the given position.
static void initShapes(qreal b_w, qreal b_h, qreal w_w, qreal w_h)
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.
const Key * getKeyPtr(int i) const
Get pointer to a key with a given number, returning nullptr if the number is out of range (read-only ...
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.
Message that a key has been selected.
void setKeyboard(const Keyboard *kb)
Setter for mKeyboard.
KeyColor
The KeyColor enum.
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.
std::string getNoteName(int keynumber) const
Get the name of the key as a string.
int8_t mCenterOnKey
Index of the key to center on.
QRectF keyShape(int8_t key0)
Returns a QRectF as shape of the given key.
static const qreal TOTAL_HEIGHT
Total height of the view.
number of the key recognized during recording
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.
int getKeyNumberOfA4() const
void selectPrevious()
Slot that will select the previous key.
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.
void setFont(const QFont &font)
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.
static const std::unique_ptr< DisplaySizeDefines > & getSingleton()
int getNumberOfKeys() const
analysis of the signal ended
piano::KeyColor getKeyColor(int keynumber) const
Get the color of the key (black / white)
KeyState
The KeyState enum.
static const qreal KEY_HEIGHT
Height of a single key.
QRectF getVisibleContentsRect() const