Add files from zip
This commit is contained in:
426
playground/plotmatrix/PlotMatrix.cpp
Normal file
426
playground/plotmatrix/PlotMatrix.cpp
Normal file
@@ -0,0 +1,426 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "PlotMatrix.h"
|
||||
|
||||
#include <QwtPlot>
|
||||
#include <QwtPlotCanvas>
|
||||
#include <QwtScaleWidget>
|
||||
#include <QwtScaleDraw>
|
||||
|
||||
#include <QLayout>
|
||||
|
||||
static void enablePlotAxis( QwtPlot* plot, int axis, bool on )
|
||||
{
|
||||
// when false we still enable the axis to have an effect
|
||||
// of the minimal extent active. Instead we hide all visible
|
||||
// parts and margins/spacings.
|
||||
|
||||
plot->setAxisVisible( axis, true );
|
||||
|
||||
QwtScaleDraw* sd = plot->axisScaleDraw( axis );
|
||||
sd->enableComponent( QwtScaleDraw::Backbone, on );
|
||||
sd->enableComponent( QwtScaleDraw::Ticks, on );
|
||||
sd->enableComponent( QwtScaleDraw::Labels, on );
|
||||
|
||||
QwtScaleWidget* sw = plot->axisWidget( axis );
|
||||
sw->setMargin( on ? 4 : 0 );
|
||||
sw->setSpacing( on ? 20 : 0 );
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
class Plot : public QwtPlot
|
||||
{
|
||||
public:
|
||||
Plot( QWidget* parent = NULL )
|
||||
: QwtPlot( parent )
|
||||
{
|
||||
QwtPlotCanvas* canvas = new QwtPlotCanvas();
|
||||
canvas->setLineWidth( 1 );
|
||||
canvas->setFrameStyle( QFrame::Box | QFrame::Plain );
|
||||
|
||||
setCanvas( canvas );
|
||||
}
|
||||
|
||||
virtual QSize sizeHint() const QWT_OVERRIDE
|
||||
{
|
||||
return minimumSizeHint();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
class PlotMatrix::PrivateData
|
||||
{
|
||||
public:
|
||||
PrivateData():
|
||||
inScaleSync( false )
|
||||
{
|
||||
isAxisEnabled[QwtAxis::XBottom] = true;
|
||||
isAxisEnabled[QwtAxis::XTop] = false;
|
||||
isAxisEnabled[QwtAxis::YLeft] = true;
|
||||
isAxisEnabled[QwtAxis::YRight] = false;
|
||||
}
|
||||
|
||||
bool isAxisEnabled[QwtAxis::AxisPositions];
|
||||
QVector< QwtPlot* > plotWidgets;
|
||||
mutable bool inScaleSync;
|
||||
};
|
||||
|
||||
PlotMatrix::PlotMatrix( int numRows, int numColumns, QWidget* parent )
|
||||
: QFrame( parent )
|
||||
{
|
||||
m_data = new PrivateData();
|
||||
m_data->plotWidgets.resize( numRows * numColumns );
|
||||
|
||||
QGridLayout* layout = new QGridLayout( this );
|
||||
layout->setSpacing( 5 );
|
||||
|
||||
for ( int row = 0; row < numRows; row++ )
|
||||
{
|
||||
for ( int col = 0; col < numColumns; col++ )
|
||||
{
|
||||
QwtPlot* plot = new Plot( this );
|
||||
|
||||
layout->addWidget( plot, row, col );
|
||||
|
||||
for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
|
||||
{
|
||||
connect( plot->axisWidget( axisPos ),
|
||||
SIGNAL(scaleDivChanged()), SLOT(scaleDivChanged()),
|
||||
Qt::QueuedConnection );
|
||||
}
|
||||
m_data->plotWidgets[row * numColumns + col] = plot;
|
||||
}
|
||||
}
|
||||
|
||||
updateLayout();
|
||||
}
|
||||
|
||||
PlotMatrix::~PlotMatrix()
|
||||
{
|
||||
delete m_data;
|
||||
}
|
||||
|
||||
int PlotMatrix::numRows() const
|
||||
{
|
||||
const QGridLayout* l = qobject_cast< const QGridLayout* >( layout() );
|
||||
if ( l )
|
||||
return l->rowCount();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int PlotMatrix::numColumns() const
|
||||
{
|
||||
const QGridLayout* l = qobject_cast< const QGridLayout* >( layout() );
|
||||
if ( l )
|
||||
return l->columnCount();
|
||||
return 0;
|
||||
}
|
||||
|
||||
QwtPlot* PlotMatrix::plotAt( int row, int column )
|
||||
{
|
||||
const int index = row * numColumns() + column;
|
||||
if ( index < m_data->plotWidgets.size() )
|
||||
return m_data->plotWidgets[index];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const QwtPlot* PlotMatrix::plotAt( int row, int column ) const
|
||||
{
|
||||
const int index = row * numColumns() + column;
|
||||
if ( index < m_data->plotWidgets.size() )
|
||||
return m_data->plotWidgets[index];
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void PlotMatrix::setAxisVisible( int axis, bool tf )
|
||||
{
|
||||
if ( QwtAxis::isValid( axis ) )
|
||||
{
|
||||
if ( tf != m_data->isAxisEnabled[axis] )
|
||||
{
|
||||
m_data->isAxisEnabled[axis] = tf;
|
||||
updateLayout();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool PlotMatrix::isAxisVisible( int axis ) const
|
||||
{
|
||||
if ( QwtAxis::isValid( axis ) )
|
||||
return m_data->isAxisEnabled[axis];
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void PlotMatrix::setAxisScale( int axis, int rowOrColumn,
|
||||
double min, double max, double step )
|
||||
{
|
||||
int row = 0;
|
||||
int col = 0;
|
||||
|
||||
if ( QwtAxis::isXAxis( axis ) )
|
||||
col = rowOrColumn;
|
||||
else
|
||||
row = rowOrColumn;
|
||||
|
||||
QwtPlot* plt = plotAt( row, col );
|
||||
if ( plt )
|
||||
{
|
||||
plt->setAxisScale( axis, min, max, step );
|
||||
plt->updateAxes();
|
||||
}
|
||||
}
|
||||
|
||||
void PlotMatrix::scaleDivChanged()
|
||||
{
|
||||
if ( m_data->inScaleSync )
|
||||
return;
|
||||
|
||||
m_data->inScaleSync = true;
|
||||
|
||||
QwtPlot* plt = NULL;
|
||||
int axisId = -1;
|
||||
int rowOrColumn = -1;
|
||||
|
||||
// find the changed axis
|
||||
for ( int row = 0; row < numRows(); row++ )
|
||||
{
|
||||
for ( int col = 0; col < numColumns(); col++ )
|
||||
{
|
||||
QwtPlot* p = plotAt( row, col );
|
||||
if ( p )
|
||||
{
|
||||
for ( int axisPos = 0; axisPos < QwtAxis::AxisPositions; axisPos++ )
|
||||
{
|
||||
if ( p->axisWidget( axisPos ) == sender() )
|
||||
{
|
||||
plt = p;
|
||||
axisId = axisPos;
|
||||
rowOrColumn = QwtAxis::isXAxis( axisId ) ? col : row;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( plt )
|
||||
{
|
||||
const QwtScaleDiv scaleDiv = plt->axisScaleDiv( axisId );
|
||||
|
||||
// synchronize the axes
|
||||
if ( QwtAxis::isXAxis( axisId ) )
|
||||
{
|
||||
for ( int row = 0; row < numRows(); row++ )
|
||||
{
|
||||
QwtPlot* p = plotAt( row, rowOrColumn );
|
||||
if ( p != plt )
|
||||
{
|
||||
p->setAxisScaleDiv( axisId, scaleDiv );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for ( int col = 0; col < numColumns(); col++ )
|
||||
{
|
||||
QwtPlot* p = plotAt( rowOrColumn, col );
|
||||
if ( p != plt )
|
||||
{
|
||||
p->setAxisScaleDiv( axisId, scaleDiv );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
updateLayout();
|
||||
}
|
||||
|
||||
m_data->inScaleSync = false;
|
||||
}
|
||||
|
||||
void PlotMatrix::updateLayout()
|
||||
{
|
||||
using namespace QwtAxis;
|
||||
|
||||
for ( int row = 0; row < numRows(); row++ )
|
||||
{
|
||||
for ( int col = 0; col < numColumns(); col++ )
|
||||
{
|
||||
QwtPlot* p = plotAt( row, col );
|
||||
if ( p )
|
||||
{
|
||||
bool showAxis[AxisPositions];
|
||||
|
||||
showAxis[XBottom] = isAxisVisible( XBottom ) && row == numRows() - 1;
|
||||
showAxis[XTop] = isAxisVisible( XTop ) && row == 0;
|
||||
showAxis[YLeft] = isAxisVisible( YLeft ) && col == 0;
|
||||
showAxis[YRight] = isAxisVisible( YRight ) && col == numColumns() - 1;
|
||||
|
||||
for ( int axis = 0; axis < AxisPositions; axis++ )
|
||||
{
|
||||
enablePlotAxis( p, axis, showAxis[axis] );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for ( int row = 0; row < numRows(); row++ )
|
||||
{
|
||||
alignAxes( row, XTop );
|
||||
alignAxes( row, XBottom );
|
||||
|
||||
alignScaleBorder( row, YLeft );
|
||||
alignScaleBorder( row, YRight );
|
||||
}
|
||||
|
||||
for ( int col = 0; col < numColumns(); col++ )
|
||||
{
|
||||
alignAxes( col, YLeft );
|
||||
alignAxes( col, YRight );
|
||||
|
||||
alignScaleBorder( col, XBottom );
|
||||
alignScaleBorder( col, XTop );
|
||||
}
|
||||
|
||||
for ( int row = 0; row < numRows(); row++ )
|
||||
{
|
||||
for ( int col = 0; col < numColumns(); col++ )
|
||||
{
|
||||
QwtPlot* p = plotAt( row, col );
|
||||
if ( p )
|
||||
p->replot();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlotMatrix::alignAxes( int rowOrColumn, int axis )
|
||||
{
|
||||
if ( QwtAxis::isYAxis( axis ) )
|
||||
{
|
||||
double maxExtent = 0;
|
||||
|
||||
for ( int row = 0; row < numRows(); row++ )
|
||||
{
|
||||
QwtPlot* p = plotAt( row, rowOrColumn );
|
||||
if ( p )
|
||||
{
|
||||
QwtScaleWidget* scaleWidget = p->axisWidget( axis );
|
||||
|
||||
QwtScaleDraw* sd = scaleWidget->scaleDraw();
|
||||
sd->setMinimumExtent( 0.0 );
|
||||
|
||||
const double extent = sd->extent( scaleWidget->font() );
|
||||
if ( extent > maxExtent )
|
||||
maxExtent = extent;
|
||||
}
|
||||
}
|
||||
|
||||
for ( int row = 0; row < numRows(); row++ )
|
||||
{
|
||||
QwtPlot* p = plotAt( row, rowOrColumn );
|
||||
if ( p )
|
||||
{
|
||||
QwtScaleWidget* scaleWidget = p->axisWidget( axis );
|
||||
scaleWidget->scaleDraw()->setMinimumExtent( maxExtent );
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
double maxExtent = 0;
|
||||
|
||||
for ( int col = 0; col < numColumns(); col++ )
|
||||
{
|
||||
QwtPlot* p = plotAt( rowOrColumn, col );
|
||||
if ( p )
|
||||
{
|
||||
QwtScaleWidget* scaleWidget = p->axisWidget( axis );
|
||||
|
||||
QwtScaleDraw* sd = scaleWidget->scaleDraw();
|
||||
sd->setMinimumExtent( 0.0 );
|
||||
|
||||
const double extent = sd->extent( scaleWidget->font() );
|
||||
if ( extent > maxExtent )
|
||||
maxExtent = extent;
|
||||
}
|
||||
}
|
||||
|
||||
for ( int col = 0; col < numColumns(); col++ )
|
||||
{
|
||||
QwtPlot* p = plotAt( rowOrColumn, col );
|
||||
if ( p )
|
||||
{
|
||||
QwtScaleWidget* scaleWidget = p->axisWidget( axis );
|
||||
scaleWidget->scaleDraw()->setMinimumExtent( maxExtent );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlotMatrix::alignScaleBorder( int rowOrColumn, int axis )
|
||||
{
|
||||
int startDist = 0;
|
||||
int endDist = 0;
|
||||
|
||||
if ( axis == QwtAxis::YLeft )
|
||||
{
|
||||
QwtPlot* plot = plotAt( rowOrColumn, 0 );
|
||||
if ( plot )
|
||||
plot->axisWidget( axis )->getBorderDistHint( startDist, endDist );
|
||||
|
||||
for ( int col = 1; col < numColumns(); col++ )
|
||||
{
|
||||
plot = plotAt( rowOrColumn, col );
|
||||
if ( plot )
|
||||
plot->axisWidget( axis )->setMinBorderDist( startDist, endDist );
|
||||
}
|
||||
}
|
||||
else if ( axis == QwtAxis::YRight )
|
||||
{
|
||||
QwtPlot* plot = plotAt( rowOrColumn, numColumns() - 1 );
|
||||
if ( plot )
|
||||
plot->axisWidget( axis )->getBorderDistHint( startDist, endDist );
|
||||
|
||||
for ( int col = 0; col < numColumns() - 1; col++ )
|
||||
{
|
||||
plot = plotAt( rowOrColumn, col );
|
||||
if ( plot )
|
||||
plot->axisWidget( axis )->setMinBorderDist( startDist, endDist );
|
||||
}
|
||||
}
|
||||
|
||||
if ( axis == QwtAxis::XTop )
|
||||
{
|
||||
QwtPlot* plot = plotAt( rowOrColumn, 0 );
|
||||
if ( plot )
|
||||
plot->axisWidget( axis )->getBorderDistHint( startDist, endDist );
|
||||
|
||||
for ( int row = 1; row < numRows(); row++ )
|
||||
{
|
||||
plot = plotAt( row, rowOrColumn );
|
||||
if ( plot )
|
||||
plot->axisWidget( axis )->setMinBorderDist( startDist, endDist );
|
||||
}
|
||||
}
|
||||
else if ( axis == QwtAxis::XBottom )
|
||||
{
|
||||
QwtPlot* plot = plotAt( numRows() - 1, rowOrColumn );
|
||||
if ( plot )
|
||||
plot->axisWidget( axis )->getBorderDistHint( startDist, endDist );
|
||||
|
||||
for ( int row = 0; row < numRows() - 1; row++ )
|
||||
{
|
||||
plot = plotAt( row, rowOrColumn );
|
||||
if ( plot )
|
||||
plot->axisWidget( axis )->setMinBorderDist( startDist, endDist );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#include "moc_PlotMatrix.cpp"
|
||||
44
playground/plotmatrix/PlotMatrix.h
Normal file
44
playground/plotmatrix/PlotMatrix.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QFrame>
|
||||
|
||||
class QwtPlot;
|
||||
|
||||
class PlotMatrix : public QFrame
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
PlotMatrix( int rows, int columns, QWidget* parent = NULL );
|
||||
virtual ~PlotMatrix();
|
||||
|
||||
int numRows() const;
|
||||
int numColumns() const;
|
||||
|
||||
QwtPlot* plotAt( int row, int column );
|
||||
const QwtPlot* plotAt( int row, int column ) const;
|
||||
|
||||
void setAxisVisible( int axisId, bool tf = true );
|
||||
bool isAxisVisible( int axisId ) const;
|
||||
|
||||
void setAxisScale( int axisId, int rowOrColumn,
|
||||
double min, double max, double step = 0 );
|
||||
|
||||
protected:
|
||||
void updateLayout();
|
||||
|
||||
private Q_SLOTS:
|
||||
void scaleDivChanged();
|
||||
|
||||
private:
|
||||
void alignAxes( int rowOrColumn, int axis );
|
||||
void alignScaleBorder( int rowOrColumn, int axis );
|
||||
|
||||
class PrivateData;
|
||||
PrivateData* m_data;
|
||||
};
|
||||
80
playground/plotmatrix/main.cpp
Normal file
80
playground/plotmatrix/main.cpp
Normal file
@@ -0,0 +1,80 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "PlotMatrix.h"
|
||||
|
||||
#include <QwtPlotGrid>
|
||||
#include <QwtPlot>
|
||||
#include <QwtScaleWidget>
|
||||
#include <QwtMath>
|
||||
|
||||
#include <QApplication>
|
||||
|
||||
namespace
|
||||
{
|
||||
class MainWindow : public PlotMatrix
|
||||
{
|
||||
public:
|
||||
MainWindow();
|
||||
};
|
||||
}
|
||||
|
||||
MainWindow::MainWindow()
|
||||
: PlotMatrix( 3, 4 )
|
||||
{
|
||||
using namespace QwtAxis;
|
||||
|
||||
setAxisVisible( YLeft );
|
||||
setAxisVisible( YRight );
|
||||
setAxisVisible( XBottom );
|
||||
|
||||
for ( int row = 0; row < numRows(); row++ )
|
||||
{
|
||||
const double v = std::pow( 10.0, row );
|
||||
|
||||
setAxisScale( YLeft, row, -v, v );
|
||||
setAxisScale( YRight, row, -v, v );
|
||||
}
|
||||
|
||||
for ( int col = 0; col < numColumns(); col++ )
|
||||
{
|
||||
const double v = std::pow( 10.0, col );
|
||||
|
||||
setAxisScale( XBottom, col, -v, v );
|
||||
setAxisScale( XTop, col, -v, v );
|
||||
}
|
||||
|
||||
for ( int row = 0; row < numRows(); row++ )
|
||||
{
|
||||
for ( int col = 0; col < numColumns(); col++ )
|
||||
{
|
||||
QwtPlot* plot = plotAt( row, col );
|
||||
plot->setCanvasBackground( QColor( Qt::darkGray ) );
|
||||
|
||||
QwtPlotGrid* grid = new QwtPlotGrid();
|
||||
grid->enableXMin( true );
|
||||
grid->setMajorPen( Qt::white, 0, Qt::DotLine );
|
||||
grid->setMinorPen( Qt::gray, 0, Qt::DotLine );
|
||||
grid->attach( plot );
|
||||
}
|
||||
}
|
||||
|
||||
plotAt( 1, 0 )->axisWidget( YLeft )->setLabelRotation( 45 );
|
||||
plotAt( 1, numColumns() - 1 )->axisWidget( YRight )->setLabelRotation( -45 );
|
||||
|
||||
updateLayout();
|
||||
}
|
||||
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
QApplication app( argc, argv );
|
||||
|
||||
MainWindow window;
|
||||
|
||||
window.resize( 800, 600 );
|
||||
window.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
15
playground/plotmatrix/plotmatrix.pro
Normal file
15
playground/plotmatrix/plotmatrix.pro
Normal file
@@ -0,0 +1,15 @@
|
||||
######################################################################
|
||||
# Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
# This file may be used under the terms of the 3-clause BSD License
|
||||
######################################################################
|
||||
|
||||
include( $${PWD}/../playground.pri )
|
||||
|
||||
TARGET = plotmatrix
|
||||
|
||||
HEADERS = \
|
||||
PlotMatrix.h
|
||||
|
||||
SOURCES = \
|
||||
PlotMatrix.cpp \
|
||||
main.cpp
|
||||
Reference in New Issue
Block a user