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 "keylabel.h" 20 #include "pianokey.h" 21 #include <QFont> 22 23 /** 24 * @file keylabel.cpp 25 * Implementation of the KeyLabel class 26 */ 27 28 namespace drumstick { namespace widgets { 29 KeyLabel(QGraphicsItem * parent)30KeyLabel::KeyLabel(QGraphicsItem *parent) : QGraphicsTextItem(parent) 31 { 32 setAcceptedMouseButtons(Qt::NoButton); 33 } 34 adjust()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 setOrientation(LabelOrientation ori)57void KeyLabel::setOrientation(LabelOrientation ori) 58 { 59 if (m_orientation != ori) { 60 m_orientation = ori; 61 switch(m_orientation) { 62 case VerticalOrientation: 63 setRotation(270); 64 break; 65 case HorizontalOrientation: 66 setRotation(0); 67 break; 68 case AutomaticOrientation: 69 default: 70 calculateRotation(); 71 break; 72 } 73 } 74 } 75 restoreColor()76void KeyLabel::restoreColor() 77 { 78 if (m_savedColor.isValid()) { 79 setDefaultTextColor(m_savedColor); 80 } 81 } 82 calculateRotation()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 setPlainText(const QString & text)96void KeyLabel::setPlainText(const QString &text) 97 { 98 QGraphicsTextItem::setPlainText(text); 99 adjustSize(); 100 if (m_orientation == AutomaticOrientation) { 101 calculateRotation(); 102 } 103 } 104 105 } // namespace widgets 106 } // namespace drumstick 107