1 /* This file is part of the Calligra project 2 * Copyright (C) 2006 Sebastian Sauer <mail@dipe.org> 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; either 7 * version 2 of the License, or (at your option) any later version. 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 #ifndef SCRIPTING_STYLE_H 21 #define SCRIPTING_STYLE_H 22 23 #include <QObject> 24 #include <KoParagraphStyle.h> 25 #include <KoCharacterStyle.h> 26 #include <KoListStyle.h> 27 28 namespace Scripting 29 { 30 31 /** 32 * The CharacterStyle class provides access to styles for characters. 33 */ 34 class CharacterStyle : public QObject 35 { 36 Q_OBJECT 37 public: CharacterStyle(QObject * parent,KoCharacterStyle * style)38 CharacterStyle(QObject* parent, KoCharacterStyle* style) : QObject(parent), m_style(style) {} ~CharacterStyle()39 virtual ~CharacterStyle() {} style()40 KoCharacterStyle* style() const { 41 return m_style; 42 } 43 public Q_SLOTS: 44 45 /** Return the user-visible name the character-style has. */ name()46 QString name() const { 47 return m_style->name(); 48 } 49 /** Set the user-visible name the character-style has. */ setName(const QString & name)50 void setName(const QString& name) { 51 m_style->setName(name); 52 } 53 54 /***** Font *****/ 55 56 /** Return the font-family name. */ family()57 QString family() const { 58 return m_style->fontFamily(); 59 } 60 /** 61 * Set the font-family name. 62 * 63 * The following python sets the font for the two character-styles 64 * mycharstyle1 and mycharstyle2; 65 * \code 66 * mycharstyle1.setFamily("Times New Roman") 67 * mycharstyle2.setFamily("Arial") 68 * \endcode 69 */ setFamily(const QString & family)70 void setFamily(const QString &family) { 71 m_style->setFontFamily(family); 72 } 73 74 /** Return the size of the font. */ size()75 qreal size() const { 76 return m_style->fontPointSize(); 77 } 78 /** 79 * Set the size of the font. 80 * 81 * Python sample that sets the font size; 82 * \code 83 * mycharstyle.setSize(12.0) 84 * \endcode 85 */ setSize(qreal size)86 void setSize(qreal size) { 87 m_style->setFontPointSize(size); 88 } 89 90 /** Return the weight of the font. */ weight()91 int weight() const { 92 return m_style->fontWeight(); 93 } 94 /** 95 * Set the weight of the font. 96 * 97 * Python sample that sets the font weight; 98 * \code 99 * if style == "normal": 100 * mycharstyle.setWeight(50) 101 * elif style == "bold": 102 * mycharstyle.setWeight(75) 103 * else: 104 * raise "Invalid style %s" % style 105 * \endcode 106 */ setWeight(int weight)107 void setWeight(int weight) { 108 m_style->setFontWeight(weight); 109 } 110 111 /** Return true if the font is italic. */ italic()112 bool italic() const { 113 return m_style->fontItalic(); 114 } 115 /** 116 * Set if the font should be italic or not. 117 * 118 * Python sample that sets the font italic for the both 119 * character-styles mycharstyle1 and mycharstyle2; 120 * \code 121 * mycharstyle1.setItalic(True) 122 * mycharstyle2.setItalic(False) 123 * \endcode 124 */ setItalic(bool italic)125 void setItalic(bool italic) { 126 m_style->setFontItalic(italic); 127 } 128 129 /** Return true if the font is bold. */ bold()130 bool bold() const { 131 return m_style->fontWeight() >= 75; 132 } 133 /** 134 * Set if the font should be bold or not. 135 * 136 * Python sample that sets the font bold for the both 137 * character-styles mycharstyle1 and mycharstyle2; 138 * \code 139 * mycharstyle1.setBold(True) 140 * mycharstyle2.setBold(False) 141 * \endcode 142 */ setBold(bool bold)143 void setBold(bool bold) { 144 m_style->setFontWeight(bold ? 75 : 50); 145 } 146 147 /** Return true if there is an underline. */ underline()148 bool underline() const { 149 return m_style->underlineStyle() != KoCharacterStyle::NoLineStyle; 150 } 151 /** Set the underline. */ setUnderline(bool underline)152 void setUnderline(bool underline) { 153 m_style->setUnderlineStyle(underline ? KoCharacterStyle::SolidLine : KoCharacterStyle::NoLineStyle); 154 } 155 156 /***** Foreground *****/ 157 158 /** Return the font-color. */ color()159 QColor color() const { 160 return m_style->foreground().color(); 161 } 162 /** 163 * Set the font-color. 164 * 165 * Python sample that sets the font text-color to red aka 166 * RGB-value FF0000 ; 167 * \code 168 * mycharstyle.setColor("#ff0000") 169 * \endcode 170 */ setColor(const QColor & color)171 void setColor(const QColor& color) { 172 QBrush brush = m_style->foreground(); 173 brush.setColor(color); 174 m_style->setForeground(brush); 175 } 176 177 /***** Background *****/ 178 179 /** Return the background-color. */ backgroundColor()180 QColor backgroundColor() const { 181 return m_style->background().color(); 182 } 183 /** 184 * Set the background-color. 185 * 186 * Python sample that sets the text- and the background-color 187 * of the character-style mycharstyle; 188 * \code 189 * mycharstyle.setColor("#ffffff") 190 * mycharstyle.setBackgroundColor("#0000ff") 191 * \endcode 192 */ setBackgroundColor(const QColor & color)193 void setBackgroundColor(const QColor &color) { 194 QBrush brush = m_style->background(); 195 brush.setColor(color); 196 m_style->setBackground(brush); 197 } 198 199 private: 200 KoCharacterStyle* m_style; 201 }; 202 203 /** 204 * The ParagraphStyle class provides access to styles for paragraphs. 205 */ 206 class ParagraphStyle : public QObject 207 { 208 Q_OBJECT Q_ENUMS(Alignment)209 Q_ENUMS(Alignment) 210 Q_ENUMS(ListStyle) 211 public: 212 ParagraphStyle(QObject* parent, KoParagraphStyle* style) : QObject(parent), m_style(style) {} ~ParagraphStyle()213 virtual ~ParagraphStyle() {} style()214 KoParagraphStyle* style() const { 215 return m_style; 216 } 217 218 enum Alignment { 219 AlignLeft = Qt::AlignLeft, 220 AlignRight = Qt::AlignRight, 221 AlignHCenter = Qt::AlignHCenter, 222 AlignJustify = Qt::AlignJustify, 223 AlignTop = Qt::AlignTop, 224 AlignBottom = Qt::AlignBottom, 225 AlignVCenter = Qt::AlignVCenter, 226 AlignCenter = Qt::AlignCenter 227 }; 228 229 enum ListStyle { 230 None = KoListStyle::None, 231 SquareItem = KoListStyle::SquareItem, 232 DiscItem = KoListStyle::Bullet, 233 Bullet = KoListStyle::Bullet, 234 CircleItem = KoListStyle::CircleItem, 235 DecimalItem = KoListStyle::DecimalItem, 236 AlphaLowerItem = KoListStyle::AlphaLowerItem, 237 AlphaUpperItem = KoListStyle::UpperAlphaItem, 238 RomanLowerItem = KoListStyle::RomanLowerItem, 239 RomanUpperItem = KoListStyle::UpperRomanItem, 240 BoxItem = KoListStyle::BoxItem 241 }; 242 243 public Q_SLOTS: 244 245 /***** Name *****/ 246 247 /** Return the user-visible name the paragraph-style has. */ name()248 QString name() const { 249 return m_style->name(); 250 } 251 252 /** Set the user-visible name the paragraph-style has. */ setName(const QString & name)253 void setName(const QString& name) { 254 m_style->setName(name); 255 } 256 257 /***** Alignment *****/ 258 259 /** 260 * Return the alignment the paragraph-style has. 261 * 262 * Valid values are; 263 * \li AlignLeft, AlignRight, AlignHCenter, AlignJustify 264 * \li AlignTop, AlignBottom, AlignVCenter, AlignCenter 265 * 266 * The following python sample demonstrates the usage; 267 * \code 268 * alignment = MyParagraphStyle.alignment() 269 * if alignment == MyParagraphStyle.AlignLeft: 270 * print "Align Left" 271 * elif alignment == MyParagraphStyle.AlignRight: 272 * print "Align Right" 273 * elif alignment == MyParagraphStyle.AlignHCenter: 274 * print "Align Center" 275 * elif alignment == MyParagraphStyle.AlignJustify: 276 * print "Align Justify" 277 * \endcode 278 */ alignment()279 int alignment() const { 280 return m_style->alignment(); 281 } 282 283 /** Set the alignment the paragraph-style has. */ setAlignment(int alignment)284 void setAlignment(int alignment) { 285 m_style->setAlignment((Qt::Alignment) alignment); 286 } 287 288 /***** Padding *****/ 289 290 /** Return the distance between text and border. */ padding()291 QRectF padding() const { 292 return QRectF(m_style->leftPadding(), m_style->topPadding(), m_style->rightPadding(), m_style->bottomPadding()); 293 } 294 295 /** Set the distance between text and border. */ setPadding(const QRectF & r)296 void setPadding(const QRectF& r) { 297 m_style->setLeftPadding(r.x()); 298 m_style->setTopPadding(r.y()); 299 m_style->setRightPadding(r.width()); 300 m_style->setBottomPadding(r.height()); 301 } 302 303 /***** Margin *****/ 304 305 /** Return the margin between text and border. */ margin()306 QRectF margin() const { 307 return QRectF(m_style->leftMargin(), m_style->topMargin(), m_style->rightMargin(), m_style->bottomMargin()); 308 } 309 310 /** Set the margin between text and border. */ setMargin(const QRectF & r)311 void setMargin(const QRectF& r) { 312 m_style->setLeftMargin(r.x()); 313 m_style->setTopMargin(r.y()); 314 m_style->setRightMargin(r.width()); 315 m_style->setBottomMargin(r.height()); 316 } 317 318 #if 0 319 /***** Border *****/ 320 321 /*TODO simplify border options even more. Probably just deal with a QVariantMap using QMetaEnum's, e.g. 322 QVariantMap border() { 323 QVariantMap map; 324 for(int i = KoParagraphStyle::HasLeftBorder; i <= KoParagraphStyle::BottomBorderColor; i++) 325 map.insert("", m_style->property("")); 326 return map; 327 } 328 */ 329 330 QRect borderStyle() { 331 return QRect(m_style->leftBorderStyle(), m_style->topBorderStyle(), m_style->rightBorderStyle(), m_style->bottomBorderStyle()); 332 } 333 334 void setBorderStyle(const QRect& rect) { 335 m_style->setLeftBorderStyle((KoParagraphStyle::BorderStyle) r.x()); 336 m_style->setTopBorderStyle((KoParagraphStyle::BorderStyle) r.y()); 337 m_style->setRightBorderStyle((KoParagraphStyle::BorderStyle) r.width()); 338 m_style->setBottomBorderStyle((KoParagraphStyle::BorderStyle) r.height()); 339 } 340 341 QRect borderSpacing() { 342 return QRect(m_style->leftBorderSpacing(), m_style->topBorderSpacing(), m_style->rightBorderSpacing(), m_style->bottomBorderSpacing()); 343 } 344 345 void setBorderSpacing(const QRect& rect) { 346 m_style->setLeftBorderSpacing(r.x()); 347 m_style->setTopBorderSpacing(r.y()); 348 m_style->setRightBorderSpacing(r.width()); 349 m_style->setBottomBorderSpacing(r.height()); 350 } 351 352 QRect borderWidth() { 353 return QRect(m_style->leftBorderWidth(), m_style->topBorderWidth(), m_style->rightBorderWidth(), m_style->bottomBorderWidth()); 354 } 355 356 void setBorderWidth(const QRect& rect) { 357 m_style->setLeftBorderWidth(r.x()); 358 m_style->setTopBorderWidth(r.y()); 359 m_style->setRightBorderWidth(r.width()); 360 m_style->setBottomBorderWidth(r.height()); 361 } 362 363 QRect innerBorderWidth() { 364 return QRect(m_style->leftInnerBorderWidth(), m_style->topInnerBorderWidth(), m_style->rightInnerBorderWidth(), m_style->bottomInnerBorderWidth()); 365 } 366 367 void setInnerBorderWidth(const QRect& rect) { 368 m_style->setLeftInnerBorderWidth(r.x()); 369 m_style->setTopInnerBorderWidth(r.y()); 370 m_style->setRightInnerBorderWidth(r.width()); 371 m_style->setBottomInnerBorderWidth(r.height()); 372 } 373 374 QRect borderColor() { 375 return QRect(m_style->leftBorderColor(), m_style->topBorderColor(), m_style->rightBorderColor(), m_style->bottomBorderColor()); 376 } 377 378 void setBorderColor(const QRect& rect) { 379 m_style->setLeftBorderColor(r.x()); 380 m_style->setTopBorderColor(r.y()); 381 m_style->setRightBorderColor(r.width()); 382 m_style->setBottomBorderColor(r.height()); 383 } 384 #endif 385 386 /***** List *****/ 387 388 /** Return true if this item is a list item else false is returned. */ isList()389 bool isList() const { 390 return m_style->listStyle() != 0; 391 } 392 393 #if 0 394 /** Return the style of listitems. */ 395 int listStyle() const { 396 //return m_style->listStyle() ? m_style->listStyle()->style() : 0; 397 KoListStyle liststyle = m_style->listStyle(); 398 return liststyle.isValid() ? new ListStyle(this, liststyle) : 0; 399 } 400 401 /** Set the style of listitems. */ 402 void setListStyle(int liststyle) { 403 if (m_style->listStyle()) 404 m_style->listStyle()->setStyle((KoListStyle::Style)liststyle); 405 else { 406 KoListStyle s; 407 s.setStyle((KoListStyle::Style) liststyle); 408 m_style->setListStyle(s); 409 } 410 } 411 #endif 412 413 /** Return the character-style for this paragraph-style. */ characterStyle()414 QObject* characterStyle() { 415 KoCharacterStyle* charstyle = m_style->characterStyle(); 416 return charstyle ? new CharacterStyle(this, charstyle) : 0; 417 } 418 /** Set the character-style for this paragraph-style. */ setCharacterStyle(QObject * style)419 void setCharacterStyle(QObject *style) { 420 CharacterStyle* charstyle = dynamic_cast<CharacterStyle*>(style); 421 KoCharacterStyle* s = charstyle ? charstyle->style() : 0; 422 if (s) m_style->setCharacterStyle(s); 423 } 424 425 private: 426 KoParagraphStyle* m_style; 427 }; 428 429 } 430 431 #endif 432