地面站终端 App
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
0 B

15 years ago
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
* Qwt Widget Library
* Copyright (C) 1997 Josef Wilgen
* Copyright (C) 2002 Uwe Rathmann
*
15 years ago
* This library is free software; you can redistribute it and/or
* modify it under the terms of the Qwt License, Version 1.0
*****************************************************************************/
#include <qpainter.h>
#include <qpen.h>
#include "qwt_painter.h"
#include "qwt_text.h"
#include "qwt_scale_map.h"
#include "qwt_scale_div.h"
#include "qwt_plot_grid.h"
class QwtPlotGrid::PrivateData
{
public:
PrivateData():
xEnabled(true),
yEnabled(true),
xMinEnabled(false),
yMinEnabled(false) {
15 years ago
}
bool xEnabled;
bool yEnabled;
bool xMinEnabled;
bool yMinEnabled;
QwtScaleDiv xScaleDiv;
QwtScaleDiv yScaleDiv;
QPen majPen;
QPen minPen;
};
//! Enables major grid, disables minor grid
QwtPlotGrid::QwtPlotGrid():
QwtPlotItem(QwtText("Grid"))
{
d_data = new PrivateData;
setZ(10.0);
}
//! Destructor
QwtPlotGrid::~QwtPlotGrid()
{
delete d_data;
}
//! \return QwtPlotItem::Rtti_PlotGrid
int QwtPlotGrid::rtti() const
{
return QwtPlotItem::Rtti_PlotGrid;
}
/*!
\brief Enable or disable vertical gridlines
\param tf Enable (true) or disable
\sa Minor gridlines can be enabled or disabled with
enableXMin()
*/
void QwtPlotGrid::enableX(bool tf)
{
if ( d_data->xEnabled != tf ) {
15 years ago
d_data->xEnabled = tf;
itemChanged();
}
}
/*!
\brief Enable or disable horizontal gridlines
\param tf Enable (true) or disable
\sa Minor gridlines can be enabled or disabled with enableYMin()
*/
void QwtPlotGrid::enableY(bool tf)
{
if ( d_data->yEnabled != tf ) {
d_data->yEnabled = tf;
15 years ago
itemChanged();
}
}
/*!
\brief Enable or disable minor vertical gridlines.
\param tf Enable (true) or disable
\sa enableX()
*/
void QwtPlotGrid::enableXMin(bool tf)
{
if ( d_data->xMinEnabled != tf ) {
15 years ago
d_data->xMinEnabled = tf;
itemChanged();
}
}
/*!
\brief Enable or disable minor horizontal gridlines
\param tf Enable (true) or disable
\sa enableY()
*/
void QwtPlotGrid::enableYMin(bool tf)
{
if ( d_data->yMinEnabled != tf ) {
15 years ago
d_data->yMinEnabled = tf;
itemChanged();
}
}
/*!
\brief Assign an x axis scale division
\param scaleDiv Scale division
\warning QwtPlotGrid uses implicit sharing (see Qt Manual) for
the scale divisions.
*/
void QwtPlotGrid::setXDiv(const QwtScaleDiv &scaleDiv)
{
if ( d_data->xScaleDiv != scaleDiv ) {
15 years ago
d_data->xScaleDiv = scaleDiv;
itemChanged();
}
}
/*!
\brief Assign a y axis division
\param sy Scale division
\warning QwtPlotGrid uses implicit sharing (see Qt Manual) for
the scale divisions.
*/
void QwtPlotGrid::setYDiv(const QwtScaleDiv &sy)
{
if ( d_data->yScaleDiv != sy ) {
d_data->yScaleDiv = sy;
15 years ago
itemChanged();
}
}
/*!
\brief Assign a pen for both major and minor gridlines
\param p Pen
\sa setMajPen(), setMinPen()
*/
void QwtPlotGrid::setPen(const QPen &p)
{
if ( d_data->majPen != p || d_data->minPen != p ) {
15 years ago
d_data->majPen = p;
d_data->minPen = p;
itemChanged();
}
}
/*!
\brief Assign a pen for the major gridlines
\param p Pen
\sa majPen(), setMinPen(), setPen()
*/
void QwtPlotGrid::setMajPen(const QPen &p)
{
if ( d_data->majPen != p ) {
15 years ago
d_data->majPen = p;
itemChanged();
}
}
/*!
\brief Assign a pen for the minor gridlines
\param p Pen
*/
void QwtPlotGrid::setMinPen(const QPen &p)
{
if ( d_data->minPen != p ) {
d_data->minPen = p;
15 years ago
itemChanged();
}
}
/*!
\brief Draw the grid
The grid is drawn into the bounding rectangle such that
15 years ago
gridlines begin and end at the rectangle's borders. The X and Y
maps are used to map the scale divisions into the drawing region
screen.
\param painter Painter
\param xMap X axis map
\param yMap Y axis
15 years ago
\param canvasRect Contents rect of the plot canvas
*/
void QwtPlotGrid::draw(QPainter *painter,
const QwtScaleMap &xMap, const QwtScaleMap &yMap,
const QRect &canvasRect) const
15 years ago
{
// draw minor gridlines
painter->setPen(d_data->minPen);
if (d_data->xEnabled && d_data->xMinEnabled) {
drawLines(painter, canvasRect, Qt::Vertical, xMap,
d_data->xScaleDiv.ticks(QwtScaleDiv::MinorTick));
drawLines(painter, canvasRect, Qt::Vertical, xMap,
d_data->xScaleDiv.ticks(QwtScaleDiv::MediumTick));
15 years ago
}
if (d_data->yEnabled && d_data->yMinEnabled) {
drawLines(painter, canvasRect, Qt::Horizontal, yMap,
d_data->yScaleDiv.ticks(QwtScaleDiv::MinorTick));
drawLines(painter, canvasRect, Qt::Horizontal, yMap,
d_data->yScaleDiv.ticks(QwtScaleDiv::MediumTick));
15 years ago
}
// draw major gridlines
painter->setPen(d_data->majPen);
if (d_data->xEnabled) {
15 years ago
drawLines(painter, canvasRect, Qt::Vertical, xMap,
d_data->xScaleDiv.ticks(QwtScaleDiv::MajorTick));
15 years ago
}
if (d_data->yEnabled) {
15 years ago
drawLines(painter, canvasRect, Qt::Horizontal, yMap,
d_data->yScaleDiv.ticks(QwtScaleDiv::MajorTick));
15 years ago
}
}
void QwtPlotGrid::drawLines(QPainter *painter, const QRect &canvasRect,
Qt::Orientation orientation, const QwtScaleMap &scaleMap,
const QwtValueList &values) const
15 years ago
{
const int x1 = canvasRect.left();
const int x2 = canvasRect.right();
const int y1 = canvasRect.top();
const int y2 = canvasRect.bottom();
for (uint i = 0; i < (uint)values.count(); i++) {
15 years ago
const int value = scaleMap.transform(values[i]);
if ( orientation == Qt::Horizontal ) {
15 years ago
if ((value >= y1) && (value <= y2))
QwtPainter::drawLine(painter, x1, value, x2, value);
} else {
15 years ago
if ((value >= x1) && (value <= x2))
QwtPainter::drawLine(painter, value, y1, value, y2);
}
}
}
/*!
\return the pen for the major gridlines
\sa setMajPen(), setMinPen(), setPen()
*/
const QPen &QwtPlotGrid::majPen() const
{
return d_data->majPen;
15 years ago
}
/*!
\return the pen for the minor gridlines
\sa setMinPen(), setMajPen(), setPen()
*/
const QPen &QwtPlotGrid::minPen() const
{
return d_data->minPen;
15 years ago
}
15 years ago
/*!
\return true if vertical gridlines are enabled
\sa enableX()
*/
bool QwtPlotGrid::xEnabled() const
{
return d_data->xEnabled;
15 years ago
}
/*!
\return true if minor vertical gridlines are enabled
\sa enableXMin()
*/
bool QwtPlotGrid::xMinEnabled() const
{
return d_data->xMinEnabled;
15 years ago
}
/*!
\return true if horizontal gridlines are enabled
\sa enableY()
*/
bool QwtPlotGrid::yEnabled() const
{
return d_data->yEnabled;
15 years ago
}
/*!
\return true if minor horizontal gridlines are enabled
\sa enableYMin()
*/
bool QwtPlotGrid::yMinEnabled() const
15 years ago
{
return d_data->yMinEnabled;
15 years ago
}
15 years ago
/*! \return the scale division of the x axis */
const QwtScaleDiv &QwtPlotGrid::xScaleDiv() const
{
return d_data->xScaleDiv;
15 years ago
}
/*! \return the scale division of the y axis */
const QwtScaleDiv &QwtPlotGrid::yScaleDiv() const
{
return d_data->yScaleDiv;
15 years ago
}
15 years ago
void QwtPlotGrid::updateScaleDiv(const QwtScaleDiv& xDiv,
const QwtScaleDiv& yDiv)
15 years ago
{
setXDiv(xDiv);
setYDiv(yDiv);
}