#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() );
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;
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()
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 );
}
}
QToolButton *clear = new QToolButton( plainPage() );
clear->setIconSet( SmallIconSet( "locationbar_erase" ) );
+ clear->setToolTip( i18n( "Clear filter" ) );
hbox->addWidget( clear );
QLabel *lbl = new QLabel( plainPage() );
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;
/**
* @class Glossary
* @author Carsten Niehaus
+ * @author Pino Toscano
*
* This class stores all items to be displayed. It also
* has access-methods to the items
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();
/**
* 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
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;
* 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
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*>
*/
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();
/**
* @return the formated HTML code for current item.
- **/
+ */
QString toHtml() const;
/**
void keyPressEvent(QKeyEvent*);
/**
- * add a new glossary
+ * Add a new glossary.
*
* @param newgloss the new glossary to add
*/