QTfrontend/statsPage.cpp
author unc0rr
Fri, 09 Jan 2009 14:47:54 +0000
changeset 1625 37aae47943ce
parent 1623 d590e6da04da
child 1636 e528696f2177
permissions -rw-r--r--
Initial health graphic implementation
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1621
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
     1
/*
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
     2
 * Hedgewars, a free turn based strategy game
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
     3
 * Copyright (c) 2009 Andrey Korotaev <unC0Rr@gmail.com>
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
     4
 *
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
     8
 *
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    12
 * GNU General Public License for more details.
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    13
 *
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    17
 */
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    18
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    19
#include <QLabel>
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    20
#include <QGridLayout>
1625
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    21
#include <QGraphicsScene>
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    22
#include <QGraphicsView>
1621
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    23
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    24
#include "statsPage.h"
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    25
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    26
PageGameStats::PageGameStats(QWidget* parent) : AbstractPage(parent)
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    27
{
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    28
	QFont * font14 = new QFont("MS Shell Dlg", 14);
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    29
	QGridLayout * pageLayout = new QGridLayout(this);
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    30
	pageLayout->setColumnStretch(0, 1);
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    31
	pageLayout->setColumnStretch(1, 1);
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    32
	pageLayout->setColumnStretch(2, 1);
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    33
1625
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    34
	BtnBack = addButton(":/res/Exit.png", pageLayout, 2, 0, true);
1621
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    35
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    36
	labelGameStats = new QLabel(this);
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    37
	labelGameStats->setTextFormat(Qt::RichText);
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    38
	pageLayout->addWidget(labelGameStats, 0, 0, 1, 3);
1625
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    39
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    40
	graphic = new QGraphicsView(this);
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    41
	graphic->scale(1.0, -1.0);
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    42
	pageLayout->addWidget(graphic, 1, 0, 1, 3);
1621
d1ded2532d3f - svn maintenance
unc0rr
parents:
diff changeset
    43
}
1622
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    44
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    45
void PageGameStats::AddStatText(const QString & msg)
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    46
{
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    47
	labelGameStats->setText(labelGameStats->text() + msg);
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    48
}
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    49
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    50
void PageGameStats::clear()
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    51
{
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    52
	labelGameStats->setText("");
1625
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    53
	healthPoints.clear();
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    54
}
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    55
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    56
void PageGameStats::renderStats()
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    57
{
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    58
	QGraphicsScene * scene = new QGraphicsScene();
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    59
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    60
	QMap<quint32, QVector<quint32> >::const_iterator i = healthPoints.constBegin();
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    61
	while (i != healthPoints.constEnd())
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    62
	{
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    63
		quint32 c = i.key();
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    64
		QColor clanColor = QColor(qRgb((c >> 16) & 255, (c >> 8) & 255, c & 255));
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    65
		QVector<quint32> hps = i.value();
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    66
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    67
		QPainterPath path;
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    68
		if (hps.size())
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    69
			path.moveTo(0, hps[0]);
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    70
		
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    71
		for(int t = 1; t < hps.size(); ++t)
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    72
			path.lineTo(t, hps[t]);
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    73
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    74
		scene->addPath(path, QPen(c));
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    75
		++i;
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    76
	}
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    77
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    78
	graphic->setScene(scene);
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
    79
	graphic->fitInView(graphic->sceneRect());
1622
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    80
}
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    81
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    82
void PageGameStats::GameStats(char type, const QString & info)
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    83
{
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    84
	switch(type) {
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    85
		case 'r' : {
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    86
			AddStatText(QString("<h1 align=\"center\">%1</h1>").arg(info));
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    87
			break;
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    88
		}
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    89
		case 'D' : {
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    90
			int i = info.indexOf(' ');
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    91
			QString message = tr("<p>The best shot award was won by <b>%1</b> with <b>%2</b> pts.</p>")
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    92
					.arg(info.mid(i + 1), info.left(i));
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    93
			AddStatText(message);
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    94
			break;
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    95
		}
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    96
		case 'k' : {
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    97
			int i = info.indexOf(' ');
1623
d590e6da04da Change one of 'best shot award' messages
unc0rr
parents: 1622
diff changeset
    98
			QString message = tr("<p>The best killer is <b>%1</b> with <b>%2</b> kills in a turn.</p>")
1622
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
    99
					.arg(info.mid(i + 1), info.left(i));
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   100
			AddStatText(message);
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   101
			break;
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   102
		}
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   103
		case 'K' : {
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   104
			QString message = tr("<p>A total of <b>%1</b> Hedgehog(s) were killed during this round.</p>").arg(info);
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   105
			AddStatText(message);
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   106
			break;
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   107
		}
1625
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
   108
		case 'H' : {
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
   109
			int i = info.indexOf(' ');
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
   110
			quint32 clan = info.left(i).toInt();
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
   111
			quint32 hp = info.mid(i + 1).toUInt();
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
   112
			healthPoints[clan].append(hp);
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
   113
			break;
37aae47943ce Initial health graphic implementation
unc0rr
parents: 1623
diff changeset
   114
		}
1622
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   115
	}
8e0d62727f01 Fully separate statistics code from main form code
unc0rr
parents: 1621
diff changeset
   116
}