1 /***************************************************************************
2 * Copyright (C) 2015 by Jens Nissen jens-chessx@gmx.net *
3 * Copyright (C) 2006 by Tobias Koenig <tokoe@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 ***************************************************************************/
10
11 #include "formatproperty.h"
12
13 #include <QtGui/QTextFormat>
14
15 #include "styleinformation.h"
16
17 #if defined(_MSC_VER) && defined(_DEBUG)
18 #define DEBUG_NEW new( _NORMAL_BLOCK, __FILE__, __LINE__ )
19 #define new DEBUG_NEW
20 #endif // _MSC_VER
21
22 using namespace OOO;
23
FontFormatProperty()24 FontFormatProperty::FontFormatProperty()
25 : m_Family( "Nimbus Sans L" )
26 {
27 }
28
apply(QTextFormat * format) const29 void FontFormatProperty::apply( QTextFormat *format ) const
30 {
31 format->setProperty( QTextFormat::FontFamily, m_Family );
32 }
33
34
setFamily(const QString & name)35 void FontFormatProperty::setFamily( const QString &name )
36 {
37 m_Family = name;
38 }
39
ParagraphFormatProperty()40 ParagraphFormatProperty::ParagraphFormatProperty()
41 : m_PageNumber( 0 ), m_WritingMode( LRTB ), m_Alignment( Qt::AlignLeft ),
42 m_HasAlignment( false ), m_BackgroundColor(Qt::transparent),
43 m_HasLeftMargin( false ), m_LeftMargin(0.0)
44 {
45 }
46
apply(QTextFormat * format) const47 void ParagraphFormatProperty::apply( QTextFormat *format ) const
48 {
49 if ( m_WritingMode == LRTB || m_WritingMode == TBLR || m_WritingMode == LR || m_WritingMode == TB )
50 format->setLayoutDirection( Qt::LeftToRight );
51 else
52 format->setLayoutDirection( Qt::RightToLeft );
53
54 if ( m_HasAlignment )
55 {
56 static_cast<QTextBlockFormat*>( format )->setAlignment( m_Alignment );
57 }
58
59 format->setProperty( QTextFormat::FrameWidth, 595 );
60
61 if ( m_HasLeftMargin )
62 {
63 static_cast<QTextBlockFormat*>( format )->setLeftMargin( m_LeftMargin );
64 }
65
66 if ( m_BackgroundColor.isValid() )
67 {
68 format->setBackground( m_BackgroundColor );
69 }
70 }
71
setPageNumber(int number)72 void ParagraphFormatProperty::setPageNumber( int number )
73 {
74 m_PageNumber = number;
75 }
76
setWritingMode(WritingMode mode)77 void ParagraphFormatProperty::setWritingMode( WritingMode mode )
78 {
79 m_WritingMode = mode;
80 }
81
writingModeIsRightToLeft() const82 bool ParagraphFormatProperty::writingModeIsRightToLeft() const
83 {
84 return ( ( m_WritingMode == RLTB ) || ( m_WritingMode == TBRL ) || ( m_WritingMode == RL ) );
85 }
86
setTextAlignment(Qt::Alignment alignment)87 void ParagraphFormatProperty::setTextAlignment( Qt::Alignment alignment )
88 {
89 m_HasAlignment = true;
90 m_Alignment = alignment;
91 }
92
setBackgroundColor(const QColor & color)93 void ParagraphFormatProperty::setBackgroundColor( const QColor &color )
94 {
95 m_BackgroundColor = color;
96 if (!m_BackgroundColor.isValid()) m_BackgroundColor = Qt::transparent;
97 }
98
setLeftMargin(const qreal margin)99 void ParagraphFormatProperty::setLeftMargin( const qreal margin )
100 {
101 m_HasLeftMargin = true;
102 m_LeftMargin = margin;
103 }
104
TextFormatProperty()105 TextFormatProperty::TextFormatProperty()
106 : m_StyleInformation( nullptr ), m_HasFontSize( false ),
107 m_FontWeight( -1 ), m_FontStyle( -1 ), m_TextPosition( 0 ),
108 m_Color(Qt::black), m_BackgroundColor(Qt::transparent), m_underline(false)
109 {
110 }
111
TextFormatProperty(const StyleInformation * information)112 TextFormatProperty::TextFormatProperty( const StyleInformation *information )
113 : m_StyleInformation( information ), m_HasFontSize( false ),
114 m_FontWeight( -1 ), m_FontStyle( -1 ), m_TextPosition( 0 ),
115 m_Color(Qt::black), m_BackgroundColor(Qt::transparent), m_underline(false)
116 {
117 }
118
apply(QTextCharFormat * format) const119 void TextFormatProperty::apply( QTextCharFormat *format ) const
120 {
121 if ( !m_FontName.isEmpty() ) {
122 if ( m_StyleInformation ) {
123 const FontFormatProperty property = m_StyleInformation->fontProperty( m_FontName );
124 property.apply( format );
125 }
126 }
127
128 if ( m_FontWeight != -1 ) {
129 QFont font = format->font();
130 font.setWeight( m_FontWeight );
131 format->setFont( font );
132 }
133
134 if ( m_HasFontSize ) {
135 QFont font = format->font();
136 font.setPointSize( m_FontSize );
137 format->setFont( font );
138 }
139
140 if ( m_FontStyle != -1 ) {
141 QFont font = format->font();
142 font.setStyle( (QFont::Style)m_FontStyle );
143 format->setFont( font );
144 }
145
146 if ( m_Color.isValid() )
147 format->setForeground( m_Color );
148
149 if ( m_BackgroundColor.isValid() )
150 format->setBackground( m_BackgroundColor );
151
152 if ( m_underline )
153 format->setUnderlineStyle(QTextCharFormat::SingleUnderline);
154
155 // TODO: get FontFormatProperty and apply it
156 // TODO: how to set the base line?!?
157 }
158
setFontSize(int size)159 void TextFormatProperty::setFontSize( int size )
160 {
161 m_HasFontSize = true;
162 m_FontSize = size;
163 }
164
setFontName(const QString & name)165 void TextFormatProperty::setFontName( const QString &name )
166 {
167 m_FontName = name;
168 }
169
setFontWeight(int weight)170 void TextFormatProperty::setFontWeight( int weight )
171 {
172 m_FontWeight = weight;
173 }
174
setFontStyle(int style)175 void TextFormatProperty::setFontStyle( int style )
176 {
177 m_FontStyle = style;
178 }
179
setTextPosition(int position)180 void TextFormatProperty::setTextPosition( int position )
181 {
182 m_TextPosition = position;
183 }
184
setColor(const QColor & color)185 void TextFormatProperty::setColor( const QColor &color )
186 {
187 m_Color = color;
188 }
189
setBackgroundColor(const QColor & color)190 void TextFormatProperty::setBackgroundColor( const QColor &color )
191 {
192 m_BackgroundColor = color;
193 if (!m_BackgroundColor.isValid()) m_BackgroundColor = Qt::transparent;
194 }
195
setUnderline(bool underline)196 void TextFormatProperty::setUnderline( bool underline )
197 {
198 m_underline = underline;
199 }
200
StyleFormatProperty()201 StyleFormatProperty::StyleFormatProperty()
202 : m_StyleInformation( nullptr ), m_DefaultStyle( false )
203 {
204 }
205
StyleFormatProperty(const StyleInformation * information)206 StyleFormatProperty::StyleFormatProperty( const StyleInformation *information )
207 : m_StyleInformation( information ), m_DefaultStyle( false )
208 {
209 }
210
applyBlock(QTextBlockFormat * format) const211 void StyleFormatProperty::applyBlock( QTextBlockFormat *format ) const
212 {
213 if ( !m_DefaultStyle && !m_Family.isEmpty() && m_StyleInformation ) {
214 const StyleFormatProperty property = m_StyleInformation->styleProperty( m_Family );
215 property.applyBlock( format );
216 }
217
218 if ( !m_ParentStyleName.isEmpty() && m_StyleInformation ) {
219 const StyleFormatProperty property = m_StyleInformation->styleProperty( m_ParentStyleName );
220 property.applyBlock( format );
221 }
222
223 m_ParagraphFormat.apply( format );
224 }
225
applyText(QTextCharFormat * format) const226 void StyleFormatProperty::applyText( QTextCharFormat *format ) const
227 {
228 if ( !m_DefaultStyle && !m_Family.isEmpty() && m_StyleInformation ) {
229 const StyleFormatProperty property = m_StyleInformation->styleProperty( m_Family );
230 property.applyText( format );
231 }
232
233 if ( !m_ParentStyleName.isEmpty() && m_StyleInformation ) {
234 const StyleFormatProperty property = m_StyleInformation->styleProperty( m_ParentStyleName );
235 property.applyText( format );
236 }
237
238 m_TextFormat.apply( format );
239 }
240
applyTableColumn(QTextTableFormat * format) const241 void StyleFormatProperty::applyTableColumn( QTextTableFormat *format ) const
242 {
243 if ( !m_DefaultStyle && !m_Family.isEmpty() && m_StyleInformation ) {
244 const StyleFormatProperty property = m_StyleInformation->styleProperty( m_Family );
245 property.applyTableColumn( format );
246 }
247
248 if ( !m_ParentStyleName.isEmpty() && m_StyleInformation ) {
249 const StyleFormatProperty property = m_StyleInformation->styleProperty( m_ParentStyleName );
250 property.applyTableColumn( format );
251 }
252
253 m_TableColumnFormat.apply( format );
254 }
255
applyTableCell(QTextBlockFormat * format) const256 void StyleFormatProperty::applyTableCell( QTextBlockFormat *format ) const
257 {
258 if ( !m_DefaultStyle && !m_Family.isEmpty() && m_StyleInformation ) {
259 const StyleFormatProperty property = m_StyleInformation->styleProperty( m_Family );
260 property.applyTableCell( format );
261 }
262
263 if ( !m_ParentStyleName.isEmpty() && m_StyleInformation ) {
264 const StyleFormatProperty property = m_StyleInformation->styleProperty( m_ParentStyleName );
265 property.applyTableCell( format );
266 }
267
268 m_TableCellFormat.apply( format );
269 }
270
setParentStyleName(const QString & parentStyleName)271 void StyleFormatProperty::setParentStyleName( const QString &parentStyleName )
272 {
273 m_ParentStyleName = parentStyleName;
274 }
275
parentStyleName() const276 QString StyleFormatProperty::parentStyleName() const
277 {
278 return m_ParentStyleName;
279 }
280
setFamily(const QString & family)281 void StyleFormatProperty::setFamily( const QString &family )
282 {
283 m_Family = family;
284 }
285
setDefaultStyle(bool defaultStyle)286 void StyleFormatProperty::setDefaultStyle( bool defaultStyle )
287 {
288 m_DefaultStyle = defaultStyle;
289 }
290
setMasterPageName(const QString & masterPageName)291 void StyleFormatProperty::setMasterPageName( const QString &masterPageName )
292 {
293 m_MasterPageName = masterPageName;
294 }
295
setParagraphFormat(const ParagraphFormatProperty & format)296 void StyleFormatProperty::setParagraphFormat( const ParagraphFormatProperty &format )
297 {
298 m_ParagraphFormat = format;
299 }
300
setTextFormat(const TextFormatProperty & format)301 void StyleFormatProperty::setTextFormat( const TextFormatProperty &format )
302 {
303 m_TextFormat = format;
304 }
305
setTableColumnFormat(const TableColumnFormatProperty & format)306 void StyleFormatProperty::setTableColumnFormat( const TableColumnFormatProperty &format )
307 {
308 m_TableColumnFormat = format;
309 }
310
setTableCellFormat(const TableCellFormatProperty & format)311 void StyleFormatProperty::setTableCellFormat( const TableCellFormatProperty &format )
312 {
313 m_TableCellFormat = format;
314 }
315
PageFormatProperty()316 PageFormatProperty::PageFormatProperty()
317 : m_PageUsage(All), m_BottomMargin(0.0), m_LeftMargin(0.0), m_TopMargin(0.0),
318 m_RightMargin(0.0), m_Height(0.0), m_Width(0.0), m_PrintOrientation(Portrait)
319 {
320 }
321
apply(QTextFormat * format) const322 void PageFormatProperty::apply( QTextFormat *format ) const
323 {
324 format->setProperty( QTextFormat::BlockBottomMargin, m_BottomMargin );
325 format->setProperty( QTextFormat::BlockLeftMargin, m_LeftMargin );
326 format->setProperty( QTextFormat::BlockTopMargin, m_TopMargin );
327 format->setProperty( QTextFormat::BlockRightMargin, m_RightMargin );
328 format->setProperty( QTextFormat::FrameWidth, m_Width );
329 format->setProperty( QTextFormat::FrameHeight, m_Height );
330 }
331
setPageUsage(PageUsage usage)332 void PageFormatProperty::setPageUsage( PageUsage usage )
333 {
334 m_PageUsage = usage;
335 }
336
setBottomMargin(double margin)337 void PageFormatProperty::setBottomMargin( double margin )
338 {
339 m_BottomMargin = margin;
340 }
341
setLeftMargin(double margin)342 void PageFormatProperty::setLeftMargin( double margin )
343 {
344 m_LeftMargin = margin;
345 }
346
setTopMargin(double margin)347 void PageFormatProperty::setTopMargin( double margin )
348 {
349 m_TopMargin = margin;
350 }
351
setRightMargin(double margin)352 void PageFormatProperty::setRightMargin( double margin )
353 {
354 m_RightMargin = margin;
355 }
356
setHeight(double height)357 void PageFormatProperty::setHeight( double height )
358 {
359 m_Height = height;
360 }
361
setWidth(double width)362 void PageFormatProperty::setWidth( double width )
363 {
364 m_Width = width;
365 }
366
setPrintOrientation(PrintOrientation orientation)367 void PageFormatProperty::setPrintOrientation( PrintOrientation orientation )
368 {
369 m_PrintOrientation = orientation;
370 }
371
width() const372 double PageFormatProperty::width() const
373 {
374 return m_Width;
375 }
376
height() const377 double PageFormatProperty::height() const
378 {
379 return m_Height;
380 }
381
margin() const382 double PageFormatProperty::margin() const
383 {
384 return m_LeftMargin;
385 }
386
ListFormatProperty()387 ListFormatProperty::ListFormatProperty()
388 : m_Type( QTextListFormat::ListDisc ), m_BackgroundColor(Qt::transparent)
389 {
390 m_Indents.resize( 10 );
391 }
392
setType(QTextListFormat::Style type)393 void ListFormatProperty::setType( QTextListFormat::Style type )
394 {
395 m_Type = type;
396 }
397
apply(QTextListFormat * format) const398 void ListFormatProperty::apply( QTextListFormat *format ) const
399 {
400 format->setStyle( m_Type );
401 format->setBackground(m_BackgroundColor);
402 }
403
addItem(int level,double indent)404 void ListFormatProperty::addItem( int level, double indent )
405 {
406 if ( level < 0 || level >= 10 )
407 return;
408
409 m_Indents[ level ] = indent;
410 }
411
TableColumnFormatProperty()412 TableColumnFormatProperty::TableColumnFormatProperty()
413 : m_Width( 0 ), isValid( false )
414 {
415 }
416
apply(QTextTableFormat * format) const417 void TableColumnFormatProperty::apply( QTextTableFormat *format ) const
418 {
419 if ( ! isValid ) {
420 return;
421 }
422 QVector<QTextLength> lengths = format->columnWidthConstraints();
423 lengths.append( QTextLength( QTextLength::FixedLength, m_Width ) );
424
425 format->setColumnWidthConstraints( lengths );
426 }
427
setWidth(double width)428 void TableColumnFormatProperty::setWidth( double width )
429 {
430 m_Width = width;
431 isValid = true;
432 }
433
TableCellFormatProperty()434 TableCellFormatProperty::TableCellFormatProperty()
435 : m_Padding( 0 ), m_HasAlignment( false ), m_BackgroundColor(Qt::transparent)
436 {
437 }
438
apply(QTextBlockFormat * format) const439 void TableCellFormatProperty::apply( QTextBlockFormat *format ) const
440 {
441 if ( m_BackgroundColor.isValid() )
442 format->setBackground( m_BackgroundColor );
443
444 if ( m_HasAlignment )
445 format->setAlignment( m_Alignment );
446 }
447
setBackgroundColor(const QColor & color)448 void TableCellFormatProperty::setBackgroundColor( const QColor &color )
449 {
450 m_BackgroundColor = color;
451 if (!m_BackgroundColor.isValid()) m_BackgroundColor = Qt::transparent;
452 }
453
setPadding(double padding)454 void TableCellFormatProperty::setPadding( double padding )
455 {
456 m_Padding = padding;
457 }
458
setAlignment(Qt::Alignment alignment)459 void TableCellFormatProperty::setAlignment( Qt::Alignment alignment )
460 {
461 m_Alignment = alignment;
462 m_HasAlignment = true;
463 }
464