QTfrontend/ui/page/pagegamestats.cpp
branchui-scaling
changeset 15304 c4fd2813b127
parent 15299 16f389fcd462
child 15488 a7d54832fad6
child 15801 dbed9069ed50
--- a/QTfrontend/ui/page/pagegamestats.cpp	Wed May 16 18:22:28 2018 +0200
+++ b/QTfrontend/ui/page/pagegamestats.cpp	Wed Jul 31 23:14:27 2019 +0200
@@ -42,38 +42,36 @@
 {
     kindOfPoints = QString("");
     defaultGraphTitle = true;
-    QGridLayout * pageLayout = new QGridLayout();
-    pageLayout->setSpacing(20);
-    pageLayout->setColumnStretch(0, 1);
-    pageLayout->setColumnStretch(1, 1);
+    pageLayout = new QGridLayout();
     pageLayout->setRowStretch(0, 1);
     pageLayout->setRowStretch(1, 20);
-    //pageLayout->setRowStretch(1, -1); this should work but there is unnecessary empty space betwin lines if used
+    pageLayout->setVerticalSpacing(20);
     pageLayout->setContentsMargins(7, 7, 7, 0);
 
-    QGroupBox * gb = new QGroupBox(this);
+    gbDetails = new QGroupBox(this);
+    gbDetails->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
     QVBoxLayout * gbl = new QVBoxLayout;
 
     // details
     labelGameStats = new QLabel(this);
-    QLabel * l = new QLabel(this);
-    l->setTextFormat(Qt::RichText);
-    l->setText("<h1><img src=\":/res/StatsD.png\"> " + PageGameStats::tr("Details") + "</h1>");
-    l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+    labelDetails = new QLabel(this);
+    labelDetails->setTextFormat(Qt::RichText);
+    labelDetails->setText("<h1><img src=\":/res/StatsD.png\"> " + PageGameStats::tr("Details").toHtmlEscaped() + "</h1>");
+    labelDetails->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
     labelGameStats->setTextFormat(Qt::RichText);
     labelGameStats->setAlignment(Qt::AlignTop);
     labelGameStats->setWordWrap(true);
-    gbl->addWidget(l);
+    gbl->addWidget(labelDetails);
     gbl->addWidget(labelGameStats);
-    gb->setLayout(gbl);
-    pageLayout->addWidget(gb, 1, 1);
+    gbDetails->setLayout(gbl);
+    pageLayout->addWidget(gbDetails, 1, 1);
 
     // graph
-    graphic = new FitGraphicsView(gb);
+    graphic = new FitGraphicsView(gbDetails);
     graphic->setObjectName("gameStatsView");
     labelGraphTitle = new QLabel(this);
     labelGraphTitle->setTextFormat(Qt::RichText);
-    labelGraphTitle->setText("<br><h1><img src=\":/res/StatsH.png\"> " + PageGameStats::tr("Health graph") + "</h1>");
+    labelGraphTitle->setText("<h1><img src=\":/res/StatsH.png\"> " + PageGameStats::tr("Health graph").toHtmlEscaped() + "</h1>");
     labelGraphTitle->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
     gbl->addWidget(labelGraphTitle);
     gbl->addWidget(graphic);
@@ -86,20 +84,21 @@
     pageLayout->addWidget(labelGameWin, 0, 0, 1, 2);
 
     // ranking box
-    gb = new QGroupBox(this);
+    gbRanks = new QGroupBox(this);
+    gbRanks->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
     gbl = new QVBoxLayout;
-    labelGameRank = new QLabel(gb);
-    l = new QLabel(this);
+    labelGameRank = new QLabel(gbRanks);
+    QLabel* l = new QLabel(this);
     l->setTextFormat(Qt::RichText);
-    l->setText("<h1><img src=\":/res/StatsR.png\"> " + PageGameStats::tr("Ranking") + "</h1>");
+    l->setText("<h1><img src=\":/res/StatsR.png\"> " + PageGameStats::tr("Ranking").toHtmlEscaped() + "</h1>");
     l->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
     gbl->addWidget(l);
     gbl->addWidget(labelGameRank);
-    gb->setLayout(gbl);
+    gbRanks->setLayout(gbl);
 
     labelGameRank->setTextFormat(Qt::RichText);
     labelGameRank->setAlignment(Qt::AlignTop);
-    pageLayout->addWidget(gb, 1, 0);
+    pageLayout->addWidget(gbRanks, 1, 0);
 
     return pageLayout;
 }
