UASQuickViewTextItem.cc 6.45 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 20
// <<<<<<< HEAD
     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);
21

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
     // 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");
// =======
//    titleLabel->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
//    titleLabel->setAlignment(Qt::AlignHCenter | Qt::AlignBottom);
//    this->layout()->addWidget(titleLabel);
//    valueLabel = new QLabel(this);
//    valueLabel->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Ignored);
//    valueLabel->setAlignment(Qt::AlignHCenter | Qt::AlignTop);
//    valueLabel->setText("0.00");
//    this->layout()->addWidget(valueLabel);
//    //spacerItem = new QSpacerItem(20,40,QSizePolicy::Minimum,QSizePolicy::Ignored);
//    //layout->addSpacerItem(spacerItem);
// >>>>>>> 34eaf1fb422146f5df3b01fad4d756343b3127c9
40 41 42
    QFont valuefont = valueLabel->font();
    valuefont.setPixelSize(this->height() / 2.0);
    valueLabel->setFont(valuefont);
43 44 45 46 47
    layout->addWidget(valueLabel);

    // And make sure the items are evenly spaced in the UASQuickView.
    layout->addSpacerItem(new QSpacerItem(20, 0, QSizePolicy::Minimum, QSizePolicy::Expanding));
    this->setLayout(layout);
48 49 50
}
void UASQuickViewTextItem::setValue(double value)
{
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    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));
    }
    else if (value >= 100000 || value <= -100000)
    {
        valueLabel->setText(QString::number(value,'f',0));
    }
71 72 73 74
}

void UASQuickViewTextItem::setTitle(QString title)
{
75 76 77 78 79 80 81 82
    if (title.indexOf(".") != -1 && title.indexOf(":") != -1)
    {
        titleLabel->setText(title.mid(title.indexOf(".")+1));
    }
    else
    {
        titleLabel->setText(title);
    }
83
}
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
int UASQuickViewTextItem::minValuePixelSize()
{
    QFont valuefont = valueLabel->font();
    QFont titlefont = titleLabel->font();
    valuefont.setPixelSize(this->height());
    titlefont.setPixelSize(valuefont.pixelSize() / 2.0);
    //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());

        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();
        }
    }
    return valuefont.pixelSize();
}
void UASQuickViewTextItem::setValuePixelSize(int size)
{
    QFont valuefont = valueLabel->font();
    QFont titlefont = titleLabel->font();
    valuefont.setPixelSize(size);
    titlefont.setPixelSize(valuefont.pixelSize() / 2.0);
    valueLabel->setFont(valuefont);
    titleLabel->setFont(titlefont);
    update();
}

133 134
void UASQuickViewTextItem::resizeEvent(QResizeEvent *event)
{
135
    return;
136 137
    QFont valuefont = valueLabel->font();
    QFont titlefont = titleLabel->font();
138 139
    valuefont.setPixelSize(this->height());
    titlefont.setPixelSize(valuefont.pixelSize() / 2.0);
140
    //spacerItem->setGeometry(QRect(0,0,20,this->height()/10.0));
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

    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);
171 172 173 174
    valueLabel->setFont(valuefont);
    titleLabel->setFont(titlefont);
    update();
}