Add files from zip

This commit is contained in:
2023-10-31 09:22:42 +01:00
parent 6bacdc5f6d
commit 4dae68036f
2788 changed files with 492537 additions and 0 deletions

View File

@@ -0,0 +1,377 @@
/*****************************************************************************
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "Editor.h"
#include <QwtPlot>
#include <QwtPlotCanvas>
#include <QwtScaleMap>
#include <QwtPlotShapeItem>
#include <QwtWidgetOverlay>
#include <QMouseEvent>
#include <QPainter>
#include <QPainterPath>
namespace
{
class Overlay : public QwtWidgetOverlay
{
public:
Overlay( QWidget* parent, Editor* editor )
: QwtWidgetOverlay( parent )
, m_editor( editor )
{
switch( editor->mode() )
{
case Editor::NoMask:
{
setMaskMode( QwtWidgetOverlay::NoMask );
setRenderMode( QwtWidgetOverlay::AutoRenderMode );
break;
}
case Editor::Mask:
{
setMaskMode( QwtWidgetOverlay::MaskHint );
setRenderMode( QwtWidgetOverlay::AutoRenderMode );
break;
}
case Editor::AlphaMask:
{
setMaskMode( QwtWidgetOverlay::AlphaMask );
setRenderMode( QwtWidgetOverlay::AutoRenderMode );
break;
}
case Editor::AlphaMaskRedraw:
{
setMaskMode( QwtWidgetOverlay::AlphaMask );
setRenderMode( QwtWidgetOverlay::DrawOverlay );
break;
}
case Editor::AlphaMaskCopyMask:
{
setMaskMode( QwtWidgetOverlay::AlphaMask );
setRenderMode( QwtWidgetOverlay::CopyAlphaMask );
break;
}
}
}
protected:
virtual void drawOverlay( QPainter* painter ) const QWT_OVERRIDE
{
m_editor->drawOverlay( painter );
}
virtual QRegion maskHint() const QWT_OVERRIDE
{
return m_editor->maskHint();
}
private:
Editor* m_editor;
};
}
Editor::Editor( QwtPlot* plot ):
QObject( plot ),
m_isEnabled( false ),
m_overlay( NULL ),
m_mode( Mask )
{
setEnabled( true );
}
Editor::~Editor()
{
delete m_overlay;
}
QwtPlot* Editor::plot()
{
return qobject_cast< QwtPlot* >( parent() );
}
const QwtPlot* Editor::plot() const
{
return qobject_cast< const QwtPlot* >( parent() );
}
void Editor::setMode( Mode mode )
{
m_mode = mode;
}
Editor::Mode Editor::mode() const
{
return m_mode;
}
void Editor::setEnabled( bool on )
{
if ( on == m_isEnabled )
return;
QwtPlot* plot = qobject_cast< QwtPlot* >( parent() );
if ( plot )
{
m_isEnabled = on;
if ( on )
{
plot->canvas()->installEventFilter( this );
}
else
{
plot->canvas()->removeEventFilter( this );
delete m_overlay;
m_overlay = NULL;
}
}
}
bool Editor::isEnabled() const
{
return m_isEnabled;
}
bool Editor::eventFilter( QObject* object, QEvent* event )
{
QwtPlot* plot = qobject_cast< QwtPlot* >( parent() );
if ( plot && object == plot->canvas() )
{
switch( event->type() )
{
case QEvent::MouseButtonPress:
{
const QMouseEvent* mouseEvent =
dynamic_cast< QMouseEvent* >( event );
if ( m_overlay == NULL &&
mouseEvent->button() == Qt::LeftButton )
{
const bool accepted = pressed( mouseEvent->pos() );
if ( accepted )
{
m_overlay = new Overlay( plot->canvas(), this );
m_overlay->updateOverlay();
m_overlay->show();
}
}
break;
}
case QEvent::MouseMove:
{
if ( m_overlay )
{
const QMouseEvent* mouseEvent =
dynamic_cast< QMouseEvent* >( event );
const bool accepted = moved( mouseEvent->pos() );
if ( accepted )
m_overlay->updateOverlay();
}
break;
}
case QEvent::MouseButtonRelease:
{
const QMouseEvent* mouseEvent =
static_cast< QMouseEvent* >( event );
if ( m_overlay && mouseEvent->button() == Qt::LeftButton )
{
released( mouseEvent->pos() );
delete m_overlay;
m_overlay = NULL;
}
break;
}
default:
break;
}
return false;
}
return QObject::eventFilter( object, event );
}
bool Editor::pressed( const QPoint& pos )
{
m_editedItem = itemAt( pos );
if ( m_editedItem )
{
m_currentPos = pos;
setItemVisible( m_editedItem, false );
return true;
}
return false; // don't accept the position
}
bool Editor::moved( const QPoint& pos )
{
if ( plot() == NULL )
return false;
const QwtScaleMap xMap = plot()->canvasMap( m_editedItem->xAxis() );
const QwtScaleMap yMap = plot()->canvasMap( m_editedItem->yAxis() );
const QPointF p1 = QwtScaleMap::invTransform( xMap, yMap, m_currentPos );
const QPointF p2 = QwtScaleMap::invTransform( xMap, yMap, pos );
const QPainterPath shape = m_editedItem->shape().translated( p2 - p1 );
m_editedItem->setShape( shape );
m_currentPos = pos;
return true;
}
void Editor::released( const QPoint& pos )
{
Q_UNUSED( pos );
if ( m_editedItem )
{
raiseItem( m_editedItem );
setItemVisible( m_editedItem, true );
}
}
QwtPlotShapeItem* Editor::itemAt( const QPoint& pos ) const
{
const QwtPlot* plot = this->plot();
if ( plot == NULL )
return NULL;
// translate pos into the plot coordinates
using namespace QwtAxis;
double coords[ AxisPositions ];
coords[ XBottom ] = plot->canvasMap( XBottom ).invTransform( pos.x() );
coords[ XTop ] = plot->canvasMap( XTop ).invTransform( pos.x() );
coords[ YLeft ] = plot->canvasMap( YLeft ).invTransform( pos.y() );
coords[ YRight ] = plot->canvasMap( YRight ).invTransform( pos.y() );
QwtPlotItemList items = plot->itemList();
for ( int i = items.size() - 1; i >= 0; i-- )
{
QwtPlotItem* item = items[ i ];
if ( item->isVisible() &&
item->rtti() == QwtPlotItem::Rtti_PlotShape )
{
QwtPlotShapeItem* shapeItem = static_cast< QwtPlotShapeItem* >( item );
const QPointF p( coords[ item->xAxis() ], coords[ item->yAxis() ] );
if ( shapeItem->boundingRect().contains( p )
&& shapeItem->shape().contains( p ) )
{
return shapeItem;
}
}
}
return NULL;
}
QRegion Editor::maskHint() const
{
return maskHint( m_editedItem );
}
QRegion Editor::maskHint( QwtPlotShapeItem* shapeItem ) const
{
const QwtPlot* plot = this->plot();
if ( plot == NULL || shapeItem == NULL )
return QRegion();
const QwtScaleMap xMap = plot->canvasMap( shapeItem->xAxis() );
const QwtScaleMap yMap = plot->canvasMap( shapeItem->yAxis() );
QRect rect = QwtScaleMap::transform( xMap, yMap,
shapeItem->shape().boundingRect() ).toRect();
const int m = 5; // some margin for the pen
return rect.adjusted( -m, -m, m, m );
}
void Editor::drawOverlay( QPainter* painter ) const
{
const QwtPlot* plot = this->plot();
if ( plot == NULL || m_editedItem == NULL )
return;
const QwtScaleMap xMap = plot->canvasMap( m_editedItem->xAxis() );
const QwtScaleMap yMap = plot->canvasMap( m_editedItem->yAxis() );
painter->setRenderHint( QPainter::Antialiasing,
m_editedItem->testRenderHint( QwtPlotItem::RenderAntialiased ) );
m_editedItem->draw( painter, xMap, yMap,
plot->canvas()->contentsRect() );
}
void Editor::raiseItem( QwtPlotShapeItem* shapeItem )
{
const QwtPlot* plot = this->plot();
if ( plot == NULL || shapeItem == NULL )
return;
const QwtPlotItemList items = plot->itemList();
for ( int i = items.size() - 1; i >= 0; i-- )
{
QwtPlotItem* item = items[ i ];
if ( shapeItem == item )
return;
if ( item->isVisible() &&
item->rtti() == QwtPlotItem::Rtti_PlotShape )
{
shapeItem->setZ( item->z() + 1 );
return;
}
}
}
void Editor::setItemVisible( QwtPlotShapeItem* item, bool on )
{
if ( plot() == NULL || item == NULL || item->isVisible() == on )
return;
const bool doAutoReplot = plot()->autoReplot();
plot()->setAutoReplot( false );
item->setVisible( on );
plot()->setAutoReplot( doAutoReplot );
/*
Avoid replot with a full repaint of the canvas.
For special combinations - f.e. using the
raster paint engine on a remote display -
this makes a difference.
*/
QwtPlotCanvas* canvas =
qobject_cast< QwtPlotCanvas* >( plot()->canvas() );
if ( canvas )
canvas->invalidateBackingStore();
plot()->canvas()->update( maskHint( item ) );
}
#include "moc_Editor.cpp"

