QTfrontend/ui/page/pagegamestats.cpp
changeset 13218 d0647647a697
parent 13217 1357dfbfa29c
child 13231 6193503c4727
equal deleted inserted replaced
13217:1357dfbfa29c 13218:d0647647a697
   172         graphic->hide();
   172         graphic->hide();
   173     } else {
   173     } else {
   174         graphic->setScene(Q_NULLPTR);
   174         graphic->setScene(Q_NULLPTR);
   175         m_scene.reset(new QGraphicsScene(this));
   175         m_scene.reset(new QGraphicsScene(this));
   176 
   176 
   177         quint32 maxValue = 0;
   177         // min and max value across the entire chart
       
   178         qint32 minValue = 0;
       
   179         qint32 maxValue = 0;
       
   180         bool minMaxValuesInitialized = false;
       
   181 
       
   182         // max data points per clan
   178         int maxDataPoints = 0;
   183         int maxDataPoints = 0;
   179         for(QMap<quint32, QVector<quint32> >::const_iterator i = healthPoints.constBegin(); i != healthPoints.constEnd(); ++i)
   184         for(QMap<qint32, QVector<qint32> >::const_iterator i = healthPoints.constBegin(); i != healthPoints.constEnd(); ++i)
   180         {
   185         {
   181             maxDataPoints = qMax(maxDataPoints, i.value().size());
   186             maxDataPoints = qMax(maxDataPoints, i.value().size());
   182         }
   187         }
   183 
   188 
   184         /* There must be at least 2 data points for any clan,
   189         /* There must be at least 2 data points for any clan,
   187             labelGraphTitle->hide();
   192             labelGraphTitle->hide();
   188             graphic->hide();
   193             graphic->hide();
   189             return;
   194             return;
   190         }
   195         }
   191 
   196 
   192         QMap<quint32, QVector<quint32> >::const_iterator i = healthPoints.constBegin();
   197         QMap<qint32, QVector<qint32> >::const_iterator i = healthPoints.constBegin();
   193         while (i != healthPoints.constEnd())
   198         while (i != healthPoints.constEnd())
   194         {
   199         {
   195             quint32 c = i.key();
   200             qint32 c = i.key();
   196             const QVector<quint32>& hps = i.value();
   201             const QVector<qint32>& hps = i.value();
   197 
   202 
   198             QPainterPath path;
   203             QPainterPath path;
   199 
   204 
   200             if (hps.size())
   205             if (hps.size()) {
   201                 path.moveTo(0, hps[0]);
   206                 path.moveTo(0, hps[0]);
       
   207                 if(minMaxValuesInitialized) {
       
   208                     minValue = qMin(minValue, hps[0]);
       
   209                     maxValue = qMax(maxValue, hps[0]);
       
   210                 } else {
       
   211                     minValue = hps[0];
       
   212                     maxValue = hps[0];
       
   213                     minMaxValuesInitialized = true;
       
   214                 }
       
   215             }
   202 
   216 
   203             for(int t = 0; t < hps.size(); ++t) {
   217             for(int t = 0; t < hps.size(); ++t) {
   204                 path.lineTo(t, hps[t]);
   218                 path.lineTo(t, hps[t]);
   205                 maxValue = qMax(maxValue, hps[t]);
   219                 maxValue = qMax(maxValue, hps[t]);
       
   220                 minValue = qMin(minValue, hps[t]);
   206             }
   221             }
   207 
   222 
   208             QPen pen(c);
   223             QPen pen(c);
   209             pen.setWidth(2);
   224             pen.setWidth(2);
   210             pen.setCosmetic(true);
   225             pen.setCosmetic(true);
   212             m_scene->addPath(path, pen);
   227             m_scene->addPath(path, pen);
   213             ++i;
   228             ++i;
   214         }
   229         }
   215 
   230 
   216         graphic->setScene(m_scene.data());
   231         graphic->setScene(m_scene.data());
   217         graphic->setSceneRect(0, 0, maxDataPoints-1, qMax(maxValue, (quint32) 1));
   232 
       
   233         // Calculate the bounding box of the final chart
       
   234         qint32 sceneMinY = minValue;
       
   235         qint32 sceneMaxY = maxValue;
       
   236         // If all values are 0 or greater, make sure to include 0 at the bottom.
       
   237         if(sceneMinY >= 0 && sceneMaxY >= 0)
       
   238             sceneMinY = 0;
       
   239         // If all values are equal, we must increase sceneMaxY, otherwise the scene rect
       
   240         // would have a height of 0 and will screw up
       
   241         if(sceneMinY == sceneMaxY)
       
   242             sceneMaxY++;
       
   243         graphic->setSceneRect(0, sceneMinY, maxDataPoints-1, sceneMaxY - sceneMinY);
       
   244 
   218         graphic->fitInView(graphic->sceneRect());
   245         graphic->fitInView(graphic->sceneRect());
   219 
   246 
   220         graphic->show();
   247         graphic->show();
   221         labelGraphTitle->show();
   248         labelGraphTitle->show();
   222     }
   249     }
   256         }
   283         }
   257         case 'H' :
   284         case 'H' :
   258         {
   285         {
   259             int i = info.indexOf(' ');
   286             int i = info.indexOf(' ');
   260             quint32 clan = info.left(i).toInt();
   287             quint32 clan = info.left(i).toInt();
   261             quint32 hp = info.mid(i + 1).toUInt();
   288             qint32 hp = info.mid(i + 1).toInt();
   262             healthPoints[clan].append(hp);
   289             healthPoints[clan].append(hp);
   263             break;
   290             break;
   264         }
   291         }
   265         case 'g' :
   292         case 'g' :
   266         {
   293         {