diff --git a/keduvocdocument/CMakeLists.txt b/keduvocdocument/CMakeLists.txt index b1a3ac1..d1bca68 100644 --- a/keduvocdocument/CMakeLists.txt +++ b/keduvocdocument/CMakeLists.txt @@ -1,141 +1,141 @@ if (BUILD_TESTING) add_subdirectory(autotests) add_subdirectory(tests) endif() ########### next target ############### include(ECMSetupVersion) ecm_setup_version(5.0.0 VARIABLE_PREFIX KDEEDU VERSION_HEADER "${CMAKE_BINARY_DIR}/keduvocdocument/keduvocdocument_version.h" PACKAGE_VERSION_FILE "${CMAKE_BINARY_DIR}/KEduVocDocumentVersion.cmake") include_directories( ${CMAKE_CURRENT_BINARY_DIR} ) set(keduvocdocument_LIB_SRCS keduvocdocument.cpp keduvocidentifier.cpp keduvocexpression.cpp keduvoctranslation.cpp keduvoccontainer.cpp keduvoclesson.cpp keduvocleitnerbox.cpp keduvoctext.cpp keduvocarticle.cpp keduvocconjugation.cpp keduvocpersonalpronoun.cpp keduvocdeclension.cpp keduvocwordtype.cpp keduvockvtmlcompability.cpp keduvockvtml2writer.cpp keduvoccsvwriter.cpp keduvoccontainermodel.cpp - #keduvoclessonmodel.cpp + keduvoclessonmodel.cpp keduvocreadonlycontainermodel.cpp keduvocvocabularymodel.cpp #keduvocwordclassmodel.cpp #keduvoccontainermimedata.cpp #keduvocvocabularymimedata.cpp readerwriters/dummyreader.cpp readerwriters/failedreader.cpp readerwriters/keduvockvtmlreader.cpp readerwriters/keduvockvtml2reader.cpp readerwriters/keduvoccsvreader.cpp readerwriters/keduvocpaukerreader.cpp readerwriters/keduvocvokabelnreader.cpp readerwriters/keduvocwqlreader.cpp readerwriters/keduvocxdxfreader.cpp readerwriters/readermanager.cpp sharedkvtmlfiles.cpp ) include(ECMGenerateHeaders) ecm_generate_headers( KdeEdu_HEADERS HEADER_NAMES KEduVocDocument KEduVocIdentifier KEduVocExpression KEduVocText KEduVocArticle KEduVocConjugation KEduVocLesson KEduVocLeitnerBox KEduVocContainer KEduVocWordFlags KEduVocMultipleChoice KEduVocTranslation KEduVocWordtype KEduVocPersonalPronoun SharedKVTMLFiles KEduVocDeclension KEduVocKVTML2Writer KEduVocContainerModel - #KEduVocLessonModel + KEduVocLessonModel KEduVocReadOnlyContainerModel KEduVocVocabularyModel #KEduVocWordClassModel #KEduVocContainerMimeData #KEduVocVocabularyMimeData REQUIRED_HEADERS KdeEdu_HEADERS ) add_library(KEduVocDocument SHARED ${keduvocdocument_LIB_SRCS}) generate_export_header(KEduVocDocument BASE_NAME KEduVocDocument) # vHanda: Add library alias? target_link_libraries(KEduVocDocument PUBLIC Qt5::Xml PRIVATE KF5::I18n KF5::KIOCore KF5::Archive ) # vHanda: Add Export Name? set_target_properties(KEduVocDocument PROPERTIES VERSION ${KDEEDU_VERSION_STRING} SOVERSION ${KDEEDU_SOVERSION} ) target_include_directories(KEduVocDocument INTERFACE "$") #Library for non-api unittests add_library(keduvocdocument_static STATIC ${keduvocdocument_LIB_SRCS}) set_target_properties(keduvocdocument_static PROPERTIES COMPILE_FLAGS -DKEDUVOCDOCUMENT_STATIC_DEFINE) target_link_libraries(keduvocdocument_static PUBLIC KF5::KIOCore Qt5::Xml Qt5::Gui PRIVATE KF5::I18n KF5::Archive ) # if we want to set our own version instead of following kde generic #set(LIB_KEDUVOCDOCUMENT_VERSION "5.0.0") #set(LIB_KEDUVOCDOCUMENT_SOVERSION "5") #set_target_properties(keduvocdocument # PROPERTIES VERSION ${LIB_KEDUVOCDOCUMENT_VERSION} # SOVERSION ${LIB_KEDUVOCDOCUMENT_SOVERSION} #) install(TARGETS KEduVocDocument EXPORT LibKEduVocDocumentTargets ${INSTALL_TARGETS_DEFAULT_ARGS}) ########### install files ############### install(FILES ${CMAKE_CURRENT_BINARY_DIR}/keduvocdocument_export.h ${KdeEdu_HEADERS} DESTINATION ${INCLUDE_INSTALL_DIR}/libkeduvocdocument COMPONENT Devel ) 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 + + ***************************************************************************/ + +/*************************************************************************** + * * + * 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 +#include +#include + +/** @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(containerIndex.internalPointer())->containerType() != KEduVocContainer::Lesson) { + return; + } + + KEduVocLesson* parentLesson = static_cast(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 + + ***************************************************************************/ + +/*************************************************************************** + * * + * 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