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
calculationprogressgroup.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 // Calculation progress group
22 //=============================================================================
23 
25 
26 #include <QHBoxLayout>
27 #include <QVBoxLayout>
28 #include <QFormLayout>
29 #include <QMessageBox>
30 #include <QDialogButtonBox>
31 #include <QDoubleSpinBox>
32 #include <QScrollArea>
33 #include <QSlider>
34 #include <QFontMetrics>
35 #include "../core/system/log.h"
36 #include "../core/calculation/calculationmanager.h"
37 #include "../core/calculation/algorithmfactorydescription.h"
38 #include "../core/messages/messagecaluclationprogress.h"
39 #include "settingsforqt.h"
40 #include "algorithmdialog.h"
41 #include "displaysize.h"
42 
43 //-----------------------------------------------------------------------------
44 // Constructor
45 //-----------------------------------------------------------------------------
46 
57 
59  : DisplaySizeDependingGroupBox(parent, new QVBoxLayout, toFlag(MODE_CALCULATION)),
60  MessageListener(false),
61  CalculationAdapter(core),
62  mCalculationInProgress(false)
63 {
64  QVBoxLayout *mainLayout = qobject_cast<QVBoxLayout*>(mMainWidgetContainer->layout());
65  QHBoxLayout *statusTextLayout = new QHBoxLayout;
66  mainLayout->addLayout(statusTextLayout);
67 
68  statusTextLayout->addWidget(new QLabel(tr("Status:")));
69  statusTextLayout->addWidget(mStatusLabel = new QLabel);
70  mStatusLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
71 
72  statusTextLayout->addStretch();
73 
74  QHBoxLayout *progressLayout = new QHBoxLayout;
75  mainLayout->addLayout(progressLayout);
76 
77  // Local class for a small push button
78  class MinSizePushButton : public QPushButton
79  {
80  public:
81  MinSizePushButton(QString text = QString()) :
82  QPushButton(text)
83  {
84  setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Preferred);
85  }
86 
87  private:
88  virtual QSize minimumSizeHint() const override
89  {
90  QFontMetrics f(fontMetrics());
91  // add also some extra spacing of 2 chars (x)
92  return f.size(Qt::TextSingleLine, text() + "xx");
93  }
94  virtual QSize sizeHint() const override
95  {
96  QSize sh(QPushButton::sizeHint());
97  sh.setWidth(std::max(minimumSizeHint().width(), sh.width()));
98  return sh;
99  }
100  };
101 
102  // Local class for a small progress bar
103  class MinSizeProgressBar : public QProgressBar
104  {
105  private:
106  virtual QSize sizeHint() const override {return QSize(0, 0);}
107  };
108 
109  // Add the progress bar
110  progressLayout->addWidget(mCalculationProgressBar = new MinSizeProgressBar);
111  mCalculationProgressBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
112  progressLayout->addWidget(mStartCancelButton = new MinSizePushButton);
113 
114  // Add the push button
115  QPushButton *showAlgorithmInfo = new MinSizePushButton(tr("Info"));
116  progressLayout->addWidget(showAlgorithmInfo);
117  QObject::connect(showAlgorithmInfo, SIGNAL(clicked()), this, SLOT(showAlgorithmInfo()));
118 
119  mainLayout->addStretch();
120 
121  // Reset the calculation
123 
124  // check if algorithm exists
125  std::string lastUsedAlgorithm(SettingsForQt::getSingleton().getLastUsedAlgorithm());
126  if (CalculationManager::getSingleton().hasAlgorithm(lastUsedAlgorithm) == false)
127  {
128  // select default algorithm
130  }
131 
132  // load algorithm information
134 
135  // show algorithm information in the title
136  updateTitle();
137 
138  // connect the start/cancel button with the cancel slot
139  QObject::connect(mStartCancelButton, SIGNAL(clicked()), this, SLOT(startCancelPressed()));
140 }
141 
142 
143 //-----------------------------------------------------------------------------
144 // Message listener and dispatcher
145 //-----------------------------------------------------------------------------
146 
154 
156 {
157  if (m->getType() == Message::MSG_CALCULATION_PROGRESS)
158  {
159  auto mcp(std::static_pointer_cast<MessageCaluclationProgress>(m));
160  switch (mcp->getCalculationType())
161  {
163  mCalculationInProgress = true;
164  // we started this calculation
165  mStatusLabel->setText(tr("Calculation started"));
166  mStartCancelButton->setText(tr("Stop calculation"));
167  break;
170  mCalculationInProgress = false;
171  QMessageBox::information(this, tr("Calculation finished"), tr("The calculation finished successfully! Now you can switch to the tuning mode and tune your piano."));
172  break;
174  mCalculationProgressBar->setValue(mcp->getValue() * 100);
175  break;
177  mStatusLabel->setText(tr("Minimizing the entropy"));
178  break;
180  {
181  QString errorText;
182  switch (mcp->getErrorCode())
183  {
185  LogW("Calculation error message was sent but no error code was set");
186  errorText = tr("An unknown error occured during the calculation.");
187  break;
189  errorText = tr("No data available.");
190  break;
192  errorText = tr("Not all keys recorded");
193  break;
195  errorText = tr("Key data inconsistent.");
196  break;
198  errorText = tr("Not enough keys recorded in left section.");
199  break;
201  errorText = tr("Not enough keys recorded in right section.");
202  break;
203  default:
204  errorText = tr("Undefined error message.");
205  LogI("Undefined error message");
206  break;
207  }
208 
209  QMessageBox::critical(this, tr("Calculation error"),
210  QString("%1<br><br><b>%2: %3</b>").arg(errorText, tr("Error code"),
211  QString("%1").arg(mcp->getErrorCode())));
212 
214  break;
215  }
216  default:
217  break;
218  }
219  }
220 }
221 
222 
223 //-----------------------------------------------------------------------------
224 // Update algorithm title
225 //-----------------------------------------------------------------------------
226 
230 
232 {
233  EptAssert(mAlgorithmSelection, "Algorithm has to be selected");
234  if (mGroupBox)
235  {
236  mGroupBox->setTitle(tr("Calculation with: %1").arg(QString::fromStdString(mAlgorithmSelection->getName())));
237  }
238 }
239 
240 
241 //-----------------------------------------------------------------------------
242 // Start calculation
243 //-----------------------------------------------------------------------------
244 
250 
252 {
254 
256 }
257 
258 
259 //-----------------------------------------------------------------------------
260 // Cancel calculation
261 //-----------------------------------------------------------------------------
262 
268 
270 {
271  mStatusLabel->setText(tr("Calculation canceled"));
272  mCalculationProgressBar->setValue(0);
273  mStartCancelButton->setText(tr("Start calculation"));
274 
277  mCalculationInProgress = false;
278 }
279 
280 
281 //-----------------------------------------------------------------------------
282 // Reset calculation
283 //-----------------------------------------------------------------------------
284 
290 
292 {
294  {
295  mStatusLabel->setText(QString()); // not enough space for the text
296  }
297  else
298  {
299  mStatusLabel->setText(tr("Press the button to start the calculation"));
300  }
301 
302  mCalculationProgressBar->setValue(0);
303  mStartCancelButton->setText(tr("Start calculation"));
304 
306  mCalculationInProgress = false;
307 }
308 
309 
310 //-----------------------------------------------------------------------------
311 // Start/Cancel button pressed
312 //-----------------------------------------------------------------------------
313 
317 
319 {
321  else onStartCalculation();
322 }
323 
324 
325 //-----------------------------------------------------------------------------
326 // Show algorithm info
327 //-----------------------------------------------------------------------------
328 
332 
334 {
335  AlgorithmDialog dialog(mAlgorithmSelection, parentWidget());
336  dialog.exec();
338  updateTitle();
340 }
std::shared_ptr< const AlgorithmInformation > mAlgorithmSelection
Pointer to the algorithm selector.
std::shared_ptr< Message > MessagePtr
Global type of a shared message pointer.
Definition: message.h:98
virtual void startCalculation(const std::string &algorithmName)
Function called by the GUI to start the calculation of the tuning curve.
static CalculationManager & getSingleton()
static Settings & getSingleton()
Get a pointer to the singleton instance.
Definition: settings.cpp:46
void onStartCalculation()
Function called to start the calculation.
void updateTitle()
Function to update the title of the algorithm in use.
bool mCalculationInProgress
Flag: calculation is in progress.
Adapter for starting and stopping the calculation process.
#define LogW(...)
Definition: log.h:56
QPushButton * mStartCancelButton
Pointer to the start and cancel button.
QProgressBar * mCalculationProgressBar
Pointer to the progress bar.
void showAlgorithmInfo()
Show information about the algorithm.
Mode where the entropy optimization is carried out.
Definition: prerequisites.h:70
void onCancelCalculation()
Function called to cancel the calculation.
CalculationProgressGroup(Core *core, QWidget *parent=nullptr)
CalculationProgressGroup::CalculationProgressGroup.
void onResetCalculation()
Function called to reset the calculation.
QLabel * mStatusLabel
Pointer to the status label.
void activateMessageListener()
#define LogI(...)
Definition: log.h:50
int toFlag(OperationMode mode)
Definition: prerequisites.h:76
virtual void setLastUsedAlgorithm(const std::string &name)
Set the name of the last used algorithm.
Definition: settings.h:59
Message that progress of any kind was made by the calculator.
Definition: message.h:59
CORE : Class managing the core.
Definition: core.h:45
std::shared_ptr< const AlgorithmInformation > loadAlgorithmInformation(const std::string &algorithmName) const
virtual void cancelCalculation()
Function called by the GUI to interrupt or stop the calculation thread.
std::shared_ptr< const AlgorithmInformation > getAlgorithmInformation() const
#define EptAssert(a, b)
Definition: eptexception.h:47
void deactivateMessageListener()
void startCancelPressed()
Slot called when the start and stop button was pressed.
std::string getDefaultAlgorithmId() const
virtual void handleMessage(MessagePtr m) overridefinal
Message handling.
static const std::unique_ptr< DisplaySizeDefines > & getSingleton()
Definition: displaysize.cpp:29
static SettingsForQt & getSingleton()
Getter function for the singleton.