summaryrefslogtreecommitdiffstats
path: root/library/components/powerbutton.cpp
blob: 6c2b716786ba3667e3f01dec40fdce4faa720d0f (plain)
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
61
62
#include "powerbutton.h"

#include <QtGui/QMenu>
#include <QtGui/QIcon>

PowerButton::PowerButton(QWidget *parent)
    : QToolButton(parent)
{
    setPopupMode(QToolButton::InstantPopup);
    setDisplayedState(BatteryPower);
    setProperty("noArrow", true);

    QMenu *menu = new QMenu(this);
    QAction *action = 0;

    action = menu->addAction(QIcon(":/components/images/unknownpower.png"), tr("Unknown or error"));
    action->setProperty("powerState", UnknownPower);
    connect(action, SIGNAL(triggered()), SLOT(emitPowerStateChanged()));

    action = menu->addAction(QIcon(":/components/images/batterypower.png"), tr("Battery"));
    action->setProperty("powerState", BatteryPower);
    connect(action, SIGNAL(triggered()), SLOT(emitPowerStateChanged()));

    action = menu->addAction(QIcon(":/components/images/wall.png"), tr("Wall"));
    action->setProperty("powerState", WallPower);
    connect(action, SIGNAL(triggered()), SLOT(emitPowerStateChanged()));

    action = menu->addAction(QIcon(":/components/images/wall_charge.png"), tr("Wall and charging"));
    action->setProperty("powerState", WallPowerChargingBattery);
    connect(action, SIGNAL(triggered()), SLOT(emitPowerStateChanged()));

    setMenu(menu);
}

PowerButton::~PowerButton()
{
}

void PowerButton::setDisplayedState(PowerState state)
{
    QIcon newIcon;
    if (state == UnknownPower)
        newIcon = QIcon(":/components/images/unknownpower.png");
    else if (state == BatteryPower)
        newIcon = QIcon(":/components/images/batterypower.png");
    else if (state == WallPower)
        newIcon = QIcon(":/components/images/wall.png");
    else if (state == WallPowerChargingBattery)
        newIcon = QIcon(":/components/images/wall_charge.png");
    setIcon(newIcon);
}

void PowerButton::emitPowerStateChanged()
{
    QAction *action = qobject_cast<QAction *>(sender());
    if (!action)
        return;
    PowerState newState = static_cast<PowerState>(action->property("powerState").toInt());

    setDisplayedState(newState);
    emit powerStateChanged(newState);
}