libs/flake

KoColorBackground.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
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; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this library; see the file COPYING.LIB.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "KoColorBackground.h"
00021 #include "KoShapeSavingContext.h"
00022 #include <KoOdfGraphicStyles.h>
00023 #include <KoOdfLoadingContext.h>
00024 #include <KoXmlNS.h>
00025 
00026 #include <QtGui/QColor>
00027 #include <QtGui/QPainter>
00028 
00029 class KoColorBackground::Private
00030 {
00031 public:
00032     Private() {
00033         color = Qt::black;
00034         style = Qt::SolidPattern;
00035     };
00036     Qt::BrushStyle style;
00037     QColor color;
00038 };
00039 
00040 KoColorBackground::KoColorBackground()
00041         : d(new Private())
00042 {
00043 }
00044 
00045 KoColorBackground::KoColorBackground(const QColor &color, Qt::BrushStyle style)
00046         : d(new Private())
00047 {
00048     if (style < Qt::SolidPattern || style >= Qt::LinearGradientPattern)
00049         style = Qt::SolidPattern;
00050     d->style = style;
00051     d->color = color;
00052 }
00053 
00054 KoColorBackground::~KoColorBackground()
00055 {
00056     delete d;
00057 }
00058 
00059 QColor KoColorBackground::color() const
00060 {
00061     return d->color;
00062 }
00063 
00064 void KoColorBackground::setColor( const QColor &color )
00065 {
00066     d->color = color;
00067 }
00068 
00069 Qt::BrushStyle KoColorBackground::style() const
00070 {
00071     return d->style;
00072 }
00073 
00074 void KoColorBackground::paint(QPainter &painter, const QPainterPath &fillPath) const
00075 {
00076     painter.setBrush(QBrush(d->color, d->style));
00077     painter.drawPath(fillPath);
00078 }
00079 
00080 void KoColorBackground::fillStyle(KoGenStyle &style, KoShapeSavingContext &context)
00081 {
00082     KoOdfGraphicStyles::saveOdfFillStyle(style, context.mainStyles(), QBrush(d->color, d->style));
00083 }
00084 
00085 bool KoColorBackground::loadStyle(KoOdfLoadingContext & context, const QSizeF &)
00086 {
00087     KoStyleStack &styleStack = context.styleStack();
00088     if (! styleStack.hasProperty(KoXmlNS::draw, "fill"))
00089         return false;
00090 
00091     QString fillStyle = styleStack.property(KoXmlNS::draw, "fill");
00092     if (fillStyle == "solid" || fillStyle == "hatch") {
00093         QBrush brush = KoOdfGraphicStyles::loadOdfFillStyle(styleStack, fillStyle, context.stylesReader());
00094         d->color = brush.color();
00095         d->style = brush.style();
00096         return true;
00097     }
00098 
00099     return false;
00100 }