File indexing completed on 2024-09-08 06:47:59
0001 /* 0002 SPDX-FileCopyrightText: 2007 Paolo Capriotti <p.capriotti@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 #include "statswidget.h" 0007 #include "stats.h" 0008 #include "kbsrenderer.h" 0009 0010 #include <QPen> 0011 #include <QPainter> 0012 #include <QPaintEvent> 0013 #include <QHBoxLayout> 0014 0015 0016 StatsWidgetElement::StatsWidgetElement(const QPixmap &icon, const QString &text, QWidget *parent) 0017 : QFrame(parent) 0018 { 0019 setData(icon, text); 0020 } 0021 0022 void StatsWidgetElement::paintEvent(QPaintEvent * /*ev*/) 0023 { 0024 QPen pen; 0025 pen.setColor(QColor(0, 0, 0, 255)); 0026 0027 QRect rect; 0028 rect.setTopLeft(QPoint(BORDER_X, BORDER_Y)); 0029 rect.setWidth(frameRect().width() - BORDER_WIDTH); 0030 rect.setHeight(frameRect().height() - BORDER_WIDTH); 0031 0032 QPainter p(this); 0033 p.setPen(pen); 0034 p.setRenderHint(QPainter::Antialiasing); 0035 p.drawRoundedRect(rect, 1000 / rect.width(), 1000 / rect.height(), Qt::RelativeSize); 0036 } 0037 0038 void StatsWidgetElement::setText(const QString &text) 0039 { 0040 QString value = QLatin1String("<font size=\"5\">") + text + QLatin1String("</font>"); 0041 m_text->setText(value); 0042 } 0043 0044 void StatsWidgetElement::setData(const QPixmap &icon, const QString &text) 0045 { 0046 m_icon = new QLabel; 0047 m_text = new QLabel; 0048 0049 m_icon->setPixmap(icon); 0050 setText(text); 0051 0052 QHBoxLayout *layout = new QHBoxLayout; 0053 layout->addSpacing(MARGIN); 0054 layout->addWidget(m_icon); 0055 layout->addStretch(width() - m_icon->width() - m_text->width()); 0056 layout->addWidget(m_text); 0057 layout->addSpacing(MARGIN); 0058 setLayout(layout); 0059 } 0060 0061 StatsWidget::StatsWidget(KBSRenderer *renderer, QWidget *parent) 0062 : QWidget(parent) 0063 , m_stats(nullptr) 0064 { 0065 m_elements[0] = new StatsWidgetElement( 0066 renderer->spritePixmap(QStringLiteral("water-impact"), QSize(32, 32)), 0067 QStringLiteral("0"), 0068 this); 0069 m_elements[0]->show(); 0070 m_elements[1] = new StatsWidgetElement( 0071 renderer->spritePixmap(QStringLiteral("hit"), QSize(32, 32)), 0072 QStringLiteral("0"), 0073 this); 0074 m_elements[1]->show(); 0075 0076 QHBoxLayout *layout = new QHBoxLayout; 0077 layout->addWidget(m_elements[0]); 0078 layout->addWidget(m_elements[1]); 0079 setLayout(layout); 0080 } 0081 0082 void StatsWidget::setWidth(int width) 0083 { 0084 int x = geometry().x(); 0085 int y = geometry().y(); 0086 int h = geometry().height(); 0087 0088 setGeometry(x, y, width, h); 0089 } 0090 0091 void StatsWidget::refresh() 0092 { 0093 if (m_stats) { 0094 m_elements[0]->setText(QString::number(m_stats->misses())); 0095 m_elements[1]->setText(QString::number(m_stats->hits())); 0096 } 0097 else { 0098 m_elements[0]->setText(QStringLiteral("0")); 0099 m_elements[1]->setText(QStringLiteral("0")); 0100 } 0101 } 0102 0103 void StatsWidget::setData(Stats* stats) 0104 { 0105 m_stats = stats; 0106 if (m_stats) { 0107 connect(m_stats.data(), &Stats::hitsChanged, this, &StatsWidget::refresh); 0108 connect(m_stats.data(), &Stats::missesChanged, this, &StatsWidget::refresh); 0109 } 0110 refresh(); 0111 } 0112 0113 #include "moc_statswidget.cpp"