kpresenter

KPrPolygonPreview.cpp

00001 // -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
00002 /* This file is part of the KDE project
00003    Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
00004    Base code from Kontour.
00005    Copyright (C) 1998 Kai-Uwe Sattler (kus@iti.cs.uni-magdeburg.de)
00006    Copyright (C) 2001 Toshitaka Fujioka <fujioka@kde.org>
00007 
00008    This library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public
00010    License as published by the Free Software Foundation; either
00011    version 2 of the License, or (at your option) any later version.
00012 
00013    This library is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public License
00019    along with this library; see the file COPYING.LIB.  If not, write to
00020    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021  * Boston, MA 02110-1301, USA.
00022 */
00023 
00024 #include "KPrPolygonPreview.h"
00025 
00026 #include <qpainter.h>
00027 
00028 #include <math.h>
00029 
00030 KPrPolygonPreview::KPrPolygonPreview( QWidget* parent, const char* name)
00031     : QFrame( parent, name )
00032 {
00033     setFrameStyle( WinPanel | Sunken );
00034     setBackgroundColor( white );
00035 
00036     setMinimumSize( 200, 100 );
00037 }
00038 
00039 
00040 void KPrPolygonPreview::drawContents( QPainter *painter )
00041 {
00042     double angle = 2 * M_PI / nCorners;
00043     double diameter = static_cast<double>( QMAX( width(), height() ) - 10 );
00044     double radius = diameter * 0.5;
00045 
00046     painter->setWindow( qRound( -radius ), qRound( -radius ), qRound( diameter ), qRound( diameter ) );
00047     painter->setViewport( 5, 5, width() - 10, height() - 10 );
00048     painter->setPen( pen );
00049     painter->setBrush( brush );
00050 
00051     QPointArray points( isConcave ? nCorners * 2 : nCorners );
00052     points.setPoint( 0, 0, qRound( -radius ) );
00053 
00054     if ( isConcave ) {
00055         angle = angle / 2.0;
00056         double a = angle;
00057         double r = radius - ( sharpness / 100.0 * radius );
00058         for ( int i = 1; i < nCorners * 2; ++i ) {
00059             double xp, yp;
00060             if ( i % 2 ) {
00061                 xp =  r * sin( a );
00062                 yp = -r * cos( a );
00063             }
00064             else {
00065                 xp = radius * sin( a );
00066                 yp = -radius * cos( a );
00067             }
00068             a += angle;
00069             points.setPoint( i, (int)xp, (int)yp );
00070         }
00071     }
00072     else {
00073         double a = angle;
00074         for ( int i = 1; i < nCorners; ++i ) {
00075             double xp = radius * sin( a );
00076             double yp = -radius * cos( a );
00077             a += angle;
00078             points.setPoint( i, (int)xp, (int)yp );
00079         }
00080     }
00081     painter->drawPolygon( points );
00082 }
00083 
00084 
00085 void KPrPolygonPreview::slotConvexConcave( bool convexConcave )
00086 {
00087     isConcave = convexConcave;
00088     repaint();
00089 }
00090 
00091 
00092 void KPrPolygonPreview::slotConvexPolygon()
00093 {
00094     isConcave = false;
00095     repaint();
00096 }
00097 
00098 
00099 void KPrPolygonPreview::slotConcavePolygon()
00100 {
00101     isConcave = true;
00102     repaint();
00103 }
00104 
00105 
00106 void KPrPolygonPreview::slotCornersValue( int value )
00107 {
00108     nCorners = value;
00109     repaint();
00110 }
00111 
00112 
00113 void KPrPolygonPreview::slotSharpnessValue( int value )
00114 {
00115     sharpness = value;
00116     repaint();
00117 }
00118 
00119 
00120 #include "KPrPolygonPreview.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys