Avogadro  1.1.0
Public Types | Public Slots | Signals | Public Member Functions | Protected Member Functions | Properties
Avogadro::PlotWidget Class Reference

Generic data plotting widget. More...

#include <avogadro/plotwidget.h>

Inheritance diagram for Avogadro::PlotWidget:
Inheritance graph
[legend]

List of all members.

Public Types

enum  Axis { LeftAxis = 0, BottomAxis, RightAxis, TopAxis }
enum  Direction {
  None = 0, Left, Down, Right,
  Up
}

Public Slots

void setShowGrid (bool show)
void setObjectToolTipShown (bool show)
bool saveImage (const QString &filename, double width, double height, double dpi, bool optimizeFontSize)
void selectPoint (PlotPoint *point)
void selectPoints (const QList< PlotPoint * > &points)
void clearAndSelectPoint (PlotPoint *point)
void clearAndSelectPoints (const QList< PlotPoint * > &points)
void clearSelection ()
void setPointFollowMouse (bool b)
void setJailedInDefaults (bool b)
void setLabelShiftDirection (Direction dir, float priority=2.0)

Signals

void pointClicked (double x, double y)
void pointClicked (const QList< PlotPoint * > &pl)
void pointClicked (PlotPoint *)
void mouseOverPoint (double x, double y)

Public Member Functions

 PlotWidget (QWidget *parent=0)
 Constructor.
virtual ~PlotWidget ()
 Destructor.
virtual QSize minimumSizeHint () const
virtual QSize sizeHint () const
void setLimits (double x1, double x2, double y1, double y2)
void setLimits (QRectF &rect)
void scaleLimits (PlotObject *po=NULL)
void setDefaultLimits (double x1, double x2, double y1, double y2)
void setDefaultLimits (QRectF &rect)
void unsetDefaultLimits ()
void setSecondaryLimits (double x1, double x2, double y1, double y2)
 Reset the secondary data limits, which control the values displayed along the top and right axes.
void clearSecondaryLimits ()
QRectF dataRect () const
QRectF defaultDataRect () const
QRectF secondaryDataRect () const
QRect pixRect () const
void addPlotObject (PlotObject *object)
void addPlotObjects (const QList< PlotObject * > &objects)
QList< PlotObject * > plotObjects () const
void removeAllPlotObjects ()
void resetPlotMask ()
void resetPlot ()
void replacePlotObject (int i, PlotObject *o)
QColor backgroundColor () const
QColor foregroundColor () const
QColor gridColor () const
void setBackgroundColor (const QColor &bg)
void setForegroundColor (const QColor &fg)
void setGridColor (const QColor &gc)
void setFontSize (int pointSize)
void setFont (QFont font)
QFont getFont ()
bool isGridShown () const
bool isObjectToolTipShown () const
bool antialiasing () const
void setAntialiasing (bool b)
int leftPadding () const
int rightPadding () const
int topPadding () const
int bottomPadding () const
void setLeftPadding (int padding)
 Set the number of pixels to the left of the plot area. Set this to -1 to revert to automatic determination of padding values.
void setRightPadding (int padding)
 Set the number of pixels to the right of the plot area. Set this to -1 to revert to automatic determination of padding values.
void setTopPadding (int padding)
 Set the number of pixels above the plot area. Set this to -1 to revert to automatic determination of padding values.
void setBottomPadding (int padding)
 Set the number of pixels below the plot area. Set this to -1 to revert to automatic determination of padding values.
void setDefaultPaddings ()
 Revert all four padding values to -1, so that they will be automatically determined.
QPointF mapToWidget (const QPointF &p) const
 Map a coordinate.
QPointF mapToData (const QPointF &p) const
 Map a coordinate.
QPointF mapFrameToData (const QPointF &p) const
 Map a coordinate.
void maskRect (const QRectF &r, float value=1.0)
void maskAlongLine (const QPointF &p1, const QPointF &p2, float value=1.0)
void placeLabel (QPainter *painter, PlotPoint *pp)
PlotAxisaxis (Axis type)
const PlotAxisaxis (Axis type) const

Protected Member Functions