View File

@@ -0,0 +1,71 @@
/*****************************************************************************
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#pragma once
#include <QwtGlobal>
#include <QObject>
#include <QPointer>
#include <QPoint>
class QwtPlot;
class QwtPlotShapeItem;
class QwtWidgetOverlay;
class QPainter;
class QRegion;
class Editor : public QObject
{
Q_OBJECT
public:
enum Mode
{
NoMask,
Mask,
AlphaMask,
AlphaMaskRedraw,
AlphaMaskCopyMask
};
Editor( QwtPlot* );
virtual ~Editor();
const QwtPlot* plot() const;
QwtPlot* plot();
virtual void setEnabled( bool on );
bool isEnabled() const;
void drawOverlay( QPainter* ) const;
QRegion maskHint() const;
virtual bool eventFilter( QObject*, QEvent*) QWT_OVERRIDE;
void setMode( Mode mode );
Mode mode() const;
private:
bool pressed( const QPoint& );
bool moved( const QPoint& );
void released( const QPoint& );
QwtPlotShapeItem* itemAt( const QPoint& ) const;
void raiseItem( QwtPlotShapeItem* );
QRegion maskHint( QwtPlotShapeItem* ) const;
void setItemVisible( QwtPlotShapeItem*, bool on );
bool m_isEnabled;
QPointer< QwtWidgetOverlay > m_overlay;
// Mouse positions
QPointF m_currentPos;
QwtPlotShapeItem* m_editedItem;
Mode m_mode;
};

View File

@@ -0,0 +1,134 @@
/*****************************************************************************
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "Plot.h"
#include "Editor.h"
#include <QwtPlotShapeItem>
#include <QwtPlotMagnifier>
#include <QwtPlotCanvas>
#include <QwtLegend>
#include <QwtPlotRenderer>
#include <QPen>
#include <QPainterPath>
namespace
{
class Legend : public QwtLegend
{
protected:
virtual QWidget* createWidget(
const QwtLegendData& legendData ) const QWT_OVERRIDE
{
QWidget* w = QwtLegend::createWidget( legendData );
if ( w )
{
w->setStyleSheet(
"border-radius: 5px;"
"padding: 2px;"
"background: LemonChiffon;"
);
}
return w;
}
};
}
Plot::Plot( QWidget* parent )
: QwtPlot( parent )
{
setAutoReplot( false );
setTitle( "Movable Items" );
const int margin = 5;
setContentsMargins( margin, margin, margin, margin );
setAutoFillBackground( true );
setPalette( QColor( "DimGray" ).lighter( 110 ) );
QwtPlotCanvas* canvas = new QwtPlotCanvas();
#if 0
// a gradient making a replot slow on X11
canvas->setStyleSheet(
"border: 2px solid Black;"
"border-radius: 15px;"
"background-color: qlineargradient( x1: 0, y1: 0, x2: 0, y2: 1,"
"stop: 0 LemonChiffon, stop: 0.5 PaleGoldenrod, stop: 1 LemonChiffon );"
);
#else
canvas->setStyleSheet(
"border: 2px inset DimGray;"
"border-radius: 15px;"
"background: LemonChiffon;"
);
#endif
setCanvas( canvas );
insertLegend( new Legend(), QwtPlot::RightLegend );
populate();
updateAxes();
for ( int axis = 0; axis < QwtAxis::AxisPositions; axis++ )
setAxisAutoScale( axis, false );
m_editor = new Editor( this );
( void ) new QwtPlotMagnifier( canvas );
}
void Plot::populate()
{
addShape( "Rectangle", ShapeFactory::Rect, "RoyalBlue",
QPointF( 30.0, 50.0 ), QSizeF( 40.0, 50.0 ) );
addShape( "Ellipse", ShapeFactory::Ellipse, "IndianRed",
QPointF( 80.0, 130.0 ), QSizeF( 50.0, 40.0 ) );
addShape( "Ring", ShapeFactory::Ring, "DarkOliveGreen",
QPointF( 30.0, 165.0 ), QSizeF( 40.0, 40.0 ) );
addShape( "Triangle", ShapeFactory::Triangle, "SandyBrown",
QPointF( 165.0, 165.0 ), QSizeF( 60.0, 40.0 ) );
addShape( "Star", ShapeFactory::Star, "DarkViolet",
QPointF( 165.0, 50.0 ), QSizeF( 40.0, 50.0 ) );
addShape( "Hexagon", ShapeFactory::Hexagon, "DarkSlateGray",
QPointF( 120.0, 70.0 ), QSizeF( 50.0, 50.0 ) );
}
void Plot::addShape( const QString& title,
ShapeFactory::Shape shape, const QColor& color,
const QPointF& pos, const QSizeF& size )
{
QwtPlotShapeItem* item = new QwtPlotShapeItem( title );
item->setItemAttribute( QwtPlotItem::Legend, true );
item->setLegendMode( QwtPlotShapeItem::LegendShape );
item->setLegendIconSize( QSize( 20, 20 ) );
item->setRenderHint( QwtPlotItem::RenderAntialiased, true );
item->setShape( ShapeFactory::path( shape, pos, size ) );
QColor fillColor = color;
fillColor.setAlpha( 200 );
QPen pen( color, 3 );
pen.setJoinStyle( Qt::MiterJoin );
item->setPen( pen );
item->setBrush( fillColor );
item->attach( this );
}
void Plot::exportPlot()
{
QwtPlotRenderer renderer;
renderer.exportTo( this, "shapes.pdf" );
}
void Plot::setMode( int mode )
{
m_editor->setMode( static_cast< Editor::Mode >( mode ) );
}
#include "moc_Plot.cpp"

View File

@@ -0,0 +1,35 @@
/*****************************************************************************
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#pragma once
#include "ShapeFactory.h"
#include <QwtPlot>
class QColor;
class QSizeF;
class QPointF;
class Editor;
class Plot : public QwtPlot
{
Q_OBJECT
public:
Plot( QWidget* parent = NULL );
public Q_SLOTS:
void exportPlot();
void setMode( int );
private:
void populate();
void addShape( const QString& title,
ShapeFactory::Shape, const QColor&,
const QPointF&, const QSizeF& );
Editor* m_editor;
};

View File

@@ -0,0 +1,119 @@
/*****************************************************************************
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "ShapeFactory.h"
#include <QPainterPath>
QPainterPath ShapeFactory::path( Shape shape,
const QPointF& pos, const QSizeF& size )
{
QRectF rect;
rect.setSize( size );
rect.moveCenter( pos );
QPainterPath path;
switch( shape )
{
case Rect:
{
path.addRect( rect );
break;
}
case Triangle:
{
QPolygonF triangle;
triangle += rect.bottomLeft();
triangle += QPointF( rect.center().x(), rect.top() );
triangle += rect.bottomRight();
path.addPolygon( triangle );
break;
}
case Ellipse:
{
path.addEllipse( rect );
break;
}
case Ring:
{
path.addEllipse( rect );
const double w = 0.25 * rect.width();
path.addEllipse( rect.adjusted( w, w, -w, -w ) );
break;
}
case Star:
{
const double cos30 = 0.866025;
const double dy = 0.25 * size.height();
const double dx = 0.5 * size.width() * cos30 / 3.0;
double x1 = pos.x() - 3 * dx;
double y1 = pos.y() - 2 * dy;
const double x2 = x1 + 1 * dx;
const double x3 = x1 + 2 * dx;
const double x4 = x1 + 3 * dx;
const double x5 = x1 + 4 * dx;
const double x6 = x1 + 5 * dx;
const double x7 = x1 + 6 * dx;
const double y2 = y1 + 1 * dy;
const double y3 = y1 + 2 * dy;
const double y4 = y1 + 3 * dy;
const double y5 = y1 + 4 * dy;
QPolygonF star;
star += QPointF( x4, y1 );
star += QPointF( x5, y2 );
star += QPointF( x7, y2 );
star += QPointF( x6, y3 );
star += QPointF( x7, y4 );
star += QPointF( x5, y4 );
star += QPointF( x4, y5 );
star += QPointF( x3, y4 );
star += QPointF( x1, y4 );
star += QPointF( x2, y3 );
star += QPointF( x1, y2 );
star += QPointF( x3, y2 );
path.addPolygon( star );
break;
}
case Hexagon:
{
const double cos30 = 0.866025;
const double dx = 0.5 * size.width() - cos30;
const double dy = 0.25 * size.height();
double x1 = pos.x() - dx;
double y1 = pos.y() - 2 * dy;
const double x2 = x1 + 1 * dx;
const double x3 = x1 + 2 * dx;
const double y2 = y1 + 1 * dy;
const double y3 = y1 + 3 * dy;
const double y4 = y1 + 4 * dy;
QPolygonF hexagon;
hexagon += QPointF( x2, y1 );
hexagon += QPointF( x3, y2 );
hexagon += QPointF( x3, y3 );
hexagon += QPointF( x2, y4 );
hexagon += QPointF( x1, y3 );
hexagon += QPointF( x1, y2 );
path.addPolygon( hexagon );
break;
}
};
path.closeSubpath();
return path;
}

View File

@@ -0,0 +1,25 @@
/*****************************************************************************
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#pragma once
class QPainterPath;
class QPointF;
class QSizeF;
namespace ShapeFactory
{
enum Shape
{
Rect,
Triangle,
Ellipse,
Ring,
Star,
Hexagon
};
QPainterPath path( Shape, const QPointF&, const QSizeF& );
}

View File

@@ -0,0 +1,20 @@
######################################################################
# Qwt Examples - Copyright (C) 2002 Uwe Rathmann
# This file may be used under the terms of the 3-clause BSD License
######################################################################
include( $${PWD}/../examples.pri )
TARGET = itemeditor
HEADERS = \
Editor.h \
ShapeFactory.h \
Plot.h
SOURCES = \
Editor.cpp \
ShapeFactory.cpp \
Plot.cpp \
main.cpp

View File

@@ -0,0 +1,60 @@
/*****************************************************************************
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "Plot.h"
#include <QApplication>
#include <QMainWindow>
#include <QToolBar>
#include <QToolButton>
#include <QComboBox>
namespace
{
class MainWindow : public QMainWindow
{
public:
MainWindow( QWidget* parent = nullptr )
: QMainWindow( parent )
{
Plot* plot = new Plot( this );
setCentralWidget( plot );
QToolBar* toolBar = new QToolBar( this );
QComboBox* modeBox = new QComboBox( toolBar );
modeBox->addItem( "No Mask" );
modeBox->addItem( "Mask" );
modeBox->addItem( "Alpha Mask" );
modeBox->addItem( "Alpha Mask/Redraw" );
modeBox->addItem( "Alpha Mask/Copy Mask" );
modeBox->setCurrentIndex( 1 );
modeBox->setSizePolicy( QSizePolicy::Fixed, QSizePolicy::Fixed );
connect( modeBox, SIGNAL(currentIndexChanged(int)),
plot, SLOT(setMode(int)) );
QToolButton* btnExport = new QToolButton( toolBar );
btnExport->setText( "Export" );
btnExport->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
connect( btnExport, SIGNAL(clicked()), plot, SLOT(exportPlot()) );
toolBar->addWidget( modeBox );
toolBar->addWidget( btnExport );
addToolBar( toolBar );
}
};
}
int main( int argc, char* argv[] )
{
QApplication app( argc, argv );
MainWindow window;
window.resize( 600, 400 );
window.show();
return app.exec();
}