UASQuickViewTextItem.cc 5.84 KB
Newer Older
1 2
#include "UASQuickViewTextItem.h"
#include <QVBoxLayout>
3
#include <QDebug>
4 5
UASQuickViewTextItem::UASQuickViewTextItem(QWidget *parent) : UASQuickViewItem(parent)
{
6 7
    // Set a standard vertical layout.
    QVBoxLayout* layout = new QVBoxLayout();
8
    layout->setMargin(0);
9 10 11
    layout->setSizeConstraint(QLayout::SetMinimumSize);

    // Create the title label. Scale the font based on available size.
12
    titleLabel = new QLabel(this);
13 14 15 16 17 18 19
    titleLabel->setAlignment(Qt::AlignHCenter);
    titleLabel->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::Minimum);
    titleLabel->setObjectName(QString::fromUtf8("title"));
    QFont titlefont = titleLabel->font();
    titlefont.setPixelSize(this->height() / 4.0);
    titleLabel->setFont(titlefont);
    layout->addWidget(titleLabel);
20

21 22 23 24 25 26
    // Create the value label. Scale the font based on available size.
    valueLabel = new QLabel(this);
    valueLabel->setAlignment(Qt::AlignHCenter);
    valueLabel->setSizePolicy(QSizePolicy::MinimumExpanding,QSizePolicy::Minimum);
    valueLabel->setObjectName(QString::fromUtf8("value"));
    valueLabel->setText("0.00");
27 28 29
    QFont valuefont = valueLabel->font();
    valuefont.setPixelSize(this->height() / 2.0);
    valueLabel->setFont(valuefont);
30 31 32
    layout->addWidget(valueLabel);

    // And make sure the items are evenly spaced in the UASQuickView.
33
    layout->addSpacerItem(new QSpacerItem(10, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
34
    this->setLayout(layout);
35 36 37
}
void UASQuickViewTextItem::setValue(double value)
{
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    if (value < 10 && value > -10)
    {
        valueLabel->setText(QString::number(value,'f',4));
    }
    else if (value < 100 && value > -100)
    {
        valueLabel->setText(QString::number(value,'f',3));
    }
    else if (value < 1000 && value > -1000)
    {
        valueLabel->setText(QString::number(value,'f',2));
    }
    else if (value < 10000 && value > -10000)
    {
        valueLabel->setText(QString::number(value,'f',1));
    }
54
    else 
55 56 57
    {
        valueLabel->setText(QString::number(value,'f',0));
    }
58 59 60 61
}

void UASQuickViewTextItem::setTitle(QString title)
{
62 63
    if (title.indexOf(".") != -1 && title.indexOf(":") != -1)
    {
64
        titleLabel->setText(title.mid(title.indexOf(".") + 1));
65 66 67 68 69
    }
    else
    {
        titleLabel->setText(title);
    }
70
}
71 72 73 74 75
int UASQuickViewTextItem::minValuePixelSize()
{
    QFont valuefont = valueLabel->font();
    QFont titlefont = titleLabel->font();
    valuefont.setPixelSize(this->height());
76
    titlefont.setPixelSize(valuefont.pointSize() * 0.75);
77 78 79 80 81 82 83 84 85 86 87 88 89
    //spacerItem->setGeometry(QRect(0,0,20,this->height()/10.0));

    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, "12345678.00"/*valueLabel->text()*/);
        //QFontMetrics titlefm( titlefont );
        //QRect titlebound = titlefm.boundingRect(0,0, titleLabel->width(), titleLabel->height(), Qt::TextWordWrap | Qt::AlignLeft, titleLabel->text());

90
        if ((valbound.width() <= valueLabel->width() && valbound.height() <= valueLabel->height())) // && (titlebound.width() <= titleLabel->width() && titlebound.height() <= titleLabel->height()))
91 92 93
            fit = true;
        else
        {
94
            if (valuefont.pointSize() - 1 <= 6)
95 96
            {
                fit = true;
97
                valuefont.setPixelSize(6);
98 99 100
            }
            else
            {
101
                valuefont.setPixelSize(valuefont.pointSize() - 1);
102
            }
103 104
            //titlefont.setPixelSize(valuefont.pointSize() / 2.0);
            //qDebug() << "Point size:" << valuefont.pointSize() << valueLabel->width() << valueLabel->height();
105 106
        }
    }
107
    return valuefont.pointSize();
108 109 110 111 112 113
}
void UASQuickViewTextItem::setValuePixelSize(int size)
{
    QFont valuefont = valueLabel->font();
    QFont titlefont = titleLabel->font();
    valuefont.setPixelSize(size);
114
    titlefont.setPixelSize(valuefont.pointSize() * 0.75);
115 116 117 118 119
    valueLabel->setFont(valuefont);
    titleLabel->setFont(titlefont);
    update();
}

120 121
void UASQuickViewTextItem::resizeEvent(QResizeEvent *event)
{
122
    Q_UNUSED(event);
123
    return;
124 125
#if 0
    // code ifdef'ed out to silence warnings
126 127
    QFont valuefont = valueLabel->font();
    QFont titlefont = titleLabel->font();
128
    valuefont.setPixelSize(this->height());
129
    titlefont.setPixelSize(valuefont.pointSize() / 2.0);
130
    //spacerItem->setGeometry(QRect(0,0,20,this->height()/10.0));
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

    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
        {
147
            if (valuefont.pointSize()-5 <= 0)
148 149 150 151 152 153
            {
                fit = true;
                valuefont.setPixelSize(5);
            }
            else
            {
154
                valuefont.setPixelSize(valuefont.pointSize() - 5);
155
            }
156 157
            //titlefont.setPixelSize(valuefont.pointSize() / 2.0);
            //qDebug() << "Point size:" << valuefont.pointSize() << valueLabel->width() << valueLabel->height();
158 159
        }
    }
160
titlefont.setPixelSize(valuefont.pointSize() / 2.0);
161 162 163
    valueLabel->setFont(valuefont);
    titleLabel->setFont(titlefont);
    update();
164
#endif
165
}