drumstick 2.10.0
C++ MIDI libraries using Qt objects, idioms, and style.
pianokey.cpp
Go to the documentation of this file.
1/*
2 Virtual Piano Widget for Qt
3 Copyright (C) 2008-2024, 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 setFlag(QGraphicsItem::ItemClipsChildrenToShape);
45}
46
47void PianoKey::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
48{
49 static const QPen blackPen(Qt::black, 1);
50 painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
51 if (m_pressed) {
52 if (m_selectedBrush.style() != Qt::NoBrush) {
53 painter->setBrush(m_selectedBrush);
54 } else {
55 painter->setBrush(QApplication::palette().highlight());
56 }
57 } else {
58 painter->setBrush(m_brush);
59 }
60 painter->setPen(blackPen);
61 painter->drawRoundedRect(rect(), 20, 15, Qt::RelativeSize);
62 if (m_usePixmap) {
63 QPixmap p = getPixmap();
64 painter->drawPixmap(rect(), p, p.rect());
65 }
66}
67
68void PianoKey::setPressed(bool p)
69{
70 if (p != m_pressed) {
71 m_pressed = p;
72 update();
73 }
74}
75
76const QPixmap& PianoKey::getPixmap() const
77{
78 static QPixmap blpixmap(QStringLiteral(":/vpiano/blkey.png"));
79 static QPixmap whpixmap(QStringLiteral(":/vpiano/whkey.png"));
80 static QColor bgColor;
81 if (!m_black && (bgColor != m_brush.color())) {
82 bgColor = m_brush.color();
83 paintPixmap(whpixmap, QColor::fromRgba(bgColor.rgba()^0xffffff));
84 }
85 if (m_pixmap.isNull()) {
86 return m_black ? blpixmap : whpixmap;
87 } else {
88 return m_pixmap;
89 }
90}
91
92QRectF PianoKey::pixmapRect() const
93{
94 return getPixmap().rect();
95}
96
97void PianoKey::resetBrush()
98{
99 m_brush = keyPalette.getColor(m_black ? 1 : 0);
100}
101
102void PianoKey::setPixmap(const QPixmap &p)
103{
104 m_pixmap = p;
105}
106
107bool PianoKey::getUsePixmap() const
108{
109 return m_usePixmap;
110}
111
112void PianoKey::setUsePixmap(bool usePixmap)
113{
114 m_usePixmap = usePixmap;
115}
116
117void PianoKey::paintPixmap(QPixmap &pixmap, const QColor& color) const
118{
119 if (!pixmap.isNull()) {
120 QPainter painter(&pixmap);
121 painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing);
122 painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
123 painter.fillRect(pixmap.rect(), color);
124 }
125}
126
127} // namespace widgets
128} // namespace drumstick
@ PAL_KEYS
Two background colors (naturals/alterations)
Definition: pianopalette.h:60
Drumstick common.
Definition: alsaclient.cpp:71
Declaration of the PianoKey class.
Piano Palette declarations.