QTfrontend/statsPage.cpp
changeset 5205 78138ae93820
parent 5204 e1a5f4d5d86a
child 5206 db775bddf771
equal deleted inserted replaced
5204:e1a5f4d5d86a 5205:78138ae93820
     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 <QGraphicsScene>
       
    22 #include <QGroupBox>
       
    23 #include <QSizePolicy>
       
    24 #include "statsPage.h"
       
    25 #include "team.h"
       
    26 
       
    27 FitGraphicsView::FitGraphicsView(QWidget* parent) : QGraphicsView(parent)
       
    28 {
       
    29 
       
    30 }
       
    31 
       
    32 void FitGraphicsView::resizeEvent(QResizeEvent * event)
       
    33 {
       
    34     Q_UNUSED(event);
       
    35 
       
    36     fitInView(sceneRect());
       
    37 }
       
    38 
       
    39 PageGameStats::PageGameStats(QWidget* parent) : AbstractPage(parent)
       
    40 {
       
    41     QGridLayout * pageLayout = new QGridLayout(this);
       
    42     pageLayout->setSpacing(20);
       
    43     pageLayout->setColumnStretch(0, 1);
       
    44     pageLayout->setColumnStretch(1, 1);
       
    45 
       
    46     BtnBack = addButton(":/res/Exit.png", pageLayout, 3, 0, true);
       
    47     BtnBack->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
       
    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);
       
    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 
       
    98 void PageGameStats::AddStatText(const QString & msg)
       
    99 {
       
   100     labelGameStats->setText(labelGameStats->text() + msg);
       
   101 }
       
   102 
       
   103 void PageGameStats::clear()
       
   104 {
       
   105     labelGameStats->setText("");
       
   106     healthPoints.clear();
       
   107     labelGameRank->setText("");
       
   108     playerPosition = 0;
       
   109     lastColor = 0;
       
   110 }
       
   111 
       
   112 void PageGameStats::renderStats()
       
   113 {
       
   114     QGraphicsScene * scene = new QGraphicsScene();
       
   115 
       
   116     QMap<quint32, QVector<quint32> >::const_iterator i = healthPoints.constBegin();
       
   117     while (i != healthPoints.constEnd())
       
   118     {
       
   119         quint32 c = i.key();
       
   120         QColor clanColor = QColor(qRgb((c >> 16) & 255, (c >> 8) & 255, c & 255));
       
   121         QVector<quint32> hps = i.value();
       
   122 
       
   123         QPainterPath path;
       
   124         if (hps.size())
       
   125             path.moveTo(0, hps[0]);
       
   126 
       
   127         for(int t = 1; t < hps.size(); ++t)
       
   128             path.lineTo(t, hps[t]);
       
   129 
       
   130         scene->addPath(path, QPen(c));
       
   131         ++i;
       
   132     }
       
   133 
       
   134     graphic->setScene(scene);
       
   135     graphic->fitInView(graphic->sceneRect());
       
   136 }
       
   137 
       
   138 void PageGameStats::GameStats(char type, const QString & info)
       
   139 {
       
   140     switch(type) {
       
   141         case 'r' : {
       
   142             labelGameWin->setText(QString("<h1 align=\"center\">%1</h1>").arg(info));
       
   143             break;
       
   144         }
       
   145         case 'D' : {
       
   146             int i = info.indexOf(' ');
       
   147             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>";
       
   148             AddStatText(message);
       
   149             break;
       
   150         }
       
   151         case 'k' : {
       
   152             int i = info.indexOf(' ');
       
   153             int num = info.left(i).toInt();
       
   154             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>";
       
   155             AddStatText(message);
       
   156             break;
       
   157         }
       
   158         case 'K' : {
       
   159             int num = info.toInt();
       
   160             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>";
       
   161             AddStatText(message);
       
   162             break;
       
   163         }
       
   164         case 'H' : {
       
   165             int i = info.indexOf(' ');
       
   166             quint32 clan = info.left(i).toInt();
       
   167             quint32 hp = info.mid(i + 1).toUInt();
       
   168             healthPoints[clan].append(hp);
       
   169             break;
       
   170         }
       
   171         case 'T': { // local team stats
       
   172             //AddStatText("<p>local team: " + info + "</p>");
       
   173             QStringList infol = info.split(":");
       
   174             HWTeam team(infol[0]);
       
   175             if(team.FileExists()) // do some better test to avoid influence from scripted/predefined teams?
       
   176             {
       
   177                 team.LoadFromFile();
       
   178                 team.Rounds++;
       
   179                 if(infol[1].toInt() > 0) // might require some better test for winning condition (or changed flag) ... WIP!
       
   180                     team.Wins++; // should draws count as wins?
       
   181                 //team.SaveToFile(); // don't save yet
       
   182             }
       
   183             break;
       
   184             }
       
   185 
       
   186         case 'P' : {
       
   187             int i = info.indexOf(' ');
       
   188             playerPosition++;
       
   189             QString color = info.left(i);
       
   190             quint32 c = color.toInt();
       
   191             QColor clanColor = QColor(qRgb((c >> 16) & 255, (c >> 8) & 255, c & 255));
       
   192 
       
   193             QString playerinfo = info.mid(i + 1);
       
   194 
       
   195             i = playerinfo.indexOf(' ');
       
   196 
       
   197             int kills = playerinfo.left(i).toInt();
       
   198             QString playername = playerinfo.mid(i + 1);
       
   199             QString image;
       
   200 
       
   201             if (lastColor == c) playerPosition--;
       
   202             lastColor = c;
       
   203 
       
   204             switch (playerPosition)
       
   205             {
       
   206                 case 1:
       
   207                 image = "<img src=\":/res/StatsMedal1.png\">";
       
   208                 break;
       
   209             case 2:
       
   210                 image = "<img src=\":/res/StatsMedal2.png\">";
       
   211                 break;
       
   212             case 3:
       
   213                 image = "<img src=\":/res/StatsMedal3.png\">";
       
   214                 break;
       
   215             default:
       
   216                 image = "<img src=\":/res/StatsMedal4.png\">";
       
   217                 break;
       
   218             }
       
   219 
       
   220             QString message;
       
   221             QString killstring = PageGameStats::tr("(%1 kill)", "", kills).arg(kills);
       
   222 
       
   223             message = QString("<p><h2>%1 %2. <font color=\"%4\">%3</font> ").arg(image, QString::number(playerPosition), playername, clanColor.name()) + killstring + "</h2></p>";
       
   224 
       
   225             labelGameRank->setText(labelGameRank->text() + message);
       
   226                 break;
       
   227         }
       
   228         case 's' : {
       
   229             int i = info.indexOf(' ');
       
   230             int num = info.left(i).toInt();
       
   231             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>";
       
   232             AddStatText(message);
       
   233             break;
       
   234         }
       
   235         case 'S' : {
       
   236             int i = info.indexOf(' ');
       
   237             int num = info.left(i).toInt();
       
   238             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>";
       
   239             AddStatText(message);
       
   240             break;
       
   241         }
       
   242         case 'B' : {
       
   243             int i = info.indexOf(' ');
       
   244             int num = info.left(i).toInt();
       
   245             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>";
       
   246             AddStatText(message);
       
   247             break;
       
   248         }
       
   249 
       
   250     }
       
   251 }