📄 qwt_plot.cpp
字号:
{
axisWidget(axisId)->setGeometry(d_data->layout->scaleRect(axisId));
if ( axisId == xBottom || axisId == xTop )
{
QRegion r(d_data->layout->scaleRect(axisId));
if ( axisEnabled(yLeft) )
r = r.subtract(QRegion(d_data->layout->scaleRect(yLeft)));
if ( axisEnabled(yRight) )
r = r.subtract(QRegion(d_data->layout->scaleRect(yRight)));
r.translate(-d_data->layout->scaleRect(axisId).x(),
-d_data->layout->scaleRect(axisId).y());
axisWidget(axisId)->setMask(r);
}
if (!axisWidget(axisId)->isVisible())
axisWidget(axisId)->show();
}
else
axisWidget(axisId)->hide();
}
if ( d_data->legend &&
d_data->layout->legendPosition() != ExternalLegend )
{
if (d_data->legend->itemCount() > 0)
{
d_data->legend->setGeometry(d_data->layout->legendRect());
d_data->legend->show();
}
else
d_data->legend->hide();
}
d_data->canvas->setGeometry(d_data->layout->canvasRect());
}
/*!
Update the focus tab order
The order is changed so that the canvas will be in front of the
first legend item, or behind the last legend item - depending
on the position of the legend.
*/
void QwtPlot::updateTabOrder()
{
#if QT_VERSION >= 0x040000
using namespace Qt; // QWidget::NoFocus/Qt::NoFocus
#else
if ( d_data->canvas->focusPolicy() == NoFocus )
return;
#endif
if ( d_data->legend.isNull()
|| d_data->layout->legendPosition() == ExternalLegend
|| d_data->legend->legendItems().count() == 0 )
{
return;
}
// Depending on the position of the legend the
// tab order will be changed that the canvas is
// next to the last legend item, or before
// the first one.
const bool canvasFirst =
d_data->layout->legendPosition() == QwtPlot::BottomLegend ||
d_data->layout->legendPosition() == QwtPlot::RightLegend;
QWidget *previous = NULL;
QWidget *w;
#if QT_VERSION >= 0x040000
w = d_data->canvas;
while ( ( w = w->nextInFocusChain() ) != d_data->canvas )
#else
if ( focusData() == NULL )
return;
while ( focusData()->next() != d_data->canvas );
while ( (w = focusData()->next()) != d_data->canvas )
#endif
{
bool isLegendItem = false;
if ( w->focusPolicy() != NoFocus
&& w->parent() && w->parent() == d_data->legend->contentsWidget() )
{
isLegendItem = true;
}
if ( canvasFirst )
{
if ( isLegendItem )
break;
previous = w;
}
else
{
if ( isLegendItem )
previous = w;
else
{
if ( previous )
break;
}
}
}
if ( previous && previous != d_data->canvas)
setTabOrder(previous, d_data->canvas);
}
/*!
Redraw the canvas.
\param painter Painter used for drawing
\warning drawCanvas calls drawCanvasItems what is also used
for printing. Applications that like to add individual
plot items better overload QwtPlot::drawCanvasItems
\sa QwtPlot::drawItems
*/
void QwtPlot::drawCanvas(QPainter *painter)
{
QwtScaleMap maps[axisCnt];
for ( int axisId = 0; axisId < axisCnt; axisId++ )
maps[axisId] = canvasMap(axisId);
drawItems(painter,
d_data->canvas->contentsRect(), maps, QwtPlotPrintFilter());
}
/*!
Redraw the canvas items.
\param painter Painter used for drawing
\param rect Bounding rectangle where to paint
\param map QwtPlot::axisCnt maps, mapping between plot and paint device coordinates
\param pfilter Plot print filter
*/
void QwtPlot::drawItems(QPainter *painter, const QRect &rect,
const QwtScaleMap map[axisCnt],
const QwtPlotPrintFilter &pfilter) const
{
painter->save();
const QwtPlotItemList& itmList = itemList();
for ( QwtPlotItemIterator it = itmList.begin();
it != itmList.end(); ++it )
{
QwtPlotItem *item = *it;
if ( item && item->isVisible() )
{
if ( !(pfilter.options() & QwtPlotPrintFilter::PrintGrid)
&& item->rtti() == QwtPlotItem::Rtti_PlotGrid )
{
continue;
}
#if QT_VERSION >= 0x040000
painter->setRenderHint(QPainter::Antialiasing,
item->testRenderHint(QwtPlotItem::RenderAntialiased) );
#endif
item->draw(painter,
map[item->xAxis()], map[item->yAxis()],
rect);
}
}
painter->restore();
}
/*!
\param axisId Axis
\return Map for the axis on the canvas. With this map pixel coordinates can
translated to plot coordinates and vice versa.
\sa QwtScaleMap, QwtPlot::transform, QwtPlot::invTransform
*/
QwtScaleMap QwtPlot::canvasMap(int axisId) const
{
QwtScaleMap map;
if ( !d_data->canvas )
return map;
map.setTransformation(axisScaleEngine(axisId)->transformation());
const QwtScaleDiv *sd = axisScaleDiv(axisId);
map.setScaleInterval(sd->lBound(), sd->hBound());
if ( axisEnabled(axisId) )
{
const QwtScaleWidget *s = axisWidget(axisId);
if ( axisId == yLeft || axisId == yRight )
{
int y = s->y() + s->startBorderDist() - d_data->canvas->y();
int h = s->height() - s->startBorderDist() - s->endBorderDist();
map.setPaintInterval(y + h - 1, y);
}
else
{
int x = s->x() + s->startBorderDist() - d_data->canvas->x();
int w = s->width() - s->startBorderDist() - s->endBorderDist();
map.setPaintInterval(x, x + w - 1);
}
}
else
{
const int margin = plotLayout()->canvasMargin(axisId);
const QRect &canvasRect = d_data->canvas->contentsRect();
if ( axisId == yLeft || axisId == yRight )
{
map.setPaintInterval(canvasRect.bottom() - margin,
canvasRect.top() + margin);
}
else
{
map.setPaintInterval(canvasRect.left() + margin,
canvasRect.right() - margin);
}
}
return map;
}
/*!
Change the margin of the plot. The margin is the space
around all components.
\param margin new margin
\sa QwtPlotLayout::setMargin(), QwtPlot::margin(), QwtPlot::plotLayout()
*/
void QwtPlot::setMargin(int margin)
{
if ( margin < 0 )
margin = 0;
if ( margin != d_data->layout->margin() )
{
d_data->layout->setMargin(margin);
updateLayout();
}
}
/*!
\return margin
\sa QwtPlot::setMargin(), QwtPlotLayout::margin(), QwtPlot::plotLayout()
*/
int QwtPlot::margin() const
{
return d_data->layout->margin();
}
/*!
\brief Change the background of the plotting area
Sets c to QColorGroup::Background of all colorgroups of
the palette of the canvas. Using canvas()->setPalette()
is a more powerful way to set these colors.
\param c new background color
*/
void QwtPlot::setCanvasBackground(const QColor &c)
{
QPalette p = d_data->canvas->palette();
for ( int i = 0; i < QPalette::NColorGroups; i++ )
{
#if QT_VERSION < 0x040000
p.setColor((QPalette::ColorGroup)i, QColorGroup::Background, c);
#else
p.setColor((QPalette::ColorGroup)i, QPalette::Background, c);
#endif
}
canvas()->setPalette(p);
}
/*!
Nothing else than: canvas()->palette().color(
QPalette::Normal, QColorGroup::Background);
\return the background color of the plotting area.
*/
const QColor & QwtPlot::canvasBackground() const
{
#if QT_VERSION < 0x040000
return canvas()->palette().color(
QPalette::Normal, QColorGroup::Background);
#else
return canvas()->palette().color(
QPalette::Normal, QPalette::Background);
#endif
}
/*!
\brief Change the border width of the plotting area
Nothing else than canvas()->setLineWidth(w),
left for compatibility only.
\param w new border width
*/
void QwtPlot::setCanvasLineWidth(int w)
{
canvas()->setLineWidth(w);
updateLayout();
}
/*!
Nothing else than: canvas()->lineWidth(),
left for compatibility only.
\return the border width of the plotting area
*/
int QwtPlot::canvasLineWidth() const
{
return canvas()->lineWidth();
}
/*!
\return \c true if the specified axis exists, otherwise \c false
\param axisId axis index
*/
bool QwtPlot::axisValid(int axisId)
{
return ((axisId >= QwtPlot::yLeft) && (axisId < QwtPlot::axisCnt));
}
/*!
Called internally when the legend has been clicked on.
Emits a legendClicked() signal.
*/
void QwtPlot::legendItemClicked()
{
if ( d_data->legend && sender()->isWidgetType() )
{
QwtPlotItem *plotItem = d_data->legend->find((QWidget *)sender());
if ( plotItem )
emit legendClicked(plotItem);
}
}
void QwtPlot::legendItemChecked(bool on)
{
if ( d_data->legend && sender()->isWidgetType() )
{
QwtPlotItem *plotItem = d_data->legend->find((QWidget *)sender());
if ( plotItem )
emit legendChecked(plotItem, on);
}
}
//! Remove all curves and markers
void QwtPlot::clear()
{
detachItems(QwtPlotItem::Rtti_PlotCurve);
detachItems(QwtPlotItem::Rtti_PlotMarker);
}
/*!
\brief Insert a legend
If the position legend is \c QwtPlot::LeftLegend or \c QwtPlot::RightLegend
the legend will be organized in one column from top to down.
Otherwise the legend items will be placed be placed in a table
with a best fit number of columns from left to right.
If pos != QwtPlot::ExternalLegend the plot widget will become
parent of the legend. It will be deleted when the plot is deleted,
or another legend is set with insertLegend().
\param legend Legend
\param pos The legend's position. For top/left position the number
of colums will be limited to 1, otherwise it will be set to
unlimited.
\param ratio Ratio between legend and the bounding rect
of title, canvas and axes. The legend will be shrinked
if it would need more space than the given ratio.
The ratio is limited to ]0.0 .. 1.0]. In case of <= 0.0
it will be reset to the default ratio.
The default vertical/horizontal ratio is 0.33/0.5.
\sa QwtPlotLayout::legendPosition(), QwtPlotLayout::setLegendPosition()
*/
void QwtPlot::insertLegend(QwtLegend *legend,
QwtPlot::LegendPosition pos, double ratio)
{
d_data->layout->setLegendPosition(pos, ratio);
if ( legend != d_data->legend )
{
if ( d_data->legend && d_data->legend->parent() == this )
delete d_data->legend;
d_data->legend = legend;
if ( d_data->legend )
{
if ( pos != ExternalLegend )
{
if ( d_data->legend->parent() != this )
{
#if QT_VERSION < 0x040000
d_data->legend->reparent(this, QPoint(0, 0));
#else
d_data->legend->setParent(this);
#endif
}
}
const QwtPlotItemList& itmList = itemList();
for ( QwtPlotItemIterator it = itmList.begin();
it != itmList.end(); ++it )
{
(*it)->updateLegend(d_data->legend);
}
QLayout *l = d_data->legend->contentsWidget()->layout();
if ( l && l->inherits("QwtDynGridLayout") )
{
QwtDynGridLayout *tl = (QwtDynGridLayout *)l;
switch(d_data->layout->legendPosition())
{
case LeftLegend:
case RightLegend:
tl->setMaxCols(1); // 1 column: align vertical
break;
case TopLegend:
case BottomLegend:
tl->setMaxCols(0); // unlimited
break;
case ExternalLegend:
break;
}
}
}
updateTabOrder();
}
updateLayout();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -