VirtualBox

Changeset 80434 in vbox for trunk/src/VBox/Frontends


Ignore:
Timestamp:
Aug 26, 2019 7:21:55 PM (5 years ago)
Author:
vboxsync
Message:

FE/Qt: bugref:9510: Some refactoring.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/src/runtime/information/UIInformationRuntime.cpp

    r80420 r80434  
    133133    const QStringList &textList() const;
    134134
    135     bool drawPieChart() const;
    136     void setDrawPieChart(bool fDrawPieChart);
     135    bool withPieChart() const;
     136    void setWithPieChart(bool fWithPieChart);
    137137
    138138    bool useGradientLineColor() const;
     
    160160
    161161    virtual void computeFontSize();
    162     void drawXAxisLabels(QPainter &painter, int iXSubAxisCount);
    163     void drawPieCharts(QPainter &painter, qulonglong iMaximum);
    164     void insertInfoLine(InfoLine enmInfoLine, const QString& strLabel, const QString &strInfo);
     162    /** @name Drawing helper functions.
     163     * @{ */
     164       void drawXAxisLabels(QPainter &painter, int iXSubAxisCount);
     165       void drawPieCharts(QPainter &painter, qulonglong iMaximum);
     166       /** Drawing an overlay rectangle over the charts to indicate that they are disabled. */
     167       void drawDisabledChartRectangle(QPainter &painter);
     168    /** @} */
    165169
    166170    UIMetric *m_pMetric;
     
    172176    int m_iMarginBottom;
    173177    QStringList m_textList;
    174     int m_iPieChartSize;
    175     QRect m_pieChartRect[DATA_SERIES_SIZE];
    176     bool m_fDrawPieChart;
     178    QRect m_lineChartRect;
     179    int m_iPieChartRadius;
     180    int m_iPieChartSpacing;
     181    bool m_fWithPieChart;
    177182    bool m_fUseGradientLineColor;
    178183    QColor m_dataSeriesColor[DATA_SERIES_SIZE];
     
    409414    , m_pMetric(pMetric)
    410415    , m_size(QSize(50, 50))
    411     , m_fDrawPieChart(false)
     416    , m_fWithPieChart(false)
    412417    , m_fUseGradientLineColor(false)
    413418{
     
    415420    connect(this, &UIChart::customContextMenuRequested,
    416421            this, &UIChart::sltCreateContextMenu);
    417 
    418422
    419423    m_dataSeriesColor[0] = QColor(Qt::red);
     
    424428    m_iMarginTop = 0.3 * qApp->QApplication::style()->pixelMetric(QStyle::PM_LayoutTopMargin);
    425429    m_iMarginBottom = 2 * qApp->QApplication::style()->pixelMetric(QStyle::PM_LayoutTopMargin);
    426     m_iPieChartSize = 1.5f * qApp->style()->pixelMetric(QStyle::PM_LargeIconSize);
    427     m_pieChartRect[0] = QRect(1.5 * m_iMarginLeft, 1.5 * m_iMarginTop, m_iPieChartSize, m_iPieChartSize);
    428     m_pieChartRect[1] = QRect(m_pieChartRect[0].x() + m_iPieChartSize + 0.5 * m_iMarginLeft, 1.5 * m_iMarginTop, m_iPieChartSize, m_iPieChartSize);
    429     m_size = QSize(6 * m_iPieChartSize, 2 * m_iPieChartSize);
     430
     431    m_iPieChartRadius = 1.2f * qApp->style()->pixelMetric(QStyle::PM_LargeIconSize);
     432    m_iPieChartSpacing = 0.3 * qApp->QApplication::style()->pixelMetric(QStyle::PM_LayoutTopMargin);
     433    m_size = QSize(10 * m_iPieChartRadius,  2 * m_iPieChartRadius);
    430434    retranslateUi();
    431435}
     
    452456}
    453457
    454 bool UIChart::drawPieChart() const
    455 {
    456     return m_fDrawPieChart;
    457 }
    458 
    459 void UIChart::setDrawPieChart(bool fDrawPieChart)
    460 {
    461     if (m_fDrawPieChart == fDrawPieChart)
     458bool UIChart::withPieChart() const
     459{
     460    return m_fWithPieChart;
     461}
     462
     463void UIChart::setWithPieChart(bool fWithPieChart)
     464{
     465    if (m_fWithPieChart == fWithPieChart)
    462466        return;
    463     m_fDrawPieChart = fDrawPieChart;
     467    m_fWithPieChart = fWithPieChart;
    464468    update();
    465469}
     
    545549{
    546550    Q_UNUSED(pEvent);
     551    if (!m_pMetric || iMaximumQueueSize <= 1)
     552        return;
     553
    547554    QPainter painter(this);
    548555    painter.setRenderHint(QPainter::Antialiasing);
    549556
    550557    /* Draw a rectanglar grid over which we will draw the line graphs: */
    551     int iChartHeight = height() - (m_iMarginTop + m_iMarginBottom);
    552     int iChartWidth = width() - (m_iMarginLeft + m_iMarginRight);
     558    QPoint chartTopLeft(m_iMarginLeft, m_iMarginTop);
     559    QSize chartSize(width() - (m_iMarginLeft + m_iMarginRight), height() - (m_iMarginTop + m_iMarginBottom));
     560
     561    m_lineChartRect = QRect(chartTopLeft, chartSize);
    553562    QColor mainAxisColor(120, 120, 120);
    554563    QColor subAxisColor(200, 200, 200);
    555564    /* Draw the main axes: */
    556565    painter.setPen(mainAxisColor);
    557     painter.drawRect(QRect(m_iMarginLeft, m_iMarginTop, iChartWidth, iChartHeight));
     566    painter.drawRect(m_lineChartRect);
    558567
    559568    /* draw Y subaxes: */
     
    562571    for (int i = 0; i < iYSubAxisCount; ++i)
    563572    {
    564         float fSubAxisY = m_iMarginTop + (i + 1) * iChartHeight / (float) (iYSubAxisCount + 1);
    565         painter.drawLine(m_iMarginLeft, fSubAxisY,
    566                          width() - m_iMarginRight, fSubAxisY);
     573        float fSubAxisY = m_iMarginTop + (i + 1) * m_lineChartRect.height() / (float) (iYSubAxisCount + 1);
     574        painter.drawLine(m_lineChartRect.left(), fSubAxisY,
     575                         m_lineChartRect.right(), fSubAxisY);
    567576    }
    568577
     
    571580    for (int i = 0; i < iXSubAxisCount; ++i)
    572581    {
    573         float fSubAxisX = m_iMarginLeft + (i + 1) * iChartWidth / (float) (iXSubAxisCount + 1);
    574         painter.drawLine(fSubAxisX, m_iMarginTop, fSubAxisX, height() - m_iMarginBottom);
    575     }
    576 
    577     QFontMetrics fontMetrics(painter.font());
    578     int iFontHeight = fontMetrics.height();
     582        float fSubAxisX = m_lineChartRect.left() + (i + 1) * m_lineChartRect.width() / (float) (iXSubAxisCount + 1);
     583        painter.drawLine(fSubAxisX, m_lineChartRect.top(), fSubAxisX, m_lineChartRect.bottom());
     584    }
    579585
    580586    /* Draw XAxis tick labels: */
     
    582588    drawXAxisLabels(painter, iXSubAxisCount);
    583589
     590
    584591    /* Draw a half-transparent rectangle over the whole widget to indicate the it is disabled: */
    585592    if (!isEnabled())
    586593    {
    587         painter.setPen(Qt::NoPen);
    588         painter.setBrush(QColor(60, 60, 60, 80));
    589         painter.drawRect(QRect(0, 0, width(), height()));
    590         painter.setPen(QColor(20, 20, 20, 180));
    591         QFont font = painter.font();
    592         font.setBold(true);
    593         /** @todo make this size dynamic. aka. autoscale the font. */
    594         font.setPixelSize(16);
    595         painter.setFont(font);
    596         painter.drawText(2 * m_iMarginLeft, 15 * m_iMarginTop, m_strGAWarning);
    597 
     594        drawDisabledChartRectangle(painter);
    598595        return;
    599596    }
    600 
    601     if (!m_pMetric || iMaximumQueueSize <= 1)
    602         return;
    603597
    604598    qulonglong iMaximum = m_pMetric->maximum();
     
    606600        return;
    607601    /* Draw the data lines: */
    608     float fBarWidth = iChartWidth / (float) (iMaximumQueueSize - 1);
    609     float fH = iChartHeight / (float)iMaximum;
     602    float fBarWidth = m_lineChartRect.width() / (float) (iMaximumQueueSize - 1);
     603    float fH = m_lineChartRect.height() / (float)iMaximum;
    610604    for (int k = 0; k < DATA_SERIES_SIZE; ++k)
    611605    {
    612606        if (m_fUseGradientLineColor)
    613607        {
    614             QLinearGradient gradient(0, 0, 0, iChartHeight);
     608            QLinearGradient gradient(0, 0, 0, m_lineChartRect.height());
    615609            gradient.setColorAt(0, Qt::black);
    616610            gradient.setColorAt(1, m_dataSeriesColor[k]);
     
    633627    }
    634628
     629    QFontMetrics fontMetrics(painter.font());
     630    int iFontHeight = fontMetrics.height();
     631
    635632    /* Draw YAxis tick labels: */
    636633    painter.setPen(mainAxisColor);
    637634    for (int i = 0; i < iYSubAxisCount + 2; ++i)
    638635    {
    639         int iTextY = 0.5 * iFontHeight + m_iMarginTop + i * iChartHeight / (float) (iYSubAxisCount + 1);
     636        int iTextY = 0.5 * iFontHeight + m_iMarginTop + i * m_lineChartRect.height() / (float) (iYSubAxisCount + 1);
    640637        QString strValue;
    641638        qulonglong iValue = (iYSubAxisCount + 1 - i) * (iMaximum / (float) (iYSubAxisCount + 1));
     
    653650    }
    654651
    655     if (m_fDrawPieChart)
     652    if (m_fWithPieChart)
    656653        drawPieCharts(painter, iMaximum);
    657654}
     
    661658    QFontMetrics fontMetrics(painter.font());
    662659    int iFontHeight = fontMetrics.height();
    663     int iChartWidth = width() - (m_iMarginLeft + m_iMarginRight);
     660
    664661    int iTotalSeconds = iPeriod * iMaximumQueueSize;
    665662    for (int i = 0; i < iXSubAxisCount + 2; ++i)
    666663    {
    667         int iTextX = m_iMarginLeft + i * iChartWidth / (float) (iXSubAxisCount + 1);
     664        int iTextX = m_lineChartRect.left() + i * m_lineChartRect.width() / (float) (iXSubAxisCount + 1);
    668665        QString strCurrentSec = QString::number(iTotalSeconds - i * iTotalSeconds / (float)(iXSubAxisCount + 1));
    669666        int iTextWidth = fontMetrics.width(strCurrentSec);
     
    671668        {
    672669            strCurrentSec += " " + m_strXAxisLabel;
    673             painter.drawText(iTextX, height() - m_iMarginBottom + iFontHeight, strCurrentSec);
     670            painter.drawText(iTextX, m_lineChartRect.bottom() + iFontHeight, strCurrentSec);
    674671        }
    675672        else
    676             painter.drawText(iTextX - 0.5 * iTextWidth, height() - m_iMarginBottom + iFontHeight, strCurrentSec);
     673            painter.drawText(iTextX - 0.5 * iTextWidth, m_lineChartRect.bottom() + iFontHeight, strCurrentSec);
    677674    }
    678675}
     
    680677void UIChart::drawPieCharts(QPainter &painter, qulonglong iMaximum)
    681678{
     679    int iAlpha = 80;
    682680    for (int i = 0; i < DATA_SERIES_SIZE; ++i)
    683681    {
     
    686684        if (!data || data->isEmpty())
    687685            continue;
     686        QRect chartRect(QPoint((i+1) * m_iPieChartSpacing + i * m_iPieChartRadius + m_iMarginLeft, m_iPieChartSpacing + m_iMarginTop),
     687                        QSize(m_iPieChartRadius, m_iPieChartRadius));
    688688
    689689        /* Draw a whole non-filled circle: */
    690         painter.setPen(QPen(Qt::gray, 1));
    691         painter.drawArc(m_pieChartRect[i], 0, 3600 * 16);
    692 
    693         QPointF center(m_pieChartRect[i].center());
     690        painter.setPen(QPen(QColor(100, 100, 100, iAlpha), 1));
     691        painter.drawArc(chartRect, 0, 3600 * 16);
     692
     693        QPointF center(chartRect.center());
    694694        QPainterPath fillPath;
    695695        fillPath.moveTo(center);
    696         fillPath.arcTo(m_pieChartRect[i], 90/*startAngle*/,
     696        fillPath.arcTo(chartRect, 90/*startAngle*/,
    697697                       -1 * 360 /*sweepLength*/);
    698698
    699699        /* First draw a white filled circle and that the arc for data: */
    700700        painter.setPen(Qt::NoPen);
    701         painter.setBrush(QColor(255, 255, 255, 175));
     701        painter.setBrush(QColor(255, 255, 255, iAlpha));
    702702        painter.drawPath(fillPath);
    703703
    704704        /* Prepare the gradient for the pie chart: */
    705705        QConicalGradient pieGradient;
    706         pieGradient.setCenter(m_pieChartRect[i].center());
     706        pieGradient.setCenter(chartRect.center());
    707707        pieGradient.setAngle(90);
    708         pieGradient.setColorAt(0, Qt::black);
    709         pieGradient.setColorAt(1, m_dataSeriesColor[i]);
     708        pieGradient.setColorAt(0, QColor(0, 0, 0, iAlpha));
     709        QColor pieColor(m_dataSeriesColor[i]);
     710        pieColor.setAlpha(iAlpha);
     711        pieGradient.setColorAt(1, pieColor);
    710712
    711713        QPainterPath dataPath;
    712714        dataPath.moveTo(center);
    713715        float fAngle = 360.f * data->back() / (float)iMaximum;
    714         dataPath.arcTo(m_pieChartRect[i], 90/*startAngle*/,
     716        dataPath.arcTo(chartRect, 90/*startAngle*/,
    715717                       -1 * fAngle /*sweepLength*/);
    716718        painter.setBrush(pieGradient);
    717719        painter.drawPath(dataPath);
    718 
    719     }
    720 }
     720    }
     721}
     722
     723void UIChart::drawDisabledChartRectangle(QPainter &painter)
     724{
     725    painter.setPen(Qt::NoPen);
     726    painter.setBrush(QColor(60, 60, 60, 80));
     727    painter.drawRect(QRect(0, 0, width(), height()));
     728    painter.setPen(QColor(20, 20, 20, 180));
     729    QFont font = painter.font();
     730    font.setBold(true);
     731    /** @todo make this size dynamic. aka. autoscale the font. */
     732    font.setPixelSize(16);
     733    painter.setFont(font);
     734    painter.drawText(2 * m_iMarginLeft, 15 * m_iMarginTop, m_strGAWarning);
     735}
     736
    721737
    722738void UIChart::sltCreateContextMenu(const QPoint &point)
     
    10331049    /* Configure charts: */
    10341050    if (m_charts.contains(m_strCPUMetricName) && m_charts[m_strCPUMetricName])
    1035         m_charts[m_strCPUMetricName]->setDrawPieChart(true);
     1051        m_charts[m_strCPUMetricName]->setWithPieChart(true);
    10361052    if (m_charts.contains(m_strRAMMetricName) && m_charts[m_strRAMMetricName])
    1037         m_charts[m_strRAMMetricName]->setDrawPieChart(false);
     1053        m_charts[m_strRAMMetricName]->setWithPieChart(false);
    10381054
    10391055    QWidget *bottomSpacerWidget = new QWidget(this);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette