1 /***************************************************************************
2 kplotwidget.h - A generic data-plotting widget
4 begin : Sun 18 May 2003
5 copyright : (C) 2003 by Jason Harris
6 email : kstars@30doradus.org
7 ***************************************************************************/
9 /***************************************************************************
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
16 ***************************************************************************/
18 #ifndef _KPLOTWIDGET_H_
19 #define _KPLOTWIDGET_H_
22 #include "kplotobject.h"
24 #define BIGTICKSIZE 10
25 #define SMALLTICKSIZE 4
33 *@short Genric data plotting widget.
36 *Widget for drawing plots. Includes adjustable axes with
37 *tickmarks and labels, and a list of KPlotObjects to be drawn.
40 class KDE_EXPORT KPlotWidget
: public QWidget
{
43 /**@short Constructor. Sets the primary x and y limits in data units.
44 *@param x1 the minimum X value in data units
45 *@param x2 the maximum X value in data units
46 *@param y1 the minimum Y value in data units
47 *@param y2 the maximum Y value in data units
48 *@param parent the parent widget
49 *@param name name label for the KPlotWidget
51 KPlotWidget( double x1
=0.0, double x2
=1.0, double y1
=0.0, double y2
=1.0, QWidget
*parent
=0, const char* name
=0 );
55 virtual ~KPlotWidget();
57 /**@short Determine the placement of major and minor tickmarks,
58 *based on the current Limit settings
60 virtual void updateTickmarks();
62 /**@short Reset the data limits.
63 *@param x1 the minimum X value in data units
64 *@param x2 the maximum X value in data units
65 *@param y1 the minimum Y value in data units
66 *@param y2 the maximum Y value in data units
68 virtual void setLimits( double x1
, double x2
, double y1
, double y2
);
70 /**@return the minimum X value in data units*/
71 virtual double x() const { return DataRect
.x(); }
73 /**@return the maximum X value in data units*/
74 virtual double x2() const { return DataRect
.x2(); }
76 /**@return the minimum Y value in data units*/
77 virtual double y() const { return DataRect
.y(); }
79 /**@return the maximum Y value in data units*/
80 virtual double y2() const { return DataRect
.y2(); }
82 /**@return the width in data units*/
83 virtual double dataWidth() const { return DataRect
.width(); }
85 /**@return the height in data units*/
86 virtual double dataHeight() const { return DataRect
.height(); }
88 /**@short Add an item to the list of KPlotObjects to be plotted.
89 *@param o pointer to the KPlotObject to be added
91 virtual void addObject( KPlotObject
*o
) { ObjectList
.append( o
); }
93 /**@short Remove all items from the list of KPlotObjects
95 virtual void clearObjectList() { ObjectList
.clear(); update(); }
97 /**@short replace an item in the KPlotObject list.
98 *@param i the index of th item to be replaced
99 *@param o pointer to the replacement KPlotObject
101 virtual void replaceObject( int i
, KPlotObject
*o
) { ObjectList
.replace( i
, o
); }
103 /**@return the number of KPlotObjects in the list
105 virtual int objectCount() const { return ObjectList
.count(); }
107 /**@return a pointer to a specific KPlotObject in the list
108 *@param i the index of the desired KPlotObject
110 virtual KPlotObject
*object( int i
) { return ObjectList
.at(i
); }
112 /**@return the background color */
113 virtual QColor
bgColor() const { return cBackground
; }
115 /**@return the foreground color */
116 virtual QColor
fgColor() const { return cForeground
; }
118 /**@return the grid color */
119 virtual QColor
gridColor() const { return cGrid
; }
121 /**@short set the background color
122 *@param bg the new background color
124 virtual void setBGColor( const QColor
&bg
) { cBackground
= bg
; setBackgroundColor( bg
); }
126 /**@short set the foreground color
127 *@param bg the new foreground color
129 virtual void setFGColor( const QColor
&fg
) { cForeground
= fg
; }
131 /**@short set the grid color
132 *@param bg the new grid color
134 virtual void setGridColor( const QColor
&gc
) { cGrid
= gc
; }
136 /**@short toggle whether plot axes are drawn.
137 *@param show if true, axes will be drawn.
138 *The axes are just a box outline around the plot.
140 virtual void setShowAxes( bool show
) { ShowAxes
= show
; }
141 /**@short toggle whether tick marks are drawn along the axes.
142 *@param show if true, tick marks will be drawn.
144 virtual void setShowTickMarks( bool show
) { ShowTickMarks
= show
; }
145 /**@short toggle whether tick labels are drawn at major tickmarks.
146 *@param show if true, tick labels will be drawn.
148 virtual void setShowTickLabels( bool show
) { ShowTickLabels
= show
; }
149 /**@short toggle whether grid lines are drawn at major tickmarks.
150 *@param show if true, grid lines will be drawn.
152 virtual void setShowGrid( bool show
) { ShowGrid
= show
; }
154 /**@short set the X-axis label
155 *@param xlabel a short string describing the data plotted on the x-axis.
156 *Set the label to an empty string to omit the axis label.
158 virtual void setXAxisLabel( QString xlabel
) { XAxisLabel
= xlabel
; }
159 /**@short set the Y-axis label
160 *@param ylabel a short string describing the data plotted on the y-axis.
161 *Set the label to an empty string to omit the axis label.
163 virtual void setYAxisLabel( QString ylabel
) { YAxisLabel
= ylabel
; }
165 /**@returns the number of pixels to the left of the plot area. Padding values
166 *are set to -1 by default; if unchanged, this function will try to guess
167 *a good value, based on whether ticklabels and/or axis labels are to be drawn.
169 virtual int leftPadding() const;
170 /**@returns the number of pixels to the right of the plot area.
171 *Padding values are set to -1 by default; if unchanged, this function will try to guess
172 *a good value, based on whether ticklabels and/or axis labels are to be drawn.
174 virtual int rightPadding() const;
175 /**@returns the number of pixels above the plot area.
176 *Padding values are set to -1 by default; if unchanged, this function will try to guess
177 *a good value, based on whether ticklabels and/or axis labels are to be drawn.
179 virtual int topPadding() const;
180 /**@returns the number of pixels below the plot area.
181 *Padding values are set to -1 by default; if unchanged, this function will try to guess
182 *a good value, based on whether ticklabels and/or axis labels are to be drawn.
184 virtual int bottomPadding() const;
186 /**@short set the number of pixels to the left of the plot area.
187 *Set this to -1 to revert to automatic determination of padding values.
189 virtual void setLeftPadding( int pad
) { LeftPadding
= pad
; }
190 /**@short set the number of pixels to the right of the plot area.
191 *Set this to -1 to revert to automatic determination of padding values.
193 virtual void setRightPadding( int pad
) { RightPadding
= pad
; }
194 /**@short set the number of pixels above the plot area.
195 *Set this to -1 to revert to automatic determination of padding values.
197 virtual void setTopPadding( int pad
) { TopPadding
= pad
; }
198 /**@short set the number of pixels below the plot area.
199 *Set this to -1 to revert to automatic determination of padding values.
201 virtual void setBottomPadding( int pad
) { BottomPadding
= pad
; }
203 /**@short revert all four padding values to be automatically determined.
205 virtual void setDefaultPadding() { LeftPadding
= -1; RightPadding
= -1; TopPadding
= -1; BottomPadding
= -1; }
208 /**@short the paint event handler, executed when update() or repaint() is called.
210 virtual void paintEvent( QPaintEvent
* /* e */ );
212 /**@short the resize event handler, called when the widget is resized.
214 virtual void resizeEvent( QResizeEvent
* /* e */ );
216 /**@short draws all of the objects onto the widget. Internal use only; one should simply call update()
217 *to draw the widget with axes and all objects.
218 *@param p pointer to the painter on which we are drawing
220 virtual void drawObjects( QPainter
*p
);
222 /**@short draws the plot axes and axis labels. Internal use only; one should simply call update()
223 *to draw the widget with axes and all objects.
224 *@param p pointer to the painter on which we are drawing
226 virtual void drawBox( QPainter
*p
);
228 /**@short modulus function for double variables.
229 *For example, dmod( 17.0, 7.0 ) returns 3.0
230 *@return the remainder after dividing b into a.
232 double dmod( double a
, double b
);
234 //The distance between major tickmarks in data units
235 double dXtick
, dYtick
;
236 //The number of major and minor tickmarks to be plotted in X and Y
237 int nmajX
, nminX
, nmajY
, nminY
;
239 //Limits of the plot area in pixel units
241 //Limits of the plot area in data units
243 //List of KPlotObjects
244 QPtrList
<KPlotObject
> ObjectList
;
247 QColor cBackground
, cForeground
, cGrid
;
249 bool ShowAxes
, ShowTickMarks
, ShowTickLabels
, ShowGrid
;
252 int LeftPadding
, RightPadding
, TopPadding
, BottomPadding
;
255 QString XAxisLabel
, YAxisLabel
;