drumstick 2.3.1
pianokey.cpp
Go to the documentation of this file.
1/*
2 Virtual Piano Widget for Qt5
3 Copyright (C) 2008-2021, 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
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 m_usePixmap(true)
40{
41 m_brush = keyPalette.getColor(black ? 1 : 0);
42 setAcceptedMouseButtons(Qt::NoButton);
43}
44
45void PianoKey::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
46{
47 static const QPen blackPen(Qt::black, 1);
48 painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
49 if (m_pressed) {
50 if (m_selectedBrush.style() != Qt::NoBrush) {
51 painter->setBrush(m_selectedBrush);
52 } else {
53 painter->setBrush(QApplication::palette().highlight());
54 }
55 } else {
56 painter->setBrush(m_brush);
57 }
58 painter->setPen(blackPen);
59 painter->drawRoundedRect(rect(), 20, 15, Qt::RelativeSize);
60 if (m_usePixmap) {
61 painter->drawPixmap(rect(), getPixmap(), pixmapRect());
62 }
63}
64
65void PianoKey::setPressed(bool p)
66{
67 if (p != m_pressed) {
68 m_pressed = p;
69 update();
70 }
71}
72
73const QPixmap& PianoKey::getPixmap() const
74{
75 static QPixmap blpixmap(QStringLiteral(":/vpiano/blkey.png"));
76 static QPixmap whpixmap(QStringLiteral(":/vpiano/whkey.png"));
77 if (m_pixmap.isNull()) {
78 return m_black ? blpixmap : whpixmap;
79 } else {
80 return m_pixmap;
81 }
82}
83
84QRectF PianoKey::pixmapRect() const
85{
86 return getPixmap().rect();
87}
88
89void PianoKey::resetBrush()
90{
91 m_brush = keyPalette.getColor(m_black ? 1 : 0);
92}
93
94void PianoKey::setPixmap(const QPixmap &p)
95{
96 m_pixmap = p;
97}
98
99bool PianoKey::getUsePixmap() const
100{
101 return m_usePixmap;
102}
103
104void PianoKey::setUsePixmap(bool usePixmap)
105{
106 m_usePixmap = usePixmap;
107}
108
109} // namespace widgets
110} // namespace drumstick
@ PAL_KEYS
Two background colors (naturals/alterations)
Definition: pianopalette.h:50
Drumstick common.
Definition: alsaclient.cpp:68
Declaration of the PianoKey class.
Piano Palette declarations.