drumstick 2.9.0
C++ MIDI libraries using Qt objects, idioms, and style.
keylabel.cpp
Go to the documentation of this file.
1/*
2 Virtual Piano Widget for Qt
3 Copyright (C) 2008-2023, 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 "keylabel.h"
20#include "pianokey.h"
21#include <QFont>
22
28namespace drumstick { namespace widgets {
29
30KeyLabel::KeyLabel(QGraphicsItem *parent) : QGraphicsTextItem(parent)
31{
32 setAcceptedMouseButtons(Qt::NoButton);
33}
34
35void KeyLabel::adjust()
36{
37 qreal ax, ay;
38 QRectF kr, br;
39 PianoKey* key = static_cast<PianoKey*>(parentItem());
40 kr = key->boundingRect();
41 br = boundingRect();
42 ax = kr.x();
43 ay = kr.height() - 5;
44 if (key->isBlack()) {
45 ay -= 70;
46 }
47 if (rotation() == 0) {
48 ax += (kr.width() - br.width()) / 2;
49 ay -= br.height();
50 } else {
51 ax += (kr.width() - br.height()) / 2;
52 }
53 setPos(ax, ay);
54 m_savedColor = defaultTextColor();
55}
56
57void KeyLabel::setOrientation(LabelOrientation ori)
58{
59 if (m_orientation != ori) {
60 m_orientation = ori;
61 switch(m_orientation) {
63 setRotation(270);
64 break;
66 setRotation(0);
67 break;
69 default:
70 calculateRotation();
71 break;
72 }
73 }
74}
75
76void KeyLabel::restoreColor()
77{
78 if (m_savedColor.isValid()) {
79 setDefaultTextColor(m_savedColor);
80 }
81}
82
83void drumstick::widgets::KeyLabel::calculateRotation()
84{
85 PianoKey* key = static_cast<PianoKey*>(parentItem());
86 QRectF kr, br;
87 kr = key->boundingRect();
88 br = boundingRect();
89 if (br.width() > kr.width()) {
90 setRotation(270);
91 } else {
92 setRotation(0);
93 }
94}
95
96void KeyLabel::setPlainText(const QString &text)
97{
98 QGraphicsTextItem::setPlainText(text);
99 adjustSize();
100 if (m_orientation == AutomaticOrientation) {
101 calculateRotation();
102 }
103}
104
105void KeyLabel::setHtml(const QString &text)
106{
107 QGraphicsTextItem::setHtml(text);
108 adjustSize();
109 if (m_orientation == AutomaticOrientation) {
110 calculateRotation();
111 }
112}
113
114} // namespace widgets
115} // namespace drumstick
@ AutomaticOrientation
Show horizonal or vertical names depending on the size.
Definition: pianokeybd.h:144
@ VerticalOrientation
Show vertical names.
Definition: pianokeybd.h:143
@ HorizontalOrientation
Show horizontal names.
Definition: pianokeybd.h:142
Declaration of the KeyLabel class.
Drumstick common.
Definition: alsaclient.cpp:68
Declaration of the PianoKey class.