LibKEduVocDocument

keduvocexpression.cpp
1/*
2 * Vocabulary Expression for KDE Edu
3 * SPDX-FileCopyrightText: 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
4 * SPDX-FileCopyrightText: 2005-2007 Peter Hedlund <peter.hedlund@kdemail.net>
5 * SPDX-FileCopyrightText: 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9#include "keduvocexpression.h"
10
11#include <QMap>
12
13class KEduVocExpression::KEduVocExpressionPrivate
14{
15public:
16 KEduVocExpressionPrivate()
17 {
18 m_active = true;
19 m_lesson = nullptr;
20 }
21 ~KEduVocExpressionPrivate();
22
23 KEduVocExpressionPrivate(const KEduVocExpressionPrivate &other);
24 KEduVocExpressionPrivate &operator=(const KEduVocExpressionPrivate &other);
25
26 bool operator==(const KEduVocExpressionPrivate &p) const;
27
28 KEduVocLesson *m_lesson;
29 bool m_active;
30
32};
33
34KEduVocExpression::KEduVocExpressionPrivate::~KEduVocExpressionPrivate()
35{
36 QMap<int, KEduVocTranslation *> translations = m_translations;
37 // empty the translations map, otherwise removal from word type will try to access them again when they don't exist any more
38 m_translations.clear();
39 qDeleteAll(translations);
40}
41
42KEduVocExpression::KEduVocExpressionPrivate::KEduVocExpressionPrivate(const KEduVocExpressionPrivate &other)
43{
44 m_active = other.m_active;
45 m_lesson = nullptr;
46}
47
48KEduVocExpression::KEduVocExpressionPrivate &KEduVocExpression::KEduVocExpressionPrivate::operator=(const KEduVocExpressionPrivate &other)
49{
50 m_active = other.m_active;
51 m_lesson = nullptr;
52
53 return *this;
54}
55
56bool KEduVocExpression::KEduVocExpressionPrivate::operator==(const KEduVocExpression::KEduVocExpressionPrivate &p) const
57{
58 return m_translations == p.m_translations && m_lesson == p.m_lesson && m_active == p.m_active;
59}
60
62 : d(new KEduVocExpressionPrivate)
63{
64}
65
67 : d(new KEduVocExpressionPrivate)
68{
69 setTranslation(0, expression.simplified());
70}
71
73 : d(new KEduVocExpressionPrivate)
74{
75 foreach (const QString &translation, translations) {
76 setTranslation(d->m_translations.count(), translation);
77 }
78}
79
81 : d(new KEduVocExpressionPrivate(*other.d))
82{
83 foreach (int key, other.d->m_translations.keys()) {
84 d->m_translations[key] = new KEduVocTranslation(*other.d->m_translations.value(key));
85 d->m_translations[key]->setEntry(this);
86 }
87}
88
89KEduVocExpression &KEduVocExpression::operator=(const KEduVocExpression &other)
90{
91 *d = *other.d;
92 foreach (int key, other.d->m_translations.keys()) {
93 d->m_translations[key] = new KEduVocTranslation(*other.d->m_translations.value(key));
94 d->m_translations[key]->setEntry(this);
95 }
96 return *this;
97}
98
99KEduVocExpression::~KEduVocExpression()
100{
101 setLesson(nullptr);
102 delete d;
103}
104
106{
107 int count = d->m_translations.count();
108
109 // remove the index we delete
110 delete d->m_translations.take(index);
111
112 // shift all other indexes, +1 for the deleted
113 for (int j = index; j < count - 1; j++) {
114 d->m_translations[j] = d->m_translations.take(j + 1);
115 }
116}
117
118void KEduVocExpression::setTranslation(int index, const QString &expr)
119{
120 if (index < 0) {
121 return;
122 }
123
124 if (!d->m_translations.contains(index)) {
125 d->m_translations[index] = new KEduVocTranslation(this);
126 }
127 d->m_translations[index]->setText(expr.simplified());
128}
129
131{
132 return d->m_lesson;
133}
134
136{
137 return d->m_active;
138}
139
141{
142 d->m_active = flag;
143}
144
146{
147 if (index == -1) { // clear grades for all languages
148 foreach (KEduVocTranslation *trans, d->m_translations) {
149 trans->resetGrades();
150 }
151 return;
152 }
153
154 // only language index
155 if (d->m_translations.contains(index)) {
156 d->m_translations[index]->resetGrades();
157 }
158}
159
160bool KEduVocExpression::operator==(const KEduVocExpression &expression) const
161{
162 return (*d == *expression.d);
163}
164
166{
167 if (translationIndices().contains(index)) {
168 return d->m_translations[index];
169 }
170 d->m_translations[index] = new KEduVocTranslation(this);
171 return d->m_translations[index];
172}
173
175{
176 if (d->m_translations.contains(index)) {
177 return nullptr;
178 }
179 return d->m_translations[index];
180}
181
182QList<int> KEduVocExpression::translationIndices() const
183{
184 return d->m_translations.keys();
185}
186
187void KEduVocExpression::setLesson(KEduVocLesson *l)
188{
189 if (d->m_lesson) {
190 d->m_lesson->removeEntry(this);
191 }
192 d->m_lesson = l;
193}
This class contains one vocabulary expression as an original with one or more translations.
void resetGrades(int index)
reset all grades of the entry
void setTranslation(int index, const QString &expression)
Add a translation to this expression.
KEduVocExpression()
default constructor for an empty vocabulary expression
KEduVocLesson * lesson() const
return the lesson
bool isActive() const
returns flag if entry is activated for queries
void setActive(bool flag=true)
set entry active (enabled for queries)
void removeTranslation(int index)
removes a translation
KEduVocTranslation * translation(int index)
Get a pointer to the translation.
class to store information about a lesson
void removeEntry(KEduVocExpression *entry)
remove an entry from the lesson
void resetGrades()
Clears grading and date information.
void clear()
bool contains(const Key &key) const const
size_type count() const const
QList< Key > keys() const const
T take(const Key &key)
T value(const Key &key, const T &defaultValue) const const
QString simplified() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 20 2024 11:49:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.