00001 /* This file is part of the KDE project 00002 * Copyright (C) 2006 Thomas Zander <zander@kde.org> 00003 * 00004 * This library is free software; you can redistribute it and/or 00005 * modify it under the terms of the GNU Library General Public 00006 * License as published by the Free Software Foundation; version 2. 00007 * 00008 * This library is distributed in the hope that it will be useful, 00009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00011 * Library General Public License for more details. 00012 * 00013 * You should have received a copy of the GNU Library General Public License 00014 * along with this library; see the file COPYING.LIB. If not, write to 00015 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00016 * Boston, MA 02110-1301, USA. 00017 */ 00018 #include "KoCharacterStyle.h" 00019 00020 #include "Styles_p.h" 00021 00022 #include <QTextBlock> 00023 #include <QTextCursor> 00024 00025 00026 KoCharacterStyle::KoCharacterStyle(QObject *parent) 00027 : QObject(parent) 00028 { 00029 m_stylesPrivate = new StylePrivate(); 00030 setFontPointSize(12.0); 00031 setFontWeight(QFont::Normal); 00032 setVerticalAlignment(QTextCharFormat::AlignNormal); 00033 setTextOutline(QPen(Qt::NoPen)); 00034 setForeground(Qt::black); 00035 } 00036 00037 KoCharacterStyle::~KoCharacterStyle() { 00038 } 00039 00040 void KoCharacterStyle::setProperty(int key, const QVariant &value) { 00041 m_stylesPrivate->add(key, value); 00042 } 00043 00044 const QVariant *KoCharacterStyle::get(int key) const { 00045 return m_stylesPrivate->get(key); 00046 } 00047 00048 double KoCharacterStyle::propertyDouble(int key) const { 00049 const QVariant *variant = get(key); 00050 if(variant == 0) 00051 return 0.0; 00052 return variant->toDouble(); 00053 } 00054 00055 QPen KoCharacterStyle::textOutline () const { 00056 const QVariant *variant = get(QTextFormat::TextOutline); 00057 if(variant == 0) { 00058 QPen pen(Qt::NoPen); 00059 return pen; 00060 } 00061 return qvariant_cast<QPen>(*variant); 00062 } 00063 00064 QColor KoCharacterStyle::underlineColor () const { 00065 const QVariant *variant = get(QTextFormat::TextUnderlineColor); 00066 if(variant == 0) { 00067 QColor color; 00068 return color; 00069 } 00070 return qvariant_cast<QColor>(*variant); 00071 } 00072 00073 QBrush KoCharacterStyle::background() const { 00074 const QVariant *variant = get(QTextFormat::BackgroundBrush); 00075 if(variant == 0) { 00076 QBrush brush; 00077 return brush; 00078 } 00079 return qvariant_cast<QBrush>(*variant); 00080 } 00081 00082 void KoCharacterStyle::clearBackground() { 00083 m_stylesPrivate->remove(QTextCharFormat::BackgroundBrush); 00084 } 00085 00086 QBrush KoCharacterStyle::foreground() const { 00087 const QVariant *variant = get(QTextFormat::ForegroundBrush); 00088 if(variant == 0) { 00089 QBrush brush; 00090 return brush; 00091 } 00092 return qvariant_cast<QBrush>(*variant); 00093 } 00094 00095 void KoCharacterStyle::clearForeground() { 00096 m_stylesPrivate->remove(QTextCharFormat::ForegroundBrush); 00097 } 00098 00099 int KoCharacterStyle::propertyInt(int key) const { 00100 const QVariant *variant = get(key); 00101 if(variant == 0) 00102 return 0; 00103 return variant->toInt(); 00104 } 00105 00106 bool KoCharacterStyle::propertyBoolean(int key) const { 00107 const QVariant *variant = get(key); 00108 if(variant == 0) 00109 return false; 00110 return variant->toBool(); 00111 } 00112 00113 void KoCharacterStyle::applyStyle(QTextCharFormat &format) const { 00114 // copy all relevant properties. 00115 static const int properties[] = { 00116 StyleId, 00117 QTextFormat::FontPointSize, 00118 QTextCharFormat::ForegroundBrush, 00119 -1 00120 }; 00121 00122 int i=0; 00123 while(properties[i] != -1) { 00124 QVariant const *variant = get(properties[i]); 00125 if(variant) format.setProperty(properties[i], *variant); 00126 i++; 00127 } 00128 } 00129 00130 void KoCharacterStyle::applyStyle(QTextBlock &block) const { 00131 QTextCursor cursor(block); 00132 QTextCharFormat cf = cursor.charFormat(); 00133 /* 00134 TODO make replacement of the style be a lot smarter. 00135 QTextBlock::Iterator fragmentIter = block.begin(); 00136 */ 00137 cursor.setPosition(block.position() + block.length()-1, QTextCursor::KeepAnchor); 00138 applyStyle(cf); 00139 cursor.mergeCharFormat(cf); 00140 cursor.setBlockCharFormat(cf); 00141 } 00142 00143 void KoCharacterStyle::applyStyle(QTextCursor *selection) const { 00144 QTextCharFormat cf = selection->charFormat(); 00145 applyStyle(cf); 00146 selection->mergeCharFormat(cf); 00147 } 00148 00149 QString KoCharacterStyle::propertyString(int key) const { 00150 const QVariant *variant = m_stylesPrivate->get(key); 00151 if(variant == 0) 00152 return QString(); 00153 return qvariant_cast<QString>(*variant); 00154 } 00155 00156 #include "KoCharacterStyle.moc"