File: pianokey.cpp

package info (click to toggle)
libdrumstick 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,876 kB
  • sloc: cpp: 25,685; xml: 122; sh: 14; makefile: 5
file content (128 lines) | stat: -rw-r--r-- 3,471 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
    Virtual Piano Widget for Qt
    Copyright (C) 2008-2024, Pedro Lopez-Cabanillas <plcl@users.sf.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along 
    with this program; If not, see <http://www.gnu.org/licenses/>.
*/

#include <QApplication>
#include <QPainter>
#include <QPalette>
#include <drumstick/pianopalette.h>

#include "pianokey.h"

/**
 * @file pianokey.cpp
 * Implementation of the PianoKey class
 */

namespace drumstick { namespace widgets {

const PianoPalette PianoKey::keyPalette(PAL_KEYS);

PianoKey::PianoKey(const QRectF &rect, const bool black, const int note)
    : QGraphicsRectItem(rect),
    m_pressed(false),
    m_note(note),
    m_black(black),
    m_usePixmap(true)
{
    m_brush = keyPalette.getColor(black ? 1 : 0);
    setAcceptedMouseButtons(Qt::NoButton);
    setFlag(QGraphicsItem::ItemClipsChildrenToShape);
}

void PianoKey::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    static const QPen blackPen(Qt::black, 1);
    painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
    if (m_pressed) {
        if (m_selectedBrush.style() != Qt::NoBrush) {
            painter->setBrush(m_selectedBrush);
        } else {
            painter->setBrush(QApplication::palette().highlight());
        }
    } else {
        painter->setBrush(m_brush);
    }
    painter->setPen(blackPen);
    painter->drawRoundedRect(rect(), 20, 15, Qt::RelativeSize);
    if (m_usePixmap) {
        QPixmap p = getPixmap();
        painter->drawPixmap(rect(), p, p.rect());
    }
}

void PianoKey::setPressed(bool p)
{
    if (p != m_pressed) {
        m_pressed = p;
        update();
    }
}

const QPixmap& PianoKey::getPixmap() const
{
    static QPixmap blpixmap(QStringLiteral(":/vpiano/blkey.png"));
    static QPixmap whpixmap(QStringLiteral(":/vpiano/whkey.png"));
    static QColor bgColor;
    if (!m_black && (bgColor != m_brush.color())) {
        bgColor = m_brush.color();
        paintPixmap(whpixmap, QColor::fromRgba(bgColor.rgba()^0xffffff));
    }
    if (m_pixmap.isNull()) {
        return m_black ? blpixmap : whpixmap;
    } else {
        return m_pixmap;
    }
}

QRectF PianoKey::pixmapRect() const
{
    return getPixmap().rect();
}

void PianoKey::resetBrush()
{
    m_brush = keyPalette.getColor(m_black ? 1 : 0);
}

void PianoKey::setPixmap(const QPixmap &p)
{
    m_pixmap = p;
}

bool PianoKey::getUsePixmap() const
{
    return m_usePixmap;
}

void PianoKey::setUsePixmap(bool usePixmap)
{
    m_usePixmap = usePixmap;
}

void PianoKey::paintPixmap(QPixmap &pixmap, const QColor& color) const
{
    if (!pixmap.isNull()) {
        QPainter painter(&pixmap);
        painter.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing);
        painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
        painter.fillRect(pixmap.rect(), color);
    }
}

} // namespace widgets
} // namespace drumstick