lib Library API Documentation

koborder.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2000, 2001 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; 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., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "koborder.h"
00021 #include <qdom.h>
00022 #include <kdebug.h>
00023 #include "kozoomhandler.h"
00024 #include "kotextformat.h"
00025 #include "korichtext.h" // for KoTextFormat
00026 
00027 KoBorder::KoBorder()
00028     : color(), style( SOLID )
00029 {
00030     setPenWidth( 1 );
00031 }
00032 
00033 KoBorder::KoBorder( const QColor & c, BorderStyle s, double width )
00034     : color( c ), style( s )
00035 {
00036     setPenWidth( width );
00037 }
00038 
00039 bool KoBorder::operator==( const KoBorder _brd ) const {
00040     return ( style == _brd.style && color == _brd.color && ptPenWidth == _brd.ptPenWidth );
00041 }
00042 
00043 bool KoBorder::operator!=( const KoBorder _brd ) const {
00044     return ( style != _brd.style || color != _brd.color || ptPenWidth != _brd.ptPenWidth );
00045 }
00046 
00047 void KoBorder::setStyle(BorderStyle _style)
00048 {
00049     style = _style;
00050     setPenWidth(ptPenWidth);
00051 }
00052 
00053 void KoBorder::setPenWidth(double _w)
00054 {
00055     ptPenWidth = _w;
00056     if ( style==KoBorder::DOUBLE_LINE)
00057     {
00058         ptWidth = 2 * ptPenWidth + 1;
00059     }
00060     else
00061         ptWidth = _w;
00062 }
00063 
00064 QPen KoBorder::borderPen( const KoBorder & _brd, int width, QColor defaultColor )
00065 {
00066     QPen pen( _brd.color, width );
00067     if ( !_brd.color.isValid() )
00068         pen.setColor( defaultColor );
00069 
00070     switch ( _brd.style ) {
00071     case KoBorder::SOLID:
00072     case KoBorder::DOUBLE_LINE:
00073         pen.setStyle( SolidLine );
00074         break;
00075     case KoBorder::DASH:
00076         pen.setStyle( DashLine );
00077         break;
00078     case KoBorder::DOT:
00079         pen.setStyle( DotLine );
00080         break;
00081     case KoBorder::DASH_DOT:
00082         pen.setStyle( DashDotLine );
00083         break;
00084     case KoBorder::DASH_DOT_DOT:
00085         pen.setStyle( DashDotDotLine );
00086         break;
00087     }
00088 
00089     return pen;
00090 }
00091 
00092 KoBorder KoBorder::loadBorder( const QDomElement & elem )
00093 {
00094     KoBorder bd;
00095     if ( elem.hasAttribute("red") )
00096     {
00097         int r = elem.attribute("red").toInt();
00098         int g = elem.attribute("green").toInt();
00099         int b = elem.attribute("blue").toInt();
00100         bd.color.setRgb( r, g, b );
00101     }
00102     bd.style = static_cast<BorderStyle>( elem.attribute("style").toInt() );
00103     bd.setPenWidth( elem.attribute("width").toDouble() );
00104     return bd;
00105 }
00106 
00107 void KoBorder::save( QDomElement & elem ) const
00108 {
00109     if (color.isValid()) {
00110         elem.setAttribute("red", color.red());
00111         elem.setAttribute("green", color.green());
00112         elem.setAttribute("blue", color.blue());
00113     }
00114     elem.setAttribute("style", static_cast<int>( style ));
00115     elem.setAttribute("width", ptPenWidth);
00116 }
00117 
00118 KoBorder::BorderStyle KoBorder::getStyle( const QString &style )
00119 {
00120     if ( style == "___ ___ __" )
00121         return KoBorder::DASH;
00122     if ( style == "_ _ _ _ _ _" )
00123         return KoBorder::DOT;
00124     if ( style == "___ _ ___ _" )
00125         return KoBorder::DASH_DOT;
00126     if ( style == "___ _ _ ___" )
00127         return KoBorder::DASH_DOT_DOT;
00128     if ( style == "===========" )
00129         return KoBorder::DOUBLE_LINE;
00130     // default
00131     return KoBorder::SOLID;
00132 }
00133 
00134 QString KoBorder::getStyle( const BorderStyle &style )
00135 {
00136     switch ( style )
00137     {
00138     case KoBorder::SOLID:
00139         return "_________";
00140     case KoBorder::DASH:
00141         return "___ ___ __";
00142     case KoBorder::DOT:
00143         return "_ _ _ _ _ _";
00144     case KoBorder::DASH_DOT:
00145         return "___ _ ___ _";
00146     case KoBorder::DASH_DOT_DOT:
00147         return "___ _ _ ___";
00148     case KoBorder::DOUBLE_LINE:
00149         return "===========";
00150     }
00151 
00152     // Keep compiler happy.
00153     return "_________";
00154 }
00155 
00156 int KoBorder::zoomWidthX( double ptWidth, KoZoomHandler * zoomHandler, int minborder )
00157 {
00158     // If a border was set, then zoom it and apply a minimum of 1, so that it's always visible.
00159     // If no border was set, apply minborder ( 0 for paragraphs, 1 for frames )
00160     return ptWidth > 0 ? QMAX( 1, zoomHandler->zoomItX( ptWidth ) /*applies qRound*/ ) : minborder;
00161 }
00162 
00163 int KoBorder::zoomWidthY( double ptWidth, KoZoomHandler * zoomHandler, int minborder )
00164 {
00165     // If a border was set, then zoom it and apply a minimum of 1, so that it's always visible.
00166     // If no border was set, apply minborder ( 0 for paragraphs, 1 for frames )
00167     return ptWidth > 0 ? QMAX( 1, zoomHandler->zoomItY( ptWidth ) /*applies qRound*/ ) : minborder;
00168 }
00169 
00170 void KoBorder::drawBorders( QPainter& painter, KoZoomHandler * zoomHandler, QRect rect, KoBorder leftBorder, KoBorder rightBorder, KoBorder topBorder, KoBorder bottomBorder, int minborder, QPen defaultPen )
00171 {
00172     int topBorderWidth = zoomWidthY( topBorder.width(), zoomHandler, minborder );
00173     int bottomBorderWidth = zoomWidthY( bottomBorder.width(), zoomHandler, minborder );
00174     int leftBorderWidth = zoomWidthX( leftBorder.width(), zoomHandler, minborder );
00175     int rightBorderWidth = zoomWidthX( rightBorder.width(), zoomHandler, minborder );
00176 
00177     int topBorderPenWidth = zoomWidthY( topBorder.penWidth(), zoomHandler, minborder );
00178     int bottomBorderPenWidth = zoomWidthY( bottomBorder.penWidth(), zoomHandler, minborder );
00179     int leftBorderPenWidth = zoomWidthX( leftBorder.penWidth(), zoomHandler, minborder );
00180     int rightBorderPenWidth = zoomWidthX( rightBorder.penWidth(), zoomHandler, minborder );
00181 
00182     // Wide pen don't draw the last pixel, so add one to the bottom and right coords
00183     int lastPixelAdj = 1;
00184 
00185     //kdDebug(32500) << "KoBorder::drawBorders widths: top=" << topBorderWidth << " bottom=" << bottomBorderWidth
00186     //               << " left=" << leftBorderWidth << " right=" << rightBorderWidth << endl;
00187 
00188     //kdDebug(32500) << "                   penWidths: top=" << topBorderPenWidth << " bottom=" << bottomBorderPenWidth
00189     //               << " left=" << leftBorderPenWidth << " right=" << rightBorderPenWidth << endl;
00190 
00191     QColor defaultColor = KoTextFormat::defaultTextColor( &painter );
00192 
00193     if ( topBorderWidth > 0 )
00194     {
00195         if ( topBorder.penWidth() > 0 )
00196             painter.setPen( KoBorder::borderPen( topBorder, topBorderPenWidth, defaultColor ) );
00197         else
00198             painter.setPen( defaultPen );
00199         int y = rect.top() - topBorderWidth + topBorderPenWidth/2;
00200         if ( topBorder.style==KoBorder::DOUBLE_LINE)
00201         {
00202             painter.drawLine( rect.left()-leftBorderWidth, y, rect.right()+2*(rightBorderPenWidth+lastPixelAdj), y );
00203             y += topBorderPenWidth + 1;
00204             painter.drawLine( rect.left()-leftBorderPenWidth, y, rect.right()+rightBorderPenWidth+lastPixelAdj, y );
00205         }
00206         else
00207         {
00208             painter.drawLine( rect.left()-leftBorderWidth, y, rect.right()+rightBorderWidth+lastPixelAdj, y );
00209         }
00210     }
00211     if ( bottomBorderWidth > 0 )
00212     {
00213         if ( bottomBorder.penWidth() > 0 )
00214             painter.setPen( KoBorder::borderPen( bottomBorder, bottomBorderPenWidth, defaultColor ) );
00215         else
00216             painter.setPen( defaultPen );
00217     //kdDebug(32500) << "bottomBorderWidth=" << bottomBorderWidth << " bottomBorderWidth/2=" << (int)bottomBorderWidth/2 << endl;
00218         int y = rect.bottom() + bottomBorderPenWidth/2 + 1;
00219     //kdDebug(32500) << "   -> bottom=" << rect.bottom() << " y=" << y << endl;
00220         if ( bottomBorder.style==KoBorder::DOUBLE_LINE)
00221         {
00222             painter.drawLine( rect.left()-leftBorderPenWidth, y, rect.right()+rightBorderPenWidth+lastPixelAdj, y );
00223             y += bottomBorderPenWidth + 1;
00224             painter.drawLine( rect.left()-leftBorderWidth, y, rect.right()+2*(rightBorderPenWidth+lastPixelAdj), y );
00225         }
00226         else
00227         {
00228             painter.drawLine( rect.left()-leftBorderWidth, y, rect.right()+rightBorderWidth+lastPixelAdj, y );
00229         }
00230     }
00231     if ( leftBorderWidth > 0 )
00232     {
00233         if ( leftBorder.penWidth() > 0 )
00234             painter.setPen( KoBorder::borderPen( leftBorder, leftBorderPenWidth, defaultColor ) );
00235         else
00236             painter.setPen( defaultPen );
00237         int x = rect.left() - leftBorderWidth + leftBorderPenWidth/2;
00238         if ( leftBorder.style==KoBorder::DOUBLE_LINE)
00239         {
00240             painter.drawLine( x, rect.top()-topBorderWidth, x, rect.bottom()+2*(bottomBorderPenWidth+lastPixelAdj) );
00241             x += leftBorderPenWidth + 1;
00242             painter.drawLine( x, rect.top()-topBorderPenWidth, x, rect.bottom()+bottomBorderPenWidth+lastPixelAdj );
00243         }
00244         else
00245         {
00246             int yTop = rect.top() - topBorderWidth;
00247             int yBottom = rect.bottom() + bottomBorderWidth;
00248             /*kdDebug(32500) << " pen=" << painter.pen() << " rect=" << rect << " topBorderWidth=" << topBorderWidth
00249                            << " painting from " << x << "," << yTop
00250                            << " to " << x << "," << yBottom << endl;*/
00251             painter.drawLine( x, yTop, x, yBottom+lastPixelAdj );
00252         }
00253     }
00254     if ( rightBorderWidth > 0 )
00255     {
00256         if ( rightBorder.penWidth() > 0 )
00257             painter.setPen( KoBorder::borderPen( rightBorder, rightBorderPenWidth, defaultColor ) );
00258         else
00259             painter.setPen( defaultPen );
00260         int x = rect.right() + rightBorderPenWidth/2 + 1;
00261         //kdDebug(32500) << "Drawing right border at x=" << x << endl;
00262         if ( rightBorder.style==KoBorder::DOUBLE_LINE)
00263         {
00264             painter.drawLine( x, rect.top()-topBorderPenWidth, x, rect.bottom()+bottomBorderPenWidth+lastPixelAdj );
00265             x += rightBorderPenWidth + 1;
00266             painter.drawLine( x, rect.top()-topBorderWidth, x, rect.bottom()+2*(bottomBorderPenWidth+lastPixelAdj) );
00267 
00268         }
00269         else
00270         {
00271             painter.drawLine( x, rect.top()-topBorderWidth, x, rect.bottom()+bottomBorderWidth+lastPixelAdj );
00272         }
00273     }
00274 }
KDE Logo
This file is part of the documentation for lib Library Version 1.3.5.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Mar 20 14:25:24 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003