@@ -143,6 +142,9 @@
 void PageGameStats::AddStatText(const QString & msg)
 {
     labelGameStats->setText(labelGameStats->text() + msg);
+    labelDetails->show();
+    labelGameStats->show();
+    gbDetails->show();
 }
 
 void PageGameStats::clear()
@@ -152,7 +154,16 @@
     labelGameRank->setText("");
     labelGameWin->setText("");
     playerPosition = 0;
+    scriptPlayerPosition = 0;
     lastColor = 0;
+    graphic->hide();
+    labelDetails->hide();
+    labelGameStats->hide();
+    gbDetails->hide();
+    gbRanks->hide();
+    pageLayout->setColumnStretch(0, 0);
+    pageLayout->setColumnStretch(1, 0);
+    pageLayout->setHorizontalSpacing(0);
 }
 
 void PageGameStats::restartBtnVisible(bool visible)
@@ -160,10 +171,15 @@
     btnRestart->setVisible(visible);
 }
 
+void PageGameStats::saveDemoBtnEnabled(bool enabled)
+{
+    btnSave->setEnabled(enabled);
+}
+
 void PageGameStats::renderStats()
 {
     if(defaultGraphTitle) {
-        labelGraphTitle->setText("<br><h1><img src=\":/res/StatsH.png\"> " + PageGameStats::tr("Health graph") + "</h1>");
+        labelGraphTitle->setText("<h1><img src=\":/res/StatsH.png\"> " + PageGameStats::tr("Health graph").toHtmlEscaped() + "</h1>");
     } else {
         defaultGraphTitle = true;
     }
@@ -173,6 +189,7 @@
         graphic->hide();
     } else {
         graphic->setScene(Q_NULLPTR);
+        gbDetails->show();
         m_scene.reset(new QGraphicsScene(this));
 
         // min and max value across the entire chart
@@ -192,6 +209,7 @@
         if(maxDataPoints < 2) {
             labelGraphTitle->hide();
             graphic->hide();
+            applySpacing();
             return;
         }
 
@@ -221,11 +239,25 @@
                 minValue = qMin(minValue, hps[t]);
             }
 
-            QPen pen(c);
+            // Draw clan health/score graph lines
+            QColor col = QColor(c);
+
+            // Special pen for very dark clan colors
+            if (!(col.red() >= cInvertTextColorAt || col.green() >= cInvertTextColorAt || col.blue() >= cInvertTextColorAt))
+            {
+                QPen pen_marker(QColor(255, 255, 255));
+                pen_marker.setWidth(3);
+                pen_marker.setStyle(Qt::DotLine);
+                pen_marker.setCosmetic(true);
+                m_scene->addPath(path, pen_marker);
+            }
+
+            // Regular pen
+            QPen pen(col);
             pen.setWidth(2);
             pen.setCosmetic(true);
+            m_scene->addPath(path, pen);
 
-            m_scene->addPath(path, pen);
             ++i;
         }
 
@@ -247,6 +279,22 @@
 
         graphic->show();
         labelGraphTitle->show();
+        gbDetails->show();
+    }
+    applySpacing();
+}
+
+void PageGameStats::applySpacing()
+{
+    if (!labelGameStats->isHidden())
+    {
+        labelGraphTitle->setText("<br>" + labelGraphTitle->text());
+    }
+    if ((!gbDetails->isHidden()) && (!gbRanks->isHidden()))
+    {
+        pageLayout->setColumnStretch(0, 1);
+        pageLayout->setColumnStretch(1, 1);
+        pageLayout->setHorizontalSpacing(20);
     }
 }
 
