drumstick 2.7.2
pianokey.cpp
Go to the documentation of this file.
1/*
2 Virtual Piano Widget for Qt
3 Copyright (C) 2008-2022, 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 <QApplication>
20#include <QPainter>
21#include <QPalette>
23
24#include "pianokey.h"
25
31namespace drumstick { namespace widgets {
32
33const PianoPalette PianoKey::keyPalette(PAL_KEYS);
34
35PianoKey::PianoKey(const QRectF &rect, const bool black, const int note)
36 : QGraphicsRectItem(rect),
37 m_pressed(false),
38 m_note(note),
39 m_black(black),
40 m_usePixmap(true)
41{
42 m_brush = keyPalette.getColor(black ? 1 : 0);
43 setAcceptedMouseButtons(Qt::NoButton);
44}
45
46void PianoKey::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
47{
48 static const QPen blackPen(Qt::black, 1);
49 painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
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(), 20, 15, Qt::RelativeSize);
61 if (m_usePixmap) {
62 QPixmap p = getPixmap();
63 painter->drawPixmap(rect(), p, p.rect());
64 }
65}
66
67void PianoKey::setPressed(bool p)
68{
69 if (p != m_pressed) {
70 m_pressed = p;
71 update();
72 }
73}
74
75const QPixmap& PianoKey::getPixmap() const
76{
77 static QPixmap blpixmap(QStringLiteral(":/vpiano/blkey.png"));
78 static QPixmap whpixmap(QStringLiteral(":/vpiano/whkey.png"));
79 static QColor bgColor;
80 if (!m_black && (bgColor != m_brush.color())) {
81 bgColor = m_brush.color();
82 paintPixmap(whpixmap, QColor::fromRgba(bgColor.rgba()^0xffffff));
83 }
84 if (m_pixmap.isNull()) {
85 return m_black ? blpixmap : whpixmap;
86 } else {
87 return m_pixmap;
88 }
89}
90
91QRectF PianoKey::pixmapRect() const
92{
93 return getPixmap().rect();
94}
95
96void PianoKey::resetBrush()
97{
98 m_brush = keyPalette.getColor(m_black ? 1 : 0);
99}
100
101void PianoKey::setPixmap(const QPixmap &p)
102{
103 m_pixmap = p;
104}
105
106bool PianoKey::getUsePixmap() const
107{
108 return m_usePixmap;
109}
110
111void PianoKey::setUsePixmap(bool usePixmap)
112{
113 m_usePixmap = usePixmap;
114}
115
116void PianoKey::paintPixmap(QPixmap &pixmap, const QColor& color) const
117{
118 if (!pixmap.isNull()) {
119 QPainter painter(&pixmap);
120 painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing);
121 painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
122 painter.fillRect(pixmap.rect(), color);
123 }
124}
125
126} // namespace widgets
127} // 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.