• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

parley

kvtlessonmodel.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 
00003                              kvtlessonmodel
00004 
00005     -----------------------------------------------------------------------
00006 
00007     copyright     : (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
00008 
00009     -----------------------------------------------------------------------
00010 
00011  ***************************************************************************/
00012 
00013 /***************************************************************************
00014  *                                                                         *
00015  *   This program is free software; you can redistribute it and/or modify  *
00016  *   it under the terms of the GNU General Public License as published by  *
00017  *   the Free Software Foundation; either version 2 of the License, or     *
00018  *   (at your option) any later version.                                   *
00019  *                                                                         *
00020  ***************************************************************************/
00021 
00022 #include "kvtlessonmodel.h"
00023 
00024 #include <QItemSelection>
00025 #include <QMimeData>
00026 
00027 #include <kdebug.h>
00028 #include <klocale.h>
00029 
00030 #include <keduvocdocument.h>
00031 #include <keduvoclesson.h>
00032 #include <keduvocexpression.h>
00033 #include <krandom.h>
00034 
00043 // void LessOptPage::slotCleanup()
00044 // {
00045 //   QVector<bool> used_lesson;
00046 //   for (int i = 0; i < (int) optionsList->count(); i++)
00047 //     used_lesson.append(false);
00048 //
00049 //   for (int i = 0; i < (int) doc->entryCount(); i++) {
00050 //     int idx = doc->entry(i)->lesson();
00051 //     if ((int) used_lesson.count() < idx)
00052 //       used_lesson.resize(idx);
00053 //     if (idx != 0)
00054 //       used_lesson[idx - 1] = true ;
00055 //   }
00056 //
00057 //   for (int i = used_lesson.count() - 1; i >= 0; i--)
00058 //     if (!used_lesson[i]) {
00059 //       for (int u = 0; u < (int) lessonIndex.count() ; u++) {
00060 //         if (lessonIndex[u] == i || lessonIndex[u] < 0) {
00061 //           m_currentLesson = i;
00062 //           slotDeleteLesson();
00063 //           break;
00064 //         }
00065 //       }
00066 //     }
00067 //
00068 //   m_currentLesson = 0;
00069 //   optionsList->setCurrentItem(optionsList->item(m_currentLesson));
00070 // }
00071 
00072 
00073 
00074 
00075 // void LessOptPage::cleanUnused(KEduVocDocument *doc, const QList<int> &lessonIndex, int old_lessons, QList<int> &lessons_in_query)
00076 // {
00077 //   QList<int> translate_index;
00078 //
00079 //   /////////////////////////////////////////////////////
00080 //   // translate_index contains new index number for each
00081 //   // old index
00082 //   for (int i = 0; i <= qMax (old_lessons, (int) lessonIndex.count()); i++)
00083 //     translate_index.append(0);
00084 //
00085 //   // now adjust lesson descriptions to new index
00086 //   for (int i = 0; i < (int) lessonIndex.size(); i++) {
00087 //     if (lessonIndex[i] >= 0)
00088 //       translate_index[lessonIndex[i] /*+ 1*/] = i + 1;
00089 //   }
00090 //
00091 //   // now adjust for query selected lessons to new index
00092 //   for (int i = (int) lessons_in_query.count() - 1; i >= 0; i--) {
00093 //     if (translate_index[lessons_in_query[i]] > 0)
00094 //       lessons_in_query[i] = translate_index[lessons_in_query[i]];
00095 //     else
00096 //       lessons_in_query.erase(lessons_in_query.begin() + i);
00097 //   }
00098 //
00099 //   // only keep remaining lesson member indices
00100 //
00101 //   // set lesson index to 0 when not needed any more
00102 //   // and translate to new index
00103 //   for (int i = 0; i < doc->entryCount(); i++) {
00104 //     if (doc->entry(i)->lesson() != 0)
00105 //       doc->entry(i)->setLesson(translate_index[doc->entry(i)->lesson()]);
00106 //   }
00107 // }
00108 
00109 void KVTLessonModel::setDocument(KEduVocDocument * doc)
00110 {
00111     m_doc = doc;
00112     reset();
00113 }
00114 
00115 int KVTLessonModel::rowCount(const QModelIndex &parent) const
00116 {
00117     Q_UNUSED(parent);
00118     return m_doc->lessonCount();
00119 }
00120 
00121 QVariant KVTLessonModel::headerData(int section, Qt::Orientation orientation, int role) const
00122 {
00123     if (role != Qt::DisplayRole)
00124         return QVariant();
00125 
00126     if (orientation == Qt::Horizontal && section == 0)
00127         return QString(i18n("Lesson"));
00128 
00129     if (orientation == Qt::Horizontal)
00130         return QString("Column %1").arg(section);
00131     else
00132         return QString("Row %1").arg(section);
00133 }
00134 
00135 Qt::ItemFlags KVTLessonModel::flags(const QModelIndex &index) const
00136 {
00137     if (index.isValid()) {
00138         return (Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsUserCheckable);
00139         // | Qt::ItemIsDragEnabled); // | Qt::ItemIsDropEnabled);
00140     }
00141     return  Qt::ItemIsEnabled; // | Qt::ItemIsDropEnabled;
00142 }
00143 
00144 Qt::DropActions KVTLessonModel::supportedDropActions() const
00145 {
00146     //return Qt::CopyAction | Qt::MoveAction;
00147     return Qt::MoveAction;
00148 }
00149 
00150 QVariant KVTLessonModel::data(const QModelIndex &index, int role) const
00151 {
00152     if (!index.isValid())
00153         return QVariant();
00154 
00155     if (index.row() >= m_doc->lessonCount())
00156         return QVariant();
00157 
00158     if (role == Qt::DisplayRole || role == Qt::EditRole)
00159         return m_doc->lesson(index.row()).name();
00160 
00162     if (role == Qt::CheckStateRole) {
00163         if (m_doc->lesson(index.row()).inPractice())
00164             return Qt::Checked;
00165         else
00166             return Qt::Unchecked;
00167     } else
00168         return QVariant();
00169 }
00170 
00171 bool KVTLessonModel::setData(const QModelIndex &index, const QVariant &value, int role)
00172 {
00173     if (!index.isValid())
00174         return false;
00175 
00176     if (index.row() >= m_doc->lessonCount())
00177         return false;
00178 
00180     if (role == Qt::EditRole) {
00181         m_doc->lesson(index.row()).setName(value.toString());
00182         emit dataChanged(index, index);
00183         return true;
00184     }
00185 
00187     if (role == Qt::CheckStateRole) {
00188         if (!m_doc->lesson(index.row()).inPractice())
00189             m_doc->lesson(index.row()).setInPractice(true);
00190         else
00191             m_doc->lesson(index.row()).setInPractice(false);
00192         m_doc->setModified();
00193         emit dataChanged(index, index);
00194         return true;
00195     }
00196     return false;
00197 }
00198 
00199 
00200 void KVTLessonModel::setAllLessonsInPractice()
00201 {
00202     for (int lesson = 0; lesson < m_doc->lessonCount(); lesson++) {
00203         m_doc->lesson(lesson).setInPractice(true);
00204     }
00205 
00206     // this is not too great - say all data has changed
00207     emit dataChanged(index(0, 0, QModelIndex()), index(rowCount(), 0, QModelIndex()));
00208     //emit signalLessonsInPracticeChanged(intLessons);
00209 }
00210 
00211 
00212 void KVTLessonModel::setNoLessonsInPractice()
00213 {
00214     for (int lesson = 0; lesson < m_doc->lessonCount(); lesson++) {
00215         m_doc->lesson(lesson).setInPractice(false);
00216     }
00217 
00218     // this is not too great - say all data has changed
00219     emit dataChanged(index(0, 0, QModelIndex()), index(rowCount(), 0, QModelIndex()));
00220     //emit signalLessonsInPracticeChanged(intLessons);
00221 }
00222 
00223 int KVTLessonModel::addLesson(const QString &lessonName)
00224 {
00225     beginInsertRows(QModelIndex(), m_doc->lessonCount(), m_doc->lessonCount());
00226     int newLessonIndex;
00227     if (lessonName.isNull()) {
00228         // add the lesson
00229         newLessonIndex = m_doc->appendLesson(QString(), true);
00230         // then name it according to its index (humans like to count from 1)
00231         m_doc->lesson(newLessonIndex).setName(i18n("Lesson %1", newLessonIndex + 1));
00232     } else {
00233         newLessonIndex = m_doc->appendLesson(lessonName);
00234     }
00235 
00236     endInsertRows();
00237     return newLessonIndex;
00238 }
00239 
00240 bool KVTLessonModel::deleteLesson(int lessonIndex, KEduVocDocument::LessonDeletion mode)
00241 {
00242     bool couldDelete = m_doc->removeLesson(lessonIndex, mode);
00243     if (couldDelete) {
00244         beginRemoveRows(QModelIndex(), lessonIndex, lessonIndex);
00245         endRemoveRows();
00246     }
00247     return couldDelete;
00248 }
00249 
00250 bool KVTLessonModel::removeRows(int row, int count, const QModelIndex &parent)
00251 {
00252     Q_UNUSED(parent);
00254     beginRemoveRows(QModelIndex(), row, row);
00255     endRemoveRows();
00256 
00257     // to support drag and drop
00258     kDebug() << "removeRows(int row, int count, const QModelIndex &parent)" << row << ", " << count;
00259     return true;
00260 }
00261 
00262 
00263 void KVTLessonModel::splitLesson(int lessonIndex, int entriesPerLesson, SplitLessonOrder order)
00264 {
00265     // list of entries in the lesson
00266     QList<int> entryList = m_doc->lesson(lessonIndex).entries();
00267 
00268     QString originalLessonName = m_doc->lesson(lessonIndex).name();
00269     int numNewLessons = entryList.count()/entriesPerLesson;
00270     if (entryList.count()%entriesPerLesson) { // modulo - fraction lesson if not 0 we need one more
00271         numNewLessons++;
00272     }
00273 
00274     // create the empty lessons
00275     int first = addLesson(originalLessonName + QString(" %1").arg(1));
00276     int last;
00277     for (int i=1; i<numNewLessons; i++) {
00278         last = addLesson(originalLessonName + QString(" %1").arg(i+1));
00279     }
00280 
00281     int lessonToFill=first; 
00282     int entries = 0; 
00283     int nextEntry=0; 
00284     while (!entryList.empty()) {
00285         if (entries == entriesPerLesson) {
00286             lessonToFill++;
00287             entries=0;
00288         }
00289 
00290         if (order == random)
00291             nextEntry = KRandom::random() % entryList.count(); 
00292         m_doc->entry(entryList.at(nextEntry))->setLesson(lessonToFill);
00293         m_doc->lesson(lessonIndex).removeEntry(entryList.at(nextEntry));
00294         entryList.removeAt(nextEntry);
00295         entries++;
00296     }
00297 
00298     if (!deleteLesson(lessonIndex, KEduVocDocument::DeleteEmptyLesson))
00299         kDebug() << "Warning - could not delete old lesson!";
00300 
00301     m_doc->setModified(true);
00302 
00304 }
00305 
00306 
00307 
00308 bool KVTLessonModel::dropMimeData(const QMimeData * data, Qt::DropAction action, int row, int column, const QModelIndex & parent)
00309 {
00310     Q_UNUSED(parent);
00311     Q_UNUSED(column);
00312     QByteArray encodedData = data->data("application/vnd.text.list");
00313     QDataStream stream(&encodedData, QIODevice::ReadOnly);
00314     QStringList newItems;
00315     int rows = 0;
00316 
00317     while (!stream.atEnd()) {
00318         QString text;
00319         stream >> text;
00320         newItems << text;
00321         ++rows;
00322     }
00323 
00324     kDebug() << "dropMimeData() " << newItems << " row: " << row << " Qt::DropAction: " << action;
00325     return false;
00326 }
00327 
00328 #include "kvtlessonmodel.moc"

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal