So far it only contains name and locale.
From now on:
- locale = two letter language code, en, es, de,...
- name = description of the contents - English, Spanish, Famous people, Definition, Muscle in latin,...
Next will be to move the articles and personal pronouns into the identifier class.
Eventually the tenses should be kept there as well.
Adapted all readers/writers.
Appending identifiers should always give sane default values.
svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=706410
keduvoccsvreader.cpp
keduvoccsvwriter.cpp
keduvocdocument.cpp
+ keduvocidentifier.cpp
keduvocexpression.cpp
keduvoctranslation.cpp
keduvoclesson.cpp
install(FILES
libkeduvocdocument_export.h
keduvocdocument.h
+ keduvocidentifier.h
keduvocexpression.h
keduvocgrade.h
keduvocgrammar.h
}
for ( int j = 0; j < languageCount; j++ )
- if ( j == 0 )
- m_doc->appendIdentifier( i18nc( "@title:column the original language column", "Original" ) );
- else
- m_doc->appendIdentifier( i18nc( "@title:column one of the translation columns", "Translation %1", j ) );
+ {
+ m_doc->appendIdentifier();
+ }
return true;
}
bool m_sortLesson;
// save these to document
- QStringList m_identifiers; //0= origin, 1,.. translations
+ QList<KEduVocIdentifier> m_identifiers;
+
int m_currentLesson;
QList<int> m_extraSizeHints;
QList<int> m_sizeHints;
}
-int KEduVocDocument::indexOfIdentifier( const QString &lang ) const
+int KEduVocDocument::indexOfIdentifier( const QString& name ) const
{
- QStringList::const_iterator first = d->m_identifiers.begin();
- int count = 0;
- while ( first != d->m_identifiers.end() ) {
- if ( *first == lang )
- return count;
- first++;
- count++;
+ for ( int i=0; i < d->m_identifiers.count(); i++ ) {
+ if ( d->m_identifiers.value(i).name() == name ) {
+ return i;
+ }
}
return -1;
}
-QString KEduVocDocument::identifier( int index ) const
+KEduVocIdentifier& KEduVocDocument::identifier( int index )
{
- if ( index >= d->m_identifiers.size() || index < 0 )
- return "";
- else
- return d->m_identifiers[index];
+ if ( index < 0 || index >= d->m_identifiers.size() ) {
+ kError() << "Invalid identifier index: " << index;
+ }
+ return d->m_identifiers[index];
}
-void KEduVocDocument::setIdentifier( int idx, const QString &id )
+void KEduVocDocument::setIdentifier( int idx, const KEduVocIdentifier &id )
{
- if ( idx < d->m_identifiers.size() && idx >= 0 ) {
+ if ( idx >= 0 && idx < d->m_identifiers.size() ) {
d->m_identifiers[idx] = id;
}
}
void KEduVocDocument::removeIdentifier( int index )
{
- if ( index < d->m_identifiers.size() && index >= 1 ) {
+ if ( index < d->m_identifiers.size() && index >= 0 ) {
d->m_identifiers.removeAt( index );
for ( int i = 0; i < d->m_vocabulary.count(); i++ )
d->m_vocabulary[i].removeTranslation( index );
return d->m_identifiers.count(); // number of translations
}
-int KEduVocDocument::appendIdentifier( const QString & id )
+int KEduVocDocument::appendIdentifier( const KEduVocIdentifier& id )
{
+ int i = d->m_identifiers.size();
d->m_identifiers.append( id );
- return d->m_identifiers.size() - 1;
+ if ( id.name().isEmpty() ) {
+ if ( i == 0 ) {
+ identifier(i).setName(i18nc("The name of the first language/column of vocabulary, if we have to guess it.", "Original"));
+ } else {
+ identifier(i).setName(i18nc( "The name of the second, third ... language/column of vocabulary, if we have to guess it.", "Translation %1", i ) );
+ }
+ }
+
+ return i;
}
+
+// int KEduVocDocument::appendIdentifier(const QString & name)
+// {
+// KEduVocIdentifier identifier;
+// identifier.setName(name);
+// return appendIdentifier(identifier);
+// }
+
+
+
//QString KEduVocDocument::lessonDescription(int idx) const
//{
// if (idx == 0)
}
-
-
#include "keduvocdocument.moc"
#include "libkeduvocdocument_export.h"
+#include "keduvocidentifier.h"
#include "keduvocgrammar.h"
#include "keduvocconjugation.h"
/**
* Appends a new identifier (usually a language)
*
- * @param id the identifier to append
+ * @param name the identifier to append
* @returns the identifier number
*/
- int appendIdentifier( const QString & id );
+// int appendIdentifier( const QString& name );
+
+ int appendIdentifier( const KEduVocIdentifier & identifier = KEduVocIdentifier());
/**
* Sets the identifier of translation
* @param index number of translation 0..x
* @param lang thr language identifier: en=english, de=german, ...
*/
- void setIdentifier( int index, const QString &lang );
+ void setIdentifier( int index, const KEduVocIdentifier& lang );
/**
* Returns the identifier of translation @p index
* @param index number of translation 0..x
* @returns the language identifier: en=english, de=german, ...
*/
- QString identifier( int index ) const;
+// QString identifier( int index ) const;
+ KEduVocIdentifier& identifier( int index );
/**
- * Removes identifier an the according translation in all entries
+ * Removes identifier and the according translations in all entries
*
- * @param index number of translation 1..x
+ * @param index number of translation 0..x
*/
void removeIdentifier( int index );
* @param lang identifier of language
* @returns index of identifier, 0 = original, 1..n = translation, -1 = not found
*/
- int indexOfIdentifier( const QString &lang ) const;
+ int indexOfIdentifier( const QString &name ) const;
// *** type methods ***
--- /dev/null
+/***************************************************************************
+
+ C++ Implementation: keduvocidentifier
+
+ -----------------------------------------------------------------------
+
+ begin : Mi Aug 29 2007
+
+ copyright : (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
+
+ -----------------------------------------------------------------------
+
+ ***************************************************************************/
+
+/***************************************************************************
+ * *
+ * 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 "keduvocidentifier.h"
+
+#include <KLocalizedString>
+
+class KEduVocIdentifier::Private
+{
+public:
+ /// the name: English, Anatomy, Fruit salad
+ QString m_name;
+ /// the locale: en, de, es, ...
+ QString m_locale;
+
+ /**
+ * Let the user provide some additional informatioin about the language.
+ * This could be Traditional/Simplified for chinese to differentiate between them.
+ */
+ QString m_comment;
+
+ /** not sure yet: language|other|??? */
+ QString m_type;
+
+ /** I, you, he, she, it... */
+ KEduVocConjugation m_personalPronouns;
+
+ /** the for english ;)
+ der, die, das ... in german */
+ KEduVocArticle m_articles;
+
+ /** Future, present and past... and many more */
+ QSet < QString > m_tenses;
+
+ /** Size hint for the width of this column - has to go somewere. Here at least we have the headers... */
+ int m_sizeHint;
+};
+
+
+KEduVocIdentifier::KEduVocIdentifier()
+: d( new Private )
+{
+ d->m_name = i18nc("A default title for a single column of vocabulary. Will hardly be used. Should be very generic.", "Title");
+ ///@todo maybe the user locale would be more appropriate
+ d->m_locale = "en";
+}
+
+
+KEduVocIdentifier::~KEduVocIdentifier()
+{
+ delete d;
+}
+
+
+KEduVocIdentifier::KEduVocIdentifier( const KEduVocIdentifier &other )
+: d( new Private )
+{
+///@todo
+ d->m_locale = other.d->m_locale;
+ d->m_name = other.d->m_name;
+
+}
+
+
+KEduVocIdentifier& KEduVocIdentifier::operator= ( const KEduVocIdentifier &other )
+{
+ d->m_locale = other.d->m_locale;
+ d->m_name = other.d->m_name;
+ return *this;
+}
+
+
+QString KEduVocIdentifier::name() const
+{
+ return d->m_name;
+}
+
+void KEduVocIdentifier::setName(const QString & name)
+{
+ d->m_name = name;
+}
+
+QString KEduVocIdentifier::locale() const
+{
+ return d->m_locale;
+}
+
+void KEduVocIdentifier::setLocale(const QString & locale)
+{
+ d->m_locale = locale;
+}
+
--- /dev/null
+/***************************************************************************
+
+ C++ Interface: keduvocidentifier
+
+ -----------------------------------------------------------------------
+
+ begin : Mi Aug 29 2007
+
+ copyright : (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
+
+ -----------------------------------------------------------------------
+
+ ***************************************************************************/
+
+/***************************************************************************
+ * *
+ * 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 KEDUVOCIDENTIFIER_H
+#define KEDUVOCIDENTIFIER_H
+
+#include "libkeduvocdocument_export.h"
+
+#include "keduvocconjugation.h"
+#include "keduvocgrammar.h"
+
+#include <QtCore/QList>
+#include <QtCore/QString>
+#include <QtCore/QSet>
+
+
+/**
+Class to store meta information about a language or any other category in the vocabulary.
+
+ @author
+*/
+class KEDUVOCDOCUMENT_EXPORT KEduVocIdentifier
+{
+public:
+ KEduVocIdentifier();
+ KEduVocIdentifier( const KEduVocIdentifier &other );
+ ~KEduVocIdentifier();
+ KEduVocIdentifier& operator= ( const KEduVocIdentifier &other );
+
+ QString name() const;
+ void setName( const QString& name );
+ QString locale() const;
+ void setLocale( const QString& name );
+
+private:
+ class Private;
+ Private * const d;
+
+};
+
+#endif
return false;
}
- QDomElement currentElement = identifierElement.firstChildElement( KVTML_IDENTIFIERTYPE );
- if ( !currentElement.isNull() ) {
- // TODO: do something with the type
+ // generate empty identifiers in the doc
+ for ( int i = m_doc->identifierCount(); i <= id; i++ ) {
+ m_doc->appendIdentifier( KEduVocIdentifier() );
}
+ // the first element, create the identifier, even if empty
+ QDomElement currentElement = identifierElement.firstChildElement( KVTML_NAME );
+ m_doc->identifier(id).setName( currentElement.text() );
+
currentElement = identifierElement.firstChildElement( KVTML_LOCALE );
- if ( !currentElement.isNull() ) {
- // TODO: do we want to use this for the identifier, or the name?
- int index = m_doc->appendIdentifier( currentElement.text() );
- if ( index != id ) {
- m_errorMessage = i18n( "identifiers out of order" );
- return false;
- }
- }
+ m_doc->identifier(id).setLocale( currentElement.text() );
- currentElement = identifierElement.firstChildElement( KVTML_NAME );
+ currentElement = identifierElement.firstChildElement( KVTML_IDENTIFIERTYPE );
if ( !currentElement.isNull() ) {
- // TODO: do something with the name
+ // TODO: do something with the type
}
currentElement = identifierElement.firstChildElement( KVTML_SIZEHINT );
// record the identifier as the locale for now
// TODO: when support for more parts of the identifier is in the document class (name, type, etc.) store those here as well
- identifier.appendChild( newTextElement( KVTML_LOCALE, m_doc->identifier( i ) ) );
+ identifier.appendChild( newTextElement( KVTML_NAME, m_doc->identifier( i ).name() ) );
+
+ identifier.appendChild( newTextElement( KVTML_LOCALE, m_doc->identifier( i ).locale() ) );
// record articles
QDomElement article = m_domDoc.createElement( KVTML_ARTICLE );
QString lang;
attribute = currentElement.attributeNode( KV_LANG );
- if ( m_doc->identifierCount() <= i ) {
- // first entry
- if ( !attribute.isNull() ) // no definition in first entry
- lang = attribute.value();
- else
- lang = "original";
- m_doc->appendIdentifier( lang );
-//kDebug() << " Identifier " << i << " is " << lang;
- } else {
- if ( !attribute.isNull() && attribute.value() != m_doc->identifier( i ) ) {
- // different originals ?
- m_errorMessage = i18n( "Ambiguous definition of language code" );
- return false;
- }
+ if (!addLanguage(i, attribute.value())) {
+ return false;
}
//---------
QString lang;
QDomAttr domAttrLang = domElementConjugChild.attributeNode( KV_LANG ); // "l"
- if ( m_doc->identifierCount() <= count ) {
- // first entry
- if ( !domAttrLang.isNull() ) // no definition in first entry
- lang = domAttrLang.value();
- else
- lang = "original";
- m_doc->appendIdentifier( lang );
- } else {
- if ( !domAttrLang.isNull() && domAttrLang.value() != m_doc->identifier( count ) ) {
- // different originals ?
- m_errorMessage = i18n( "Ambiguous definition of language code" );
- return false;
- }
+ if (!addLanguage(count, domAttrLang.value())) {
+ return false;
}
+
} else if ( domElementConjugChild.tagName() == KV_CON_TYPE ) { // this means reading translations KV_CON_TYPE == "t"
//----------
// Attribute
if ( m_doc->entryCount() == 0 ) { // this is because in kvtml the languages are saved in the FIRST ENTRY ONLY.
//kDebug() << " Read Expression with identifiers: " << lang;
// new translation
- if ( lang.isEmpty() ) {
- if ( i == 0 ) {
- lang = "original";
- } else {
- // no definition in first entry ?
- lang.setNum( m_doc->identifierCount() );
- lang.prepend( "translation " );
- }
-
- }
- if ( m_doc->identifierCount() <= i )
- m_doc->appendIdentifier( lang );
- } else {
- if ( lang != m_doc->identifier( i ) && !lang.isEmpty() ) {
- // different language ?
- m_errorMessage = i18n( "ambiguous definition of language code" );
+ if (!addLanguage(i, attribute.value())) {
return false;
}
}
-
//---------
// Children
return true;
}
+
+bool KEduVocKvtmlReader::addLanguage( int languageId, const QString& language)
+{
+ if ( m_doc->identifierCount() <= languageId ) {
+ m_doc->appendIdentifier();
+ // first entry
+ if ( !language.isEmpty() ) { // no definition in first entry
+ m_doc->identifier(languageId).setLocale(language);
+ m_doc->identifier(languageId).setName(language);
+ }
+ } else {
+ if ( !language.isEmpty() ) {
+ if ( language != m_doc->identifier( languageId ).locale() ) {
+ // different originals ?
+ m_errorMessage = i18n( "Ambiguous definition of language code" );
+ return false;
+ }
+ }
+ }
+}
+
+
+
+
#include "keduvockvtmlreader.moc"
bool readDoc( KEduVocDocument *doc );
+ /**
+ * Attempt to add a language/locale. Language/locale are set to the same value.
+ * No error if already there with the same value.
+ * @param languageId idenifier number
+ * @param language name
+ * @return true if successful
+ */
+ bool addLanguage( int languageId, const QString& language);
+
bool readLesson( QDomElement &domElementParent );
bool readArticle( QDomElement &domElementParent );
bool readConjug( QDomElement &domElementParent, QList<KEduVocConjugation> &curr_conjug );
- bool readOptions( QDomElement &domElementParent );
+// bool readOptions( QDomElement &domElementParent );
bool readType( QDomElement &domElementParent );
bool readTense( QDomElement &domElementParent );
bool readUsage( QDomElement &domElementParent );
QString s;
domElementOriginal.setAttribute( KV_SIZEHINT, m_doc->sizeHint( 0 ) );
- s = m_doc->identifier( 0 ).simplified();
+ s = m_doc->identifier( 0 ).name().simplified();
if ( s.isEmpty() )
s = "original";
domElementOriginal.setAttribute( KV_LANG, s );
QString s;
domElementTranslation.setAttribute( KV_SIZEHINT, m_doc->sizeHint( trans ) );
- s = m_doc->identifier( trans ).simplified();
+ s = m_doc->identifier( trans ).name().simplified();
if ( s.isEmpty() ) {
s.setNum( trans );
s.prepend( "translation " );
QString indef;
QString s;
- for ( int lfn = 0; lfn < qMin( m_doc->articleCount(), m_doc->identifierCount() ); lfn++ )
+ for ( int i = 0; i < qMin( m_doc->articleCount(), m_doc->identifierCount() ); i++ )
{
QDomElement domElementEntry = domDoc.createElement( KV_ART_ENTRY );
- if ( lfn == 0 ) {
- s = m_doc->identifier( 0 ).simplified();
+ if ( i == 0 ) {
+ s = m_doc->identifier( 0 ).name().simplified();
if ( s.isEmpty() )
s = "original";
} else {
- s = m_doc->identifier( lfn ).simplified();
+ s = m_doc->identifier( i ).name().simplified();
if ( s.isEmpty() ) {
- s.setNum( lfn );
+ s.setNum( i );
s.prepend( "translation " );
}
}
domElementEntry.setAttribute( KV_LANG, s );
- m_doc->article( lfn ).getFemale( &def, &indef );
+ m_doc->article( i ).getFemale( &def, &indef );
if ( !def.isEmpty() ) {
QDomElement domElementFD = domDoc.createElement( KV_ART_FD );
QDomText domTextFD = domDoc.createTextNode( def );
domElementEntry.appendChild( domElementFI );
}
- m_doc->article( lfn ).getMale( &def, &indef );
+ m_doc->article( i ).getMale( &def, &indef );
if ( !def.isEmpty() ) {
QDomElement domElementMD = domDoc.createElement( KV_ART_MD );
QDomText domTextMD = domDoc.createTextNode( def );
domElementEntry.appendChild( domElementMI );
}
- m_doc->article( lfn ).getNatural( &def, &indef );
+ m_doc->article( i ).getNatural( &def, &indef );
if ( !def.isEmpty() ) {
QDomElement domElementND = domDoc.createElement( KV_ART_ND );
QDomText domTextND = domDoc.createTextNode( def );
for ( int ent = 0; ent < qMin( curr_conjug.count(), m_doc->identifierCount() ); ent++ ) {
QDomElement domElementEntry = domDoc.createElement( KV_CON_ENTRY );
- s = m_doc->identifier( ent ).simplified();
+ s = m_doc->identifier( ent ).name().simplified();
if ( s.isEmpty() ) {
s.setNum( ent );
s.prepend( "translation " );
{
m_doc->setAuthor( "http://pauker.sf.net" );
///Pauker does not provide any column titles
- m_doc->appendIdentifier( i18n( "Front Side" ) );
- m_doc->appendIdentifier( i18n( "Reverse Side" ) );
+ m_doc->appendIdentifier();
+ m_doc->appendIdentifier();
while ( !atEnd() ) {
readNext();
begin : Wed Jun 15 19:32:00 PDT 2005
copyright : (C) 2005, 2007 Peter Hedlund <peter.hedlund@kdemail.net>
+ (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
-----------------------------------------------------------------------
lang1 = inputStream.readLine();
languages = lang1.split( "\"," );
- m_doc->appendIdentifier( languages[0].mid( 1 ) );
- m_doc->appendIdentifier( languages[1].mid( 1 ) );
+ m_doc->appendIdentifier();
+ m_doc->identifier(0).setLocale( languages[0].mid( 1 ) );
+ m_doc->identifier(0).setName( languages[0].mid( 1 ) );
+ m_doc->appendIdentifier();
+ m_doc->identifier(1).setLocale( languages[1].mid( 1 ) );
+ m_doc->identifier(1).setName( languages[1].mid( 1 ) );
keepGoing = true;
while ( keepGoing )
-----------------------------------------------------------------------
copyright : (C) 2004, 2007 Peter Hedlund <peter.hedlund@kdemail.net>
(C) 2005 Eric Pignet
+ (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
***************************************************************************/
p = s.indexOf( " [", 0 );
s = s.left( p );
s = s.simplified();
- m_doc->appendIdentifier( s );
- m_doc->appendIdentifier( inputStream.readLine() );
+ m_doc->appendIdentifier();
+ m_doc->identifier(0).setLocale( s );
+ m_doc->identifier(0).setName( s );
+ s = inputStream.readLine();
+ m_doc->appendIdentifier();
+ m_doc->identifier(1).setLocale( s );
+ m_doc->identifier(1).setName( s );
while ( !s.isNull() ) {
s = inputStream.readLine();
{
///The language attributes are required and should be ISO 639-2 codes, but you never know...
QStringRef id1 = attributes().value( "lang_from" );
- if ( !id1.isNull() )
- m_doc->appendIdentifier( id1.toString().toLower() );
- else
- m_doc->appendIdentifier( i18nc( "@title:column the original language column", "Original" ) );
-
+ m_doc->appendIdentifier();
+ if ( !id1.isNull() ) {
+ m_doc->identifier(0).setLocale( id1.toString().toLower() );
+ m_doc->identifier(0).setName( id1.toString().toLower() );
+ }
QStringRef id2 = attributes().value( "lang_to" );
- if ( !id2.isNull() )
- m_doc->appendIdentifier( id2.toString().toLower() );
- else
- m_doc->appendIdentifier( i18nc( "@title:column one of the translation columns", "Translation" ) );
+ m_doc->appendIdentifier();
+ if ( !id2.isNull() ) {
+ m_doc->identifier(1).setLocale( id2.toString().toLower() );
+ m_doc->identifier(1).setName( id2.toString().toLower() );
+ }
while ( !atEnd() ) {
readNext();
// open the file
doc->open( fileUrl );
- QString locale = doc->identifier( 0 );
+ QString locale = doc->identifier( 0 ).name();
// make sure the locale sub-folder exists
KUrl pathUrl( fileUrl );