UASQuickViewTextItem.cc 3.1 KB
Newer Older
1 2
#include "UASQuickViewTextItem.h"
#include <QVBoxLayout>
3
#include <QDebug>
4 5 6 7
UASQuickViewTextItem::UASQuickViewTextItem(QWidget *parent) : UASQuickViewItem(parent)
{
    QVBoxLayout *layout = new QVBoxLayout();
    this->setLayout(layout);
8 9
    layout->setSpacing(0);
    layout->setMargin(0);
10
    titleLabel = new QLabel(this);
11
    titleLabel->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
12
    titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
13 14
    this->layout()->addWidget(titleLabel);
    valueLabel = new QLabel(this);
15
    valueLabel->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
16
    valueLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
17
    valueLabel->setText("0.00");
18
    this->layout()->addWidget(valueLabel);
19 20
    //spacerItem = new QSpacerItem(20,40,QSizePolicy::Minimum,QSizePolicy::Ignored);
    //layout->addSpacerItem(spacerItem);
21 22 23 24 25 26
    QFont valuefont = valueLabel->font();
    QFont titlefont = titleLabel->font();
    valuefont.setPixelSize(this->height() / 2.0);
    titlefont.setPixelSize(this->height() / 4.0);
    valueLabel->setFont(valuefont);
    titleLabel->setFont(titlefont);
27 28 29
}
void UASQuickViewTextItem::setValue(double value)
{
30
    valueLabel->setText(QString::number(value,'f',4));
31 32 33 34
}

void UASQuickViewTextItem::setTitle(QString title)
{
35 36 37 38 39 40 41 42
    if (title.indexOf(".") != -1 && title.indexOf(":") != -1)
    {
        titleLabel->setText(title.mid(title.indexOf(".")+1));
    }
    else
    {
        titleLabel->setText(title);
    }
43
}
44 45 46 47
void UASQuickViewTextItem::resizeEvent(QResizeEvent *event)
{
    QFont valuefont = valueLabel->font();
    QFont titlefont = titleLabel->font();
48 49
    valuefont.setPixelSize(this->height());
    titlefont.setPixelSize(valuefont.pixelSize() / 2.0);
50
    //spacerItem->setGeometry(QRect(0,0,20,this->height()/10.0));
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

    QFontMetrics metrics(valuefont);
    //valuefont.setPixelSize(this->height() / 2.0);
    bool fit = false;
    while (!fit)
    {

        QFontMetrics valfm( valuefont );
        QRect valbound = valfm.boundingRect(0,0, valueLabel->width(), valueLabel->height(), Qt::TextWordWrap | Qt::AlignLeft, valueLabel->text());
        //QFontMetrics titlefm( titlefont );
        //QRect titlebound = titlefm.boundingRect(0,0, titleLabel->width(), titleLabel->height(), Qt::TextWordWrap | Qt::AlignLeft, titleLabel->text());

        if ((valbound.width() <= valueLabel->width() && valbound.height() <= valueLabel->height()))// && (titlebound.width() <= titleLabel->width() && titlebound.height() <= titleLabel->height()))
            fit = true;
        else
        {
            if (valuefont.pixelSize()-5 <= 0)
            {
                fit = true;
                valuefont.setPixelSize(5);
            }
            else
            {
                valuefont.setPixelSize(valuefont.pixelSize() - 5);
            }
            //titlefont.setPixelSize(valuefont.pixelSize() / 2.0);
            //qDebug() << "Point size:" << valuefont.pixelSize() << valueLabel->width() << valueLabel->height();
        }
    }
titlefont.setPixelSize(valuefont.pixelSize() / 2.0);
81 82 83 84
    valueLabel->setFont(valuefont);
    titleLabel->setFont(titlefont);
    update();
}