From: Frederik Gladhorn Date: Fri, 21 Sep 2007 22:10:44 +0000 (+0000) Subject: Rewrite the article class using enums, only one get/set function is needed now. X-Git-Tag: v3.94.0~25 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=09ad862fb235b1b19572726fb634a27ef110ed59;p=libqmvoc.git Rewrite the article class using enums, only one get/set function is needed now. So far we only read/write singular. But support is there for plural and dual. Deprecated old constructor with arguments, but leave it for now, since porting will take some time. This breaks compability with articles in kvtml2 docs! Port parley to use that stuff. svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=715352 --- diff --git a/keduvocdocument/keduvocgrammar.cpp b/keduvocdocument/keduvocgrammar.cpp index c31fe52..748c76c 100644 --- a/keduvocdocument/keduvocgrammar.cpp +++ b/keduvocdocument/keduvocgrammar.cpp @@ -26,6 +26,8 @@ #include "keduvocgrammar.h" +#include +#include class KEduVocComparison::Private { @@ -117,17 +119,14 @@ QString KEduVocComparison::l3() const } + + //================================================================= class KEduVocArticle::Private { public: - QString fem_def; - QString fem_indef; - QString mal_def; - QString mal_indef; - QString nat_def; - QString nat_indef; + QMap m_articles; }; KEduVocArticle::KEduVocArticle() @@ -137,27 +136,25 @@ KEduVocArticle::KEduVocArticle() KEduVocArticle::KEduVocArticle( const KEduVocArticle &other ) :d( new Private ) { - operator= ( other ); + d->m_articles = other.d->m_articles; } KEduVocArticle &KEduVocArticle::operator= ( const KEduVocArticle& other ) { - d->fem_def = other.d->fem_def; - d->fem_indef = other.d->fem_indef; - d->mal_def = other.d->mal_def; - d->mal_indef = other.d->mal_indef; - d->nat_def = other.d->nat_def; - d->nat_indef = other.d->nat_indef; - + d->m_articles = other.d->m_articles; return *this; } -KEduVocArticle::KEduVocArticle( const QString &fem_def, const QString &fem_indef, const QString &mal_def, const QString &mal_indef, const QString &nat_def, const QString &nat_indef ) +KEduVocArticle::KEduVocArticle( const QString &fem_def, const QString &fem_indef, const QString &mal_def, const QString &mal_indef, const QString &neu_def, const QString &neu_indef ) :d( new Private ) { - setFemale( fem_def, fem_indef ); - setMale( mal_def, mal_indef ); - setNeutral( nat_def, nat_indef ); + setArticle( mal_def, Singular, Definite, Masculine ); + setArticle( fem_def, Singular, Definite, Feminine ); + setArticle( neu_def, Singular, Definite, Neuter ); + + setArticle( mal_indef, Singular, Indefinite, Masculine ); + setArticle( fem_indef, Singular, Indefinite, Feminine ); + setArticle( neu_indef, Singular, Indefinite, Neuter ); } KEduVocArticle::~KEduVocArticle() @@ -165,51 +162,22 @@ KEduVocArticle::~KEduVocArticle() delete d; } -void KEduVocArticle::setFemale( const QString &def, const QString &indef ) -{ - d->fem_def = def; - d->fem_indef = indef; -} - -void KEduVocArticle::setMale( const QString &def, const QString &indef ) +QString KEduVocArticle::article(ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender) { - d->mal_def = def; - d->mal_indef = indef; + if ( d->m_articles.contains( indexOf(number, definite, gender) ) ) { + return d->m_articles.value( indexOf(number, definite, gender) ); + } + return QString(); } - -void KEduVocArticle::setNeutral( const QString &def, const QString &indef ) +void KEduVocArticle::setArticle(const QString & article, ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender) { - d->nat_def = def; - d->nat_indef = indef; + kDebug() << article << "#" << number << "def" << definite << "indef" << gender << "index" << indexOf(number, definite, gender); + d->m_articles[indexOf(number, definite, gender)] = article; } - -void KEduVocArticle::getFemale( QString *def, QString *indef ) const +int KEduVocArticle::indexOf(ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender) { - if ( def ) - *def = d->fem_def; - if ( indef ) - *indef = d->fem_indef; + return number + (definite * NumberMAX) + (gender * NumberMAX * DefinitenessMAX); } - - -void KEduVocArticle::getMale( QString *def, QString *indef ) const -{ - if ( def ) - *def = d->mal_def; - if ( indef ) - *indef = d->mal_indef; -} - - -void KEduVocArticle::getNeutral( QString *def, QString *indef ) const -{ - if ( def ) - *def = d->nat_def; - if ( indef ) - *indef = d->nat_indef; -} - - diff --git a/keduvocdocument/keduvocgrammar.h b/keduvocdocument/keduvocgrammar.h index efa7248..3795e46 100644 --- a/keduvocdocument/keduvocgrammar.h +++ b/keduvocdocument/keduvocgrammar.h @@ -43,18 +43,21 @@ public: enum ArticleNumber { Singular, Dual, - Plural + Plural, + NumberMAX }; enum ArticleGender { Masculine, Feminine, - Neuter + Neuter, + GenderMAX }; enum ArticleDefiniteness { Definite, - Indefinite + Indefinite, + DefinitenessMAX }; @@ -69,7 +72,7 @@ public: KEduVocArticle( const KEduVocArticle &other ); /** - * The constructor with arguments + * DEPRECATED The constructor with arguments, assumes singular! * @param fem_def reference to a QString with the definite female article * @param fem_indef reference to a QString with the indefinite female article * @param mal_def reference to a QString with the definite male article @@ -77,58 +80,27 @@ public: * @param nat_def reference to a QString with the definite neutral article * @param nat_indef reference to a QString with the indefinite neutral article */ - KEduVocArticle( const QString &fem_def, const QString &fem_indef, const QString &mal_def, const QString &mal_indef, const QString &nat_def, const QString &nat_indef ); + KDE_DEPRECATED KEduVocArticle( const QString &fem_def, const QString &fem_indef, const QString &mal_def, const QString &mal_indef, const QString &nat_def, const QString &nat_indef ); /** * default destructor, deletes the d pointer */ ~KEduVocArticle(); - /** - * Sets the female articles - * @param def const reference to a QString with the definite female article - * @param indef const reference to a QString with the indefinite female article - */ - void setFemale( const QString &def, const QString &indef ); /** - * Sets the male articles - * @param def const reference to a QString with the definite male article - * @param indef const reference to a QString with the indefinite male article - */ - void setMale( const QString &def, const QString &indef ); - - /** - * Sets the neutral articles - * @param def const reference to a QString with the definite neutral article - * @param indef const reference to a QString with the indefinite neutral article - */ - void setNeutral( const QString &def, const QString &indef ); - - /** get the female articles - * @param def pointer to the definite form - * @param indef pointer to the indefinite form + * assignment operator for d-pointer copying */ - void getFemale( QString *def, QString *indef ) const; + KEduVocArticle &operator= ( const KEduVocArticle& other ); - /** get the male articles - * @param def pointer to the definite form - * @param indef pointer to the indefinite form - */ - void getMale( QString *def, QString *indef ) const; - /** get the neutral articles - * @param def pointer to the definite form - * @param indef pointer to the indefinite form - */ - void getNeutral( QString *def, QString *indef ) const; + QString article(ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender); - /** - * assignment operator for d-pointer copying - */ - KEduVocArticle &operator= ( const KEduVocArticle& other ); + void setArticle(const QString& article, ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender); private: + int indexOf(ArticleNumber number, ArticleDefiniteness definite, ArticleGender gender); + class Private; Private * const d; }; diff --git a/keduvocdocument/keduvockvtml2reader.cpp b/keduvocdocument/keduvockvtml2reader.cpp index ab9780a..e12c37b 100644 --- a/keduvocdocument/keduvockvtml2reader.cpp +++ b/keduvocdocument/keduvockvtml2reader.cpp @@ -497,7 +497,7 @@ bool KEduVocKvtml2Reader::readArticle( QDomElement &articleElement, int identifi QString mal_indef = ""; QString nat_indef = ""; - QDomElement currentElement = articleElement.firstChildElement( KVTML_DEFINITE ); + QDomElement currentElement = articleElement.firstChildElement( KVTML_SINGULAR ).firstChildElement( KVTML_DEFINITE ); if ( !currentElement.isNull() ) { QDomElement subElement = currentElement.firstChildElement( KVTML_MALE ); @@ -516,7 +516,7 @@ bool KEduVocKvtml2Reader::readArticle( QDomElement &articleElement, int identifi } } - currentElement = articleElement.firstChildElement( KVTML_INDEFINITE ); + currentElement = articleElement.firstChildElement( KVTML_SINGULAR ).firstChildElement( KVTML_INDEFINITE ); if ( !currentElement.isNull() ) { QDomElement subElement = currentElement.firstChildElement( KVTML_MALE ); diff --git a/keduvocdocument/keduvockvtml2writer.cpp b/keduvocdocument/keduvockvtml2writer.cpp index a962d47..e4619c2 100644 --- a/keduvocdocument/keduvockvtml2writer.cpp +++ b/keduvocdocument/keduvockvtml2writer.cpp @@ -207,44 +207,59 @@ bool KEduVocKvtml2Writer::writeLessons( QDomElement &lessonsElement ) bool KEduVocKvtml2Writer::writeArticle( QDomElement &articleElement, int article ) { + ///@todo: this is ugly as hell and writes only singular + + QDomElement singular = m_domDoc.createElement( KVTML_SINGULAR ); + QDomElement definite = m_domDoc.createElement( KVTML_DEFINITE ); QDomElement indefinite = m_domDoc.createElement( KVTML_INDEFINITE ); QString def; QString indef; // male - m_doc->identifier(article).article().getMale( &def, &indef ); - if ( !def.isEmpty() ) { - definite.appendChild( newTextElement( KVTML_MALE, def ) ); + QString articleString; + articleString = m_doc->identifier(article).article().article( KEduVocArticle::Singular, KEduVocArticle::Definite, KEduVocArticle::Masculine ); + if ( !articleString.isEmpty() ) { + definite.appendChild( newTextElement( KVTML_MALE, articleString ) ); } - if ( !indef.isEmpty() ) { - indefinite.appendChild( newTextElement( KVTML_MALE, indef ) ); + articleString = m_doc->identifier(article).article().article( KEduVocArticle::Singular, KEduVocArticle::Indefinite, KEduVocArticle::Masculine ); + if ( !articleString.isEmpty() ) { + indefinite.appendChild( newTextElement( KVTML_MALE, articleString ) ); } // female - m_doc->identifier(article).article().getFemale( &def, &indef ); - if ( !def.isEmpty() ) { - definite.appendChild( newTextElement( KVTML_FEMALE, def ) ); + articleString = m_doc->identifier(article).article().article( KEduVocArticle::Singular, KEduVocArticle::Definite, KEduVocArticle::Feminine ); + if ( !articleString.isEmpty() ) { + definite.appendChild( newTextElement( KVTML_FEMALE, articleString ) ); } - if ( !indef.isEmpty() ) { - indefinite.appendChild( newTextElement( KVTML_FEMALE, indef ) ); + articleString = m_doc->identifier(article).article().article( KEduVocArticle::Singular, KEduVocArticle::Indefinite, KEduVocArticle::Feminine ); + if ( !articleString.isEmpty() ) { + indefinite.appendChild( newTextElement( KVTML_FEMALE, articleString ) ); } // neutral - m_doc->identifier(article).article().getNeutral( &def, &indef ); - if ( !def.isEmpty() ) { - definite.appendChild( newTextElement( KVTML_NEUTRAL, def ) ); + articleString = m_doc->identifier(article).article().article( KEduVocArticle::Singular, KEduVocArticle::Definite, KEduVocArticle::Neuter ); + if ( !articleString.isEmpty() ) { + definite.appendChild( newTextElement( KVTML_NEUTRAL, articleString ) ); } - if ( !indef.isEmpty() ) { - indefinite.appendChild( newTextElement( KVTML_NEUTRAL, indef ) ); + articleString = m_doc->identifier(article).article().article( KEduVocArticle::Singular, KEduVocArticle::Indefinite, KEduVocArticle::Neuter ); + if ( !articleString.isEmpty() ) { + indefinite.appendChild( newTextElement( KVTML_NEUTRAL, articleString ) ); } + + + if ( definite.hasChildNodes() ) { - articleElement.appendChild( definite ); + singular.appendChild( definite ); } if ( indefinite.hasChildNodes() ) { - articleElement.appendChild( indefinite ); + singular.appendChild( indefinite ); + } + + if ( singular.hasChildNodes() ) { + articleElement.appendChild( singular ); } return true; } diff --git a/keduvocdocument/keduvockvtmlwriter.cpp b/keduvocdocument/keduvockvtmlwriter.cpp index fe8a631..c75a958 100644 --- a/keduvocdocument/keduvockvtmlwriter.cpp +++ b/keduvocdocument/keduvockvtmlwriter.cpp @@ -345,49 +345,57 @@ bool KEduVocKvtmlWriter::writeArticle( QDomDocument &domDoc, QDomElement &domEle s = m_doc->identifier(i).name().simplified(); domElementEntry.setAttribute( KV_LANG, s ); - m_doc->identifier(i).article().getFemale( &def, &indef ); - if ( !def.isEmpty() ) { + QString articleString; + // female + articleString = m_doc->identifier(i).article().article( KEduVocArticle::Singular, KEduVocArticle::Definite, KEduVocArticle::Feminine ); + if ( !articleString.isEmpty() ) { QDomElement domElementFD = domDoc.createElement( KV_ART_FD ); - QDomText domTextFD = domDoc.createTextNode( def ); + QDomText domTextFD = domDoc.createTextNode( articleString ); domElementFD.appendChild( domTextFD ); domElementEntry.appendChild( domElementFD ); } - if ( !indef.isEmpty() ) { + articleString = m_doc->identifier(i).article().article( KEduVocArticle::Singular, KEduVocArticle::Indefinite, KEduVocArticle::Feminine ); + if ( !articleString.isEmpty() ) { QDomElement domElementFI = domDoc.createElement( KV_ART_FI ); - QDomText domTextFI = domDoc.createTextNode( indef ); + QDomText domTextFI = domDoc.createTextNode( articleString ); domElementFI.appendChild( domTextFI ); domElementEntry.appendChild( domElementFI ); } - m_doc->identifier(i).article().getMale( &def, &indef ); - if ( !def.isEmpty() ) { + + // male + articleString = m_doc->identifier(i).article().article( KEduVocArticle::Singular, KEduVocArticle::Definite, KEduVocArticle::Masculine ); + if ( !articleString.isEmpty() ) { QDomElement domElementMD = domDoc.createElement( KV_ART_MD ); - QDomText domTextMD = domDoc.createTextNode( def ); + QDomText domTextMD = domDoc.createTextNode( articleString ); domElementMD.appendChild( domTextMD ); domElementEntry.appendChild( domElementMD ); } - if ( !indef.isEmpty() ) { + articleString = m_doc->identifier(i).article().article( KEduVocArticle::Singular, KEduVocArticle::Indefinite, KEduVocArticle::Masculine ); + if ( !articleString.isEmpty() ) { QDomElement domElementMI = domDoc.createElement( KV_ART_MI ); - QDomText domTextMI = domDoc.createTextNode( indef ); + QDomText domTextMI = domDoc.createTextNode( articleString ); domElementMI.appendChild( domTextMI ); domElementEntry.appendChild( domElementMI ); } - m_doc->identifier(i).article().getNeutral( &def, &indef ); - if ( !def.isEmpty() ) { + // neuter + articleString = m_doc->identifier(i).article().article( KEduVocArticle::Singular, KEduVocArticle::Definite, KEduVocArticle::Neuter ); + if ( !articleString.isEmpty() ) { QDomElement domElementND = domDoc.createElement( KV_ART_ND ); - QDomText domTextND = domDoc.createTextNode( def ); + QDomText domTextND = domDoc.createTextNode( articleString ); domElementND.appendChild( domTextND ); domElementEntry.appendChild( domElementND ); } - if ( !indef.isEmpty() ) { + articleString = m_doc->identifier(i).article().article( KEduVocArticle::Singular, KEduVocArticle::Indefinite, KEduVocArticle::Neuter ); + if ( !articleString.isEmpty() ) { QDomElement domElementNI = domDoc.createElement( KV_ART_NI ); - QDomText domTextNI = domDoc.createTextNode( indef ); + QDomText domTextNI = domDoc.createTextNode( articleString ); domElementNI.appendChild( domTextNI ); domElementEntry.appendChild( domElementNI );