kword

KWTableStyle.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2002 Nash Hoogwater <nrhoogwater@wanadoo.nl>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; using
7 version 2 of the License.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include "KWTableStyle.h"
21#include "KWDocument.h"
22#include "KWFrame.h"
23
24#include <KoGenStyles.h>
25#include <KoOasisStyles.h>
26#include <KoOasisContext.h>
27#include <KoXmlNS.h>
28
29#include <kdebug.h>
30#include <tdelocale.h>
31#include <tqdom.h>
32
33KWTableStyleCollection::KWTableStyleCollection()
34 : KoUserStyleCollection( TQString::fromLatin1( "table" ) )
35{
36}
37
38void KWTableStyleCollection::saveOasis( KoGenStyles& mainStyles, KoSavingContext& savingContext ) const
39{
40 if ( !isDefault() ) {
41 for ( TQValueList<KoUserStyle *>::const_iterator styleIt = m_styleList.begin(), styleEnd = m_styleList.end() ; styleIt != styleEnd ; ++styleIt )
42 {
43 KWTableStyle* style = static_cast<KWTableStyle *>( *styleIt );
44 style->saveOasis( mainStyles, savingContext );
45 }
46 }
47}
48
49int KWTableStyleCollection::loadOasisStyles( KoOasisContext& context, const KoStyleCollection& paragraphStyles, const KWFrameStyleCollection& frameStyles )
50{
51 TQValueVector<TQDomElement> userStyles = context.oasisStyles().userStyles();
52 bool defaultStyleDeleted = false;
53 int stylesLoaded = 0;
54 for (unsigned int item = 0; item < userStyles.count(); item++) {
55 TQDomElement styleElem = userStyles[item];
56 Q_ASSERT( !styleElem.isNull() );
57
58 if ( styleElem.attributeNS( KoXmlNS::style, "family", TQString() ) != "table-cell" )
59 continue;
60
61 if ( !defaultStyleDeleted ) {
62 KWTableStyle *s = findStyle( defaultStyleName() );
63 //kdDebug() << "KWTableStyleCollection::loadOasisStyles looking for " << defaultStyleName() << ", to delete it. Found " << s << endl;
64 if(s) // delete the standard style.
65 removeStyle(s);
66 defaultStyleDeleted = true;
67 }
68
69 KWTableStyle *sty = new KWTableStyle( TQString(), 0, 0 );
70 // Load the style
71 sty->loadOasis( styleElem, context, paragraphStyles, frameStyles );
72 // Style created, now let's try to add it
73 sty = static_cast<KWTableStyle *>( addStyle( sty ) );
74
75 kdDebug() << " Loaded table cell style " << sty->name() << " - now " << count() << " styles" << endl;
76 ++stylesLoaded;
77 }
78 return stylesLoaded;
79}
80
81/******************************************************************/
82/* Class: KWTableStyle */
83/******************************************************************/
84
85KWTableStyle::KWTableStyle( const TQString & name, KoParagStyle * _paragStyle, KWFrameStyle * _frameStyle )
86 : KoUserStyle( name )
87{
88 m_paragStyle = _paragStyle;
89 m_frameStyle = _frameStyle;
90}
91
92KWTableStyle::KWTableStyle( TQDomElement & parentElem, KWDocument *_doc, int /*docVersion*/ )
93 : KoUserStyle( TQString() )
94{
95 TQDomElement element = parentElem.namedItem( "NAME" ).toElement();
96 if ( ( !element.isNull() ) && ( element.hasAttribute("value") ) ) {
97 m_name = element.attribute( "value" );
98 m_displayName = i18n( "Style name", m_name.utf8() );
99 } else
100 kdWarning() << "No NAME tag in table style!" << endl;
101
102 element = parentElem.namedItem( "PFRAMESTYLE" ).toElement();
103 m_frameStyle = 0;
104 if ( ( !element.isNull() ) && ( element.hasAttribute("name") ) )
105 m_frameStyle = _doc->frameStyleCollection()->findStyleByDisplayName( element.attribute( "name" ) );
106
107 if ( !m_frameStyle ) {
108 if ( !_doc->frameStyleCollection()->isEmpty() )
109 m_frameStyle = _doc->frameStyleCollection()->frameStyleAt( 0 );
110 else { // Isn't possible ( I hope )
111 KWFrameStyle * standardFrameStyle = new KWFrameStyle( "Plain" );
112 standardFrameStyle->setBackgroundColor(TQColor("white"));
113 standardFrameStyle->setTopBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
114 standardFrameStyle->setRightBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
115 standardFrameStyle->setLeftBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
116 standardFrameStyle->setBottomBorder(KoBorder(TQColor("black"),KoBorder::SOLID,0));
117 _doc->frameStyleCollection()->addStyle( standardFrameStyle );
118 m_frameStyle = _doc->frameStyleCollection()->frameStyleAt( 0 );
119 }
120 }
121
122 element = parentElem.namedItem( "PSTYLE" ).toElement();
123 m_paragStyle = 0;
124 if ( ( !element.isNull() ) && ( element.hasAttribute("name") ) )
125 m_paragStyle = _doc->styleCollection()->findStyleByDisplayName( element.attribute( "name" ) );
126
127 if ( !m_paragStyle ) {
128 if ( _doc->styleCollection()->styleList().count()>0 )
129 m_paragStyle = _doc->styleCollection()->styleAt( 0 );
130 else { // Isn't possible ( I hope )
131 KoParagStyle * standardStyle = new KoParagStyle( "Standard" );
132 standardStyle->format().setFont( _doc->defaultFont() );
133 _doc->styleCollection()->addStyle( standardStyle );
134 m_paragStyle = _doc->styleCollection()->styleAt( 0 );
135 }
136 }
137}
138
139void KWTableStyle::operator=( const KWTableStyle &rhs )
140{
141 KoUserStyle::operator=( rhs );
142 m_paragStyle = rhs.paragraphStyle();
143 m_frameStyle = rhs.frameStyle();
144}
145
146void KWTableStyle::saveTableStyle( TQDomElement & parentElem )
147{
148 TQDomDocument doc = parentElem.ownerDocument();
149 TQDomElement element = doc.createElement( "NAME" );
150 parentElem.appendChild( element );
151 element.setAttribute( "value", displayName() );
152
153 if (m_frameStyle)
154 {
155 element = doc.createElement( "PFRAMESTYLE" );
156 parentElem.appendChild( element );
157 element.setAttribute( "name", m_frameStyle->displayName() );
158 }
159 if (m_paragStyle)
160 {
161 element = doc.createElement( "PSTYLE" );
162 parentElem.appendChild( element );
163 element.setAttribute( "name", m_paragStyle->displayName() );
164 }
165
166}
167
168KWTableStyle *KWTableStyle::loadStyle( TQDomElement & parentElem, KWDocument *_doc, int docVersion )
169{
170 return new KWTableStyle( parentElem, _doc, docVersion );
171}
172
173void KWTableStyle::saveOasis( KoGenStyles& mainStyles, KoSavingContext& /*savingContext*/ ) const
174{
175 KoGenStyle tableCellStyle( KWDocument::STYLE_TABLE_CELL_USER, "table-cell" );
176 tableCellStyle.addAttribute( "style:display-name", displayName() );
177 if ( m_frameStyle )
178 tableCellStyle.addProperty( "koffice:frame-style-name", m_frameStyle->name() );
179 if ( m_paragStyle )
180 tableCellStyle.addProperty( "koffice:paragraph-style-name", m_paragStyle->name() );
181
182 // try to preserve existing internal name, if it looks adequate (no spaces)
183 // ## TODO: check XML-Schemacs NCName conformity
184 const bool nameIsConform = !m_name.isEmpty() && m_name.find( ' ' ) == -1;
185 TQString newName;
186 if ( nameIsConform )
187 newName = mainStyles.lookup( tableCellStyle, m_name, KoGenStyles::DontForceNumbering );
188 else
189 newName = mainStyles.lookup( tableCellStyle, "tc" );
190 const_cast<KWTableStyle*>( this )->m_name = newName;
191}
192
193void KWTableStyle::loadOasis( TQDomElement & styleElem, KoOasisContext& context, const KoStyleCollection& paragraphStyles, const KWFrameStyleCollection& frameStyles )
194{
195 // Load name
196 m_name = styleElem.attributeNS( KoXmlNS::style, "name", TQString() );
197 m_displayName = styleElem.attributeNS( KoXmlNS::style, "display-name", TQString() );
198 if ( m_displayName.isEmpty() )
199 m_displayName = m_name;
200 kdDebug() << k_funcinfo << m_name << " " << m_displayName << endl;
201
202 KoStyleStack& styleStack = context.styleStack();
203 styleStack.setTypeProperties( "table-cell" );
204
205 styleStack.save();
206 context.addStyles( &styleElem, "table-cell" ); // Load all parents - only because we don't support inheritance.
207
208 const TQString frameStyleName = styleStack.attributeNS( KoXmlNS::koffice, "frame-style-name" );
209 m_frameStyle = frameStyles.findStyle( frameStyleName );
210 if ( !m_frameStyle )
211 kdWarning(32001) << "Frame style " << frameStyleName << " not found!" << endl;
212
213 const TQString paragraphStyleName = styleStack.attributeNS( KoXmlNS::koffice, "paragraph-style-name" );
214 m_paragStyle = paragraphStyles.findStyle( paragraphStyleName );
215 if ( !m_paragStyle )
216 kdWarning(32001) << "Paragraph style " << paragraphStyleName << " not found!" << endl;
217
218 styleStack.restore();
219}
Collection of frame styles.
KWFrameStyle * findStyle(const TQString &name) const
Find style based on the untranslated name name.
KWFrameStyle * addStyle(KWFrameStyle *sty)
See KoUserStyleCollection::addStyle.
KWFrameStyle * frameStyleAt(int i) const
Return style number i.
KWFrameStyle * findStyleByDisplayName(const TQString &name) const
Find style based on the display name name.
The user-visible style for frames (borders and background).
KWTableStyle * findStyle(const TQString &name) const
Find style based on the untranslated name name.
KWTableStyle * addStyle(KWTableStyle *sty)
See KoUserStyleCollection::addStyle.
Table style (can created/modified/deleted by the user) A table style is simply a paragraph style and ...
static KWTableStyle * loadStyle(TQDomElement &parentElem, KWDocument *_doc, int docVersion=2)
load (old xml format)
void loadOasis(TQDomElement &styleElem, KoOasisContext &context, const KoStyleCollection &paragraphStyles, const KWFrameStyleCollection &frameStyles)
load (new oasis xml format)
KWTableStyle(const TQString &name, KoParagStyle *_style, KWFrameStyle *_frameStyle)
Create a blank framestyle (with default attributes).
void saveOasis(KoGenStyles &mainStyles, KoSavingContext &savingContext) const
save (new oasis xml format)
void saveTableStyle(TQDomElement &parentElem)
save (old xml format)