File indexing completed on 2024-04-14 03:46:38

0001 /*
0002  * SPDX-FileCopyrightText: 2007 Jeremy Whiting <jpwhiting@kde.org>
0003  * SPDX-FileCopyrightText: 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef KEDUVOCCONTAINER_H
0008 #define KEDUVOCCONTAINER_H
0009 
0010 #include "keduvocdocument_export.h"
0011 
0012 #include "keduvoctext.h"
0013 
0014 #include <QList>
0015 #include <QUrl>
0016 
0017 class KEduVocDocument;
0018 class KEduVocExpression;
0019 
0020 /** class to store information about a container - that can be a lesson or word types */
0021 class KEDUVOCDOCUMENT_EXPORT KEduVocContainer
0022 {
0023     // make this a template?
0024 
0025 public:
0026     enum EnumContainerType {
0027         Container,
0028         Lesson,
0029         WordType,
0030         Leitner,
0031     };
0032 
0033     enum EnumEntriesRecursive {
0034         NotRecursive = 0,
0035         Recursive = 1,
0036     };
0037 
0038     /** default constructor */
0039     explicit KEduVocContainer(const QString &name, EnumContainerType type, KEduVocContainer *parent = nullptr);
0040 
0041     void appendChildContainer(KEduVocContainer *child);
0042     void insertChildContainer(int row, KEduVocContainer *child);
0043     void deleteChildContainer(int row);
0044     void removeChildContainer(int row);
0045     KEduVocContainer *childContainer(int row);
0046 
0047     /**
0048      * Retrieve a child container by its name
0049      * Returns 0 if no container is found
0050      * @param name container name
0051      * @return the child container
0052      */
0053     KEduVocContainer *childContainer(const QString &name);
0054 
0055     QList<KEduVocContainer *> childContainers();
0056 
0057     /**
0058      * Find a child container
0059      * @param name
0060      * @return
0061      */
0062     //     KEduVocContainer *childContainer(const QString& name);
0063 
0064     int childContainerCount() const;
0065 
0066     int row() const;
0067     virtual KEduVocContainer *parent();
0068 
0069     /** copy constructor for d-pointer safe copying */
0070     KEduVocContainer(const KEduVocContainer &other);
0071 
0072     /** destructor */
0073     virtual ~KEduVocContainer();
0074 
0075     /** assignment operator */
0076     KEduVocContainer &operator=(const KEduVocContainer &);
0077 
0078     /** @return the containing KEduVocDocument */
0079     KEduVocDocument *document() const;
0080 
0081     /** set the container name
0082      * @param name text to set for the name
0083      */
0084     void setName(const QString &name);
0085 
0086     /** get the container name */
0087     QString name();
0088 
0089     /** get a list of all entries in the container */
0090     virtual QList<KEduVocExpression *> entries(EnumEntriesRecursive recursive = NotRecursive) = 0;
0091     virtual int entryCount(EnumEntriesRecursive recursive = NotRecursive) = 0;
0092     virtual KEduVocExpression *entry(int row, EnumEntriesRecursive recursive = NotRecursive) = 0;
0093 
0094     /**
0095      * Removes a translation. This has to be called when a language is removed from a document.
0096      * @param translation
0097      */
0098     void removeTranslation(int translation);
0099 
0100     bool inPractice();
0101     void setInPractice(bool inPractice);
0102 
0103     /** equality operator */
0104     bool operator==(const KEduVocContainer &other) const;
0105 
0106     /**
0107      * The type of this container. @see EnumContainerType
0108      * @return
0109      */
0110     KEduVocContainer::EnumContainerType containerType();
0111 
0112     /**
0113      * Set the type of container.
0114      * For convenience by default this is taken over from the parent, so no need to set.
0115      * @param type the new type
0116      */
0117     void setContainerType(KEduVocContainer::EnumContainerType type);
0118 
0119     /** get the image url for this container if it exists */
0120     QUrl imageUrl();
0121 
0122     /** set the image url for this container
0123      * @param url               url of the image
0124      */
0125     void setImageUrl(const QUrl &url);
0126 
0127     double averageGrade(int translation, EnumEntriesRecursive recursive);
0128 
0129     int expressionsOfGrade(int translation, grade_t grade, EnumEntriesRecursive recursive);
0130 
0131     /**
0132      * Remove grades from all entries of this lessons
0133      * @param translation which translation to remove. -1 for all.
0134      * @param recursive whether to include child lessons
0135      */
0136     void resetGrades(int translation, EnumEntriesRecursive recursive);
0137 
0138 protected:
0139     QList<KEduVocExpression *> entriesRecursive();
0140 
0141     /**
0142      * Set the child entry cache to invalid
0143      */
0144     void invalidateChildLessonEntries();
0145 
0146     /**
0147      * Recreate the cache of entries in this lesson's child lessons.
0148      */
0149     void updateChildLessonEntries();
0150 
0151     // Used by KEduVocLesson when the Document creates the top level lesson.
0152     explicit KEduVocContainer(const QString &name, EnumContainerType type, KEduVocDocument *document);
0153 
0154 private:
0155     class Private;
0156     Private *const d;
0157 };
0158 
0159 #endif