kpresenter

KPrPolygonPreview.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 Thorsten Zachmann <zachmann@kde.org>
3 Base code from Kontour.
4 Copyright (C) 1998 Kai-Uwe Sattler (kus@iti.cs.uni-magdeburg.de)
5 Copyright (C) 2001 Toshitaka Fujioka <fujioka@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21*/
22
23#include "KPrPolygonPreview.h"
24
25#include <tqpainter.h>
26
27#include <math.h>
28
29KPrPolygonPreview::KPrPolygonPreview( TQWidget* parent, const char* name)
30 : TQFrame( parent, name )
31{
32 setFrameStyle( WinPanel | Sunken );
33 setBackgroundColor( white );
34
35 setMinimumSize( 200, 100 );
36}
37
38
39void KPrPolygonPreview::drawContents( TQPainter *painter )
40{
41 double angle = 2 * M_PI / nCorners;
42 double diameter = static_cast<double>( TQMAX( width(), height() ) - 10 );
43 double radius = diameter * 0.5;
44
45 painter->setWindow( tqRound( -radius ), tqRound( -radius ), tqRound( diameter ), tqRound( diameter ) );
46 painter->setViewport( 5, 5, width() - 10, height() - 10 );
47 painter->setPen( pen );
48 painter->setBrush( brush );
49
50 TQPointArray points( isConcave ? nCorners * 2 : nCorners );
51 points.setPoint( 0, 0, tqRound( -radius ) );
52
53 if ( isConcave ) {
54 angle = angle / 2.0;
55 double a = angle;
56 double r = radius - ( sharpness / 100.0 * radius );
57 for ( int i = 1; i < nCorners * 2; ++i ) {
58 double xp, yp;
59 if ( i % 2 ) {
60 xp = r * sin( a );
61 yp = -r * cos( a );
62 }
63 else {
64 xp = radius * sin( a );
65 yp = -radius * cos( a );
66 }
67 a += angle;
68 points.setPoint( i, (int)xp, (int)yp );
69 }
70 }
71 else {
72 double a = angle;
73 for ( int i = 1; i < nCorners; ++i ) {
74 double xp = radius * sin( a );
75 double yp = -radius * cos( a );
76 a += angle;
77 points.setPoint( i, (int)xp, (int)yp );
78 }
79 }
80 painter->drawPolygon( points );
81}
82
83
84void KPrPolygonPreview::slotConvexConcave( bool convexConcave )
85{
86 isConcave = convexConcave;
87 repaint();
88}
89
90
91void KPrPolygonPreview::slotConvexPolygon()
92{
93 isConcave = false;
94 repaint();
95}
96
97
98void KPrPolygonPreview::slotConcavePolygon()
99{
100 isConcave = true;
101 repaint();
102}
103
104
105void KPrPolygonPreview::slotCornersValue( int value )
106{
107 nCorners = value;
108 repaint();
109}
110
111
112void KPrPolygonPreview::slotSharpnessValue( int value )
113{
114 sharpness = value;
115 repaint();
116}
117
118
119#include "KPrPolygonPreview.moc"