From: Jeremy Paul Whiting Date: Sun, 19 Aug 2007 12:53:07 +0000 (+0000) Subject: document class, readers use new lesson class X-Git-Tag: v3.93.0~76 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=e08c169eba75ddf547d6a8cb2a5a3cf02ff60f08;p=libqmvoc.git document class, readers use new lesson class svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=701814 --- diff --git a/keduvocdocument/keduvoccsvwriter.cpp b/keduvocdocument/keduvoccsvwriter.cpp index 4e0deac..ccfff0c 100644 --- a/keduvocdocument/keduvoccsvwriter.cpp +++ b/keduvocdocument/keduvoccsvwriter.cpp @@ -44,8 +44,8 @@ bool KEduVocCsvWriter::writeDoc(KEduVocDocument *doc, const QString &generator) outputStream.setDevice(m_outputFile); outputStream.setCodec("UTF-8"); - outputStream << i18n("! Title:") << separator << m_doc->title() << "\n"; - outputStream << i18n("! Author:") << separator << m_doc->author() << "\n"; + outputStream << i18nc("@item:intable the title of the document will be written here", "! Title:") << separator << m_doc->title() << "\n"; + outputStream << i18nc("@item:intable the author will be written here", "! Author:") << separator << m_doc->author() << "\n"; KEduVocExpression *expression; int idCount = m_doc->identifierCount(); diff --git a/keduvocdocument/keduvocdocument.cpp b/keduvocdocument/keduvocdocument.cpp index 19683b8..8443ee5 100644 --- a/keduvocdocument/keduvocdocument.cpp +++ b/keduvocdocument/keduvocdocument.cpp @@ -30,6 +30,7 @@ #include #include "keduvocexpression.h" +#include "keduvoclesson.h" #include "keduvockvtmlwriter.h" #include "keduvockvtml2writer.h" #include "keduvoccsvreader.h" @@ -72,7 +73,7 @@ public: QString m_querytrans; QList m_vocabulary; QList m_lessonsInQuery; - QStringList m_lessonDescriptions; + //QStringList m_lessonDescriptions; QStringList m_typeDescriptions; QStringList m_tenseDescriptions; QStringList m_usageDescriptions; @@ -85,6 +86,9 @@ public: QList m_articles; QList m_conjugations; + + // make this a map so removals don't require renumbering :) + QMap m_lessons; LeitnerSystem* m_leitnerSystem; bool m_activeLeitnerSystem; @@ -93,7 +97,7 @@ public: void KEduVocDocument::KEduVocDocumentPrivate::init() { - m_lessonDescriptions.clear(); + m_lessons.clear(); m_articles.clear(); m_typeDescriptions.clear(); m_tenseDescriptions.clear(); @@ -956,43 +960,67 @@ int KEduVocDocument::identifierCount() const return d->m_identifiers.count(); // number of translations } - int KEduVocDocument::appendIdentifier(const QString & id) { d->m_identifiers.append(id); return d->m_identifiers.size() - 1; } +//QString KEduVocDocument::lessonDescription(int idx) const +//{ +// if (idx == 0) +// return i18nc("@label:listbox","no lesson"); -QString KEduVocDocument::lessonDescription(int idx) const -{ - if (idx == 0) - return i18n(""); +// if (idx <= 0 || idx > d->m_lessons.size() ) +// return ""; - if (idx <= 0 || idx > d->m_lessonDescriptions.size() ) - return ""; +// return d->m_lessons[idx-1].description(); +//} - return d->m_lessonDescriptions[idx-1]; -} +//int KEduVocDocument::lessonIndex(const QString &description) const +//{ +// return d->m_lessonDescriptions.indexOf(description) +1; +//} -int KEduVocDocument::lessonIndex(const QString &description) const + +int KEduVocDocument::addLesson(const QString &lessonName, int position) { - return d->m_lessonDescriptions.indexOf(description) +1; + if (position == -1) + { + // no position was specified, so put it wherever there's a slot + position = 1; + while (d->m_lessons.contains(position)) + { + ++position; + } + } + + KEduVocLesson *lesson = new KEduVocLesson; + lesson->setDescription(lessonName); + d->m_lessons.insert(position, lesson); + return position; } - -int KEduVocDocument::appendLesson(const QString &lessonName) +const QMap &KEduVocDocument::lessons() const { - d->m_lessonDescriptions.append(lessonName); - return d->m_lessonDescriptions.count(); // counting from 1 + return d->m_lessons; } - -void KEduVocDocument::renameLesson(const int lessonIndex, const QString &lessonName) +KEduVocLesson * KEduVocDocument::lesson(int index) { - d->m_lessonDescriptions.replace(lessonIndex-1, lessonName); // counting from 1 + KEduVocLesson * retval(NULL); + if (d->m_lessons.contains(index)) + { + retval = d->m_lessons[index]; + } + return retval; } +//void KEduVocDocument::renameLesson(const int lessonIndex, const QString &lessonName) +//{ +// d->m_lessonDescriptions.replace(lessonIndex-1, lessonName); // counting from 1 +//} + bool KEduVocDocument::lessonInQuery(int lessonIndex) const { @@ -1140,13 +1168,19 @@ void KEduVocDocument::setCurrentLesson(int lesson) QStringList KEduVocDocument::lessonDescriptions() const { - return d->m_lessonDescriptions; + QStringList descriptions; + QList lessonObjects = lessons().values(); + for (int i = 0; i < lessonObjects.count(); ++i) + { + descriptions.append(lessonObjects[i]->description()); + } + return descriptions; } int KEduVocDocument::lessonCount() const { - return d->m_lessonDescriptions.count(); + return d->m_lessons.count(); } bool KEduVocDocument::deleteLesson(int lessonIndex, int deleteMode) @@ -1170,7 +1204,7 @@ bool KEduVocDocument::deleteLesson(int lessonIndex, int deleteMode) } // reduce lesson // finally just remove the lesson name - d->m_lessonDescriptions.removeAt(lessonIndex-1); // because of the damned 0 arghh + //d->m_lessonDescriptions.removeAt(lessonIndex-1); // because of the damned 0 arghh int currentInQuery = d->m_lessonsInQuery.indexOf(lessonIndex); if(currentInQuery != -1) @@ -1186,43 +1220,43 @@ bool KEduVocDocument::deleteLesson(int lessonIndex, int deleteMode) } -void KEduVocDocument::setLessonDescriptions(const QStringList &names) -{ - d->m_lessonDescriptions = names; -} - -void KEduVocDocument::moveLesson(int from, int to) -{ -///@todo move in query as well! - // still counting from 1 - d->m_lessonDescriptions.move(from -1, to -1); - - /* - to > from? - lesson >= from && lesson < to: lesson++ - to < from? - lesson >= to && lesson < from: lesson++ - */ - for (int ent = 0; ent < entryCount(); ent++) { - // put from directly to to - if (entry(ent)->lesson() == from) { - entry(ent)->setLesson(to); - } - else - { - if(to > from) - { - if(entry(ent)->lesson() >= from && entry(ent)->lesson() < to) - entry(ent)->setLesson(entry(ent)->lesson()-1); - } - else - { - if(entry(ent)->lesson() >= to && entry(ent)->lesson() < from) - entry(ent)->setLesson(entry(ent)->lesson()+1); - } - } - } -} +//void KEduVocDocument::setLessonDescriptions(const QStringList &names) +//{ +// d->m_lessonDescriptions = names; +//} + +//void KEduVocDocument::moveLesson(int from, int to) +//{ +/////@todo move in query as well! +// // still counting from 1 +// d->m_lessonDescriptions.move(from -1, to -1); + +// /* +// to > from? +// lesson >= from && lesson < to: lesson++ +// to < from? +// lesson >= to && lesson < from: lesson++ +// */ +// for (int ent = 0; ent < entryCount(); ent++) { +// // put from directly to to +// if (entry(ent)->lesson() == from) { +// entry(ent)->setLesson(to); +// } +// else +// { +// if(to > from) +// { +// if(entry(ent)->lesson() >= from && entry(ent)->lesson() < to) +// entry(ent)->setLesson(entry(ent)->lesson()-1); +// } +// else +// { +// if(entry(ent)->lesson() >= to && entry(ent)->lesson() < from) +// entry(ent)->setLesson(entry(ent)->lesson()+1); +// } +// } +// } +//} int KEduVocDocument::search(const QString &substr, int id, int first, int last, bool word_start) { diff --git a/keduvocdocument/keduvocdocument.h b/keduvocdocument/keduvocdocument.h index b162326..6658cea 100644 --- a/keduvocdocument/keduvocdocument.h +++ b/keduvocdocument/keduvocdocument.h @@ -38,6 +38,7 @@ class QStringList; class KEduVocExpression; +class KEduVocLesson; class LeitnerSystem; /** @@ -429,34 +430,20 @@ public: */ void setCurrentLesson(int lesson); - /** - * Get the real name of a lesson from it's index as QString. - * @param index lesson index - * @returns the description (Name) of the lesson with index @p index . - */ - QString lessonDescription(int index) const; - - /** - * Get the index from the long name of a lesson. - * @param description lesson name - * @returns the index of the lesson (from its name) - * -1 if the lesson does not exist + /** get a lesson object + * @returns a pointer to the lesson object at the specified index or NULL if there isn't one */ - int lessonIndex(const QString &description) const; - - /** - * Append a new lesson to the list of lessons. - * @param lessonName name for the new lesson - * @returns the index of the new lesson + KEduVocLesson *lesson(int index); + + /** get all lesson objects + * @returns a map of pointers to lesson objects */ - int appendLesson(const QString &lessonName); + const QMap &lessons() const; /** - * Rename a lesson. - * @param lessonIndex index of lesson - * @param lessonName new name for the lesson + * @returns the number of lessons defined */ - void renameLesson(const int lessonIndex, const QString &lessonName); + int lessonCount() const; /** * Get list of ALL lessons that are selected for query. @@ -489,15 +476,12 @@ public: void removeLessonFromQuery(int lessonIndex); /** - * All lesson descriptions as stringlist. - * @returns a list of defined lessons - */ - QStringList lessonDescriptions() const; - - /** - * @returns the number of lessons defined + * Append a new lesson to the list of lessons. + * @param lessonName name for the new lesson + * @param position lesson number to use (-1 to find the next hole to put it in) + * @returns the index of the new lesson */ - int lessonCount() const; + int addLesson(const QString &lessonName, int position = -1); /** * Delete a lesson. @@ -507,18 +491,46 @@ public: */ bool deleteLesson(int lessonIndex, int deleteMode); + ///** + // * Get the real name of a lesson from it's index as QString. + // * @param index lesson index + // * @returns the description (Name) of the lesson with index @p index . + // */ + //QString lessonDescription(int index) const; + + /** + * Get the index from the long name of a lesson. + * @param description lesson name + * @returns the index of the lesson (from its name) + * -1 if the lesson does not exist + */ + //int lessonIndex(const QString &description) const; + + /** + * Rename a lesson. + * @param lessonIndex index of lesson + * @param lessonName new name for the lesson + */ + //void renameLesson(const int lessonIndex, const QString &lessonName); + + /** + * All lesson descriptions as stringlist. + * @returns a list of defined lessons + */ + QStringList lessonDescriptions() const; + /** * Sets the description of the lesson * @param names list of all names of the lessons */ - void setLessonDescriptions(const QStringList &names); + //void setLessonDescriptions(const QStringList &names); /** * Moves the lesson at index position from to index position to. * @param from the lesson to be moved * @param to the new position */ - void moveLesson(int from, int to); + //void moveLesson(int from, int to); // *** conjugation methods *** diff --git a/keduvocdocument/keduvockvtml2reader.cpp b/keduvocdocument/keduvockvtml2reader.cpp index bfea563..90dd72d 100644 --- a/keduvocdocument/keduvockvtml2reader.cpp +++ b/keduvocdocument/keduvockvtml2reader.cpp @@ -25,6 +25,7 @@ #include #include "keduvocdocument.h" +#include "keduvoclesson.h" #include "kvtml2defs.h" #include "kvtmldefs.h" #include "keduvockvtmlreader.h" @@ -354,6 +355,7 @@ bool KEduVocKvtml2Reader::readEntry(QDomElement &entryElement) } // TODO: probably should insert at id position with a check to see if it exists + // may be useful for detecting corrupt documents m_doc->insertEntry(&expr, id); return result; } @@ -498,7 +500,7 @@ bool KEduVocKvtml2Reader::readLesson(QDomElement &lessonElement) QDomElement currentElement = lessonElement.firstChildElement(KVTML_NAME); if (!currentElement.isNull()) { - lessonId = m_doc->appendLesson(currentElement.text()); + lessonId = m_doc->addLesson(currentElement.text()); } else { @@ -534,6 +536,7 @@ bool KEduVocKvtml2Reader::readLesson(QDomElement &lessonElement) // TODO: once we have a lesson class, add each of these entryids to the lesson // set this lesson for the given enty m_doc->entry(entryId)->setLesson(lessonId); + m_doc->lesson(lessonId)->addEntry(entryId); currentElement = currentElement.nextSiblingElement(KVTML_ENTRYID); } diff --git a/keduvocdocument/keduvockvtml2writer.cpp b/keduvocdocument/keduvockvtml2writer.cpp index 668326a..2342da1 100644 --- a/keduvocdocument/keduvockvtml2writer.cpp +++ b/keduvocdocument/keduvockvtml2writer.cpp @@ -22,6 +22,7 @@ #include "keduvocdocument.h" #include "keduvocexpression.h" +#include "keduvoclesson.h" #include "kvtmldefs.h" #include "kvtml2defs.h" @@ -402,36 +403,37 @@ bool KEduVocKvtml2Writer::writeIdentifiers(QDomElement &identifiersElement) bool KEduVocKvtml2Writer::writeLessons(QDomElement &lessonsElement) { - if (m_doc->lessonDescriptions().count() == 0) + if (m_doc->lessonCount() == 0) return true; - int count = 1; // this starts at 1 because appendLesson returns the count of the lessondescriptions - // instead of the index the lesson was appended at (size() - 1) - foreach(QString lesson, m_doc->lessonDescriptions()) + QMap lessons = m_doc->lessons(); + + foreach(int lessonid, lessons.keys()) { + KEduVocLesson * thisLesson = lessons[lessonid]; + // make lesson element - QDomElement thisLesson = m_domDoc.createElement(KVTML_LESSON); - + QDomElement thisLessonElement = m_domDoc.createElement(KVTML_LESSON); + // add a name - thisLesson.appendChild(newTextElement(KVTML_NAME, lesson)); + thisLessonElement.appendChild(newTextElement(KVTML_NAME, thisLesson->description())); // add a inquery tag - thisLesson.appendChild(newTextElement(KVTML_QUERY, m_doc->lessonInQuery(count) ? KVTML_TRUE : KVTML_FALSE)); + thisLessonElement.appendChild(newTextElement(KVTML_QUERY, m_doc->lessonInQuery(lessonid) ? KVTML_TRUE : KVTML_FALSE)); // add a current tag - thisLesson.appendChild(newTextElement(KVTML_CURRENT, m_doc->currentLesson() == count ? KVTML_TRUE : KVTML_FALSE)); + thisLessonElement.appendChild(newTextElement(KVTML_CURRENT, m_doc->currentLesson() == lessonid ? KVTML_TRUE : KVTML_FALSE)); // TODO: add the entryids... for (int i = 0; i < m_doc->entryCount(); ++i) { - if (m_doc->entry(i)->lesson() == count) + if (m_doc->entry(i)->lesson() == lessonid) { - thisLesson.appendChild(newTextElement(KVTML_ENTRYID, QString::number(i))); + thisLessonElement.appendChild(newTextElement(KVTML_ENTRYID, QString::number(i))); } } - lessonsElement.appendChild(thisLesson); - ++count; + lessonsElement.appendChild(thisLessonElement); } return true; diff --git a/keduvocdocument/keduvockvtmlreader.cpp b/keduvocdocument/keduvockvtmlreader.cpp index d3a67cd..1651907 100644 --- a/keduvocdocument/keduvockvtmlreader.cpp +++ b/keduvocdocument/keduvockvtmlreader.cpp @@ -26,6 +26,7 @@ #include #include "keduvocdocument.h" +#include "keduvoclesson.h" #include "kvtmldefs.h" KEduVocKvtmlReader::KEduVocKvtmlReader(QIODevice *file) @@ -192,7 +193,6 @@ bool KEduVocKvtmlReader::readBody(QDomElement &domElementParent) bool KEduVocKvtmlReader::readLesson(QDomElement &domElementParent) { QString s; - QStringList descriptions; QDomAttr attribute; QDomElement currentElement; @@ -212,9 +212,7 @@ bool KEduVocKvtmlReader::readLesson(QDomElement &domElementParent) if (entryList.length() <= 0) return false; - descriptions.clear(); QList inQueryList; - inQueryList.clear(); for (int i = 0; i < entryList.count(); ++i) { currentElement = entryList.item(i).toElement(); @@ -241,13 +239,12 @@ bool KEduVocKvtmlReader::readLesson(QDomElement &domElementParent) s = currentElement.text(); if (s.isNull()) s = ""; - descriptions.append(s); + m_doc->addLesson(s, no); } } if (inQueryList.count() > 0) m_doc->setLessonsInQuery(inQueryList); - m_doc->setLessonDescriptions(descriptions); return true; } @@ -1054,22 +1051,17 @@ bool KEduVocKvtmlReader::readExpression(QDomElement &domElementParent) attribute = domElementParent.attributeNode(KV_LESS_MEMBER); if (!attribute.isNull()) + { lesson = attribute.value().toInt(); + } - if (lesson && lesson > m_doc->lessonDescriptions().count()) + if (lesson && lesson > m_doc->lessonCount()) { - // description missing ? - QString s; - QStringList sl = m_doc->lessonDescriptions(); - for (int i = m_doc->lessonDescriptions().count(); i < lesson; i++) - { - s.setNum(i + 1); - s.prepend("#"); //create descriptions from number - sl.append(s); - } - m_doc->setLessonDescriptions(sl); + // it's from a lesson that hasn't been added yet + // so make sure this lesson is in the document + m_doc->addLesson(QString("#") + QString::number(lesson), lesson); } - + attribute = domElementParent.attributeNode(KV_SELECTED); if (!attribute.isNull()) inquery = attribute.value() == "1" ? true : false; @@ -1270,7 +1262,6 @@ bool KEduVocKvtmlReader::readExpression(QDomElement &domElementParent) expr.translation(i).gradeFrom(0).setQueryDate(qdate); expr.translation(0).gradeFrom(i).setQueryDate(r_qdate); } -//kDebug() << "KEduVocKvtmlReader::readExpression(): id: " << i << " translation: " << textstr; // Next translation currentElement = currentElement.nextSiblingElement(KV_TRANS); @@ -1281,6 +1272,9 @@ bool KEduVocKvtmlReader::readExpression(QDomElement &domElementParent) m_doc->setQueryIdentifier(q_org, q_trans); m_doc->appendEntry(&expr); + // also add this entryid to the lesson it's part of + m_doc->lesson(lesson)->addEntry(m_doc->entryCount()); + return true; } diff --git a/keduvocdocument/keduvockvtmlwriter.cpp b/keduvocdocument/keduvockvtmlwriter.cpp index 7b751b7..4619377 100644 --- a/keduvocdocument/keduvockvtmlwriter.cpp +++ b/keduvocdocument/keduvockvtmlwriter.cpp @@ -24,6 +24,7 @@ #include #include "keduvocdocument.h" +#include "keduvoclesson.h" #include "keduvocexpression.h" #include "kvtmldefs.h" @@ -101,7 +102,7 @@ bool KEduVocKvtmlWriter::writeDoc(KEduVocDocument *doc, const QString &generator if (entry->lesson() != 0) { int lm = entry->lesson(); - if (lm > m_doc->lessonDescriptions().count()) + if (lm > m_doc->lessonCount()) { // should not be kError() << "index of lesson member too high: " << lm << endl; @@ -301,30 +302,28 @@ bool KEduVocKvtmlWriter::writeDoc(KEduVocDocument *doc, const QString &generator bool KEduVocKvtmlWriter::writeLesson(QDomDocument &domDoc, QDomElement &domElementParent) { - if (m_doc->lessonDescriptions().count() == 0) + if (m_doc->lessonCount() == 0) return true; QDomElement domElementLesson = domDoc.createElement(KV_LESS_GRP); domElementLesson.setAttribute(KV_SIZEHINT, m_doc->sizeHint(-1)); - int count = 1; - foreach(QString lesson, m_doc->lessonDescriptions()) + const QMap lessons = m_doc->lessons(); + QList keys = lessons.keys(); + for (int i = 0; i < keys.size(); ++i) { - if (!lesson.isNull()) - { - QDomElement domElementDesc = domDoc.createElement(KV_LESS_DESC); - QDomText domTextDesc = domDoc.createTextNode(lesson); - - domElementDesc.setAttribute(KV_LESS_NO, count); - if (m_doc->currentLesson() == count) - domElementDesc.setAttribute(KV_LESS_CURR, 1); - if (m_doc->lessonInQuery(count)) - domElementDesc.setAttribute(KV_LESS_QUERY, 1); - - domElementDesc.appendChild(domTextDesc); - domElementLesson.appendChild(domElementDesc); - count++; - } + int thiskey = keys[i]; + QDomElement domElementDesc = domDoc.createElement(KV_LESS_DESC); + QDomText domTextDesc = domDoc.createTextNode(lessons[thiskey]->description()); + + domElementDesc.setAttribute(KV_LESS_NO, thiskey); + if (m_doc->currentLesson() == thiskey) + domElementDesc.setAttribute(KV_LESS_CURR, 1); + if (m_doc->lessonInQuery(thiskey)) + domElementDesc.setAttribute(KV_LESS_QUERY, 1); + + domElementDesc.appendChild(domTextDesc); + domElementLesson.appendChild(domElementDesc); } domElementParent.appendChild(domElementLesson); diff --git a/keduvocdocument/keduvocvokabelnreader.cpp b/keduvocdocument/keduvocvokabelnreader.cpp index df169f8..12a9c1f 100644 --- a/keduvocdocument/keduvocvokabelnreader.cpp +++ b/keduvocdocument/keduvocvokabelnreader.cpp @@ -67,8 +67,7 @@ bool KEduVocVokabelnReader::readDoc(KEduVocDocument *doc) QStringList titles, languages, - words, - lessonDescriptions; + words; bool keepGoing = true; @@ -142,14 +141,11 @@ bool KEduVocVokabelnReader::readDoc(KEduVocDocument *doc) lessonDescr = inputStream.readLine(); lessonDescr = lessonDescr.mid(1, lessonDescr.length() - 2); if (!lessonDescr.isEmpty()) - lessonDescriptions.append(lessonDescr); + m_doc->addLesson(lessonDescr); else break; inputStream.readLine(); } - if (lessonDescriptions.count() > 0) - m_doc->setLessonDescriptions(lessonDescriptions); - return true; } diff --git a/keduvocdocument/keduvocxdxfreader.cpp b/keduvocdocument/keduvocxdxfreader.cpp index 5e69f30..9829a38 100644 --- a/keduvocdocument/keduvocxdxfreader.cpp +++ b/keduvocdocument/keduvocxdxfreader.cpp @@ -69,13 +69,13 @@ void KEduVocXdxfReader::readXdxf() if(!id1.isNull()) m_doc->appendIdentifier(id1.toString().toLower()); else - m_doc->appendIdentifier(i18n("Original")); + m_doc->appendIdentifier(i18nc("@title:column the original language column", "Original")); QStringRef id2 = attributes().value("lang_to"); if(!id2.isNull()) m_doc->appendIdentifier(id2.toString().toLower()); else - m_doc->appendIdentifier(i18n("Translation")); + m_doc->appendIdentifier(i18nc("@title:column one of the translation columns", "Translation")); while (!atEnd()) { readNext();