Skip to content

Commit

Permalink
Integrated Comment class.
Browse files Browse the repository at this point in the history
  • Loading branch information
FBergeron committed Jan 4, 2010
1 parent 64ba390 commit 2293dcf
Show file tree
Hide file tree
Showing 15 changed files with 74 additions and 72 deletions.
6 changes: 5 additions & 1 deletion src/Comment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ void Comment::setText( const QString& text ) {
this->text = text;
}

bool Comment::isEmpty() const {
return( this->text.isEmpty() );
}

bool Comment::isMarkedForDeletion() const {
return( markedForDeletion );
}
Expand All @@ -36,7 +40,7 @@ void Comment::setMarkedForDeletion( bool isMarkedForDeletion ) {
}

QDataStream& operator<<( QDataStream& out, const Comment& comment ) {
out << comment.uid << comment.text;
out << comment.uid.toString() << comment.text;
return( out );
}

Expand Down
3 changes: 2 additions & 1 deletion src/Comment.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ class Comment {
public:

Comment( const QUuid& uid = QUuid(), const QString& text = QString() );
Comment( const QString& text = QString() );
Comment( const QString& text );
Comment( const Comment& comment );
~Comment();

QUuid getUid() const;

const QString getText() const;
void setText( const QString& text );
bool isEmpty() const;

bool isMarkedForDeletion() const;
void setMarkedForDeletion( bool isMarkedForDeletion );
Expand Down
47 changes: 4 additions & 43 deletions src/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,45 +68,6 @@ int Controller::getQuizAnswerCount() const {
return( quiz ? quiz->getAnswerCount() : 0 );
}

//ProgressData Controller::getProgressData( const QString& currTermUid /* = QString::null */ ) {
// QString firstLang = prefs.getFirstLanguage();
// QString testLang = prefs.getTestLanguage();
//
// Statistics::instance()->loadTermData( firstLang, testLang );
//
// ProgressData progressData;
//
// if( !currTermUid.isNull() ) {
// TermData termData = Statistics::instance()->getTermData( currTermUid );
//
// progressData.currTerm.repetition = termData.repetition;
// progressData.currTerm.easinessFactor = termData.easinessFactor;
// progressData.currTerm.daysToNextRepetition = ( termData.nextRepetitionDate.isNull() ? 0 : QDate::currentDate().daysTo( termData.nextRepetitionDate ) );
// progressData.currTerm.daysToLastRepetition = ( termData.lastRepetitionDate.isNull() ? INT_MIN : termData.lastRepetitionDate.daysTo( QDate::currentDate() ) );
// progressData.currTerm.successCount = termData.successCount;
// progressData.currTerm.missCount = termData.missCount;
// }
//
// getSchedule( progressData.scheduleForDay );
//
// float efSum = 0.0f;
// float successRateSum = 0.0f;
// progressData.efValueCount = 0;
// progressData.successRateValueCount = 0;
// getDataDistribution( progressData.efDistribution, efSum, progressData.efValueCount,
// progressData.successRateDistribution, successRateSum, progressData.successRateValueCount );
// if( progressData.efValueCount > 0 )
// progressData.efAverage = efSum / progressData.efValueCount;
// if( progressData.successRateValueCount > 0 )
// progressData.successRateAverage = successRateSum / progressData.successRateValueCount;
//
// progressData.efStandardDeviation = 0.0f;
// progressData.successRateStandardDeviation = 0.0f;
// getDataStandardDeviation( progressData.efAverage, progressData.successRateAverage, progressData.efStandardDeviation, progressData.successRateStandardDeviation );
//
// return( progressData );
//}

ProgressData Controller::getProgressData( const BiUidKey& key /* = BiUidKey() */ ) {
QString firstLang = prefs.getFirstLanguage();
QString testLang = prefs.getTestLanguage();
Expand Down Expand Up @@ -307,7 +268,7 @@ Vocabulary* Controller::addVocabulary( Folder* parentFolder, Vocabulary* vocab =
}
for( Term::CommentMap::ConstIterator it3 = term.commentsBegin(); it3 != term.commentsEnd(); it3++ ) {
const BilingualKey& key = it3.key();
const QString& comment = it3.data();
const Comment& comment = it3.data();
newTerm.addComment( key, comment );
}
if( !term.getImagePath().isNull() ) {
Expand Down Expand Up @@ -1697,13 +1658,13 @@ void Controller::searchRec( const QString& query, Vocabulary* vocab, QValueList<
const QString& firstLang = prefs.getFirstLanguage();
const QString& testLang = prefs.getTestLanguage();
BilingualKey key( firstLang, testLang );
if( term.isCommentExists( key ) && term.getComment( key ).find( query ) != -1 )
if( term.isCommentExists( key ) && term.getComment( key ).getText().find( query ) != -1 )
isStringFound = true;
}
else {
for( Term::CommentMap::ConstIterator it = term.commentsBegin(); it != term.commentsEnd(); it++ ) {
const QString& comment = it.data();
if( comment.find( query ) != -1 ) {
const Comment& comment = it.data();
if( comment.getText().find( query ) != -1 ) {
isStringFound = true;
break;
}
Expand Down
4 changes: 2 additions & 2 deletions src/QuizFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ void QuizFrame::setTerm( const Term& term ) {
const QString testLangAlt = testLangTranslation.getAlt();
const QString testLangWord = testLangTranslation.getWord();

QString comment;
Comment comment;
BilingualKey commentKey( controller->getQuizFirstLanguage(), controller->getQuizTestLanguage() );
if( term.isCommentExists( commentKey ) )
comment = term.getComment( commentKey );
Expand All @@ -319,7 +319,7 @@ void QuizFrame::setTerm( const Term& term ) {
testLangTermLineEdit->setText( testLangWord );
testLangTermLineEdit->setCursorPosition( 0 );

commentMultiLineEdit->setText( comment );
commentMultiLineEdit->setText( comment.getText() );
commentMultiLineEdit->setCursorPosition( 0, 0 );

Folder* vocabTree = controller->getVocabTree();
Expand Down
1 change: 1 addition & 0 deletions src/QuizFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <qwidgetstack.h>
#include <qpe/resource.h>
#include "CharacterDialog.h"
#include "Comment.h"
#include "Controller.h"
#include "GradeButton.h"
#include "ImageBox.h"
Expand Down
27 changes: 17 additions & 10 deletions src/Term.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Term::Term( const Term& term ) : uid( term.uid ), id( term.id ), vocabUid( term.
}
for( CommentMap::ConstIterator it = term.comments.begin(); it != term.comments.end(); it++ ) {
const BilingualKey& key = it.key();
const QString& comment = it.data();
const Comment& comment = it.data();
addComment( key, comment );
}
}
Expand Down Expand Up @@ -92,7 +92,7 @@ int Term::getTranslationCount() const {
return( translations.count() );
}

void Term::addComment( const BilingualKey& key, const QString& comment ) {
void Term::addComment( const BilingualKey& key, const Comment& comment ) {
comments.insert( key, comment );
}

Expand All @@ -112,11 +112,11 @@ bool Term::isCommentExists( const BilingualKey& key ) const {
return( comments.contains( key ) );
}

QString& Term::getComment( const BilingualKey& key ) {
Comment& Term::getComment( const BilingualKey& key ) {
return( comments[ key ] );
}

QString Term::getComment( const BilingualKey& key ) const {
Comment Term::getComment( const BilingualKey& key ) const {
return( comments[ key ] );
}

Expand Down Expand Up @@ -150,22 +150,23 @@ QDataStream& operator>>( QDataStream& in, Term& term ) {
QString tempVocabUidStr;
QUuid tempVocabUid;
Term::TranslationMap tempTranslations;

Term::CommentMap tempComments;
Term::OldCommentMap tempOldComments;
QString tempImagePath;
if( Term::isOldFormat ) {
int tempVocabId; // This value is dismissed. The parent vocabulary will affect the vocabUid and the uid after reading all the terms.
in >> tempId;
in >> tempVocabId;
in >> tempTranslations;
in >> tempComments;
in >> tempOldComments;
in >> tempImagePath;
}
else {
in >> tempUidStr;
tempUid = QUuid( tempUidStr );
//in >> tempVocabUidStr;
//tempVocabUid = QUuid( tempVocabUidStr );
in >> tempTranslations >> tempComments;
in >> tempTranslations;
in >> tempComments;
in >> tempImagePath;
}
term = Term( tempId, tempVocabUid, tempUid );
Expand All @@ -175,13 +176,19 @@ QDataStream& operator>>( QDataStream& in, Term& term ) {
if( trans.getLanguage() )
term.addTranslation( trans );
}
// Temporary for conversion from 0.11.x to 0.12.x.
for( Term::OldCommentMap::ConstIterator it = tempOldComments.begin(); it != tempOldComments.end(); it++ ) {
const BilingualKey& key = it.key();
const QString& commentText = it.data();
Comment comment( commentText ); // Uid will be assigned by the parent vocabulary.
term.addComment( key, comment );
}
for( Term::CommentMap::ConstIterator it = tempComments.begin(); it != tempComments.end(); it++ ) {
const BilingualKey& key = it.key();
const QString& comment = it.data();
const Comment& comment = it.data();
term.addComment( key, comment );
}
term.setImagePath( tempImagePath );

return( in );
}

10 changes: 6 additions & 4 deletions src/Term.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <qmap.h>
#include <qstring.h>
#include "BilingualKey.h"
#include "Comment.h"
#include "Translation.h"

class Term {
Expand All @@ -22,7 +23,8 @@ class Term {
~Term();

typedef QMap<QString, Translation> TranslationMap;
typedef QMap<BilingualKey, QString> CommentMap;
typedef QMap<BilingualKey, QString> OldCommentMap; // Temporary for data conversion from 0.11.x to 0.12.x.
typedef QMap<BilingualKey, Comment> CommentMap;

const QUuid getUid() const;
void setUid( const QUuid& uid );
Expand All @@ -44,12 +46,12 @@ class Term {
TranslationMap::ConstIterator translationsEnd() const;
int getTranslationCount() const;

void addComment( const BilingualKey& key, const QString& comment );
void addComment( const BilingualKey& key, const Comment& comment );
void removeComment( const BilingualKey& key );
void removeComments( const QString& language );
bool isCommentExists( const BilingualKey& key ) const;
QString& getComment( const BilingualKey& key );
QString getComment( const BilingualKey& key ) const;
Comment& getComment( const BilingualKey& key );
Comment getComment( const BilingualKey& key ) const;
CommentMap::ConstIterator commentsBegin() const;
CommentMap::ConstIterator commentsEnd() const;
const QString getImagePath() const;
Expand Down
10 changes: 8 additions & 2 deletions src/TermDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ void TermDialog::updateModel() {
testLangTranslation.setAlt( testLangTermAltLineEdit->text() );

BilingualKey commentKey( controller->getPreferences().getFirstLanguage(), controller->getPreferences().getTestLanguage() );
editedTerm->addComment( commentKey, commentMultiLineEdit->text() );
if( !editedTerm->isCommentExists( commentKey ) ) {
Comment comment( Util::createUuid() );
editedTerm->addComment( commentKey, comment );
}

Comment& comment = editedTerm->getComment( commentKey );
comment.setText( commentMultiLineEdit->text() );

// If the path refers to an image in toMOTko's vocabulary, we use a relative path instead.
QString vocabLocation = controller->getApplicationDirName() + "/" + vocab.getParent()->getPath() +
Expand Down Expand Up @@ -259,7 +265,7 @@ void TermDialog::updateUi() {
testLangTermAltLineEdit->setCursorPosition( 0 );
BilingualKey commentKey( controller->getPreferences().getFirstLanguage(), controller->getPreferences().getTestLanguage() );
if( editedTerm->isCommentExists( commentKey ) ) {
commentMultiLineEdit->setText( editedTerm->getComment( commentKey ) );
commentMultiLineEdit->setText( editedTerm->getComment( commentKey ).getText() );
commentMultiLineEdit->setCursorPosition( 0, 0 );
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ QString Util::term2Xml( const Term& term, QStringList* languages, uint indentLev
}
for( Term::CommentMap::ConstIterator it = term.commentsBegin(); it != term.commentsEnd(); it++ ) {
const BilingualKey& key = it.key();
const QString& comment = it.data();
const Comment& comment = it.data();
if( !comment.isEmpty() ) {
if( !languages || ( languages->contains( key.getFirstLanguage() ) && languages->contains( key.getSecondLanguage() ) ) )
ts << indent << "\t<comment languages=\"" << key.toString() << "\">" << Util::escapeXml( comment ) << "</comment>" << endl;
ts << indent << "\t<comment languages=\"" << key.toString() << "\">" << Util::escapeXml( comment.getText() ) << "</comment>" << endl;
}
}
ts << indent << QString( "</term>" ) << endl;
Expand Down
1 change: 1 addition & 0 deletions src/Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <zlib.h>
#include <stdlib.h>
#include "Base.h"
#include "Comment.h"
#include "PixMap.h"
#include "Term.h"

Expand Down
8 changes: 5 additions & 3 deletions src/VocabParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ bool VocabParser::startElement( const QString&, const QString&, const QString& q
tempCh = QString();
if( qname == QString( "comment" ) ) {
commentKey = attribs.value( QString( "languages" ) );
comment = QString();
commentText = QString();
}
}
else if( qname == QString( "trans" ) ) {
Expand Down Expand Up @@ -53,9 +53,11 @@ bool VocabParser::endElement( const QString&, const QString&, const QString& qna
if( qname == QString( "word" ) )
word = tempCh;
else if( qname == QString( "comment" ) ) {
comment = tempCh;
if( languages.count() == 0 || ( languages.contains( commentKey.getFirstLanguage() ) && languages.contains( commentKey.getSecondLanguage() ) ) )
commentText = tempCh;
if( languages.count() == 0 || ( languages.contains( commentKey.getFirstLanguage() ) && languages.contains( commentKey.getSecondLanguage() ) ) ) {
Comment comment( Util::createUuid(), commentText );
term.addComment( commentKey, comment );
}
}
else if( qname == QString( "alt" ) )
alt = tempCh;
Expand Down
3 changes: 2 additions & 1 deletion src/VocabParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <stdlib.h>
#include <string.h>
#include "BilingualKey.h"
#include "Comment.h"
#include "Term.h"
#include "Translation.h"
#include "Vocabulary.h"
Expand All @@ -33,7 +34,7 @@ class VocabParser : public QXmlDefaultHandler {

QString lang;
QString word;
QString comment;
QString commentText;
BilingualKey commentKey;
QString alt;

Expand Down
17 changes: 16 additions & 1 deletion src/Vocabulary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ QDataStream& readOldFormat( QDataStream& in, Vocabulary& vocab, Q_UINT16 version
Term& term = it.data();
term.setUid( Util::createUuid() );
term.setVocabUid( tempUid );
// We need to generate uid for translation instances for this version.
// We need to generate uid for translation and comment instances for this version.
if( version == 0x0010 ) {
QValueList<Translation> convertedTranslations;
for( Term::TranslationMap::ConstIterator it2 = term.translationsBegin(); it2 != term.translationsEnd(); it2++ ) {
Expand All @@ -419,6 +419,21 @@ QDataStream& readOldFormat( QDataStream& in, Vocabulary& vocab, Q_UINT16 version
term.removeTranslation( trans.getLanguage() );
term.addTranslation( trans );
}

Term::CommentMap convertedComments;
for( Term::CommentMap::ConstIterator it2 = term.commentsBegin(); it2 != term.commentsEnd(); it2++ ) {
const BilingualKey& key = it2.key();
const Comment& comment = it2.data();
Comment convertedComment( Util::createUuid(), comment.getText() );
convertedComments.insert( key, convertedComment );
}
// Replace the old comment instances by converted ones.
for( Term::CommentMap::ConstIterator it2 = term.commentsBegin(); it2 != term.commentsEnd(); it2++ ) {
const BilingualKey& key = it2.key();
const Comment& comment = it2.data();
term.removeComment( key );
term.addComment( key, comment );
}
}
if( !term.getImagePath().isNull() ) {
QFileInfo imageFileInfo( term.getImagePath() );
Expand Down
2 changes: 1 addition & 1 deletion src/VocabularyManagerFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,7 @@ void VocabularyManagerFrame::pasteTerms() {
}
for( Term::CommentMap::ConstIterator it3 = term.commentsBegin(); it3 != term.commentsEnd(); it3++ ) {
const BilingualKey& key = it3.key();
const QString& comment = it3.data();
const Comment& comment = it3.data();
newTerm.addComment( key, comment );
}
if( !term.getImagePath().isNull() ) {
Expand Down
3 changes: 2 additions & 1 deletion todo.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
ToDo
----
I need to add Uid and markedForDeletion to translations and comments. Stats should be associated to a pair of translation uids instead of term uid. This way, if I cut terms (actually translations + comments), and paste them somewhere else, I will be able to keep the link with the associated stats. Doing so, I should also add isMarkedForDeletion property for translations. Because the Translations will have uid, I don't need to have a property markedForDeletion for stats. I will be able to find the associated stats using the Translations uid, instead.
When deleting a term (and its translations), remove the associated stats. Now that Translations have uid, I don't need to have a property markedForDeletion for stats. I will be able to find the associated stats using the Translations uid, instead.
I have added markedForDeletion for Translation. Now, do I need to consider its value in some methods of Term.cpp? For example, operator>>() and operator<<()?
Before trying this on the Zaurus, check the memory it takes before and after. I'm curious how much memory it takes to use uid for translation and comments. Is it a big deal? I have about 4250 words at the moment.
Should I remove empty Translations and Comments in TermDialog::updateModel()?
Verify this case: We have a glossary with terms in 3 translations. We delete a term (in 2 translations). Will the remaining translation survive? yes
Make sure that when we remove a term that references to it are also removed (markedForStudy and test, etc.)
When we remove a term (or some of its translations), we must delete its associated stats as well.
Expand Down

0 comments on commit 2293dcf

Please sign in to comment.