SETestKCL  1.0.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MainWindow.cpp
Go to the documentation of this file.
1 #include "MainWindow.h"
2 
3 #include <iostream>
4 
5 #include <QtGui>
6 #include <QAction>
7 #include <QDialogButtonBox>
8 #include <QGroupBox>
9 #include <QLabel>
10 #include <QLineEdit>
11 #include <QMenu>
12 #include <QMenuBar>
13 #include <QPushButton>
14 #include <QTextEdit>
15 #include <QVBoxLayout>
16 #include <QHBoxLayout>
17 #include <QGridLayout>
18 #include <QFormLayout>
19 #include <QComboBox>
20 #include <QSpinBox>
21 #include <QMessageBox>
22 #include <QKeyEvent>
23 #include <QElapsedTimer>
24 
25 #include "qcustomplot.h"
26 #include "BinomialCalculator.h"
27 
29 {
30  this->Usage =
31  "Please press one of the following key: \n \
32  - 'y': When you hear 'yes' \n \
33  - 'n': When you hear 'no' \n \
34  - 'z': To undo the last operation \n \
35  - 'r': Toggle the refresh mode for the summary (continuous / non-continuous) \n \
36  - 's': To show (refresh) the summary of the negotiation \n \
37  - 'q': To quit the program \n";
38 
39  this->createUsageBox();
40  this->createCounterBox();
41  this->createSummaryBox();
42  this->buttonBox = new QDialogButtonBox(QDialogButtonBox::Close);
43  connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
44  connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
45  QVBoxLayout *mainLayout = new QVBoxLayout;
46  mainLayout->addWidget(this->usageBox, 0, Qt::AlignTop);
47  mainLayout->addWidget(this->counterBox, 0, Qt::AlignTop);
48  mainLayout->addWidget(this->summaryBox, 1);
49  mainLayout->addWidget(this->buttonBox, 0, Qt::AlignBottom);
50  this->setLayout(mainLayout);
51  this->setWindowTitle(tr("Negotiation Study"));
52 
53  this->Calculator = NULL;
54  this->RefreshMode = MainWindow::Refresh_Continuous;
55  this->timer = new QElapsedTimer();
56  this->timer->start();
57 }
58 
59 void MainWindow::createUsageBox()
60 {
61 
62  this->usageBox = new QGroupBox(tr("Usage"));
63  QHBoxLayout *layout = new QHBoxLayout;
64  QLabel* usageLabel = new QLabel (tr (this->Usage.c_str()));
65  layout->addWidget (usageLabel);
66  this->usageBox->setLayout(layout);
67 }
68 
69 void MainWindow::createCounterBox()
70 {
71  this->counterBox = new QGroupBox(tr("Counter"));
72  QGridLayout *layout = new QGridLayout;
73  this->numberOfYesLabel = new QLabel(tr("Yes: 0"));
74  layout->addWidget(this->numberOfYesLabel, 0, 0);
75  this->numberOfNoLabel = new QLabel(tr("No: 0"));
76  layout->addWidget(this->numberOfNoLabel, 0, 1);
77  QFont f;
78  f = this->numberOfYesLabel->font();
79  f.setPointSize (24);
80  f.setBold (true);
81  this->numberOfYesLabel->setFont (f);
82  f = this->numberOfNoLabel->font();
83  f.setPointSize (24);
84  f.setBold (true);
85  this->numberOfNoLabel->setFont (f);
86  this->counterBox->setLayout(layout);
87 }
88 
89 void MainWindow::createSummaryBox()
90 {
91  this->summaryBox = new QGroupBox(tr("Summary"));
92  QGridLayout *layout = new QGridLayout;
93  this->eTimeLabel = new QLabel(tr("Elapsed Time: 0.00 min"));
94  layout->addWidget(this->eTimeLabel, 0, 0, Qt::AlignTop);
95  this->ratioLabel = new QLabel(tr("Ratio of 'yes' over 'no': 0.00"));
96  layout->addWidget(this->ratioLabel, 0, 1, Qt::AlignTop);
97  this->yRateLabel = new QLabel(tr("Rate of 'yes': 0.00 / min"));
98  layout->addWidget(this->yRateLabel, 1, 0, Qt::AlignTop);
99  this->nRateLabel = new QLabel(tr("Rate of 'no': 0.00 / min"));
100  layout->addWidget(this->nRateLabel, 1, 1, Qt::AlignTop);
101 
102  this->customPlot = new QCustomPlot();
103  this->customPlot->legend->setVisible(true);
104  this->customPlot->legend->setFont(QFont("Helvetica",9));
105  this->customPlot->addGraph();
106  this->customPlot->graph(0)->setName("Current Binomial Distribution");
107  this->customPlot->graph(0)->setPen(QPen(Qt::blue));
108  this->customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20)));
109  this->customPlot->addGraph();
110  this->customPlot->graph(1)->setName("Number of Yes");
111  this->customPlot->graph(1)->setPen(QPen(Qt::red));
112  this->customPlot->xAxis2->setVisible(true);
113  this->customPlot->xAxis2->setTickLabels(false);
114  this->customPlot->yAxis2->setVisible(true);
115  this->customPlot->yAxis2->setTickLabels(false);
116  connect(this->customPlot->xAxis, SIGNAL(rangeChanged(QCPRange)), this->customPlot->xAxis2, SLOT(setRange(QCPRange)));
117  connect(this->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)), this->customPlot->yAxis2, SLOT(setRange(QCPRange)));
119  layout->addWidget(this->customPlot, 2, 0, 1, 2);
120  layout->setRowStretch (0, 0);
121  layout->setRowStretch (1, 0);
122  layout->setRowStretch (2, 1);
123  this->summaryBox->setLayout(layout);
124 }
125 
126 void MainWindow::keyPressEvent(QKeyEvent* e)
127 {
128  switch(e->key())
129  {
130  case Qt::Key_Y:
131  this->ListOfOperations.push_back (MainWindow::Operation_Yes);
132  this->UpdateCounter();
133  if (this->RefreshMode == MainWindow::Refresh_Continuous)
134  this->UpdateSummary();
135  break;
136  case Qt::Key_N:
137  this->ListOfOperations.push_back (MainWindow::Operation_No);
138  this->UpdateCounter();
139  if (this->RefreshMode == MainWindow::Refresh_Continuous)
140  this->UpdateSummary();
141  break;
142  case Qt::Key_Z:
143  if (this->ListOfOperations.size() >= 1)
144  this->ListOfOperations.pop_back();
145  this->UpdateCounter();
146  if (this->RefreshMode == MainWindow::Refresh_Continuous)
147  this->UpdateSummary();
148  break;
149  case Qt::Key_S:
150  this->UpdateCounter();
151  this->UpdateSummary();
152  break;
153  case Qt::Key_Q:
154  this->UpdateCounter();
155  this->UpdateSummary();
156  //\todo emit a signal to quit the program (close window)
157  emit done(0);
158  break;
159  case Qt::Key_R:
160  this->RefreshMode = ! this->RefreshMode;
161  break;
162  default:
163  break;
164  }
165 }
166 
168 {
169  if (this->counterBox == NULL)
170  return;
171  unsigned int y = std::count (this->ListOfOperations.begin(), this->ListOfOperations.end(), MainWindow::Operation_Yes);
172  unsigned int n = std::count (this->ListOfOperations.begin(), this->ListOfOperations.end(), MainWindow::Operation_No);
173  this->numberOfYesLabel->setText(tr("Yes: %1").arg(y));
174  this->numberOfNoLabel->setText(tr("No: %1").arg(n));
175 }
176 
177 
179 {
180  if (this->summaryBox == NULL)
181  return;
182 
183  unsigned int y = std::count (this->ListOfOperations.begin(), this->ListOfOperations.end(), MainWindow::Operation_Yes);
184  unsigned int n = std::count (this->ListOfOperations.begin(), this->ListOfOperations.end(), MainWindow::Operation_No);
185  float ratio = this->ListOfOperations.size() ? (float)(y)/(float)(this->ListOfOperations.size()) : 0;
186  float elapsed_minutes = (float) this->timer->elapsed() / (1000.0 * 60.0);
187  float yrate = (float)(y) / elapsed_minutes;
188  float nrate = (float)(n) / elapsed_minutes;
189 
190  this->eTimeLabel->setText(tr("Elapsed Time: %1 min").arg(QString::number (elapsed_minutes, 'f', 2 )));
191  this->ratioLabel->setText(tr("Ratio of 'yes' over 'no': %1").arg(QString::number (ratio, 'f', 2 )));
192  this->yRateLabel->setText(tr("Rate of 'yes': %1 / min").arg(QString::number (yrate, 'f', 2 )));
193  this->nRateLabel->setText(tr("Rate of 'no': %1 / min").arg(QString::number (nrate, 'f', 2 )));
194 
195  if (this->Calculator == NULL ||
196  !this->ListOfOperations.size())
197  return;
198 
199  this->Calculator->SetNumberOfExperiments (this->ListOfOperations.size());
200  this->Calculator->SetProbabilityOfSuccess (0.5);
201 
202  std::vector<float> pmf;
203  float max_p = 0.0;
204 
205  for (unsigned int i=0; i<this->ListOfOperations.size(); i++)
206  {
207  float p = this->Calculator->GetProbabilityMassFunction (i);
208  pmf.push_back (p);
209  max_p = std::max (p, max_p);
210  }
211 
212  QVector<double> x0(this->ListOfOperations.size()), y0(this->ListOfOperations.size());
213  for (unsigned int i=0; i<this->ListOfOperations.size(); ++i)
214  {
215  x0[i] = i;
216  y0[i] = pmf[i];
217  }
218 
219  QVector<double> x1 (2), y1 (2);
220  x1[0] = y;
221  x1[1] = y;
222  y1[0] = 0;
223  y1[1] = max_p;
224 
225  this->customPlot->graph(0)->setData(x0, y0);
226  this->customPlot->graph(1)->setData(x1, y1);
227  this->customPlot->graph(0)->rescaleAxes();
228  this->customPlot->graph(1)->rescaleAxes();
229  this->customPlot->replot();
230 }
QCPAxis * xAxis2
Definition: qcustomplot.h:1819
QCPLegend * legend
Definition: qcustomplot.h:1820
Represents the range an axis is encompassing.
Definition: qcustomplot.h:476
0x008 Plottables are selectable (e.g. graphs, curves, bars,... see QCPAbstractPlottable) ...
Definition: qcustomplot.h:158
void SetNumberOfExperiments(unsigned int n)
void setPen(const QPen &pen)
void setData(QCPDataMap *data, bool copy=false)
QCPGraph * graph(int index) const
void setBrush(const QBrush &brush)
The central class of the library. This is the QWidget which displays the plot and interacts with the ...
Definition: qcustomplot.h:1680
QCPAxis * yAxis
Definition: qcustomplot.h:1819
Q_SLOT void replot(QCustomPlot::RefreshPriority refreshPriority=QCustomPlot::rpHint)
void SetProbabilityOfSuccess(float p)
0x001 Axis ranges are draggable (see QCPAxisRect::setRangeDrag, QCPAxisRect::setRangeDragAxes) ...
Definition: qcustomplot.h:155
QCPAxis * yAxis2
Definition: qcustomplot.h:1819
void setVisible(bool on)
QCPGraph * addGraph(QCPAxis *keyAxis=0, QCPAxis *valueAxis=0)
void UpdateSummary(void)
Definition: MainWindow.cpp:178
void setName(const QString &name)
QCPAxis * xAxis
Definition: qcustomplot.h:1819
void UpdateCounter(void)
Definition: MainWindow.cpp:167
void keyPressEvent(QKeyEvent *e)
Definition: MainWindow.cpp:126
void setTickLabels(bool show)
0x002 Axis ranges are zoomable with the mouse wheel (see QCPAxisRect::setRangeZoom, QCPAxisRect::setRangeZoomAxes)
Definition: qcustomplot.h:156
float GetProbabilityMassFunction(unsigned int k) const
void setInteractions(const QCP::Interactions &interactions)
void rescaleAxes(bool onlyEnlarge, bool includeErrorBars) const
void setFont(const QFont &font)