libs/flake

KoLineBorder.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002  * Copyright (C) 2006-2007 Thomas Zander <zander@kde.org>
00003  * Copyright (C) 2006-2008 Jan Hambrecht <jaham@gmx.net>
00004  * Copyright (C) 2007,2009 Thorsten Zachmann <zachmann@kde.org>
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  *
00011  * This library is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Library General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Library General Public License
00017  * along with this library; see the file COPYING.LIB.  If not, write to
00018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019  * Boston, MA 02110-1301, USA.
00020  */
00021 
00022 #include "KoLineBorder.h"
00023 #include "KoViewConverter.h"
00024 #include "KoShape.h"
00025 #include "KoShapeSavingContext.h"
00026 
00027 #include <QPainterPath>
00028 
00029 #include <KoGenStyle.h>
00030 #include <KoGenStyles.h>
00031 #include <KoOdfGraphicStyles.h>
00032 
00033 #include <math.h>
00034 
00035 class KoLineBorder::Private
00036 {
00037 public:
00038     QColor color;
00039     QPen pen;
00040     QBrush brush;
00041 };
00042 
00043 KoLineBorder::KoLineBorder()
00044         : d(new Private())
00045 {
00046     d->color = QColor(Qt::black);
00047     // we are not rendering stroke with zero width anymore
00048     // so lets use a default width of 1.0
00049     d->pen.setWidthF(1.0);
00050 }
00051 
00052 KoLineBorder::KoLineBorder(const KoLineBorder & other)
00053         : KoShapeBorderModel(), d(new Private())
00054 {
00055     d->color = other.d->color;
00056     d->pen = other.d->pen;
00057     d->brush = other.d->brush;
00058 }
00059 
00060 KoLineBorder::KoLineBorder(qreal lineWidth, const QColor &color)
00061         : d(new Private())
00062 {
00063     d->pen.setWidthF(qMax(qreal(0.0), lineWidth));
00064     d->pen.setJoinStyle(Qt::MiterJoin);
00065     d->color = color;
00066 }
00067 
00068 KoLineBorder::~KoLineBorder()
00069 {
00070     delete d;
00071 }
00072 
00073 KoLineBorder& KoLineBorder::operator = (const KoLineBorder & rhs)
00074 {
00075     if (this == &rhs)
00076         return *this;
00077 
00078     d->pen = rhs.d->pen;
00079     d->color = rhs.d->color;
00080     d->brush = rhs.d->brush;
00081 
00082     return *this;
00083 }
00084 
00085 void KoLineBorder::fillStyle(KoGenStyle &style, KoShapeSavingContext &context)
00086 {
00087     QPen pen = d->pen;
00088     if (d->brush.gradient())
00089         pen.setBrush(d->brush);
00090     else
00091         pen.setColor(d->color);
00092     KoOdfGraphicStyles::saveOdfStrokeStyle(style, context.mainStyles(), pen);
00093 }
00094 
00095 void KoLineBorder::borderInsets(const KoShape *shape, KoInsets &insets)
00096 {
00097     Q_UNUSED(shape);
00098     qreal lineWidth = d->pen.widthF();
00099     if (lineWidth < 0)
00100         lineWidth = 1;
00101     lineWidth *= 0.5; // since we draw a line half inside, and half outside the object.
00102 
00103     // if we have square cap, we need a little more space
00104     // -> sqrt( (0.5*penWidth)^2 + (0.5*penWidth)^2 )
00105     if( capStyle() == Qt::SquareCap )
00106         lineWidth *= M_SQRT2;
00107 
00108     insets.top = lineWidth;
00109     insets.bottom = lineWidth;
00110     insets.left = lineWidth;
00111     insets.right = lineWidth;
00112 }
00113 
00114 bool KoLineBorder::hasTransparency()
00115 {
00116     return d->color.alpha() > 0;
00117 }
00118 
00119 void KoLineBorder::paintBorder(KoShape *shape, QPainter &painter, const KoViewConverter &converter)
00120 {
00121     KoShape::applyConversion(painter, converter);
00122 
00123     QPen pen = d->pen;
00124 
00125     if (d->brush.gradient())
00126         pen.setBrush(d->brush);
00127     else
00128         pen.setColor(d->color);
00129 
00130     if(!pen.isCosmetic())
00131         painter.strokePath(shape->outline(), pen);
00132 }
00133 
00134 void KoLineBorder::paintBorder(KoShape *shape, QPainter &painter, const KoViewConverter &converter, const QColor & color )
00135 {
00136     KoShape::applyConversion(painter, converter);
00137 
00138     QPen pen = d->pen;
00139     pen.setColor(color);
00140 
00141     if(!pen.isCosmetic()) {
00142         painter.strokePath(shape->outline(), pen);
00143     }
00144 }
00145 
00146 void KoLineBorder::setCapStyle(Qt::PenCapStyle style)
00147 {
00148     d->pen.setCapStyle(style);
00149 }
00150 
00151 Qt::PenCapStyle KoLineBorder::capStyle() const
00152 {
00153     return d->pen.capStyle();
00154 }
00155 
00156 void KoLineBorder::setJoinStyle(Qt::PenJoinStyle style)
00157 {
00158     d->pen.setJoinStyle(style);
00159 }
00160 
00161 Qt::PenJoinStyle KoLineBorder::joinStyle() const
00162 {
00163     return d->pen.joinStyle();
00164 }
00165 
00166 void KoLineBorder::setLineWidth(qreal lineWidth)
00167 {
00168     d->pen.setWidthF(qMax(qreal(0.0), lineWidth));
00169 }
00170 
00171 qreal KoLineBorder::lineWidth() const
00172 {
00173     return d->pen.widthF();
00174 }
00175 
00176 void KoLineBorder::setMiterLimit(qreal miterLimit)
00177 {
00178     d->pen.setMiterLimit(miterLimit);
00179 }
00180 
00181 qreal KoLineBorder::miterLimit() const
00182 {
00183     return d->pen.miterLimit();
00184 }
00185 
00186 const QColor & KoLineBorder::color() const
00187 {
00188     return d->color;
00189 }
00190 
00191 void KoLineBorder::setColor(const QColor & color)
00192 {
00193     d->color = color;
00194 }
00195 
00196 void KoLineBorder::setLineStyle(Qt::PenStyle style, const QVector<qreal> &dashes)
00197 {
00198     if (style < Qt::CustomDashLine)
00199         d->pen.setStyle(style);
00200     else
00201         d->pen.setDashPattern(dashes);
00202 }
00203 
00204 Qt::PenStyle KoLineBorder::lineStyle() const
00205 {
00206     return d->pen.style();
00207 }
00208 
00209 QVector<qreal> KoLineBorder::lineDashes() const
00210 {
00211     return d->pen.dashPattern();
00212 }
00213 
00214 void KoLineBorder::setDashOffset(qreal dashOffset)
00215 {
00216     d->pen.setDashOffset(dashOffset);
00217 }
00218 
00219 qreal KoLineBorder::dashOffset() const
00220 {
00221     return d->pen.dashOffset();
00222 }
00223 
00224 void KoLineBorder::setLineBrush(const QBrush & brush)
00225 {
00226     d->brush = brush;
00227 }
00228 
00229 QBrush KoLineBorder::lineBrush() const
00230 {
00231     return d->brush;
00232 }