kword

KWFrameStyle.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Nash Hoogwater <nrhoogwater@wanadoo.nl>
00003                  2005 David Faure <faure@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; using
00008    version 2 of the License.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "KWFrameStyle.h"
00022 #include "KWDocument.h"
00023 #include "KWFrame.h"
00024 
00025 #include <KoGenStyles.h>
00026 #include <KoOasisStyles.h>
00027 #include <KoOasisContext.h>
00028 #include <KoXmlNS.h>
00029 
00030 #include <kdebug.h>
00031 #include <klocale.h>
00032 #include <qdom.h>
00033 
00034 KWFrameStyleCollection::KWFrameStyleCollection()
00035     : KoUserStyleCollection( QString::fromLatin1( "frame" ) )
00036 {
00037 }
00038 
00039 void KWFrameStyleCollection::saveOasis( KoGenStyles& mainStyles, KoSavingContext& savingContext ) const
00040 {
00041     if ( !isDefault() ) {
00042         for ( QValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
00043         {
00044             KWFrameStyle* style = static_cast<KWFrameStyle *>( *styleIt );
00045             style->saveOasis( mainStyles, savingContext );
00046         }
00047     }
00048 }
00049 
00050 int KWFrameStyleCollection::loadOasisStyles( KoOasisContext& context )
00051 {
00052     QValueVector<QDomElement> userStyles = context.oasisStyles().userStyles();
00053     bool defaultStyleDeleted = false;
00054     int stylesLoaded = 0;
00055     for (unsigned int item = 0; item < userStyles.count(); item++) {
00056         QDomElement styleElem = userStyles[item];
00057     Q_ASSERT( !styleElem.isNull() );
00058 
00059         if ( styleElem.attributeNS( KoXmlNS::style, "family", QString::null ) != "graphic" )
00060             continue;
00061 
00062         if ( !defaultStyleDeleted ) {
00063             KWFrameStyle *s = findStyle( defaultStyleName() );
00064             //kdDebug() << "KWFrameStyleCollection::loadOasisStyles looking for " << defaultStyleName() << ", to delete it. Found " << s << endl;
00065             if(s) // delete the standard style.
00066                 removeStyle(s);
00067             defaultStyleDeleted = true;
00068         }
00069 
00070         KWFrameStyle *sty = new KWFrameStyle( QString::null );
00071         // Load the style
00072         sty->loadOasis( styleElem, context );
00073         // Style created, now let's try to add it
00074         sty = static_cast<KWFrameStyle *>( addStyle( sty ) );
00075 
00076         kdDebug() << " Loaded frame style " << sty->name() << " - now " << count() << " styles" << endl;
00077         ++stylesLoaded;
00078     }
00079     return stylesLoaded;
00080 }
00081 
00082 /******************************************************************/
00083 /* Class: KWFrameStyle                                            */
00084 /******************************************************************/
00085 
00086 KWFrameStyle::KWFrameStyle( const QString & name )
00087     : KoUserStyle( name )
00088 {
00089     m_backgroundColor.setColor( Qt::white );
00090 }
00091 
00092 KWFrameStyle::KWFrameStyle( const QString & name, KWFrame * frame )
00093     : KoUserStyle( name )
00094 {
00095     m_backgroundColor = frame->backgroundColor();
00096     m_borderLeft = frame->leftBorder();
00097     m_borderRight = frame->rightBorder();
00098     m_borderTop = frame->topBorder();
00099     m_borderBottom = frame->bottomBorder();
00100 }
00101 
00102 KWFrameStyle::KWFrameStyle( QDomElement & parentElem, int /*docVersion=2*/ )
00103     : KoUserStyle( QString::null )
00104 {
00105     QDomElement element = parentElem.namedItem( "NAME" ).toElement();
00106     if ( ( !element.isNull() ) && ( element.hasAttribute("value") ) ) {
00107         m_name = element.attribute( "value" );
00108         m_displayName = i18n( "Style name", m_name.utf8() );
00109     } else
00110         kdWarning() << "No NAME tag in frame style!" << endl;
00111 
00112     element = parentElem.namedItem( "LEFTBORDER" ).toElement();
00113     if ( !element.isNull() )
00114         m_borderLeft = KoBorder::loadBorder( element );
00115     else
00116         m_borderLeft.setPenWidth( 0 );
00117 
00118     element = parentElem.namedItem( "RIGHTBORDER" ).toElement();
00119     if ( !element.isNull() )
00120         m_borderRight = KoBorder::loadBorder( element );
00121     else
00122         m_borderRight.setPenWidth( 0 );
00123 
00124     element = parentElem.namedItem( "TOPBORDER" ).toElement();
00125     if ( !element.isNull() )
00126         m_borderTop = KoBorder::loadBorder( element );
00127     else
00128         m_borderTop.setPenWidth( 0 );
00129 
00130     element = parentElem.namedItem( "BOTTOMBORDER" ).toElement();
00131     if ( !element.isNull() )
00132         m_borderBottom = KoBorder::loadBorder( element );
00133     else
00134         m_borderBottom.setPenWidth( 0 );
00135 
00136     QColor c("white");
00137     if ( parentElem.hasAttribute("red") )
00138         c.setRgb(
00139             KWDocument::getAttribute( parentElem, "red", 0 ),
00140             KWDocument::getAttribute( parentElem, "green", 0 ),
00141             KWDocument::getAttribute( parentElem, "blue", 0 ) );
00142 
00143     m_backgroundColor = QBrush( c );
00144 }
00145 
00146 KWFrameStyle::KWFrameStyle( const KWFrameStyle &rhs )
00147     : KoUserStyle( QString::null )
00148 {
00149     operator=( rhs );
00150 }
00151 
00152 void KWFrameStyle::operator=( const KWFrameStyle &rhs )
00153 {
00154     KoUserStyle::operator=( rhs );
00155     m_backgroundColor = rhs.m_backgroundColor;
00156     m_borderLeft = rhs.m_borderLeft;
00157     m_borderRight = rhs.m_borderRight;
00158     m_borderTop = rhs.m_borderTop;
00159     m_borderBottom = rhs.m_borderBottom;
00160 }
00161 
00162 int KWFrameStyle::compare( const KWFrameStyle & frameStyle ) const
00163 {
00164     int flags = 0;
00165     if ( m_borderLeft != frameStyle.m_borderLeft
00166          || m_borderRight != frameStyle.m_borderRight
00167          || m_borderTop != frameStyle.m_borderTop
00168          || m_borderBottom != frameStyle.m_borderBottom )
00169         flags |= Borders;
00170     if ( m_backgroundColor.color() != frameStyle.m_backgroundColor.color() )
00171         flags |= Background;
00172 
00173     return flags;
00174 }
00175 
00176 
00177 void KWFrameStyle::saveFrameStyle( QDomElement & parentElem )
00178 {
00179     QDomDocument doc = parentElem.ownerDocument();
00180     QDomElement element = doc.createElement( "NAME" );
00181     parentElem.appendChild( element );
00182     element.setAttribute( "value", displayName() );
00183 
00184     if ( m_borderLeft.width() > 0 )
00185     {
00186         element = doc.createElement( "LEFTBORDER" );
00187         parentElem.appendChild( element );
00188         m_borderLeft.save( element );
00189     }
00190     if ( m_borderRight.width() > 0 )
00191     {
00192         element = doc.createElement( "RIGHTBORDER" );
00193         parentElem.appendChild( element );
00194         m_borderRight.save( element );
00195     }
00196     if ( m_borderTop.width() > 0 )
00197     {
00198         element = doc.createElement( "TOPBORDER" );
00199         parentElem.appendChild( element );
00200         m_borderTop.save( element );
00201     }
00202     if ( m_borderBottom.width() > 0 )
00203     {
00204         element = doc.createElement( "BOTTOMBORDER" );
00205         parentElem.appendChild( element );
00206         m_borderBottom.save( element );
00207     }
00208 
00209     if(m_backgroundColor.color().isValid())
00210     {
00211         parentElem.setAttribute( "red", m_backgroundColor.color().red() );
00212         parentElem.setAttribute( "green", m_backgroundColor.color().green() );
00213         parentElem.setAttribute( "blue", m_backgroundColor.color().blue() );
00214     }
00215 }
00216 
00217 KWFrameStyle *KWFrameStyle::loadStyle( QDomElement & parentElem, int docVersion )
00218 {
00219     return new KWFrameStyle( parentElem, docVersion );
00220 }
00221 
00222 void KWFrameStyle::saveOasis( KoGenStyles& mainStyles, KoSavingContext& savingContext ) const
00223 {
00224     Q_UNUSED( savingContext );
00225 
00226     KoGenStyle frameStyle( KWDocument::STYLE_FRAME_USER, "graphic" );
00227     frameStyle.addAttribute( "style:display-name", displayName() );
00228 
00229     // Borders (taken from KWFrame::saveBorderProperties)
00230     if (  ( m_borderLeft == m_borderRight )
00231           && ( m_borderLeft == m_borderTop )
00232           && ( m_borderLeft == m_borderBottom ) )
00233     {
00234         frameStyle.addProperty( "fo:border", m_borderLeft.saveFoBorder() );
00235     }
00236     else
00237     {
00238         frameStyle.addProperty( "fo:border-left", m_borderLeft.saveFoBorder() );
00239         frameStyle.addProperty( "fo:border-right", m_borderRight.saveFoBorder() );
00240         frameStyle.addProperty( "fo:border-top", m_borderTop.saveFoBorder() );
00241         frameStyle.addProperty( "fo:border-bottom", m_borderBottom.saveFoBorder() );
00242     }
00243 
00244     // Background (taken from KWFrame::saveBorderProperties)
00245     if ( m_backgroundColor.style() == Qt::NoBrush )
00246         frameStyle.addProperty( "fo:background-color", "transparent" );
00247     else if ( m_backgroundColor.color().isValid() )
00248         frameStyle.addProperty( "fo:background-color", m_backgroundColor.color().name() );
00249 
00250     // try to preserve existing internal name, if it looks adequate (no spaces)
00251     // ## TODO: check XML-Schemacs NCName conformity
00252     const bool nameIsConform = !m_name.isEmpty() && m_name.find( ' ' ) == -1;
00253     QString newName = m_name;
00254     if ( nameIsConform )
00255         newName = mainStyles.lookup( frameStyle, m_name, KoGenStyles::DontForceNumbering );
00256     else
00257         newName = mainStyles.lookup( frameStyle, "fr" );
00258     const_cast<KWFrameStyle*>( this )->m_name = newName;
00259 }
00260 
00261 void KWFrameStyle::loadOasis( QDomElement & styleElem, KoOasisContext& context )
00262 {
00263     // Load name
00264     m_name = styleElem.attributeNS( KoXmlNS::style, "name", QString::null );
00265     m_displayName = styleElem.attributeNS( KoXmlNS::style, "display-name", QString::null );
00266     if ( m_displayName.isEmpty() )
00267         m_displayName = m_name;
00268 
00269     KoStyleStack& styleStack = context.styleStack();
00270     styleStack.setTypeProperties( "graphic" );
00271 
00272     styleStack.save();
00273     context.addStyles( &styleElem, "graphic" ); // Load all parents - only because we don't support inheritance.
00274 
00275     if ( styleStack.hasAttributeNS( KoXmlNS::fo, "background-color" ) ) {
00276         QString color = styleStack.attributeNS( KoXmlNS::fo, "background-color" );
00277         if ( color == "transparent" )
00278             m_backgroundColor = QBrush( QColor(), Qt::NoBrush );
00279         else
00280             m_backgroundColor = QBrush( QColor( color ) /*, brush style is a dead feature, ignored */ );
00281     }
00282 
00283     m_borderLeft.loadFoBorder( styleStack.attributeNS( KoXmlNS::fo, "border", "left") );
00284     m_borderRight.loadFoBorder( styleStack.attributeNS( KoXmlNS::fo, "border", "right") );
00285     m_borderTop.loadFoBorder( styleStack.attributeNS( KoXmlNS::fo, "border", "top") );
00286     m_borderBottom.loadFoBorder( styleStack.attributeNS( KoXmlNS::fo, "border", "bottom") );
00287 
00288     styleStack.restore();
00289 }
KDE Home | KDE Accessibility Home | Description of Access Keys