]> Git trees. - libqmvoc.git/commitdiff
Small changes in the Glossary class.
authorPino Toscano <pino@kde.org>
Sun, 11 Sep 2005 21:30:46 +0000 (21:30 +0000)
committerPino Toscano <pino@kde.org>
Sun, 11 Sep 2005 21:30:46 +0000 (21:30 +0000)
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] => <img src="/path/foo.png"/>

CCMAIL: Jason Harris <kstars@30doradus.org>

svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=459794

kdeeduui/kdeeduglossary.cpp
kdeeduui/kdeeduglossary.h

index 40ab28b13b066bbbb2526d61710d3f1bfdf100ae..89cc3e749788a66eb48c595c166cac0084265671 100644 (file)
 #include <qlayout.h>
 #include <qlist.h>
 #include <qpushbutton.h>
+#include <qregexp.h>
 #include <qsplitter.h>
 #include <qstringlist.h>
 #include <qtoolbutton.h>
 
+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<GlossaryItem*> 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<GlossaryItem*>::iterator it = m_itemlist.begin();
        const QList<GlossaryItem*>::iterator itEnd = m_itemlist.end();
-       QString path = m_picturepath;
-       QString firstpart = "<img src=\"";
-       firstpart += path;
+       QString imgtag = "<img src=\"" + m_picturepath + "/" + "\\1\" />";
+       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<Glossary*>::iterator itGl = m_glossaries.begin();
        const QList<Glossary*>::iterator itGlEnd = m_glossaries.end();
        bool found = false;
index 64252b5ca17910f097e14d794db961227a897902..8b2dd284523753df712a1b3dd1d4e376cbaf44e8 100644 (file)
@@ -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<GlossaryItem*>
                 */
@@ -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
                 */