/
competitorwidget.cpp
133 lines (114 loc) · 4.21 KB
/
competitorwidget.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* This source file is part of Cooperation, an implementation of the
* Prisoner's Dilemna game.
*
* Copyright (C) 2012 by Nathan Geffen
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cmath>
#include <QGridLayout>
#include <QLabel>
#include "game/game.h"
#include "game/competitor.h"
#include "competitorwidget.h"
#include "competitorshape.h"
#include "game/common.h"
CompetitorWidget::CompetitorWidget(int maxDiameter,
Competitor& competitor,
DisplayMethodology displayMethodology,
QColor color,
QWidget *parent) :
QWidget(parent), competitor_(competitor),
maxDiameter_(maxDiameter ), maxScore_(0), savedScore_(0),
displayMethodology_(displayMethodology), color_(color)
{
nameLabel_ = new QLabel( competitor_.output().toUpper(), this );
QFont font = nameLabel_->font();
font.setPointSize(6);
nameLabel_->setFont(font);
scoreLabel_ = new QLabel( QString().setNum(0), this );
scoreLabel_->setFont(font);
topline_ = new QHBoxLayout();
topline_->addWidget(nameLabel_);
topline_->addWidget(scoreLabel_);
layout_ = new QGridLayout();
layout_->addLayout(topline_, 0, 0);
layout_->setRowStretch(0, 1);
competitorShape_ = new CompetitorShape(maxDiameter / 6,
color_);
layout_->addWidget(competitorShape_, 1, 0);
layout_->setRowStretch(1, 10);
setLayout(layout_);
}
void CompetitorWidget::executeUpdate(int min_score,
int max_score,
bool updateScore)
{
QRect rect = layout_->cellRect(1, 0);
maxDiameter_ = rect.height();
setMinScore(min_score);
setMaxScore(max_score);
updateScore_ = updateScore;
scoreLabel_->setText( QString().setNum( competitor_.getScore() ) );
if ( maxScore_ == 0 ) {
competitorShape_->setRadius(5);
} else {
if ( competitor_.getScore() == maxScore_ ) {
competitorShape_->setShape(square);
double radius = (double) maxDiameter_ / 2.0;
double circleArea = PI * radius * radius;
competitorShape_->setRadius( (int) sqrt(circleArea) );
} else {
competitorShape_->setShape(circle);
if ( displayMethodology_ == RATIO) {
updateUsingRatio();
} else if ( displayMethodology_ == RANK ){
updateUsingRank();
} else {
updateUsingRatioNormalizedToZero();
}
}
}
if ( updateScore_ ) {
savedScore_ = competitor_.getScore();
}
competitorShape_->executeUpdate();
}
void CompetitorWidget::updateUsingRatio()
{
competitorShape_->setShape(circle);
double ratio = (double) maxScore_ / competitor_.getScore() ;
double maxRadius = (double) maxDiameter_ / 2.0;
double radius = ( ratio > 0 ) ? sqrt( (maxRadius * maxRadius ) / ratio ) : 0;
if ( radius < 3 ) radius = 3;
competitorShape_->setRadius( (int) radius );
}
void CompetitorWidget::updateUsingRatioNormalizedToZero()
{
competitorShape_->setShape(circle);
double ratio = (double) (maxScore_ - minScore_) /
( competitor_.getScore() - minScore_ + 1) ;
double maxRadius = (double) maxDiameter_ / 2.0;
double radius = ( ratio > 0 ) ? sqrt( (maxRadius * maxRadius ) / ratio ) : 0;
if ( radius < 3 ) radius = 3;
competitorShape_->setRadius( (int) radius );
}
void CompetitorWidget::updateUsingRank()
{
const Game* game = competitor_.getGame();
int rank = game->getRank( competitor_.getScore() );
int competitors = game->getNumberCompetitors();
int diameter = (double) (competitors - rank) / competitors * maxDiameter_;
competitorShape_->setRadius( (int) diameter / 2);
}