1 /*
2     SPDX-FileCopyrightText: 2013 Andreas Cord-Landwehr <cordlandwehr@kde.org>
3 
4     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
5 */
6 
7 #include "phonemegroup.h"
8 #include "phoneme.h"
9 
10 #include "artikulate_debug.h"
11 
PhonemeGroup()12 PhonemeGroup::PhonemeGroup()
13     : QObject(nullptr)
14 {
15 }
16 
~PhonemeGroup()17 PhonemeGroup::~PhonemeGroup()
18 {
19     for (auto phoneme : m_phonemes) {
20         phoneme->deleteLater();
21     }
22     m_phonemes.clear();
23 }
24 
id() const25 QString PhonemeGroup::id() const
26 {
27     return m_id;
28 }
29 
setId(const QString & id)30 void PhonemeGroup::setId(const QString &id)
31 {
32     if (id != m_id) {
33         m_id = id;
34         emit idChanged();
35     }
36 }
37 
title() const38 QString PhonemeGroup::title() const
39 {
40     return m_title;
41 }
42 
setTitle(const QString & title)43 void PhonemeGroup::setTitle(const QString &title)
44 {
45     if (QString::compare(title, m_title) != 0) {
46         m_title = title;
47         emit titleChanged();
48     }
49 }
50 
description() const51 QString PhonemeGroup::description() const
52 {
53     return m_description;
54 }
55 
setDescription(const QString & description)56 void PhonemeGroup::setDescription(const QString &description)
57 {
58     m_description = description;
59     emit descriptionChanged();
60 }
61 
phonemes() const62 QVector<std::shared_ptr<Phoneme>> PhonemeGroup::phonemes() const
63 {
64     return m_phonemes;
65 }
66 
contains(std::shared_ptr<Phoneme> phoneme) const67 bool PhonemeGroup::contains(std::shared_ptr<Phoneme> phoneme) const
68 {
69     for (auto testPhoneme : m_phonemes) {
70         if (QString::compare(testPhoneme->id(), phoneme->id()) == 0) {
71             return true;
72         }
73     }
74     return false;
75 }
76 
addPhoneme(std::unique_ptr<Phoneme> phoneme)77 std::shared_ptr<Phoneme> PhonemeGroup::addPhoneme(std::unique_ptr<Phoneme> phoneme)
78 {
79     std::shared_ptr<Phoneme> newPhoneme(std::move(phoneme));
80     if (!contains(newPhoneme)) {
81         m_phonemes.append(newPhoneme);
82     } else {
83         qCWarning(ARTIKULATE_LOG) << "Phoneme identifier already registered in group " << m_title << ", aborting";
84     }
85     return std::shared_ptr<Phoneme>();
86 }
87 
addPhoneme(const QString & identifier,const QString & title)88 std::shared_ptr<Phoneme> PhonemeGroup::addPhoneme(const QString &identifier, const QString &title)
89 {
90     Q_ASSERT(!identifier.isEmpty());
91 
92     // check that identifier is not used
93     for (auto phoneme : m_phonemes) {
94         if (QString::compare(phoneme->id(), identifier) == 0) {
95             qCWarning(ARTIKULATE_LOG) << "Phoneme identifier " << identifier << " already registered in group " << m_title << ", aborting";
96             return std::shared_ptr<Phoneme>();
97         }
98     }
99 
100     std::unique_ptr<Phoneme> newPhoneme(new Phoneme);
101     newPhoneme->setId(identifier);
102     newPhoneme->setTitle(title);
103     return addPhoneme(std::move(newPhoneme));
104 }
105