kword

KWFrameStyle.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Nash Hoogwater <nrhoogwater@wanadoo.nl>
3 2005 David Faure <faure@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; using
8 version 2 of the License.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "KWFrameStyle.h"
22#include "KWDocument.h"
23#include "KWFrame.h"
24
25#include <KoGenStyles.h>
26#include <KoOasisStyles.h>
27#include <KoOasisContext.h>
28#include <KoXmlNS.h>
29
30#include <kdebug.h>
31#include <tdelocale.h>
32#include <tqdom.h>
33
34KWFrameStyleCollection::KWFrameStyleCollection()
35 : KoUserStyleCollection( TQString::fromLatin1( "frame" ) )
36{
37}
38
39void KWFrameStyleCollection::saveOasis( KoGenStyles& mainStyles, KoSavingContext& savingContext ) const
40{
41 if ( !isDefault() ) {
42 for ( TQValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
43 {
44 KWFrameStyle* style = static_cast<KWFrameStyle *>( *styleIt );
45 style->saveOasis( mainStyles, savingContext );
46 }
47 }
48}
49
50int KWFrameStyleCollection::loadOasisStyles( KoOasisContext& context )
51{
52 TQValueVector<TQDomElement> userStyles = context.oasisStyles().userStyles();
53 bool defaultStyleDeleted = false;
54 int stylesLoaded = 0;
55 for (unsigned int item = 0; item < userStyles.count(); item++) {
56 TQDomElement styleElem = userStyles[item];
57 Q_ASSERT( !styleElem.isNull() );
58
59 if ( styleElem.attributeNS( KoXmlNS::style, "family", TQString() ) != "graphic" )
60 continue;
61
62 if ( !defaultStyleDeleted ) {
63 KWFrameStyle *s = findStyle( defaultStyleName() );
64 //kdDebug() << "KWFrameStyleCollection::loadOasisStyles looking for " << defaultStyleName() << ", to delete it. Found " << s << endl;
65 if(s) // delete the standard style.
66 removeStyle(s);
67 defaultStyleDeleted = true;
68 }
69
70 KWFrameStyle *sty = new KWFrameStyle( TQString() );
71 // Load the style
72 sty->loadOasis( styleElem, context );
73 // Style created, now let's try to add it
74 sty = static_cast<KWFrameStyle *>( addStyle( sty ) );
75
76 kdDebug() << " Loaded frame style " << sty->name() << " - now " << count() << " styles" << endl;
77 ++stylesLoaded;
78 }
79 return stylesLoaded;
80}
81
82/******************************************************************/
83/* Class: KWFrameStyle */
84/******************************************************************/
85
86KWFrameStyle::KWFrameStyle( const TQString & name )
87 : KoUserStyle( name )
88{
89 m_backgroundColor.setColor( TQt::white );
90}
91
92KWFrameStyle::KWFrameStyle( const TQString & name, KWFrame * frame )
93 : KoUserStyle( name )
94{
95 m_backgroundColor = frame->backgroundColor();
96 m_borderLeft = frame->leftBorder();
97 m_borderRight = frame->rightBorder();
98 m_borderTop = frame->topBorder();
99 m_borderBottom = frame->bottomBorder();
100}
101
102KWFrameStyle::KWFrameStyle( TQDomElement & parentElem, int /*docVersion=2*/ )
103 : KoUserStyle( TQString() )
104{
105 TQDomElement element = parentElem.namedItem( "NAME" ).toElement();
106 if ( ( !element.isNull() ) && ( element.hasAttribute("value") ) ) {
107 m_name = element.attribute( "value" );
108 m_displayName = i18n( "Style name", m_name.utf8() );
109 } else
110 kdWarning() << "No NAME tag in frame style!" << endl;
111
112 element = parentElem.namedItem( "LEFTBORDER" ).toElement();
113 if ( !element.isNull() )
114 m_borderLeft = KoBorder::loadBorder( element );
115 else
116 m_borderLeft.setPenWidth( 0 );
117
118 element = parentElem.namedItem( "RIGHTBORDER" ).toElement();
119 if ( !element.isNull() )
120 m_borderRight = KoBorder::loadBorder( element );
121 else
122 m_borderRight.setPenWidth( 0 );
123
124 element = parentElem.namedItem( "TOPBORDER" ).toElement();
125 if ( !element.isNull() )
126 m_borderTop = KoBorder::loadBorder( element );
127 else
128 m_borderTop.setPenWidth( 0 );
129
130 element = parentElem.namedItem( "BOTTOMBORDER" ).toElement();
131 if ( !element.isNull() )
132 m_borderBottom = KoBorder::loadBorder( element );
133 else
134 m_borderBottom.setPenWidth( 0 );
135
136 TQColor c("white");
137 if ( parentElem.hasAttribute("red") )
138 c.setRgb(
139 KWDocument::getAttribute( parentElem, "red", 0 ),
140 KWDocument::getAttribute( parentElem, "green", 0 ),
141 KWDocument::getAttribute( parentElem, "blue", 0 ) );
142
143 m_backgroundColor = TQBrush( c );
144}
145
147 : KoUserStyle( TQString() )
148{
149 operator=( rhs );
150}
151
152void KWFrameStyle::operator=( const KWFrameStyle &rhs )
153{
154 KoUserStyle::operator=( rhs );
155 m_backgroundColor = rhs.m_backgroundColor;
156 m_borderLeft = rhs.m_borderLeft;
157 m_borderRight = rhs.m_borderRight;
158 m_borderTop = rhs.m_borderTop;
159 m_borderBottom = rhs.m_borderBottom;
160}
161
162int KWFrameStyle::compare( const KWFrameStyle & frameStyle ) const
163{
164 int flags = 0;
165 if ( m_borderLeft != frameStyle.m_borderLeft
166 || m_borderRight != frameStyle.m_borderRight
167 || m_borderTop != frameStyle.m_borderTop
168 || m_borderBottom != frameStyle.m_borderBottom )
169 flags |= Borders;
170 if ( m_backgroundColor.color() != frameStyle.m_backgroundColor.color() )
171 flags |= Background;
172
173 return flags;
174}
175
176
177void KWFrameStyle::saveFrameStyle( TQDomElement & parentElem )
178{
179 TQDomDocument doc = parentElem.ownerDocument();
180 TQDomElement element = doc.createElement( "NAME" );
181 parentElem.appendChild( element );
182 element.setAttribute( "value", displayName() );
183
184 if ( m_borderLeft.width() > 0 )
185 {
186 element = doc.createElement( "LEFTBORDER" );
187 parentElem.appendChild( element );
188 m_borderLeft.save( element );
189 }
190 if ( m_borderRight.width() > 0 )
191 {
192 element = doc.createElement( "RIGHTBORDER" );
193 parentElem.appendChild( element );
194 m_borderRight.save( element );
195 }
196 if ( m_borderTop.width() > 0 )
197 {
198 element = doc.createElement( "TOPBORDER" );
199 parentElem.appendChild( element );
200 m_borderTop.save( element );
201 }
202 if ( m_borderBottom.width() > 0 )
203 {
204 element = doc.createElement( "BOTTOMBORDER" );
205 parentElem.appendChild( element );
206 m_borderBottom.save( element );
207 }
208
209 if(m_backgroundColor.color().isValid())
210 {
211 parentElem.setAttribute( "red", m_backgroundColor.color().red() );
212 parentElem.setAttribute( "green", m_backgroundColor.color().green() );
213 parentElem.setAttribute( "blue", m_backgroundColor.color().blue() );
214 }
215}
216
217KWFrameStyle *KWFrameStyle::loadStyle( TQDomElement & parentElem, int docVersion )
218{
219 return new KWFrameStyle( parentElem, docVersion );
220}
221
222void KWFrameStyle::saveOasis( KoGenStyles& mainStyles, KoSavingContext& savingContext ) const
223{
224 Q_UNUSED( savingContext );
225
226 KoGenStyle frameStyle( KWDocument::STYLE_FRAME_USER, "graphic" );
227 frameStyle.addAttribute( "style:display-name", displayName() );
228
229 // Borders (taken from KWFrame::saveBorderProperties)
230 if ( ( m_borderLeft == m_borderRight )
231 && ( m_borderLeft == m_borderTop )
232 && ( m_borderLeft == m_borderBottom ) )
233 {
234 frameStyle.addProperty( "fo:border", m_borderLeft.saveFoBorder() );
235 }
236 else
237 {
238 frameStyle.addProperty( "fo:border-left", m_borderLeft.saveFoBorder() );
239 frameStyle.addProperty( "fo:border-right", m_borderRight.saveFoBorder() );
240 frameStyle.addProperty( "fo:border-top", m_borderTop.saveFoBorder() );
241 frameStyle.addProperty( "fo:border-bottom", m_borderBottom.saveFoBorder() );
242 }
243
244 // Background (taken from KWFrame::saveBorderProperties)
245 if ( m_backgroundColor.style() == TQt::NoBrush )
246 frameStyle.addProperty( "fo:background-color", "transparent" );
247 else if ( m_backgroundColor.color().isValid() )
248 frameStyle.addProperty( "fo:background-color", m_backgroundColor.color().name() );
249
250 // try to preserve existing internal name, if it looks adequate (no spaces)
251 // ## TODO: check XML-Schemacs NCName conformity
252 const bool nameIsConform = !m_name.isEmpty() && m_name.find( ' ' ) == -1;
253 TQString newName = m_name;
254 if ( nameIsConform )
255 newName = mainStyles.lookup( frameStyle, m_name, KoGenStyles::DontForceNumbering );
256 else
257 newName = mainStyles.lookup( frameStyle, "fr" );
258 const_cast<KWFrameStyle*>( this )->m_name = newName;
259}
260
261void KWFrameStyle::loadOasis( TQDomElement & styleElem, KoOasisContext& context )
262{
263 // Load name
264 m_name = styleElem.attributeNS( KoXmlNS::style, "name", TQString() );
265 m_displayName = styleElem.attributeNS( KoXmlNS::style, "display-name", TQString() );
266 if ( m_displayName.isEmpty() )
267 m_displayName = m_name;
268
269 KoStyleStack& styleStack = context.styleStack();
270 styleStack.setTypeProperties( "graphic" );
271
272 styleStack.save();
273 context.addStyles( &styleElem, "graphic" ); // Load all parents - only because we don't support inheritance.
274
275 if ( styleStack.hasAttributeNS( KoXmlNS::fo, "background-color" ) ) {
276 TQString color = styleStack.attributeNS( KoXmlNS::fo, "background-color" );
277 if ( color == "transparent" )
278 m_backgroundColor = TQBrush( TQColor(), TQt::NoBrush );
279 else
280 m_backgroundColor = TQBrush( TQColor( color ) /*, brush style is a dead feature, ignored */ );
281 }
282
283 m_borderLeft.loadFoBorder( styleStack.attributeNS( KoXmlNS::fo, "border", "left") );
284 m_borderRight.loadFoBorder( styleStack.attributeNS( KoXmlNS::fo, "border", "right") );
285 m_borderTop.loadFoBorder( styleStack.attributeNS( KoXmlNS::fo, "border", "top") );
286 m_borderBottom.loadFoBorder( styleStack.attributeNS( KoXmlNS::fo, "border", "bottom") );
287
288 styleStack.restore();
289}
KWFrameStyle * findStyle(const TQString &name) const
Find style based on the untranslated name name.
KWFrameStyle * addStyle(KWFrameStyle *sty)
See KoUserStyleCollection::addStyle.
The user-visible style for frames (borders and background)
static KWFrameStyle * loadStyle(TQDomElement &parentElem, int docVersion=2)
load (old xml format)
KWFrameStyle(const TQString &name)
Create a blank framestyle (with default attributes)
void saveOasis(KoGenStyles &mainStyles, KoSavingContext &savingContext) const
save (new oasis xml format)
void loadOasis(TQDomElement &styleElem, KoOasisContext &context)
load (new oasis xml format)
void saveFrameStyle(TQDomElement &parentElem)
save (old xml format)
This class represents a single frame.
Definition KWFrame.h:68
const KoBorder & leftBorder() const
All borders can be custom drawn with their own colors etc.
Definition KWFrame.h:170