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
recordingqualitybar.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 //=============================================================================
21 // Quality recording bar
22 //=============================================================================
23 
24 #include "recordingqualitybar.h"
25 #include <QStyleFactory>
26 #include <QStylePainter>
27 #include <QStyleOptionFocusRect>
28 #include <iostream>
29 #include <cmath>
30 #include "../core/messages/messagefinalkey.h"
31 #include "../core/messages/messagekeyselectionchanged.h"
32 
33 //-----------------------------------------------------------------------------
34 // Constructor
35 //-----------------------------------------------------------------------------
36 
38  QProgressBar(parent) {
39 
40  setFormat(tr("Quality"));
41  setWhatsThis(tr("This bar displays the quality of the recording. All of the recorded keys should have an almost equal quality before starting the calculation."));
42  setTextVisible(false);
43 
44  setRange(0, 10000);
45 
46  setStyle(QStyleFactory::create("fusion"));
47 
48  setOrientation(Qt::Vertical);
49  setTextDirection(BottomToTop);
50 
51  setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding);
52  updateValue(0);
53 }
54 
55 
56 //-----------------------------------------------------------------------------
57 // Functions suggesting the minimal sizes
58 //-----------------------------------------------------------------------------
59 
61  QFontMetrics fm(fontMetrics());
62  QRect br(fm.boundingRect(text()));
63  return QSize(QProgressBar::minimumSizeHint().width(), br.width());
64 }
65 
67  QFontMetrics fm(fontMetrics());
68  QRect br(fm.boundingRect(text()));
69  return QSize(QProgressBar::sizeHint().width(), br.width() + 10);
70 }
71 
72 
73 //-----------------------------------------------------------------------------
74 // Update the quality measure bar
75 //-----------------------------------------------------------------------------
76 
86 
88  if (value() == v * 10000) {
89  return;
90  }
91  setValue(v * 10000);
92  QPalette p = this->palette();
93 
94  int red = static_cast<int>(255*std::pow(1-v,0.3));
95  int green = static_cast<int>(255*std::pow(v,0.5));
96  p.setColor(QPalette::Highlight, QColor(red,green,0));
97 
98  this->setPalette(p);
99 
100  update();
101  show();
102 }
103 
104 
105 //-----------------------------------------------------------------------------
106 // Message handler
107 //-----------------------------------------------------------------------------
108 
110  switch (m->getType())
111  {
113  {
114  auto message(std::static_pointer_cast<MessageFinalKey>(m));
115  double quality = message->getFinalKey()->getRecognitionQuality();
116  updateValue(quality);
117  break;
118  }
120  auto message(std::static_pointer_cast<MessageKeySelectionChanged>(m));
121  const Key *key = message->getKey();
122  if (!key) {
123  updateValue(0);
124  } else {
126  }
127  break;
128  }
130  updateValue(0);
131  break;
132  default:
133  break;
134  }
135 }
136 
137 
138 //-----------------------------------------------------------------------------
139 // Paint the quality bar
140 //-----------------------------------------------------------------------------
141 
142 void RecordingQualityBar::paintEvent(QPaintEvent * event) {
143  QProgressBar::paintEvent(event);
144 
145  QPainter painter(this);
146 
147  const QRect cr(contentsRect());
148 
149  painter.save();
150  painter.translate(contentsRect().center());
151  if (orientation() == Qt::Vertical) {
152  painter.rotate(-90);
153  painter.drawText(QRect(-cr.height() / 2, -cr.width() / 2, cr.height(), cr.width()), Qt::AlignHCenter | Qt::AlignVCenter, text());
154  } else {
155  painter.drawText(QRect(-cr.width() / 2, -cr.height() / 2, cr.width(), cr.height()), Qt::AlignCenter, text());
156  }
157  painter.restore();
158 }
virtual void handleMessage(MessagePtr m) overridefinal
Message handling.
std::shared_ptr< Message > MessagePtr
Global type of a shared message pointer.
Definition: message.h:98
virtual QSize minimumSizeHint() const overridefinal
Hint for the minimal widget size.
Class describing a single piano key.
Definition: key.h:45
Message that a key has been selected.
Definition: message.h:63
virtual QSize sizeHint() const overridefinal
Hint for the optimal widget size.
double getRecognitionQuality() const
Get quality of recognition.
Definition: key.cpp:155
void paintEvent(QPaintEvent *event) overridefinal
Reimplemented paint function that will draw the vertical text.
final key information after recording
Definition: message.h:55
RecordingQualityBar(QWidget *parent=nullptr)
Default constructor.
void updateValue(float v)
Function to update the current value.
keystroke recognized and recording started
Definition: message.h:51