@@ -256,14 +304,14 @@
     {
         case 'r' :
         {
-            labelGameWin->setText(QString("<h1 align=\"center\">%1</h1>").arg(info));
+            labelGameWin->setText(QString("<h1 align=\"center\">%1</h1>").arg(info.toHtmlEscaped()));
             break;
         }
         case 'D' :
         {
             int i = info.indexOf(' ');
             int num = info.left(i).toInt();
-            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.", "", num).arg(info.mid(i + 1), info.left(i)) + "</p>";
+            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.", "", num).arg(info.mid(i + 1).toHtmlEscaped(), info.left(i)) + "</p>";
             AddStatText(message);
             break;
         }
@@ -271,7 +319,7 @@
         {
             int i = info.indexOf(' ');
             int num = info.left(i).toInt();
-            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>";
+            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).toHtmlEscaped(), info.left(i)) + "</p>";
             AddStatText(message);
             break;
         }
@@ -294,22 +342,12 @@
         {
             // TODO: change default picture or add change pic capability
             defaultGraphTitle = false;
-            labelGraphTitle->setText("<br><h1><img src=\":/res/StatsR.png\"> " + info + "</h1>");
+            labelGraphTitle->setText("<h1><img src=\":/res/StatsR.png\"> " + info.toHtmlEscaped() + "</h1>");
             break;
         }
         case 'T':   // local team stats
         {
-            //AddStatText("<p>local team: " + info + "</p>");
-            QStringList infol = info.split(":");
-            HWTeam team(infol[0]);
-            if(team.fileExists()) // do some better test to avoid influence from scripted/predefined teams?
-            {
-                team.loadFromFile();
-                team.incRounds();
-                if(infol[1].toInt() > 0) // might require some better test for winning condition (or changed flag) ... WIP!
-                    team.incWins(); // should draws count as wins?
-                //team.SaveToFile(); // don't save yet
-            }
+            // unused
             break;
         }
         case 'p' :
@@ -329,14 +367,21 @@
 
             i = playerinfo.indexOf(' ');
 
-            int kills = playerinfo.left(i).toInt();
+            QString killsString = playerinfo.left(i);
+            int kills = killsString.toInt();
             QString playername = playerinfo.mid(i + 1);
             QString image;
 
             if (lastColor == c) playerPosition--;
             lastColor = c;
 
