summaryrefslogtreecommitdiffstats
path: root/kig/misc/kigpainter.h
blob: e7f884f4a76753c61c293bfdf9e57829deed299b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/**
 This file is part of Kig, a KDE program for Interactive Geometry...
 Copyright (C) 2002-2003  Dominique Devriese <devriese@kde.org>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 USA
**/


#ifndef KIGPAINTER_H
#define KIGPAINTER_H

#include "coordinate.h"
#include "rect.h"
#include "screeninfo.h"

#include <qpainter.h>
#include <qcolor.h>

#include <vector>

class KigWidget;
class QPaintDevice;
class CoordinateSystem;
class ObjectHierarchy;
class ConicPolarData;
class CubicCartesianData;
class LineData;
class CurveImp;
class KigDocument;
class ObjectHolder;

/**
 * KigPainter is an extended QPainter.
 *
 * Currently the only difference is that it translates coordinates
 * from and to the internal coordinates/ the widget coordinates...
 *
 * It calls KigWidget::appendOverlay() for all of the places it draws in...
 */
class KigPainter
  : public Qt
{
protected:
  // don't blaim me for this mutable hack.  It's TT that hasn't got
  // its consts correctly...
  mutable QPainter mP;

  QColor color;
  PenStyle style;
  int pointstyle;
  int width;
  BrushStyle brushStyle;
  QColor brushColor;

  const KigDocument& mdoc;
  ScreenInfo msi;

  bool mNeedOverlay;
  int overlayenlarge;
public:
  /**
   * construct a new KigPainter:
   * the ScreenInfo is used to map the document coordinates to the
   * widget coordinates.  This is done transparently to the objects.
   * needOverlay sets whether we try to remember the places we're
   * drawing on using the various overlay methods. @see overlay()
   */
  KigPainter( const ScreenInfo& r, QPaintDevice* device, const KigDocument& doc,
              bool needOverlay = true );
  ~KigPainter();

  /**
   * what rect are we drawing on ?
   */
  Rect window();

  QPoint toScreen( const Coordinate p ) const;
  QRect toScreen( const Rect r ) const;
  QRect toScreenEnlarge( const Rect r ) const;
  Coordinate fromScreen( const QPoint& p ) const;
  Rect fromScreen( const QRect& r ) const;

  // colors and stuff...
  void setStyle( const PenStyle c );
  void setColor( const QColor& c );
  /**
   * setting this to -1 means to use the default width for the object
   * being drawn..  a point -> 5, other objects -> 1
   */
  void setWidth( const int c );
  void setPointStyle( const int p );
  void setPen( const QPen& p );
  void setBrushStyle( const BrushStyle c );
  void setBrush( const QBrush& b );
  void setBrushColor( const QColor& c );

  QColor getColor() const;
  bool getNightVision( ) const;

  double pixelWidth();

  /**
   * this is called by some drawing functions that modify the 'entire'
   * screen, i.e. they do so many changes that it's easier to just
   * update the entire screen, or else i have been to lazy to
   * implement an appropriate overlay function ;)
   * it clears mOverlay, and sets it to the entire widget...
   */
  void setWholeWinOverlay();

  /**
   * draw an object ( by calling its draw function.. )
   */
  void drawObject( const ObjectHolder* o, bool sel );
  void drawObjects( const std::vector<ObjectHolder*>& os, bool sel );
  template<typename iter>
  void drawObjects( iter begin, iter end, bool sel )
    {
      for ( ; begin != end; ++begin )
        drawObject( *begin, sel );
    }

  /**
   * draw a generic curve...
   */
  void drawCurve( const CurveImp* curve );

  /**
   * draws text in a standard manner, convenience function...
   */
  void drawTextStd( const QPoint& p, const QString& s );

  /**
   * draws a rect filled up with a pattern of cyan lines...
   */
  void drawFilledRect( const QRect& );

  /**
   * draw a rect..
   */
  void drawRect( const Rect& r );

  /**
   * overload, mainly for drawing the selection rectangle by
   * KigWidget...
   */
  void drawRect( const QRect& r );

  /**
   * draw a circle...
   */
  void drawCircle( const Coordinate& center, const double radius );

  /**
   * draw a segment...
   */
  void drawSegment ( const Coordinate& from, const Coordinate& to );
  void drawSegment( const LineData& d );

  /**
   * draw a ray...
   */
  void drawRay( const Coordinate& a, const Coordinate& b );
  void drawRay( const LineData& d );

  /**
   * draw a line...
   */
  void drawLine ( const Coordinate& p1, const Coordinate& p2 );
  void drawLine( const LineData& d );

  /**
   * draw a point...  This means a single point, as in
   * QPainter::drawPoint(), unlike drawFatPoint()...
   */
  void drawPoint( const Coordinate& p );

  /**
   * draw a thick point..  This is what the user sees when he draws a
   * point.  In fact it isn't a point, but a filled circle of a
   * certain radius...
   */
  void drawFatPoint( const Coordinate& p );

  /**
   * draw a polygon defined by the points in pts...
   */
  void drawPolygon( const std::vector<QPoint>& pts, bool winding = false, int index = 0, int npoints = -1 );
  void drawPolygon( const std::vector<Coordinate>& pts, bool winding = false, int index = 0, int npoints = -1 );

  /**
   * draw an area defined by the points in pts filled with the set
   * color...
   */
  void drawArea( const std::vector<Coordinate>& pts, bool border = true );

  /**
   * draw the angle with center point, with size angle, starting
   * at the angle startAngle..  Angles should be in radians.
   */
  void drawAngle( const Coordinate& point, const double startangle,
                  const double angle );

  /**
   * draw the arc ( a part of a circle ), of the circle with center
   * center, radius radius, with size angle, starting at the angle
   * startAngle..  Angles should be in radians..
   */
  void drawArc( const Coordinate& center, const double radius,
                const double startangle, const double angle );

  /**
   * draw a vector ( with an arrow etc. )
   */

  void drawVector( const Coordinate& a, const Coordinate& b );

  /**
   * draw text...
   * \see QPainter::drawText()
   */
  void drawText( const Rect r, const QString s, int textFlags = 0,
                 int len = -1);
  void drawText( const Coordinate p, const QString s,
                 int textFlags = 0, int len = -1);

  void drawSimpleText( const Coordinate& c, const QString s );
  void drawTextFrame( const Rect& frame, const QString& s, bool needframe );

  const Rect boundingRect( const Rect& r, const QString s,
                            int f = 0, int l = -1 ) const;

  const Rect boundingRect( const Coordinate& c, const QString s,
                            int f = 0, int l = -1 ) const;

  const Rect simpleBoundingRect( const Coordinate& c, const QString s );

  void drawGrid( const CoordinateSystem& c, bool showGrid = true, bool showAxes = true );

  const std::vector<QRect>& overlay() { return mOverlay; }

protected:
  /**
   * adds a number of rects to mOverlay so that the rects entirely
   * contain the circle...
   * \see mOverlay
   */
  void circleOverlay( const Coordinate& centre, double radius );
  // this works recursively...
  void circleOverlayRecurse( const Coordinate& centre, double radius, const Rect& currentRect );

  /**
   * adds some rects to mOverlay, so that they cover the segment p1p2
   * completely...
   * \see Object::getOverlay()
   */
  void segmentOverlay( const Coordinate& p1, const Coordinate& p2 );

  /**
   * ...
   */
  void pointOverlay( const Coordinate& p1 );

  /**
   * ...
   * \see drawText(), QPainter::boundingRect()
   */
  void textOverlay( const QRect& r, const QString s, int textFlags, int len );

  /**
   * the size we want the overlay rects to be...
   */
  double overlayRectSize();

  std::vector<QRect> mOverlay;
};

#endif