kword

KWTableStyle.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 "KWTableStyle.h"
00021 #include "KWDocument.h"
00022 #include "KWFrame.h"
00023 
00024 #include <KoGenStyles.h>
00025 #include <KoOasisStyles.h>
00026 #include <KoOasisContext.h>
00027 #include <KoXmlNS.h>
00028 
00029 #include <kdebug.h>
00030 #include <klocale.h>
00031 #include <qdom.h>
00032 
00033 KWTableStyleCollection::KWTableStyleCollection()
00034     : KoUserStyleCollection( QString::fromLatin1( "table" ) )
00035 {
00036 }
00037 
00038 void KWTableStyleCollection::saveOasis( KoGenStyles& mainStyles, KoSavingContext& savingContext ) const
00039 {
00040     if ( !isDefault() ) {
00041         for ( QValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
00042         {
00043             KWTableStyle* style = static_cast<KWTableStyle *>( *styleIt );
00044             style->saveOasis( mainStyles, savingContext );
00045         }
00046     }
00047 }
00048 
00049 int KWTableStyleCollection::loadOasisStyles( KoOasisContext& context, const KoStyleCollection& paragraphStyles, const KWFrameStyleCollection& frameStyles )
00050 {
00051     QValueVector<QDomElement> userStyles = context.oasisStyles().userStyles();
00052     bool defaultStyleDeleted = false;
00053     int stylesLoaded = 0;
00054     for (unsigned int item = 0; item < userStyles.count(); item++) {
00055         QDomElement styleElem = userStyles[item];
00056     Q_ASSERT( !styleElem.isNull() );
00057 
00058         if ( styleElem.attributeNS( KoXmlNS::style, "family", QString::null ) != "table-cell" )
00059             continue;
00060 
00061         if ( !defaultStyleDeleted ) {
00062             KWTableStyle *s = findStyle( defaultStyleName() );
00063             //kdDebug() << "KWTableStyleCollection::loadOasisStyles looking for " << defaultStyleName() << ", to delete it. Found " << s << endl;
00064             if(s) // delete the standard style.
00065                 removeStyle(s);
00066             defaultStyleDeleted = true;
00067         }
00068 
00069         KWTableStyle *sty = new KWTableStyle( QString::null, 0, 0 );
00070         // Load the style
00071         sty->loadOasis( styleElem, context, paragraphStyles, frameStyles );
00072         // Style created, now let's try to add it
00073         sty = static_cast<KWTableStyle *>( addStyle( sty ) );
00074 
00075         kdDebug() << " Loaded table cell style " << sty->name() << " - now " << count() << " styles" << endl;
00076         ++stylesLoaded;
00077     }
00078     return stylesLoaded;
00079 }
00080 
00081 /******************************************************************/
00082 /* Class: KWTableStyle                                            */
00083 /******************************************************************/
00084 
00085 KWTableStyle::KWTableStyle( const QString & name, KoParagStyle * _paragStyle, KWFrameStyle * _frameStyle )
00086     : KoUserStyle( name )
00087 {
00088     m_paragStyle = _paragStyle;
00089     m_frameStyle = _frameStyle;
00090 }
00091 
00092 KWTableStyle::KWTableStyle( QDomElement & parentElem, KWDocument *_doc, int /*docVersion*/ )
00093     : KoUserStyle( QString::null )
00094 {
00095     QDomElement element = parentElem.namedItem( "NAME" ).toElement();
00096     if ( ( !element.isNull() ) && ( element.hasAttribute("value") ) ) {
00097         m_name = element.attribute( "value" );
00098         m_displayName = i18n( "Style name", m_name.utf8() );
00099     } else
00100         kdWarning() << "No NAME tag in table style!" << endl;
00101 
00102     element = parentElem.namedItem( "PFRAMESTYLE" ).toElement();
00103     m_frameStyle = 0;
00104     if ( ( !element.isNull() ) && ( element.hasAttribute("name") ) )
00105         m_frameStyle = _doc->frameStyleCollection()->findStyleByDisplayName( element.attribute( "name" ) );
00106 
00107     if ( !m_frameStyle ) {
00108         if ( !_doc->frameStyleCollection()->isEmpty() )
00109             m_frameStyle = _doc->frameStyleCollection()->frameStyleAt( 0 );
00110         else { // Isn't possible ( I hope )
00111             KWFrameStyle * standardFrameStyle = new KWFrameStyle( "Plain" );
00112             standardFrameStyle->setBackgroundColor(QColor("white"));
00113             standardFrameStyle->setTopBorder(KoBorder(QColor("black"),KoBorder::SOLID,0));
00114             standardFrameStyle->setRightBorder(KoBorder(QColor("black"),KoBorder::SOLID,0));
00115             standardFrameStyle->setLeftBorder(KoBorder(QColor("black"),KoBorder::SOLID,0));
00116             standardFrameStyle->setBottomBorder(KoBorder(QColor("black"),KoBorder::SOLID,0));
00117             _doc->frameStyleCollection()->addStyle( standardFrameStyle );
00118             m_frameStyle = _doc->frameStyleCollection()->frameStyleAt( 0 );
00119         }
00120     }
00121 
00122     element = parentElem.namedItem( "PSTYLE" ).toElement();
00123     m_paragStyle = 0;
00124     if ( ( !element.isNull() ) && ( element.hasAttribute("name") ) )
00125         m_paragStyle = _doc->styleCollection()->findStyleByDisplayName( element.attribute( "name" ) );
00126 
00127     if ( !m_paragStyle ) {
00128         if ( _doc->styleCollection()->styleList().count()>0 )
00129             m_paragStyle = _doc->styleCollection()->styleAt( 0 );
00130         else { // Isn't possible ( I hope )
00131             KoParagStyle * standardStyle = new KoParagStyle( "Standard" );
00132             standardStyle->format().setFont( _doc->defaultFont() );
00133             _doc->styleCollection()->addStyle( standardStyle );
00134             m_paragStyle = _doc->styleCollection()->styleAt( 0 );
00135         }
00136     }
00137 }
00138 
00139 void KWTableStyle::operator=( const KWTableStyle &rhs )
00140 {
00141     KoUserStyle::operator=( rhs );
00142     m_paragStyle = rhs.paragraphStyle();
00143     m_frameStyle = rhs.frameStyle();
00144 }
00145 
00146 void KWTableStyle::saveTableStyle( QDomElement & parentElem )
00147 {
00148     QDomDocument doc = parentElem.ownerDocument();
00149     QDomElement element = doc.createElement( "NAME" );
00150     parentElem.appendChild( element );
00151     element.setAttribute( "value", displayName() );
00152 
00153     if (m_frameStyle)
00154     {
00155         element = doc.createElement( "PFRAMESTYLE" );
00156         parentElem.appendChild( element );
00157         element.setAttribute( "name", m_frameStyle->displayName() );
00158     }
00159     if (m_paragStyle)
00160     {
00161         element = doc.createElement( "PSTYLE" );
00162         parentElem.appendChild( element );
00163         element.setAttribute( "name", m_paragStyle->displayName() );
00164     }
00165 
00166 }
00167 
00168 KWTableStyle *KWTableStyle::loadStyle( QDomElement & parentElem, KWDocument *_doc, int docVersion )
00169 {
00170     return new KWTableStyle( parentElem, _doc, docVersion );
00171 }
00172 
00173 void KWTableStyle::saveOasis( KoGenStyles& mainStyles, KoSavingContext& /*savingContext*/ ) const
00174 {
00175     KoGenStyle tableCellStyle( KWDocument::STYLE_TABLE_CELL_USER, "table-cell" );
00176     tableCellStyle.addAttribute( "style:display-name", displayName() );
00177     tableCellStyle.addProperty( "koffice:frame-style-name", m_frameStyle->name() );
00178     tableCellStyle.addProperty( "koffice:paragraph-style-name", m_paragStyle->name() );
00179 
00180     // try to preserve existing internal name, if it looks adequate (no spaces)
00181     // ## TODO: check XML-Schemacs NCName conformity
00182     const bool nameIsConform = !m_name.isEmpty() && m_name.find( ' ' ) == -1;
00183     QString newName;
00184     if ( nameIsConform )
00185         newName = mainStyles.lookup( tableCellStyle, m_name, KoGenStyles::DontForceNumbering );
00186     else
00187         newName = mainStyles.lookup( tableCellStyle, "tc" );
00188     const_cast<KWTableStyle*>( this )->m_name = newName;
00189 }
00190 
00191 void KWTableStyle::loadOasis( QDomElement & styleElem, KoOasisContext& context, const KoStyleCollection& paragraphStyles, const KWFrameStyleCollection& frameStyles )
00192 {
00193     // Load name
00194     m_name = styleElem.attributeNS( KoXmlNS::style, "name", QString::null );
00195     m_displayName = styleElem.attributeNS( KoXmlNS::style, "display-name", QString::null );
00196     if ( m_displayName.isEmpty() )
00197         m_displayName = m_name;
00198     kdDebug() << k_funcinfo << m_name << " " << m_displayName << endl;
00199 
00200     KoStyleStack& styleStack = context.styleStack();
00201     styleStack.setTypeProperties( "table-cell" );
00202 
00203     styleStack.save();
00204     context.addStyles( &styleElem, "table-cell" ); // Load all parents - only because we don't support inheritance.
00205 
00206     const QString frameStyleName = styleStack.attributeNS( KoXmlNS::koffice, "frame-style-name" );
00207     m_frameStyle = frameStyles.findStyle( frameStyleName );
00208     if ( !m_frameStyle )
00209         kdWarning(32001) << "Frame style " << frameStyleName << " not found!" << endl;
00210 
00211     const QString paragraphStyleName = styleStack.attributeNS( KoXmlNS::koffice, "paragraph-style-name" );
00212     m_paragStyle = paragraphStyles.findStyle( paragraphStyleName );
00213     if ( !m_paragStyle )
00214         kdWarning(32001) << "Paragraph style " << paragraphStyleName << " not found!" << endl;
00215 
00216     styleStack.restore();
00217 }
KDE Home | KDE Accessibility Home | Description of Access Keys