summaryrefslogtreecommitdiffstats
path: root/examples/statemachine/citizenquartz/clockdisplay.cpp
blob: 717b88138024aef8c0d94d26eb0110ed4431c6e9 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
133
134
135
136
137
138
139
140
#include "clockdisplay.h"

#include <QPainter>
#include <QTimer>

ClockDisplay::ClockDisplay(QGraphicsItem *parent)
    : QGraphicsItem(parent), 
      m_displayMode(CurrentTimeMode),
      m_currentTime(QDate(1970, 1, 1), QTime(0, 0)),
      m_alarm(0, 0),
      m_alarmEnabled(false),
      m_blink(false)

{
    m_text = new QGraphicsTextItem(this);
    
    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(toggleBlinkFlag()));
    timer->start(500);
    
    QFont font = m_text->font();
    font.setPointSizeF(20.0);
    m_text->setFont(font);

    m_text->translate(-100.0, -20.0);

    QPixmap pm(":/images/alarm.png");
    m_alarmSymbol = new QGraphicsPixmapItem(pm, this);
    m_alarmSymbol->translate(-100.0, -60.0);
    m_alarmSymbol->setOffset(5.0, 5.0);
    m_alarmSymbol->scale(0.5, 0.5);
}

ClockDisplay::~ClockDisplay()
{
}

void ClockDisplay::toggleBlinkFlag()
{
    m_blink = !m_blink;
    update();
}

void ClockDisplay::updateText() 
{
    switch (m_displayMode) {
    case EditSecondMode:
        if (m_blink) {
            m_text->setHtml(m_currentTime.toString("hh:mm:'<font color=\"transparent\">'ss'</font>'"));
            break;
        } 
        // fall throoough
    case EditMinuteMode:
        if (m_blink) {
            m_text->setHtml(m_currentTime.toString("hh:'<font color=\"transparent\">'mm'</font>':ss"));
            break;
        }
        
        // fall throoough
    case EditHourMode:
        if (m_blink) {
            m_text->setHtml(m_currentTime.toString("'<font color=\"transparent\">'hh'</font>':mm:ss"));
            break;
        }

        // fall throoough
    case CurrentTimeMode:
        m_text->setHtml(m_currentTime.toString("hh:mm:ss"));
        break;

    case EditMonthMode:
        if (m_blink) {
            m_text->setHtml(m_currentTime.toString("yyyy.'<font color=\"transparent\">'MM'</font>'.dd"));
            break;
        }
        
        // fall throoough
    case EditDayMode:
        if (m_blink) {
            m_text->setHtml(m_currentTime.toString("yyyy.MM.'<font color=\"transparent\">'dd'</font>'"));
            break;
        }

        // fall throoough
    case EditYearMode:
        if (m_blink) {
            m_text->setHtml(m_currentTime.toString("'<font color=\"transparent\">'yyyy'</font>'.MM.dd"));
            break;
        }

        // fall throoough
    case CurrentDateMode:
        m_text->setHtml(m_currentTime.toString("yyyy.MM.dd"));
        break;

    case EditAlarmHourMode:
        if (m_blink) {
            m_text->setHtml(m_alarm.toString("'<font color=\"transparent\">'hh'</font>':mm"));
            break;        
        }

        // fall throooough
    case EditAlarmTenMinuteMode:
    case EditAlarmMinuteMode:
        if (m_blink) {
            m_text->setHtml(m_alarm.toString("hh:'<font color=\"transparent\">'mm'</font>'"));
            break;
        }

        // fall throoough
    case AlarmMode:
        m_text->setHtml(m_alarm.toString("hh:mm"));
        break;

    default:
        m_text->setHtml("Not implemented");
    };
}

QRectF ClockDisplay::boundingRect() const 
{
    return QRectF(-100.0, -60.0, 200.0, 120.0);
}

void ClockDisplay::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    if (m_alarmEnabled)
        m_alarmSymbol->setVisible(true);

    updateText(); 

    // Screen
    painter->drawRoundedRect(boundingRect(), 15.0, 15.0, Qt::RelativeSize);

    // Button grid
    painter->drawLine(QPointF(-100.0, -20.0), QPointF(100.0, -20.0));
    painter->drawLine(QPointF(-50.0, -60.0), QPointF(-50.0, -20.0));
    painter->drawLine(QPointF(0.0, -60.0), QPointF(0.0, -20.0));
    painter->drawLine(QPointF(50.0, -60.0), QPointF(50.0, -20.0));
}