kword

KWTableTemplate.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Nash Hoogwater <nrhoogwater@wanadoo.nl>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; using
00007    version 2 of the License.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include "KWTableTemplate.h"
00021 
00022 #include "KWDocument.h"
00023 #include "KWFrameStyle.h"
00024 #include "KoParagStyle.h"
00025 
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 #include <qdom.h>
00029 
00030 
00031 /******************************************************************/
00032 /* Class: KWTableTemplateCollection                                  */
00033 /******************************************************************/
00034 
00035 KWTableTemplateCollection::KWTableTemplateCollection()
00036 {
00037     m_templateList.setAutoDelete( false );
00038     m_deletedTemplates.setAutoDelete( true );
00039     m_lastTemplate = 0L;
00040 }
00041 
00042 KWTableTemplateCollection::~KWTableTemplateCollection()
00043 {
00044     //kdDebug() << "KWTableTemplateCollection::destructor" << endl;
00045     clear();
00046 }
00047 
00048 void KWTableTemplateCollection::clear()
00049 {
00050     m_templateList.setAutoDelete( true );
00051     m_templateList.clear();
00052     m_deletedTemplates.clear();
00053     m_lastTemplate = 0;
00054 }
00055 
00056 KWTableTemplate* KWTableTemplateCollection::findTableTemplate( const QString & _name )
00057 {
00058     // Caching, to speed things up
00059     if ( m_lastTemplate && m_lastTemplate->name() == _name )
00060         return m_lastTemplate;
00061 
00062     QPtrListIterator<KWTableTemplate> templateIt( m_templateList );
00063     for ( ; templateIt.current(); ++templateIt )
00064     {
00065         if ( templateIt.current()->name() == _name ) {
00066             m_lastTemplate = templateIt.current();
00067             return m_lastTemplate;
00068         }
00069     }
00070 
00071 //    if(_name == "Plain") return m_styleList.at(0); // fallback..
00072 
00073     return 0L;
00074 }
00075 
00076 
00077 KWTableTemplate* KWTableTemplateCollection::addTableTemplate( KWTableTemplate * tt )
00078 {
00079     // First check for duplicates.
00080     for ( KWTableTemplate* p = m_templateList.first(); p != 0L; p = m_templateList.next() )
00081     {
00082         if ( p->name() == tt->name() ) {
00083             // Replace existing template
00084             if ( tt != p )
00085             {
00086                 *p = *tt;
00087                 delete tt;
00088             }
00089             return p;
00090         }
00091     }
00092     m_templateList.append( tt );
00093     return tt;
00094 }
00095 
00096 void KWTableTemplateCollection::removeTableTemplate ( KWTableTemplate *tt )
00097 {
00098     if( m_templateList.removeRef( tt )) {
00099         if ( m_lastTemplate == tt )
00100             m_lastTemplate = 0L;
00101         // Remember to delete this template when deleting the document
00102         m_deletedTemplates.append( tt );
00103     }
00104 }
00105 
00106 /******************************************************************/
00107 /* Class: KWTableTemplate                                            */
00108 /******************************************************************/
00109 
00110 KWTableTemplate::KWTableTemplate( const QString & name, KWTableStyle * _firstRow, KWTableStyle * _firstCol,
00111                      KWTableStyle *_lastRow, KWTableStyle *_lastCol, KWTableStyle *_bodyCell,
00112                      KWTableStyle *_topLeftCorner, KWTableStyle *_topRightCorner,
00113                      KWTableStyle *_bottomLeftCorner, KWTableStyle *_bottomRightCorner )
00114 {
00115     m_name = name;
00116     m_firstRow = _firstRow;
00117     m_firstCol = _firstCol;
00118     m_lastRow = _lastRow;
00119     m_lastCol = _lastCol;
00120     m_bodyCell = _bodyCell;
00121     m_topLeftCorner = _topLeftCorner;
00122     m_topRightCorner = _topRightCorner;
00123     m_bottomRightCorner = _bottomRightCorner;
00124     m_bottomLeftCorner = _bottomLeftCorner;
00125 }
00126 
00127 KWTableTemplate::KWTableTemplate( QDomElement & parentElem, KWDocument *_doc, int /*docVersion*/ )
00128 {
00129     m_topRightCorner = 0L;
00130     m_topLeftCorner = 0L;
00131     m_bottomRightCorner = 0L;
00132     m_bottomLeftCorner = 0L;
00133 
00134     QDomElement element = parentElem.namedItem( "NAME" ).toElement();
00135     if ( ( !element.isNull() ) && ( element.hasAttribute("value") ) )
00136         m_name = element.attribute( "value" );
00137 
00138     element = parentElem.namedItem( "BODYCELL" ).toElement();
00139     if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
00140          && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
00141         m_bodyCell = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
00142     else {
00143         KWTableStyle *ts = _doc->tableStyleCollection()->findStyle( "Plain" );
00144         if (ts) {
00145             setBodyCell( ts );
00146         }
00147         else {
00148             ts = new KWTableStyle( "Plain", 0L, 0L );
00149             KWFrameStyle *fs = _doc->frameStyleCollection()->findStyle( "Plain" );
00150             KoParagStyle *s = _doc->styleCollection()->findStyle( "Standard" );
00151             if ( fs )
00152                 ts->setFrameStyle( fs );
00153             else {
00154                 KWFrameStyle * standardFrameStyle = new KWFrameStyle( "Plain" );
00155                 standardFrameStyle->setBackgroundColor(QColor("white"));
00156                 standardFrameStyle->setTopBorder(KoBorder(QColor("black"),KoBorder::SOLID,0));
00157                 standardFrameStyle->setRightBorder(KoBorder(QColor("black"),KoBorder::SOLID,0));
00158                 standardFrameStyle->setLeftBorder(KoBorder(QColor("black"),KoBorder::SOLID,0));
00159                 standardFrameStyle->setBottomBorder(KoBorder(QColor("black"),KoBorder::SOLID,0));
00160                 _doc->frameStyleCollection()->addStyle( standardFrameStyle );
00161                 ts->setFrameStyle( fs );
00162             }
00163 
00164             if ( s )
00165                 ts->setParagraphStyle( s );
00166             else {
00167                 KoParagStyle * standardStyle = new KoParagStyle( "Standard" );
00168                 standardStyle->format().setFont( _doc->defaultFont() );
00169                 _doc->styleCollection()->addStyle( standardStyle );
00170                 ts->setParagraphStyle( s );
00171             }
00172             setBodyCell ( ts );
00173         }
00174     }
00175     element = parentElem.namedItem( "FIRSTROW" ).toElement();
00176     if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
00177          && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
00178     {
00179         m_firstRow = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
00180 
00181         if ( element.hasAttribute("topleftcorner") )
00182             m_topLeftCorner = m_firstRow;
00183         if ( element.hasAttribute("toprightcorner") )
00184             m_topRightCorner = m_firstRow;
00185     }
00186     else
00187         m_firstRow = m_bodyCell;
00188 
00189     element = parentElem.namedItem( "FIRSTCOL" ).toElement();
00190     if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
00191          && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
00192     {
00193         m_firstCol = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
00194 
00195         if ( element.hasAttribute("topleftcorner") )
00196             m_topLeftCorner = m_firstCol;
00197         if ( element.hasAttribute("bottomleftcorner") )
00198             m_bottomLeftCorner = m_firstCol;
00199     }
00200     else
00201         m_firstCol = m_bodyCell;
00202 
00203     element = parentElem.namedItem( "LASTROW" ).toElement();
00204     if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
00205          && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
00206     {
00207         m_lastRow = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
00208 
00209         if ( ( !element.isNull() ) && ( element.hasAttribute("bottomrightcorner") ) )
00210             m_bottomRightCorner = m_lastRow;
00211         if ( ( !element.isNull() ) && ( element.hasAttribute("bottomleftcorner") ) )
00212             m_bottomLeftCorner = m_lastRow;
00213     }
00214     else
00215         m_lastRow = m_bodyCell;
00216 
00217     element = parentElem.namedItem( "LASTCOL" ).toElement();
00218     if ( ( !element.isNull() ) && ( element.hasAttribute("name") )
00219          && ( _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) ) ) )
00220     {
00221         m_lastCol = _doc->tableStyleCollection()->findStyle( element.attribute( "name" ) );
00222 
00223         if ( element.hasAttribute("toprightcorner") )
00224             m_topRightCorner = m_lastCol;
00225         if ( element.hasAttribute("bottomrightcorner") )
00226             m_bottomRightCorner = m_lastCol;
00227     }
00228     else
00229         m_lastCol = m_bodyCell;
00230 
00231     if (!m_topRightCorner) m_topRightCorner = m_bodyCell;
00232     if (!m_topLeftCorner) m_topLeftCorner = m_bodyCell;
00233     if (!m_bottomRightCorner) m_bottomRightCorner = m_bodyCell;
00234     if (!m_bottomLeftCorner) m_bottomLeftCorner = m_bodyCell;
00235 }
00236 
00237 void KWTableTemplate::operator=( const KWTableTemplate &rhs )
00238 {
00239     m_name = rhs.m_name;
00240     m_firstRow = rhs.pFirstRow();
00241     m_firstCol = rhs.pFirstCol();
00242     m_lastRow = rhs.pLastRow();
00243     m_lastCol = rhs.pLastCol();
00244     m_bodyCell = rhs.pBodyCell();
00245     m_topLeftCorner = rhs.pTopLeftCorner();
00246     m_topRightCorner = rhs.pTopRightCorner();
00247     m_bottomRightCorner = rhs.pBottomRightCorner();
00248     m_bottomLeftCorner = rhs.pBottomLeftCorner();
00249 }
00250 
00251 QString KWTableTemplate::displayName() const
00252 {
00253     return i18n( "Style name", name().utf8() );
00254 }
00255 
00256 // TODO
00257 void KWTableTemplate::saveTableTemplate( QDomElement & parentElem )
00258 {
00259     QDomDocument doc = parentElem.ownerDocument();
00260     QDomElement element = doc.createElement( "NAME" );
00261     parentElem.appendChild( element );
00262     element.setAttribute( "value", name() );
00263 
00264     if (m_bodyCell)
00265     {
00266         element = doc.createElement( "BODYCELL" );
00267         parentElem.appendChild( element );
00268         element.setAttribute( "name", m_bodyCell->name() );
00269     }
00270     if (m_firstRow)
00271     {
00272         element = doc.createElement( "FIRSTROW" );
00273         parentElem.appendChild( element );
00274         element.setAttribute( "name", m_firstRow->name() );
00275     }
00276     if (m_firstCol)
00277     {
00278         element = doc.createElement( "FIRSTCOL" );
00279         parentElem.appendChild( element );
00280         element.setAttribute( "name", m_firstCol->name() );
00281     }
00282     if (m_lastRow)
00283     {
00284         element = doc.createElement( "LASTROW" );
00285         parentElem.appendChild( element );
00286         element.setAttribute( "name", m_lastRow->name() );
00287     }
00288     if (m_lastCol)
00289     {
00290         element = doc.createElement( "LASTCOL" );
00291         parentElem.appendChild( element );
00292         element.setAttribute( "name", m_lastCol->name() );
00293     }
00294 }
00295 
00296 KWTableTemplate *KWTableTemplate::loadTemplate( QDomElement & parentElem, KWDocument *_doc, int docVersion )
00297 {
00298     return new KWTableTemplate(parentElem, _doc, docVersion);
00299 }
KDE Home | KDE Accessibility Home | Description of Access Keys