[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [libkeduvocdocument/itemmodels] keduvocdocument: Add LessonModel
From:       Rahul Chowdhury <rahulc93 () gmail ! com>
Date:       2015-02-08 19:55:28
Message-ID: E1YKXx2-00059k-K5 () scm ! kde ! org
[Download RAW message or body]

Git commit be0c21de64036686bd547a8151848e7bfff5acd9 by Rahul Chowdhury.
Committed on 08/02/2015 at 19:55.
Pushed by rahulc into branch 'itemmodels'.

Add LessonModel

M  +2    -2    keduvocdocument/CMakeLists.txt
A  +115  -0    keduvocdocument/keduvoclessonmodel.cpp     [License: GPL (v2+)]
A  +55   -0    keduvocdocument/keduvoclessonmodel.h     [License: GPL (v2+)]

http://commits.kde.org/libkeduvocdocument/be0c21de64036686bd547a8151848e7bfff5acd9

diff --git a/keduvocdocument/CMakeLists.txt b/keduvocdocument/CMakeLists.txt
index 5f4115b..5cdc553 100644
--- a/keduvocdocument/CMakeLists.txt
+++ b/keduvocdocument/CMakeLists.txt
@@ -31,7 +31,7 @@ set(keduvocdocument_LIB_SRCS
     keduvockvtml2writer.cpp
     keduvoccsvwriter.cpp
     keduvoccontainermodel.cpp
-    #keduvoclessonmodel.cpp
+    keduvoclessonmodel.cpp
     keduvocreadonlycontainermodel.cpp
     keduvocvocabularymodel.cpp
     #keduvocwordclassmodel.cpp
@@ -73,7 +73,7 @@ ecm_generate_headers( KdeEdu_HEADERS
     KEduVocDeclension
     KEduVocKVTML2Writer
     KEduVocContainerModel
-    #KEduVocLessonModel
+    KEduVocLessonModel
     KEduVocReadOnlyContainerModel
     KEduVocVocabularyModel
     #KEduVocWordClassModel
diff --git a/keduvocdocument/keduvoclessonmodel.cpp \
b/keduvocdocument/keduvoclessonmodel.cpp new file mode 100644
index 0000000..d9508b5
--- /dev/null
+++ b/keduvocdocument/keduvoclessonmodel.cpp
@@ -0,0 +1,115 @@
+/***************************************************************************
+
+    Copyright 2008-2009 Frederik Gladhorn <gladhorn@kde.org>
+
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#include "keduvoclessonmodel.h"
+
+#include <KRandom>
+#include <KLocalizedString>
+#include <QtGui/QFont>
+
+/** @file
+  * Implementation of LessonModel.
+  * Functions to create the model from the lessons of the vocabulary document.
+  */
+
+
+KEduVocLessonModel::KEduVocLessonModel(QObject * parent)
+    : KEduVocContainerModel(KEduVocContainer::Lesson, parent)
+{
+}
+
+KEduVocContainer * KEduVocLessonModel::rootContainer() const
+{
+    if (!getDoc()) {
+        return 0;
+    }
+    return getDoc()->lesson();
+}
+
+Qt::ItemFlags KEduVocLessonModel::flags(const QModelIndex &index) const
+{
+    if (index.isValid() && index.parent() == QModelIndex()) {
+        return (Qt::ItemIsEnabled
+                | Qt::ItemIsEditable
+                | Qt::ItemIsSelectable);
+    }
+
+    // the name column should be checkable to select lessons for practice
+    return  KEduVocContainerModel::flags(index);
+}
+
+QVariant KEduVocLessonModel::data(const QModelIndex & index, int role) const
+{
+    if (index.isValid() && !index.parent().isValid()) {
+        if (index.column() == 0) {
+            switch (role) {
+            case Qt::DisplayRole:
+                return i18nc("display of the name of the vocabulary collection", \
"Collection: %1", KEduVocContainerModel::data(index, role).toString()); +            \
case Qt::FontRole: +                QFont f;
+                f.setBold(true);
+                return f;
+            }
+        }
+    }
+    return KEduVocContainerModel::data(index, role);
+}
+
+bool KEduVocLessonModel::setData(const QModelIndex &index, const QVariant &value, \
int role) +{
+    if (index.isValid() && !index.parent().isValid()) {
+        if (index.column() == ContainerNameColumn && role == Qt::EditRole) {
+            ///@todo decouple the root lesson and document title
+            getDoc()->setTitle(value.toString());
+        }
+    }
+    return KEduVocContainerModel::setData(index, value, role);
+}
+
+void KEduVocLessonModel::splitLesson(const QModelIndex& containerIndex, int \
entriesPerLesson, SplitLessonOrder order) +{
+    if (!containerIndex.isValid()) {
+        return;
+    }
+
+    if (static_cast<KEduVocContainer*>(containerIndex.internalPointer())->containerType() \
!= KEduVocContainer::Lesson) { +        return;
+    }
+
+    KEduVocLesson* parentLesson = \
static_cast<KEduVocLesson*>(containerIndex.internalPointer()); +
+    int numNewLessons = parentLesson->entryCount() / entriesPerLesson;
+    // modulo - fraction lesson if not 0 we need one more
+    if (parentLesson->entryCount() % entriesPerLesson) {
+        numNewLessons++;
+    }
+
+    while (parentLesson->entryCount() > 0) {
+        beginInsertRows(containerIndex, parentLesson->entryCount(), \
parentLesson->entryCount()); +        KEduVocLesson* child = new \
KEduVocLesson(parentLesson->name() +                + QString(" \
%1").arg(parentLesson->childContainerCount() + 1), parentLesson); +        \
parentLesson->appendChildContainer(child); +        endInsertRows();
+
+        while (parentLesson->entryCount() > 0 && child->entryCount() < \
entriesPerLesson) { +            // next entry to be assigned to one of the new \
lessons +            int nextEntry = 0;
+            if (order == Random) {
+                nextEntry = KRandom::random() % parentLesson->entryCount();
+                child->appendEntry(parentLesson->entry(nextEntry));
+            }
+        }
+    }
+}
diff --git a/keduvocdocument/keduvoclessonmodel.h \
b/keduvocdocument/keduvoclessonmodel.h new file mode 100644
index 0000000..bf9b123
--- /dev/null
+++ b/keduvocdocument/keduvoclessonmodel.h
@@ -0,0 +1,55 @@
+/***************************************************************************
+
+    Copyright 2008-2009 Frederik Gladhorn <gladhorn@kde.org>
+
+ ***************************************************************************/
+
+/***************************************************************************
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ ***************************************************************************/
+
+#ifndef KEDUVOCLESSONMODEL_H
+#define KEDUVOCLESSONMODEL_H
+
+#include "keduvoccontainermodel.h"
+
+/**
+  * Model for the tree of lessons.
+  */
+class KEduVocLessonModel : public KEduVocContainerModel
+{
+    Q_OBJECT
+
+public:
+    /** When splitting a lesson into smaller ones - how to sort the entries into \
lessons.*/ +    enum SplitLessonOrder {
+        Sorted,    /**< The order of the entries in the document */
+        Random /**< Randomized */
+    };
+
+    explicit KEduVocLessonModel(QObject *parent = 0);
+
+    virtual Qt::ItemFlags flags(const QModelIndex &index) const;
+    virtual QVariant data(const QModelIndex &index, int role) const;
+    virtual bool setData(const QModelIndex &index, const QVariant &value, int role = \
Qt::EditRole); +
+    /**
+     * Divide a lesson into smaller ones.
+     * Tip: If you create a lesson that is >= the original one and use random order, \
you get your lesson reshuffled. Maybe that is sometimes useful. For now the lessons \
are appended at the end. +     * @param lessonIndex lesson to split
+     * @param entriesPerLesson number of entries in each new lesson
+     * @param order one of SplitLessonOrder
+        */
+    void splitLesson(const QModelIndex& containerIndex, int entriesPerLesson, \
SplitLessonOrder order); +
+protected:
+    KEduVocContainer * rootContainer() const;
+};
+
+
+#endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic