File: sealedbox.cpp

package info (click to toggle)
qspeakers 1.8.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 900 kB
  • sloc: cpp: 4,593; xml: 708; sh: 13; makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,382 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <QLocale>
#include <QDebug>

#include "sealedbox.h"

SealedBox::SealedBox(double vol)
{
    setVolume(vol);
}

void SealedBox::setVolume(double vol)
{
    volume = double ((int)(vol * 100) / 100.0);
}

double SealedBox::getVolume() const
{
    return volume;
}

QDomElement SealedBox::toDomElement(QDomDocument& doc) const
{
    QDomElement e = Box::toDomElement(doc);
    e.setAttribute("type", "sealed");

    QLocale c(QLocale::C);
    e.setAttribute("volume", c.toString(volume));

    return e;
}

void SealedBox::fromDomElement(const QDomElement &e)
{
    Box::fromDomElement(e);
    if (e.attribute("type") != "sealed") {
        qWarning() << __func__ << "wrong box type! (not sealed, giving up)";
        return;
    }

    QLocale c(QLocale::C);
    volume = c.toDouble(e.attribute("volume"));
}

void SealedBox::render(QPainter *painter, const QRectF &area) const
{
    QFont orig = painter->font();

    painter->drawRoundedRect(area.toRect(), 5, 5);

    QFont font;
    font.setBold(false);
    painter->setFont(font);

    QString text = QObject::tr("Volume: %1 L").arg(QString::number(getVolume(), 'f', 2));
    QRectF where(area.left() + BOX_RENDER_MARGINS, area.top(), area.width() - 2 * BOX_RENDER_MARGINS, area.height());
    QTextOption option(Qt::AlignVCenter|Qt::AlignLeft);
    painter->drawText(where, text, option);

    painter->setFont(orig);
}