virtual bool event (QEvent *)
virtual void mouseMoveEvent (QMouseEvent *event)
virtual void mousePressEvent (QMouseEvent *event)
virtual void mouseDoubleClickEvent (QMouseEvent *event)
virtual void mouseReleaseEvent (QMouseEvent *event)
virtual void wheelEvent (QWheelEvent *event)
virtual void paintEvent (QPaintEvent *)
virtual void resizeEvent (QResizeEvent *)
virtual void drawAxes (QPainter *p)
void setPixRect ()
QList< PlotPoint * > pointsUnderPoint (const QPoint &p) const
PlotPointpointNearestPoint (const QPoint &p) const

Properties

int leftPadding
int rightPadding
int topPadding
int bottomPadding
QColor backgroundColor
QColor foregroundColor
QColor gridColor
bool grid
bool objectToolTip

Detailed Description

Generic data plotting widget.

 *Widget for drawing plots. The basic idea behind PlotWidget is that
 *you don't have to worry about any transformation from your data's
 *natural units to screen pixel coordinates; this is handled internally
 *by the widget.

 *Data to be plotted are represented by one or more instances of
 *PlotObject.  PlotObject contains a list of QPointFs to be plotted

(again, in the data's natural units), as well as information about how *the data are to be rendered in the plot (i.e., as separate points or *connected by lines? With what color and point style? etc). See *PlotObject for more information.

*PlotWidget automatically adds axis labels with tickmarks and tick *labels. These are encapsulated in the PlotAxis class. All you have *to do is set the limits of the plotting area in data units, and *PlotWidget wil figure out the optimal positions and labels for the *tickmarks on the axes.

*Use setDefaultLimits() to set the limits that a double click will restore. *This will also call setLimits() to update the plot. Not using *setDefaultLimits() or calling unsetDefaultLimits() will disable double *click.restoration.

*Example of usage:

     PlotWidget *kpw = new PlotWidget( parent );
     // setting our limits for the plot
     kpw->setDefaultLimits( 1.0, 5.0, 1.0, 25.0 );

     // creating a plot object whose points are connected by red lines ...
     PlotObject *kpo = new PlotObject( Qt::red, PlotObject::Lines );
     // ... adding some points to it ...
     for ( float x = 1.0; x <= 5.0; x += 0.1 )
     kpo->addPoint( x, x*x );

     // ... and adding the object to the plot widget
     kpw->addPlotObject( kpo );
Note:
PlotWidget will take care of the objects added to it, so when *clearing the objects list (eg with removeAllPlotObjects()) any previous *reference to a PlotObject already added to a PlotWidget will be invalid.
Author:
Jason Harris
Version:
1.1

Member Enumeration Documentation

The four types of plot axes.

Enumerator:
LeftAxis 

the left axis

BottomAxis 

the bottom axis

RightAxis 

the right axis

TopAxis 

the top axis

The four directions for label shift.


Constructor & Destructor Documentation

Avogadro::PlotWidget::PlotWidget ( QWidget parent = 0) [explicit]

Constructor.

Parameters:
parentthe parent widget

References minimumSizeHint(), QWidget::resize(), QWidget::setAttribute(), setDefaultPaddings(), and QWidget::setMinimumSize().


Member Function Documentation

Add an item to the list of PlotObjects to be plotted.

Note:
do not use this multiple time if many objects have to be added, addPlotObjects() is strongly suggested in this case
Parameters:
objectthe PlotObject to be added

References QWidget::update().

void Avogadro::PlotWidget::addPlotObjects ( const QList< PlotObject * > &  objects)

Add more than one PlotObject at one time.

Parameters:
objectsthe list of PlotObjects to be added

References QWidget::update().

Returns:
whether the antialiasing is active Antialiasing is not active by default.
Returns:
the axis of the specified type, or 0 if no axis has been set.
See also:
Axis

References QHash::value().

Referenced by clearSecondaryLimits(), drawAxes(), resetPlot(), saveImage(), and setSecondaryLimits().

const PlotAxis * Avogadro::PlotWidget::axis ( Axis  type) const
Returns:
the axis of the specified type, or 0 if no axis has been set.
See also:
Axis

References QHash::value().

QColor Avogadro::PlotWidget::backgroundColor ( ) const
Returns:
the background color of the plot.

The default color is black.

Referenced by paintEvent(), and saveImage().

int Avogadro::PlotWidget::bottomPadding ( ) const
Returns:
the number of pixels below the plot area. Padding values are set to -1 by default; if unchanged, this function will try to guess a good value, based on whether ticklabels and/or axis labels are to be drawn.

Referenced by drawAxes(), and setPixRect().

void Avogadro::PlotWidget::clearAndSelectPoints ( const QList< PlotPoint * > &  points) [slot]

Just like selectPoints(), but clears selection first.

Parameters:
pointsQList of PlotPoints to be highlighted
See also:
selectPoint() clearAndSelectPoint() selectPoints() clearSelection() setPointFollowMouse()

References QList::at(), clearSelection(), QList::size(), and QWidget::update().

Unset the secondary limits, so the top and right axes show the same tickmarks as the bottom and left axes (no tickmark labels will be drawn for the top and right axes in this case)

References axis(), RightAxis, Avogadro::PlotAxis::setTickMarks(), TopAxis, and QWidget::update().

Referenced by resetPlot().

Returns:
the rectangle representing the boundaries of the current plot, in natural data units.
See also:
setLimits() defaultDataRect()

Referenced by mouseMoveEvent(), mouseReleaseEvent(), and wheelEvent().

Returns:
the rectangle representing the boundaries of the default plot, in natural data units.
See also:
setLimits() dataRect()

Referenced by mouseDoubleClickEvent(), mouseMoveEvent(), setJailedInDefaults(), and unsetDefaultLimits().

void Avogadro::PlotWidget::drawAxes ( QPainter p) [protected, virtual]
bool Avogadro::PlotWidget::event ( QEvent e) [protected, virtual]
QColor Avogadro::PlotWidget::foregroundColor ( ) const
Returns:
the foreground color, used for axes, tickmarks and associated labels.

The default color is white.

Referenced by drawAxes(), and saveImage().

Get the current font

QColor Avogadro::PlotWidget::gridColor ( ) const
Returns:
the grid color.

The default color is gray.

Referenced by drawAxes(), and saveImage().

Returns:
whether the grid lines are shown Grid lines are not shown by default.
Returns:
whether the tooltip for the point objects is shown. Tooltips are enabled by default.
int Avogadro::PlotWidget::leftPadding ( ) const
Returns:
the number of pixels to the left of the plot area.

Padding values are set to -1 by default; if unchanged, this function will try to guess a good value, based on whether ticklabels and/or axis labels need to be drawn.

Referenced by drawAxes(), event(), mapFrameToData(), paintEvent(), and setPixRect().

Map a coordinate.

Parameters:
pfrom the frame's pixel rect to the data rect. Used mainly when drawing.
pthe point to be converted, in pixels
Returns:
the coordinate in natural data units

References leftPadding(), topPadding(), QPointF::x(), and QPointF::y().

Referenced by mouseMoveEvent(), mousePressEvent(), and mouseReleaseEvent().

Map a coordinate.

Parameters:
pfrom the physical pixel rect to the data rect. Used mainly when drawing.
pthe point to be converted, in pixels
Returns:
the coordinate in natural data units

References QPointF::x(), and QPointF::y().

Map a coordinate.

Parameters:
pfrom the data rect to the physical pixel rect. Used mainly when drawing.
pthe point to be converted, in natural data units
Returns:
the coordinate in the pixel coordinate system

References QPointF::x(), and QPointF::y().

Referenced by Avogadro::PlotObject::draw(), mouseMoveEvent(), mousePressEvent(), placeLabel(), pointNearestPoint(), and pointsUnderPoint().

void Avogadro::PlotWidget::maskAlongLine ( const QPointF p1,
const QPointF p2,
float  value = 1.0 
)

Indicate that object labels should try to avoid the line joining the two given points (in pixel coordinates).

Note:
You should not normally call this function directly. It is called by PlotObject when lines are drawn in the plot.
Parameters:
p1the starting point for the line
p2the ending point for the line
valueAllows you to determine how strongly the line should be avoided. Larger values are avoided more strongly.

References QColor::red(), QColor::rgba(), QColor::setAlpha(), QColor::setRed(), QPointF::toPoint(), QWidget::x(), QPointF::x(), QWidget::y(), and QPointF::y().

Referenced by Avogadro::PlotObject::draw().

void Avogadro::PlotWidget::maskRect ( const QRectF r,
float  value = 1.0 
)

Indicate that object labels should try to avoid the given rectangle in the plot. The rectangle is in pixel coordinates.

Note:
You should not normally call this function directly. It is called by PlotObject when points, bars and labels are drawn.
Parameters:
rthe rectangle defining the region in the plot that text labels should avoid (in pixel coordinates)
valueAllows you to determine how strongly the rectangle should be avoided. Larger values are avoided more strongly.

References QRect::bottom(), QRect::intersected(), QRect::left(), QColor::red(), QColor::rgba(), QRect::right(), QColor::setAlpha(), QColor::setRed(), QRect::top(), and QRectF::toRect().

Referenced by Avogadro::PlotObject::draw(), and placeLabel().

Returns:
suggested minimum size for the plot widget

Reimplemented from QWidget.

Referenced by PlotWidget().

void Avogadro::PlotWidget::mouseDoubleClickEvent ( QMouseEvent event) [protected, virtual]
void Avogadro::PlotWidget::mouseMoveEvent ( QMouseEvent event) [protected, virtual]
void Avogadro::PlotWidget::mouseOverPoint ( double  x,
double  y 
) [signal]

Get the point in data units from a mouseover. Use setMouseTracking(true) to enable.

Parameters:
xX value
yY value

Referenced by mouseMoveEvent().

void Avogadro::PlotWidget::mousePressEvent ( QMouseEvent event) [protected, virtual]
void Avogadro::PlotWidget::mouseReleaseEvent ( QMouseEvent event) [protected, virtual]
void Avogadro::PlotWidget::paintEvent ( QPaintEvent e) [protected, virtual]
Returns:
the rectangle representing the boundaries of the current plot, in screen pixel units.

Referenced by Avogadro::PlotObject::draw(), drawAxes(), mouseMoveEvent(), resetPlotMask(), and wheelEvent().

void Avogadro::PlotWidget::placeLabel ( QPainter painter,
PlotPoint pp 
)

Place an object label optimally in the plot. This function will attempt to place the label as close as it can to the point to which the label belongs, while avoiding overlap with regions of the plot that have been masked.

Note:
You should not normally call this function directly. It is called internally in PlotObject::draw().
Parameters:
painterPointer to the painter on which to draw the label
pppointer to the PlotPoint whose label is to be drawn.

References QList::append(), QList::contains(), QPainter::device(), QPainter::drawLine(), QPainter::drawRoundRect(), QPainter::drawText(), QPainter::font(), Avogadro::PlotPoint::label(), QRectF::left(), mapToWidget(), maskRect(), QRectF::moveLeft(), QRectF::moveTop(), QPainter::pen(), QWidget::pos(), Avogadro::PlotPoint::position(), QPainter::setBrush(), QPen::setColor(), QPainter::setPen(), QList::size(), QRectF::top(), QPointF::toPoint(), QRectF::width(), QPointF::x(), and QPointF::y().

Referenced by Avogadro::PlotObject::draw().

Returns:
the current list of plot objects
void Avogadro::PlotWidget::pointClicked ( double  x,
double  y 
) [signal]

Get the point in data units selected by a left click.

Parameters:
xX value
yY value

Referenced by mousePressEvent().

void Avogadro::PlotWidget::pointClicked ( const QList< PlotPoint * > &  pl) [signal]

Get a list of points in data units within 4 pixels of a left click.

Parameters:
plList of PlotPoints

Get the nearest point to a left click event.

Parameters:
pPlotPoint nearest click location
PlotPoint * Avogadro::PlotWidget::pointNearestPoint ( const QPoint p) const [protected]
Returns:
the nearest point in the plot to p
Parameters:
pThe screen position from which to check for plot points.

References mapToWidget(), Avogadro::PlotObject::points(), Avogadro::PlotPoint::position(), QWidget::rect(), and QRect::width().

Referenced by mouseMoveEvent(), and mousePressEvent().

QList< PlotPoint * > Avogadro::PlotWidget::pointsUnderPoint ( const QPoint p) const [protected]
Returns:
a list of points in the plot which are within 4 pixels of the screen position given as an argument.
Parameters:
pThe screen position from which to check for plot points.

References mapToWidget(), Avogadro::PlotObject::points(), and Avogadro::PlotPoint::position().

Referenced by event(), and mousePressEvent().

Remove and delete all items from the list of PlotObjects

References QWidget::update().

Replace an item in the PlotObject list.

Parameters:
ithe index of the item to be replaced
opointer to the replacement PlotObject

References QWidget::update().

Clear the object list, reset the data limits, and remove axis labels

References axis(), BottomAxis, clearSecondaryLimits(), LeftAxis, resetPlotMask(), RightAxis, Avogadro::PlotAxis::setLabel(), Avogadro::PlotAxis::setTickLabelsShown(), and TopAxis.

Reset the mask used for non-overlapping labels so that all regions of the plot area are considered empty.

References pixRect(), QColor::rgb(), QColor::setAlpha(), and QWidget::size().

Referenced by paintEvent(), resetPlot(), and resizeEvent().

void Avogadro::PlotWidget::resizeEvent ( QResizeEvent e) [protected, virtual]

The resize event handler, called when the widget is resized.

Reimplemented from QWidget.

References resetPlotMask(), and setPixRect().

int Avogadro::PlotWidget::rightPadding ( ) const
Returns:
the number of pixels to the right of the plot area. Padding values are set to -1 by default; if unchanged, this function will try to guess a good value, based on whether ticklabels and/or axis labels are to be drawn.

Referenced by setPixRect().

bool Avogadro::PlotWidget::saveImage ( const QString filename,
double  width,
double  height,
double  dpi,
bool  optimizeFontSize 
) [slot]

Determine the limits of the plot by examining the PlotPoints. Omit PlotPoint *po to examine all PlotObjects.

Parameters:
pothe PlotObject to be used for scaling. Omit to look at all PlotObjects.
See also:
setLimits()

References QList::first(), QList::isEmpty(), Avogadro::PlotObject::points(), setDefaultLimits(), Avogadro::PlotPoint::x(), and Avogadro::PlotPoint::y().

Returns:
the rectangle representing the boundaries of the secondary data limits, if they have been set. Otherwise, this function behaves the same as dataRect().
See also:
setSecondaryLimits()

Referenced by drawAxes(), and saveImage().

void Avogadro::PlotWidget::selectPoint ( PlotPoint point) [slot]

Mark the indicated point with a yellow circle.

Parameters:
pointPlotPoint to be highlighted
See also:
clearAndSelectPoint() selectPoints() clearAndSelectPoints() clearSelection() setPointFollowMouse()

References QWidget::update(), Avogadro::PlotPoint::x(), and Avogadro::PlotPoint::y().

void Avogadro::PlotWidget::selectPoints ( const QList< PlotPoint * > &  points) [slot]

Mark the indicated points with yellow circles.

Parameters:
pointsQList of PlotPoints to be highlighted
See also:
selectPoint() clearAndSelectPoint() clearAndSelectPoints() clearSelection() setPointFollowMouse()

References QList::at(), QList::size(), and QWidget::update().

Toggle antialiased drawing.

Parameters:
bif true, the plot graphics will be antialiased.

References QWidget::update().

Set the background color

Parameters:
bgthe new background color

Reimplemented from QWidget.

References QWidget::update().

void Avogadro::PlotWidget::setDefaultLimits ( double  x1,
double  x2,
double  y1,
double  y2 
)

Set the default data limits for the plot. These are used for restoring the limits to a "normal" setting when the widget is double clicked. Calling this will also update the plot to the new limits specified.

Parameters:
x1the minimum X value in data units
x2the maximum X value in data units
y1the minimum Y value in data units
y2the maximum Y value in data units
See also:
setLimits() unsetDefaultLimits()

References setLimits().

Referenced by scaleLimits().

Set the font

Parameters:
fontthe new font as QFont

References QWidget::font(), and QWidget::update().

void Avogadro::PlotWidget::setFontSize ( int  pointSize)

Set the font size

Parameters:
pointSizethe new font size in pt

Set the foreground color

Parameters:
fgthe new foreground color

References QWidget::update().

Set the grid color

Parameters:
gcthe new grid color

References QWidget::update().

Don't permit moving away from default limits

References defaultDataRect(), QRectF::height(), setLimits(), QRectF::width(), QRectF::x(), and QRectF::y().

void Avogadro::PlotWidget::setLimits ( double  x1,
double  x2,
double  y1,
double  y2 
)

Set new data limits for the plot.

Parameters:
x1the minimum X value in data units
x2the maximum X value in data units
y1the minimum Y value in data units
y2the maximum Y value in data units
See also:
scaleLimits() setDefaultLimits()

References QWidget::update().

Referenced by mouseDoubleClickEvent(), mouseMoveEvent(), mouseReleaseEvent(), setDefaultLimits(), setJailedInDefaults(), and wheelEvent().

Set new data limits for the plot.

Parameters:
rectrectangle containing all possible values of X and Y in data units
See also:
scaleLimits() setDefaultLimits()

References QWidget::update().

void Avogadro::PlotWidget::setObjectToolTipShown ( bool  show) [slot]

Toggle the display of a tooltip for point objects.

Parameters:
showwhether show the tooltip.
See also:
isObjectToolTipShown()

References QWidget::show().

void Avogadro::PlotWidget::setPixRect ( ) [protected]

Synchronize the PixRect with the current widget size and padding settings.

References bottomPadding(), QWidget::contentsRect(), QRect::height(), leftPadding(), rightPadding(), topPadding(), and QRect::width().

Referenced by paintEvent(), and resizeEvent().

Highlight the point nearest the mouse with a blue circle.

See also:
selectPoint() clearAndSelectPoint() selectPoints() clearAndSelectPoints() clearSelection()

References QWidget::setMouseTracking(), and QWidget::update().

void Avogadro::PlotWidget::setSecondaryLimits ( double  x1,
double  x2,
double  y1,
double  y2 
)

Reset the secondary data limits, which control the values displayed along the top and right axes.

All data points are plotted using the coordinates defined by setLimits(), so this function is only useful for showing alternate tickmark labels along the top and right edges. For example, if you were plotting temperature on the X-axis, you could use Centigrade units for the primary (bottom) axis, using setLimits( 0.0, 100.0, 0.0, 1.0 ). If you also wanted to show Farenheit units along the secondary (top) axis, you would additionally use setSecondaryLimits( 32.0, 212.0, 0.0, 1.0 ). The data added to the plot would have x-coordinates in Centigrade degrees.

Parameters:
x1the minimum X value in secondary data units
x2the maximum X value in secondary data units
y1the minimum Y value in secondary data units
y2the maximum Y value in secondary data units
See also:
setLimits()

References axis(), RightAxis, Avogadro::PlotAxis::setTickMarks(), TopAxis, and QWidget::update().

void Avogadro::PlotWidget::setShowGrid ( bool  show) [slot]

Toggle whether grid lines are drawn at major tickmarks.

Parameters:
showif true, grid lines will be drawn.
See also:
isGridShown()

References QWidget::show(), and QWidget::update().

Returns:
suggested size for the plot widget

Reimplemented from QFrame.

References QWidget::size().

int Avogadro::PlotWidget::topPadding ( ) const
Returns:
the number of pixels above the plot area. Padding values are set to -1 by default; if unchanged, this function will try to guess a good value, based on whether ticklabels and/or axis labels are to be drawn.

Referenced by event(), mapFrameToData(), paintEvent(), and setPixRect().

Clears the default limits and disables double click restoration.

See also:
setLimits() setDefaultLimits()

References defaultDataRect().

void Avogadro::PlotWidget::wheelEvent ( QWheelEvent event) [protected, virtual]

The documentation for this class was generated from the following files: