File indexing completed on 2024-04-21 03:50:59

0001 /*
0002     SPDX-FileCopyrightText: 2008-2009 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "lessonmodel.h"
0007 
0008 #include "parleydocument.h"
0009 #include <KLocalizedString>
0010 #include <QFont>
0011 #include <QRandomGenerator>
0012 
0013 /** @file
0014  * Implementation of LessonModel.
0015  * Functions to create the model from the lessons of the vocabulary document.
0016  */
0017 
0018 LessonModel::LessonModel(QObject *parent)
0019     : ContainerModel(KEduVocContainer::Lesson, parent)
0020 {
0021 }
0022 
0023 KEduVocContainer *LessonModel::rootContainer() const
0024 {
0025     if (!m_doc) {
0026         return nullptr;
0027     }
0028     return m_doc->lesson();
0029 }
0030 
0031 Qt::ItemFlags LessonModel::flags(const QModelIndex &index) const
0032 {
0033     if (index.isValid() && index.parent() == QModelIndex()) {
0034         return (Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable);
0035     }
0036 
0037     // the name column should be checkable to select lessons for practice
0038     return ContainerModel::flags(index);
0039 }
0040 
0041 QVariant LessonModel::data(const QModelIndex &index, int role) const
0042 {
0043     if (index.isValid() && !index.parent().isValid()) {
0044         if (index.column() == 0) {
0045             switch (role) {
0046             case Qt::DisplayRole:
0047                 return i18nc("display of the name of the vocabulary collection", "Collection: %1", ContainerModel::data(index, role).toString());
0048             case Qt::FontRole:
0049                 QFont f;
0050                 f.setBold(true);
0051                 return f;
0052             }
0053         }
0054     }
0055     return ContainerModel::data(index, role);
0056 }
0057 
0058 bool LessonModel::setData(const QModelIndex &index, const QVariant &value, int role)
0059 {
0060     if (index.isValid() && !index.parent().isValid()) {
0061         if (index.column() == ContainerNameColumn && role == Qt::EditRole) {
0062             ///@todo decouple the root lesson and document title
0063             m_doc->setTitle(value.toString());
0064         }
0065     }
0066     return ContainerModel::setData(index, value, role);
0067 }
0068 
0069 void LessonModel::splitLesson(const QModelIndex &containerIndex, int entriesPerLesson, SplitLessonOrder order)
0070 {
0071     if (!containerIndex.isValid()) {
0072         return;
0073     }
0074 
0075     if (static_cast<KEduVocContainer *>(containerIndex.internalPointer())->containerType() != KEduVocContainer::Lesson) {
0076         return;
0077     }
0078 
0079     KEduVocLesson *parentLesson = static_cast<KEduVocLesson *>(containerIndex.internalPointer());
0080 
0081     int numNewLessons = parentLesson->entryCount() / entriesPerLesson;
0082     // modulo - fraction lesson if not 0 we need one more
0083     if (parentLesson->entryCount() % entriesPerLesson) {
0084         numNewLessons++;
0085     }
0086 
0087     while (parentLesson->entryCount() > 0) {
0088         beginInsertRows(containerIndex, parentLesson->entryCount(), parentLesson->entryCount());
0089         KEduVocLesson *child = new KEduVocLesson(parentLesson->name() + QStringLiteral(" %1").arg(parentLesson->childContainerCount() + 1), parentLesson);
0090         parentLesson->appendChildContainer(child);
0091         endInsertRows();
0092 
0093         while (parentLesson->entryCount() > 0 && child->entryCount() < entriesPerLesson) {
0094             // next entry to be assigned to one of the new lessons
0095             int nextEntry = 0;
0096             if (order == Random) {
0097                 nextEntry = QRandomGenerator::global()->bounded(parentLesson->entryCount());
0098                 child->appendEntry(parentLesson->entry(nextEntry));
0099             }
0100         }
0101     }
0102 }
0103 
0104 #include "moc_lessonmodel.cpp"