aboutsummaryrefslogtreecommitdiffstats
path: root/examples/qml/tutorials/extending-qml-advanced/advanced6-Property-value-source/happybirthdaysong.cpp
blob: 7a756a4a717c6f1b7cd29952dd1bb29b354d598c (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

#include "happybirthdaysong.h"

#include <QTimer>

HappyBirthdaySong::HappyBirthdaySong(QObject *parent) : QObject(parent)
{
    auto *timer = new QTimer(this);
    QObject::connect(timer, &QTimer::timeout, this, &HappyBirthdaySong::advance);
    timer->start(1000);
}

void HappyBirthdaySong::setTarget(const QQmlProperty &target)
{
    m_target = target;
}

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

void HappyBirthdaySong::setName(const QString &name)
{
    if (m_name != name) {
        m_name = name;
        emit nameChanged();
    }

    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 << "";
}

void HappyBirthdaySong::advance()
{
    m_line = (m_line + 1) % m_lyrics.count();

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