summaryrefslogtreecommitdiffstats
path: root/examples/declarative/extending/binding/happybirthday.cpp
blob: dd1247ff797f2c84776ff0b7d12ff2b09433b4c1 (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
#include "happybirthday.h"
#include <QTimer>

HappyBirthday::HappyBirthday(QObject *parent)
: QObject(parent), m_line(-1)
{
    setName(QString());
    QTimer *timer = new QTimer(this);
    QObject::connect(timer, SIGNAL(timeout()), this, SLOT(advance()));
    timer->start(1000);
}

void HappyBirthday::setTarget(const QmlMetaProperty &p)
{
    m_target = p;
}

QString HappyBirthday::name() const
{
    return m_name;
}

void HappyBirthday::setName(const QString &name)
{
    if (m_name == name)
        return;

    m_name = name;

    m_lyrics.clear();
    m_lyrics << "Happy birthday to you,";
    m_lyrics << "Happy birthday to you,";
    m_lyrics << "Happy birthday dear " + m_name + ",";
    m_lyrics << "Happy birthday to you!";
    m_lyrics << "";
    
    emit nameChanged();
}
    
void HappyBirthday::advance()
{
    m_line = (m_line + 1) % m_lyrics.count();

    m_target.write(m_lyrics.at(m_line));
}

QML_DEFINE_TYPE(People, 1, 0, 0, HappyBirthday, HappyBirthday);