parley
kvtlessonmodel.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
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
00140 }
00141 return Qt::ItemIsEnabled;
00142 }
00143
00144 Qt::DropActions KVTLessonModel::supportedDropActions() const
00145 {
00146
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
00207 emit dataChanged(index(0, 0, QModelIndex()), index(rowCount(), 0, QModelIndex()));
00208
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
00219 emit dataChanged(index(0, 0, QModelIndex()), index(rowCount(), 0, QModelIndex()));
00220
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
00229 newLessonIndex = m_doc->appendLesson(QString(), true);
00230
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
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
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) {
00271 numNewLessons++;
00272 }
00273
00274
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"