Entropy Piano Tuner  1.1.3 (documentation not yet complete)
An open-source experimental software for piano tuning by entropy minimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
graphicskeyitem.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright 2015 Haye Hinrichsen, Christoph Wick
3  *
4  * This file is part of Entropy Piano Tuner.
5  *
6  * Entropy Piano Tuner is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * Entropy Piano Tuner is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14  * more details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * Entropy Piano Tuner. If not, see http://www.gnu.org/licenses/.
18  *****************************************************************************/
19 
20 #include "graphicskeyitem.h"
21 #include <QPainter>
22 #include <QPainterPath>
23 #include <QFontDatabase>
24 #include <QTextLayout>
25 
26 
27 const std::array<qreal, GraphicsKeyItem::KC_COUNT> KEY_RADIUS_FACTOR {{0.18, 0.1}};
28 
29 const std::array<std::array<qreal, 4>, GraphicsKeyItem::KC_COUNT> MARGINS {{ {{0, 0, 0, 0}}, {{0.5, 0, -0.5, 0}} }};
30 
31 std::array<QPainterPath, GraphicsKeyItem::KC_COUNT> GraphicsKeyItem::mKeyShapes;
32 std::array<QRectF, GraphicsKeyItem::KC_COUNT> GraphicsKeyItem::mKeyRects;
33 
34 void GraphicsKeyItem::initShapes(qreal b_w, qreal b_h, qreal w_w, qreal w_h) {
35  if (mKeyShapes[KC_BLACK].elementCount() > 0) {return;} // already initialized
36 
37  mKeyRects[KC_BLACK] = QRectF(0, 0, b_w, b_h);
38  mKeyRects[KC_WHITE] = QRectF(0, 0, w_w, w_h);
39 
40  const std::array<qreal, KC_COUNT> radius {{b_w * KEY_RADIUS_FACTOR[0], w_w * KEY_RADIUS_FACTOR[1]}};
41  std::array<QRectF, KC_COUNT> rWithMargin = mKeyRects;
42 
43  for (int i = 0; i < KC_COUNT; ++i) {
44  const auto &m = MARGINS[i];
45  rWithMargin[i].adjust(m[0], m[1], m[2], m[3]);
46 
47  mKeyShapes[i].addRoundedRect(rWithMargin[i], radius[i], radius[i]);
48  mKeyShapes[i].addRect(rWithMargin[i].left(), rWithMargin[i].top(), rWithMargin[i].width(), radius[i] * 2);
49 
50  mKeyShapes[i].setFillRule(Qt::WindingFill);
51  mKeyShapes[i] = mKeyShapes[i].simplified();
52  }
53 }
54 
55 
56 GraphicsKeyItem::GraphicsKeyItem(KeyColor color, const QString &text, const QColor &textColor) :
57  QGraphicsRectItem(mKeyRects[color]),
58  mKeyColor(color),
59  mText(text),
60  mTextColor(textColor)
61 {
62 }
63 
64 void GraphicsKeyItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) {
65  // QGraphicsRectItem::paint(painter, option, widget);
66  Q_UNUSED(widget);
67  Q_UNUSED(option);
68 
69  painter->setPen(pen());
70  painter->setBrush(brush());
71  painter->drawPath(mKeyShapes[mKeyColor]);
72 
73  // draw text
74  painter->rotate(-90);
75  const qreal textFieldWidth = rect().height() * 0.94;
76  const qreal textFieldHeight = rect().width();
77  painter->translate(-textFieldWidth, 0);
78  QPen p;
79  p.setBrush(QBrush(mTextColor));
80  painter->setPen(p);
81  painter->setBrush(Qt::NoBrush);
82  painter->setFont(mFont);
83 
84  painter->drawText(QRectF(0, 0, textFieldWidth, textFieldHeight), Qt::AlignVCenter | Qt::AlignLeft, mText);
85 }
static const int KC_WHITE
static void initShapes(qreal b_w, qreal b_h, qreal w_w, qreal w_h)
virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget=0) override
const KeyColor mKeyColor
static const int KC_COUNT
const QString mText
const std::array< qreal, GraphicsKeyItem::KC_COUNT > KEY_RADIUS_FACTOR
static std::array< QRectF, piano::KC_COUNT > mKeyRects
const std::array< std::array< qreal, 4 >, GraphicsKeyItem::KC_COUNT > MARGINS
KeyColor
The KeyColor enum.
Definition: pianodefines.h:51
GraphicsKeyItem(KeyColor color, const QString &text, const QColor &textColor)
const QColor mTextColor
static const int KC_BLACK
static std::array< QPainterPath, piano::KC_COUNT > mKeyShapes