QTfrontend/pagegamestats.cpp
changeset 6060 fdfc01419815
parent 6059 ddf020d0941a
child 6061 15b4b485a1c5
equal deleted inserted replaced
6059:ddf020d0941a 6060:fdfc01419815
     1 /*
       
     2  * Hedgewars, a free turn based strategy game
       
     3  * Copyright (c) 2010-2011 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * This program is free software; you can redistribute it and/or modify
       
     6  * it under the terms of the GNU General Public License as published by
       
     7  * the Free Software Foundation; version 2 of the License
       
     8  *
       
     9  * This program is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12  * GNU General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU General Public License
       
    15  * along with this program; if not, write to the Free Software
       
    16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
       
    17  */
       
    18 
       
    19 #include <QLabel>
       
    20 #include <QGridLayout>
       
    21 #include <QHBoxLayout>
       
    22 #include <QGraphicsScene>
       
    23 #include <QGroupBox>
       
    24 #include <QSizePolicy>
       
    25 
       
    26 #include "pagegamestats.h"
       
    27 #include "team.h"
       
    28 
       
    29 FitGraphicsView::FitGraphicsView(QWidget* parent) : QGraphicsView(parent)
       
    30 {
       
    31 
       
    32 }
       
    33 
       
    34 void FitGraphicsView::resizeEvent(QResizeEvent * event)
       
    35 {
       
    36     Q_UNUSED(event);
       
    37 
       
    38     fitInView(sceneRect());
       
    39 }
       
    40 
       
    41 QLayout * PageGameStats::bodyLayoutDefinition()
       
    42 {
       
    43     QGridLayout * pageLayout = new QGridLayout();
       
    44     pageLayout->setSpacing(20);
       
    45     pageLayout->setColumnStretch(0, 1);
       
    46     pageLayout->setColumnStretch(1, 1);
       
    47     pageLayout->setContentsMargins(7, 7, 7, 0);
       
    48 
       
    49     QGroupBox * gb = new QGroupBox(this);
       
    50     QVBoxLayout * gbl = new QVBoxLayout;
       
    51 
       
    52     // details
       
    53     labelGameStats = new QLabel(this);
       
    54     QLabel * l = new QLabel(this);
       
    55     l->setTextFormat(Qt::RichText);
       
    56     l->setText("<h1><img src=\":/res/StatsD.png\"> " + PageGameStats::tr("Details") + "</h1>");
       
    57     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    58     labelGameStats->setTextFormat(Qt::RichText);
       
    59     labelGameStats->setAlignment(Qt::AlignTop);
       
    60     labelGameStats->setWordWrap(true);
       
    61     gbl->addWidget(l);
       
    62     gbl->addWidget(labelGameStats);
       
    63     gb->setLayout(gbl);
       
    64     pageLayout->addWidget(gb, 1, 1, 1, 2);
       
    65     
       
    66     // graph
       
    67     graphic = new FitGraphicsView(gb);
       
    68     l = new QLabel(this);
       
    69     l->setTextFormat(Qt::RichText);
       
    70     l->setText("<br><h1><img src=\":/res/StatsH.png\"> " + PageGameStats::tr("Health graph") + "</h1>");
       
    71     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    72     gbl->addWidget(l);
       
    73     gbl->addWidget(graphic);
       
    74     graphic->scale(1.0, -1.0);
       
    75     graphic->setBackgroundBrush(QBrush(Qt::black));
       
    76     
       
    77     labelGameWin = new QLabel(this);
       
    78     labelGameWin->setTextFormat(Qt::RichText);
       
    79     pageLayout->addWidget(labelGameWin, 0, 0, 1, 2);
       
    80 
       
    81     // ranking box
       
    82     gb = new QGroupBox(this);
       
    83     gbl = new QVBoxLayout;
       
    84     labelGameRank = new QLabel(gb);
       
    85     l = new QLabel(this);
       
    86     l->setTextFormat(Qt::RichText);
       
    87     l->setText("<h1><img src=\":/res/StatsR.png\"> " + PageGameStats::tr("Ranking") + "</h1>");
       
    88     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    89     gbl->addWidget(l);
       
    90     gbl->addWidget(labelGameRank);
       
    91     gb->setLayout(gbl);
       
    92 
       
    93     labelGameRank->setTextFormat(Qt::RichText);
       
    94     labelGameRank->setAlignment(Qt::AlignTop);
       
    95     pageLayout->addWidget(gb, 1, 0);
       
    96 
       
    97     return pageLayout;
       
    98 }
       
    99 
       
   100 QLayout * PageGameStats::footerLayoutDefinition()
       
   101 {
       
   102     QHBoxLayout * bottomLayout = new QHBoxLayout();
       
   103 
       
   104     btnSave = addButton(":/res/Save.png", bottomLayout, 0, true);
       
   105     btnSave->setStyleSheet("QPushButton{margin: 24px 0 0 0;}");
       
   106     bottomLayout->setAlignment(btnSave, Qt::AlignRight | Qt::AlignBottom);
       
   107 
       
   108     return bottomLayout;
       
   109 }
       
   110 
       
   111 void PageGameStats::connectSignals()
       
   112 {
       
   113     connect(btnSave, SIGNAL(clicked()), this, SIGNAL(saveDemoRequested()));
       
   114 }
       
   115 
       
   116 PageGameStats::PageGameStats(QWidget* parent) : AbstractPage(parent)
       
   117 {
       
   118     initPage();
       
   119 }
       
   120 
       
   121 void PageGameStats::AddStatText(const QString & msg)
       
   122 {
       
   123     labelGameStats->setText(labelGameStats->text() + msg);
       
   124 }
       
   125 
       
   126 void PageGameStats::clear()
       
   127 {
       
   128     labelGameStats->setText("");
       
   129     healthPoints.clear();
       
   130     labelGameRank->setText("");
       
   131     playerPosition = 0;
       
   132     lastColor = 0;
       
   133 }
       
   134 
       
   135 void PageGameStats::renderStats()
       
   136 {
       
   137     QGraphicsScene * scene = new QGraphicsScene();
       
   138 
       
   139     QMap<quint32, QVector<quint32> >::const_iterator i = healthPoints.constBegin();
       
   140     while (i != healthPoints.constEnd())
       
   141     {
       
   142         quint32 c = i.key();
       
   143         QColor clanColor = QColor(qRgb((c >> 16) & 255, (c >> 8) & 255, c & 255));
       
   144         QVector<quint32> hps = i.value();
       
   145 
       
   146         QPainterPath path;
       
   147         if (hps.size())
       
   148             path.moveTo(0, hps[0]);
       
   149 
       
   150         for(int t = 1; t < hps.size(); ++t)
       
   151             path.lineTo(t, hps[t]);
       
   152 
       
   153         scene->addPath(path, QPen(c));
       
   154         ++i;
       
   155     }
       
   156 
       
   157     graphic->setScene(scene);
       
   158     graphic->fitInView(graphic->sceneRect());
       
   159 }
       
   160 
       
   161 void PageGameStats::GameStats(char type, const QString & info)
       
   162 {
       
   163     switch(type) {
       
   164         case 'r' : {
       
   165             labelGameWin->setText(QString("<h1 align=\"center\">%1</h1>").arg(info));
       
   166             break;
       
   167         }
       
   168         case 'D' : {
       
   169             int i = info.indexOf(' ');
       
   170             QString message = "<p><img src=\":/res/StatsBestShot.png\"> " + PageGameStats::tr("The best shot award was won by <b>%1</b> with <b>%2</b> pts.").arg(info.mid(i + 1), info.left(i)) + "</p>";
       
   171             AddStatText(message);
       
   172             break;
       
   173         }
       
   174         case 'k' : {
       
   175             int i = info.indexOf(' ');
       
   176             int num = info.left(i).toInt();
       
   177             QString message = "<p><img src=\":/res/StatsBestKiller.png\"> " + PageGameStats::tr("The best killer is <b>%1</b> with <b>%2</b> kills in a turn.", "", num).arg(info.mid(i + 1), info.left(i)) + "</p>";
       
   178             AddStatText(message);
       
   179             break;
       
   180         }
       
   181         case 'K' : {
       
   182             int num = info.toInt();
       
   183             QString message = "<p><img src=\":/res/StatsHedgehogsKilled.png\"> " +  PageGameStats::tr("A total of <b>%1</b> hedgehog(s) were killed during this round.", "", num).arg(num) + "</p>";
       
   184             AddStatText(message);
       
   185             break;
       
   186         }
       
   187         case 'H' : {
       
   188             int i = info.indexOf(' ');
       
   189             quint32 clan = info.left(i).toInt();
       
   190             quint32 hp = info.mid(i + 1).toUInt();
       
   191             healthPoints[clan].append(hp);
       
   192             break;
       
   193         }
       
   194         case 'T': { // local team stats
       
   195             //AddStatText("<p>local team: " + info + "</p>");
       
   196             QStringList infol = info.split(":");
       
   197             HWTeam team(infol[0]);
       
   198             if(team.fileExists()) // do some better test to avoid influence from scripted/predefined teams?
       
   199             {
       
   200                 team.loadFromFile();
       
   201                 team.incRounds();
       
   202                 if(infol[1].toInt() > 0) // might require some better test for winning condition (or changed flag) ... WIP!
       
   203                     team.incWins(); // should draws count as wins?
       
   204                 //team.SaveToFile(); // don't save yet
       
   205             }
       
   206             break;
       
   207             }
       
   208 
       
   209         case 'P' : {
       
   210             int i = info.indexOf(' ');
       
   211             playerPosition++;
       
   212             QString color = info.left(i);
       
   213             quint32 c = color.toInt();
       
   214             QColor clanColor = QColor(qRgb((c >> 16) & 255, (c >> 8) & 255, c & 255));
       
   215 
       
   216             QString playerinfo = info.mid(i + 1);
       
   217 
       
   218             i = playerinfo.indexOf(' ');
       
   219 
       
   220             int kills = playerinfo.left(i).toInt();
       
   221             QString playername = playerinfo.mid(i + 1);
       
   222             QString image;
       
   223 
       
   224             if (lastColor == c) playerPosition--;
       
   225             lastColor = c;
       
   226 
       
   227             switch (playerPosition)
       
   228             {
       
   229                 case 1:
       
   230                 image = "<img src=\":/res/StatsMedal1.png\">";
       
   231                 break;
       
   232             case 2:
       
   233                 image = "<img src=\":/res/StatsMedal2.png\">";
       
   234                 break;
       
   235             case 3:
       
   236                 image = "<img src=\":/res/StatsMedal3.png\">";
       
   237                 break;
       
   238             default:
       
   239                 image = "<img src=\":/res/StatsMedal4.png\">";
       
   240                 break;
       
   241             }
       
   242 
       
   243             QString message;
       
   244             QString killstring = PageGameStats::tr("(%1 kill)", "", kills).arg(kills);
       
   245 
       
   246             message = QString("<p><h2>%1 %2. <font color=\"%4\">%3</font> ").arg(image, QString::number(playerPosition), playername, clanColor.name()) + killstring + "</h2></p>";
       
   247 
       
   248             labelGameRank->setText(labelGameRank->text() + message);
       
   249                 break;
       
   250         }
       
   251         case 's' : {
       
   252             int i = info.indexOf(' ');
       
   253             int num = info.left(i).toInt();
       
   254             QString message = "<p><img src=\":/res/StatsMostSelfDamage.png\"> " + PageGameStats::tr("<b>%1</b> thought it's good to shoot his own hedgehogs with <b>%2</b> pts.", "", num).arg(info.mid(i + 1)).arg(num) + "</p>";
       
   255             AddStatText(message);
       
   256             break;
       
   257         }
       
   258         case 'S' : {
       
   259             int i = info.indexOf(' ');
       
   260             int num = info.left(i).toInt();
       
   261             QString message = "<p><img src=\":/res/StatsSelfKilled.png\"> " + PageGameStats::tr("<b>%1</b> killed <b>%2</b> of his own hedgehogs.", "", num).arg(info.mid(i + 1)).arg(num) + "</p>";
       
   262             AddStatText(message);
       
   263             break;
       
   264         }
       
   265         case 'B' : {
       
   266             int i = info.indexOf(' ');
       
   267             int num = info.left(i).toInt();
       
   268             QString message = "<p><img src=\":/res/StatsSkipped.png\"> " + PageGameStats::tr("<b>%1</b> was scared and skipped turn <b>%2</b> times.", "", num).arg(info.mid(i + 1)).arg(num) + "</p>";
       
   269             AddStatText(message);
       
   270             break;
       
   271         }
       
   272 
       
   273     }
       
   274 }