-            switch (playerPosition)
+            unsigned int realPlayerPosition;
+            if(scriptPlayerPosition == 0)
+                realPlayerPosition = playerPosition;
+            else
+                realPlayerPosition = scriptPlayerPosition;
+
+            switch (realPlayerPosition)
             {
                 case 1:
                     image = "<img src=\":/res/StatsMedal1.png\">";
@@ -354,25 +399,43 @@
 
             QString message;
             QString killstring;
-            if(kindOfPoints.compare("") == 0) {
+            if(kindOfPoints.isEmpty()) {
                 //: Number of kills in stats screen, written after the team name
                 killstring = PageGameStats::tr("(%1 kill)", "", kills).arg(kills);
+            } else if (kindOfPoints == "!POINTS") {
+                //: Number of points in stats screen, written after the team name
+                killstring = PageGameStats::tr("(%1 point(s))", "", kills).arg(kills);
+            } else if (kindOfPoints == "!TIME") {
+                //: Time in seconds
+                killstring = PageGameStats::tr("(%L1 second(s))", "", kills).arg((double) kills/1000, 0, 'f', 3);
+            } else if (kindOfPoints.startsWith("!TIME") && kindOfPoints.length() == 6) {
+                int len = kindOfPoints.at(6).digitValue();
+                if(len != -1)
+                    killstring = PageGameStats::tr("(%L1 second(s))", "", kills).arg((double) kills/1000, 0, 'f', len);
+                else
+                    qWarning("SendStat: siPointType received with !TIME and invalid number length!");
+            } else if (kindOfPoints == "!CRATES") {
+                killstring = PageGameStats::tr("(%1 crate(s))", "", kills).arg(kills);
+            } else if (kindOfPoints == "!EMPTY") {
+                killstring = QString("");
             } else {
                 //: For custom number of points in the stats screen, written after the team name. %1 is the number, %2 is the word. Example: “4 points”
                 killstring = PageGameStats::tr("(%1 %2)", "", kills).arg(kills).arg(kindOfPoints);
-                kindOfPoints = QString("");
             }
+            kindOfPoints = QString("");
 
-            message = QString("<p><h2>%1 %2. <font color=\"%4\">%3</font> ").arg(image, QString::number(playerPosition), playername, clanColor.name()) + killstring + "</h2></p>";
+            message = QString("<p><h2>%1 %2. <font color=\"%4\">%3</font> ").arg(image, QString::number(realPlayerPosition), playername.toHtmlEscaped(), clanColor.name().toHtmlEscaped()) + killstring.toHtmlEscaped() + "</h2></p>";
 
             labelGameRank->setText(labelGameRank->text() + message);
+            scriptPlayerPosition = 0;
+            gbRanks->show();
             break;
         }
         case 's' :
         {
             int i = info.indexOf(' ');
             int num = info.left(i).toInt();
-            QString message = "<p><img src=\":/res/StatsMostSelfDamage.png\"> " + PageGameStats::tr("<b>%1</b> thought it's good to shoot their own hedgehogs for <b>%2</b> pts.", "", num).arg(info.mid(i + 1)).arg(num) + "</p>";
+            QString message = "<p><img src=\":/res/StatsMostSelfDamage.png\"> " + PageGameStats::tr("<b>%1</b> thought it's good to shoot their own hedgehogs for <b>%2</b> pts.", "", num).arg(info.mid(i + 1).toHtmlEscaped()).arg(num) + "</p>";
             AddStatText(message);
             break;
         }
@@ -380,7 +443,7 @@
         {
             int i = info.indexOf(' ');
             int num = info.left(i).toInt();
-            QString message = "<p><img src=\":/res/StatsSelfKilled.png\"> " + PageGameStats::tr("<b>%1</b> killed <b>%2</b> of their own hedgehogs.", "", num).arg(info.mid(i + 1)).arg(num) + "</p>";
+            QString message = "<p><img src=\":/res/StatsSelfKilled.png\"> " + PageGameStats::tr("<b>%1</b> killed <b>%2</b> of their own hedgehogs.", "", num).arg(info.mid(i + 1).toHtmlEscaped()).arg(num) + "</p>";
             AddStatText(message);
             break;
         }
@@ -388,16 +451,26 @@
         {
             int i = info.indexOf(' ');
             int num = info.left(i).toInt();
-            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>";
+            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).toHtmlEscaped()).arg(num) + "</p>";
             AddStatText(message);
             break;
         }
         case 'c' :
         {
-            QString message = "<p><img src=\":/res/StatsCustomAchievement.png\"> "+info+" </p>";
+            QString message = "<p><img src=\":/res/StatsCustomAchievement.png\"> "+info.toHtmlEscaped()+" </p>";
             AddStatText(message);
             break;
         }
-
+        case 'R' :
+        {
+            scriptPlayerPosition = info.toInt();
+            break;
+        }
+        case 'h' :
+        {
+            QString message = "<p><img src=\":/res/StatsEverAfter.png\"> " + PageGameStats::tr("With everyone having the same clan color, there was no reason to fight. And so the hedgehogs happily lived in peace ever after.").toHtmlEscaped() + "</p>";
+            AddStatText(message);
+            break;
+        }
     }
 }