From f4e4c0409641e0fab69f454c11bb80a897511d96 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Sun, 11 Sep 2005 21:30:46 +0000 Subject: [PATCH] Small changes in the Glossary class. As, when loading a glossary, we always have a glossary, even if empty, moved the Glossary loading from a static constructor to a normal constructor, and put some code in an init() function. Moved some methods from inline to the cpp, just to have more controls. Improved the algorithm for the replacing: [img]foo.png[/img] => CCMAIL: Jason Harris svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=459794 --- kdeeduui/kdeeduglossary.cpp | 84 ++++++++++++++++++++++++------------- kdeeduui/kdeeduglossary.h | 50 +++++++++++----------- 2 files changed, 81 insertions(+), 53 deletions(-) diff --git a/kdeeduui/kdeeduglossary.cpp b/kdeeduui/kdeeduglossary.cpp index 40ab28b..89cc3e7 100644 --- a/kdeeduui/kdeeduglossary.cpp +++ b/kdeeduui/kdeeduglossary.cpp @@ -27,20 +27,45 @@ #include #include #include +#include #include #include #include +Glossary::Glossary( const KURL& url, const QString& path ) +{ + init( url, path ); +} + Glossary::Glossary() { - // setting a generic name for a new glossary - m_name = i18n( "Glossary" ); + init( KURL(), QString() ); } Glossary::~Glossary() { } +void Glossary::init( const KURL& url, const QString& path ) +{ + // setting a generic name for a new glossary + m_name = i18n( "Glossary" ); + + if ( url.isEmpty() ) + return; + + QDomDocument doc( "document" ); + + setPicturePath( path ); + + if ( loadLayout( doc, url ) ) + { + setItemlist( readItems( doc ) ); + if ( !m_picturepath.isEmpty() ) + fixImagePath(); + } +} + bool Glossary::loadLayout( QDomDocument &Document, const KURL& url ) { QFile layoutFile( url.path() ); @@ -54,7 +79,7 @@ bool Glossary::loadLayout( QDomDocument &Document, const KURL& url ) if (!layoutFile.open(QIODevice::ReadOnly)) return false; - ///Check if document is well-formed + // check if document is well-formed if (!Document.setContent(&layoutFile)) { kdDebug() << "wrong xml" << endl; @@ -71,24 +96,25 @@ bool Glossary::isEmpty() const return m_itemlist.count() == 0; } - -Glossary* Glossary::readFromXML( const KURL& url, const QString& path ) +void Glossary::setName( const QString& name ) { - QDomDocument doc( "document" ); - - Glossary *glossary = new Glossary(); - - glossary->setPicturePath( path ); + if ( name.isEmpty()) + return; + m_name = name; +} - if ( glossary->loadLayout( doc, url ) ) - { - QList itemList; - itemList = glossary->readItems( doc ); - glossary->setItemlist( itemList ); - glossary->fixImagePath(); - } +void Glossary::setPicturePath( const QString& path ) +{ + if ( path.isEmpty()) + return; + m_picturepath = path; +} - return glossary; +void Glossary::setBackgroundPicture( const QString& filename ) +{ + if ( filename.isEmpty()) + return; + m_backgroundpicture = filename; } void Glossary::fixImagePath() @@ -96,14 +122,17 @@ void Glossary::fixImagePath() kdDebug() << "Glossary::fixImagePath()" << endl; QList::iterator it = m_itemlist.begin(); const QList::iterator itEnd = m_itemlist.end(); - QString path = m_picturepath; - QString firstpart = ""; + QRegExp exp( "\\[img\\]([^[]+)\\[/img\\]" ); for ( ; it != itEnd ; ++it ) { - ( *it )->setDesc( ( *it )->desc().replace("[img]", firstpart ) ); - ( *it )->setDesc( ( *it )->desc().replace("[/img]", "\" />" ) ); + QString tmp = ( *it )->desc(); + while ( exp.search( tmp ) > -1 ) + { + tmp = tmp.replace( exp, imgtag ); + } + ( *it )->setDesc( tmp ); } } @@ -183,6 +212,7 @@ GlossaryDialog::GlossaryDialog( bool folded, QWidget *parent, const char *name) QToolButton *clear = new QToolButton( plainPage() ); clear->setIconSet( SmallIconSet( "locationbar_erase" ) ); + clear->setToolTip( i18n( "Clear filter" ) ); hbox->addWidget( clear ); QLabel *lbl = new QLabel( plainPage() ); @@ -317,11 +347,9 @@ void GlossaryDialog::slotClicked( Q3ListViewItem *item ) if ( !item ) return; - /** - * The next lines are searching for the correct KnowledgeItem - * in the m_itemList. When it is found the HTML will be - * generated - */ + // The next lines are searching for the correct KnowledgeItem + // in the m_itemList. When it is found the HTML will be + // generated QList::iterator itGl = m_glossaries.begin(); const QList::iterator itGlEnd = m_glossaries.end(); bool found = false; diff --git a/kdeeduui/kdeeduglossary.h b/kdeeduui/kdeeduglossary.h index 64252b5..8b2dd28 100644 --- a/kdeeduui/kdeeduglossary.h +++ b/kdeeduui/kdeeduglossary.h @@ -30,6 +30,7 @@ class GlossaryItem; /** * @class Glossary * @author Carsten Niehaus + * @author Pino Toscano * * This class stores all items to be displayed. It also * has access-methods to the items @@ -37,7 +38,20 @@ class GlossaryItem; class Glossary { public: + /** + * Creates a new glossary and add contents from an XML file. + * Use isEmpty() to know if any items were loaded. + * + * @param url the path of the file to load + * @param path the path of the pictures + */ + Glossary( const KURL& url, const QString& path = 0 ); + + /** + * Creates a new empty glossary + */ Glossary(); + virtual ~Glossary(); /** @@ -67,9 +81,7 @@ class Glossary * Every glossary can have a name. It will be * set to @p name */ - void setName( const QString& name ){ - m_name = name; - } + void setName( const QString& name ); /** * @returns the name of the glossary @@ -85,25 +97,12 @@ class Glossary m_itemlist = list; } - /** - * Read a glossary from an XML file. - * - * @param url The path of the file to load - * @param path The path of the pictures. Will be used as m_picturepath - * - * @return a pointer to the loaded glossary. Even in case of - * error, this won't return 0 but an empty Glossary. - */ - static Glossary* readFromXML( const KURL& url, const QString& path = 0 ); - /** * Every glossaryitem can show pictures. [img src="foo.png] - * will look for the file foo.png in the path defined be + * will look for the file foo.png in the path defined by * @p path */ - void setPicturePath( const QString& path ){ - m_picturepath = path; - } + void setPicturePath( const QString& path ); QString picturePath()const{ return m_picturepath; @@ -114,9 +113,7 @@ class Glossary * of the htmlview. The dialog * will use the file specifiec by the @p filename */ - void setBackgroundPicture( const QString& filename ){ - m_backgroundpicture = filename; - } + void setBackgroundPicture( const QString& filename ); /** * @return the picuture used as the background in @@ -125,10 +122,13 @@ class Glossary QString backgroundPicture()const{ return m_backgroundpicture; } + + protected: + void init( const KURL& url, const QString& path ); private: /** - * This methods parses the given xml-code. It will extract + * This methods parses the given XML code. It will extract * the information of the items and return them as a * QList */ @@ -137,7 +137,7 @@ class Glossary QString m_backgroundpicture; /** - * replaces the [img]-pseudocode with valid html. The path where + * replaces the [img]-pseudocode with valid HTML. The path where * the pictures are stored will be used for pictures */ void fixImagePath(); @@ -216,7 +216,7 @@ class GlossaryItem /** * @return the formated HTML code for current item. - **/ + */ QString toHtml() const; /** @@ -256,7 +256,7 @@ class GlossaryDialog : public KDialogBase void keyPressEvent(QKeyEvent*); /** - * add a new glossary + * Add a new glossary. * * @param newgloss the new glossary to add */ -- 2.47.3