drumstick 2.0.0
pianokey.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 "pianokey.h"
20#include <QApplication>
21#include <QPainter>
22#include <QPalette>
24
29
30namespace drumstick { namespace widgets {
31
32const PianoPalette PianoKey::keyPalette(PAL_KEYS);
33
34PianoKey::PianoKey(const QRectF &rect, const bool black, const int note)
35 : QGraphicsRectItem(rect),
36 m_pressed(false),
37 m_note(note),
38 m_black(black)
39{
40 m_brush = keyPalette.getColor(black ? 1 : 0);
41 setAcceptedMouseButtons(Qt::NoButton);
42}
43
44void PianoKey::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
45{
46 static const QPen blackPen(Qt::black, 1);
47 static const QPen grayPen(QBrush(Qt::gray), 1, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
48 painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
49
50 if (m_pressed) {
51 if (m_selectedBrush.style() != Qt::NoBrush) {
52 painter->setBrush(m_selectedBrush);
53 } else {
54 painter->setBrush(QApplication::palette().highlight());
55 }
56 } else {
57 painter->setBrush(m_brush);
58 }
59 painter->setPen(blackPen);
60 painter->drawRoundedRect(rect(), 15, 15, Qt::RelativeSize);
61 if (m_black) {
62 painter->drawPixmap(rect(), getPixmap(), pixmapRect());
63 } else {
64 QPointF points[3] = {
65 QPointF(rect().left()+1.5, rect().bottom()-1),
66 QPointF(rect().right()-1, rect().bottom()-1),
67 QPointF(rect().right()-1, rect().top()+1),
68 };
69 painter->setPen(grayPen);
70 painter->drawPolyline(points, 3);
71 }
72}
73
74void PianoKey::setPressed(bool p)
75{
76 if (p != m_pressed) {
77 m_pressed = p;
78 update();
79 }
80}
81
82QPixmap& PianoKey::getPixmap() const
83{
84 static QPixmap pixmap(QStringLiteral(":/vpiano/blkey.png"));
85 return pixmap;
86}
87
88QRectF PianoKey::pixmapRect() const
89{
90 return getPixmap().rect();
91}
92
93void PianoKey::resetBrush()
94{
95 m_brush = keyPalette.getColor(m_black ? 1 : 0);
96}
97
98} // namespace widgets
99} // namespace drumstick
The PianoPalette class.
@ PAL_KEYS
Two background colors (naturals/alterations).
Drumstick Widgets library MIDI related widgets and functions.
Drumstick common.
Declaration of the PianoKey class.
Piano Palette declarations.