Released version 6.1.3
This commit is contained in:
commit
a94503cb82
1885 changed files with 276310 additions and 0 deletions
374
examples/itemeditor/editor.cpp
Normal file
374
examples/itemeditor/editor.cpp
Normal file
|
@ -0,0 +1,374 @@
|
|||
#include "editor.h"
|
||||
#include <qwt_plot.h>
|
||||
#include <qwt_plot_canvas.h>
|
||||
#include <qwt_scale_map.h>
|
||||
#include <qwt_plot_shapeitem.h>
|
||||
#include <qevent.h>
|
||||
|
||||
class Overlay: public QwtWidgetOverlay
|
||||
{
|
||||
public:
|
||||
Overlay( QWidget *parent, Editor *editor ):
|
||||
QwtWidgetOverlay( parent ),
|
||||
d_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
|
||||
{
|
||||
d_editor->drawOverlay( painter );
|
||||
}
|
||||
|
||||
virtual QRegion maskHint() const
|
||||
{
|
||||
return d_editor->maskHint();
|
||||
}
|
||||
|
||||
private:
|
||||
Editor *d_editor;
|
||||
};
|
||||
|
||||
Editor::Editor( QwtPlot* plot ):
|
||||
QObject( plot ),
|
||||
d_isEnabled( false ),
|
||||
d_overlay( NULL ),
|
||||
d_mode( Mask )
|
||||
{
|
||||
setEnabled( true );
|
||||
}
|
||||
|
||||
|
||||
Editor::~Editor()
|
||||
{
|
||||
delete d_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 )
|
||||
{
|
||||
d_mode = mode;
|
||||
}
|
||||
|
||||
Editor::Mode Editor::mode() const
|
||||
{
|
||||
return d_mode;
|
||||
}
|
||||
|
||||
void Editor::setEnabled( bool on )
|
||||
{
|
||||
if ( on == d_isEnabled )
|
||||
return;
|
||||
|
||||
QwtPlot *plot = qobject_cast<QwtPlot *>( parent() );
|
||||
if ( plot )
|
||||
{
|
||||
d_isEnabled = on;
|
||||
|
||||
if ( on )
|
||||
{
|
||||
plot->canvas()->installEventFilter( this );
|
||||
}
|
||||
else
|
||||
{
|
||||
plot->canvas()->removeEventFilter( this );
|
||||
|
||||
delete d_overlay;
|
||||
d_overlay = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool Editor::isEnabled() const
|
||||
{
|
||||
return d_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 ( d_overlay == NULL &&
|
||||
mouseEvent->button() == Qt::LeftButton )
|
||||
{
|
||||
const bool accepted = pressed( mouseEvent->pos() );
|
||||
if ( accepted )
|
||||
{
|
||||
d_overlay = new Overlay( plot->canvas(), this );
|
||||
|
||||
d_overlay->updateOverlay();
|
||||
d_overlay->show();
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case QEvent::MouseMove:
|
||||
{
|
||||
if ( d_overlay )
|
||||
{
|
||||
const QMouseEvent* mouseEvent =
|
||||
dynamic_cast< QMouseEvent* >( event );
|
||||
|
||||
const bool accepted = moved( mouseEvent->pos() );
|
||||
if ( accepted )
|
||||
d_overlay->updateOverlay();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case QEvent::MouseButtonRelease:
|
||||
{
|
||||
const QMouseEvent* mouseEvent =
|
||||
static_cast<QMouseEvent* >( event );
|
||||
|
||||
if ( d_overlay && mouseEvent->button() == Qt::LeftButton )
|
||||
{
|
||||
released( mouseEvent->pos() );
|
||||
|
||||
delete d_overlay;
|
||||
d_overlay = NULL;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return QObject::eventFilter( object, event );
|
||||
}
|
||||
|
||||
bool Editor::pressed( const QPoint& pos )
|
||||
{
|
||||
d_editedItem = itemAt( pos );
|
||||
if ( d_editedItem )
|
||||
{
|
||||
d_currentPos = pos;
|
||||
setItemVisible( d_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( d_editedItem->xAxis() );
|
||||
const QwtScaleMap yMap = plot()->canvasMap( d_editedItem->yAxis() );
|
||||
|
||||
const QPointF p1 = QwtScaleMap::invTransform( xMap, yMap, d_currentPos );
|
||||
const QPointF p2 = QwtScaleMap::invTransform( xMap, yMap, pos );
|
||||
|
||||
#if QT_VERSION >= 0x040600
|
||||
const QPainterPath shape = d_editedItem->shape().translated( p2 - p1 );
|
||||
#else
|
||||
const double dx = p2.x() - p1.x();
|
||||
const double dy = p2.y() - p1.y();
|
||||
|
||||
QPainterPath shape = d_editedItem->shape();
|
||||
for ( int i = 0; i < shape.elementCount(); i++ )
|
||||
{
|
||||
const QPainterPath::Element &el = shape.elementAt( i );
|
||||
shape.setElementPositionAt( i, el.x + dx, el.y + dy );
|
||||
}
|
||||
#endif
|
||||
|
||||
d_editedItem->setShape( shape );
|
||||
d_currentPos = pos;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Editor::released( const QPoint& pos )
|
||||
{
|
||||
Q_UNUSED( pos );
|
||||
|
||||
if ( d_editedItem )
|
||||
{
|
||||
raiseItem( d_editedItem );
|
||||
setItemVisible( d_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
|
||||
double coords[ QwtPlot::axisCnt ];
|
||||
coords[ QwtPlot::xBottom ] =
|
||||
plot->canvasMap( QwtPlot::xBottom ).invTransform( pos.x() );
|
||||
coords[ QwtPlot::xTop ] =
|
||||
plot->canvasMap( QwtPlot::xTop ).invTransform( pos.x() );
|
||||
coords[ QwtPlot::yLeft ] =
|
||||
plot->canvasMap( QwtPlot::yLeft ).invTransform( pos.y() );
|
||||
coords[ QwtPlot::yRight ] =
|
||||
plot->canvasMap( QwtPlot::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( d_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 || d_editedItem == NULL )
|
||||
return;
|
||||
|
||||
const QwtScaleMap xMap = plot->canvasMap( d_editedItem->xAxis() );
|
||||
const QwtScaleMap yMap = plot->canvasMap( d_editedItem->yAxis() );
|
||||
|
||||
painter->setRenderHint( QPainter::Antialiasing,
|
||||
d_editedItem->testRenderHint( QwtPlotItem::RenderAntialiased ) );
|
||||
d_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 ) );
|
||||
|
||||
}
|
||||
|
66
examples/itemeditor/editor.h
Normal file
66
examples/itemeditor/editor.h
Normal file
|
@ -0,0 +1,66 @@
|
|||
#ifndef _EDITOR_H_
|
||||
#define _EDITOR_H_
|
||||
|
||||
#include <qobject.h>
|
||||
#include <qregion.h>
|
||||
#include <qpointer.h>
|
||||
#include <qwt_widget_overlay.h>
|
||||
|
||||
class QwtPlot;
|
||||
class QwtPlotShapeItem;
|
||||
class QPainter;
|
||||
class QPoint;
|
||||
|
||||
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 *);
|
||||
|
||||
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 *item, bool on );
|
||||
|
||||
bool d_isEnabled;
|
||||
QPointer<QwtWidgetOverlay> d_overlay;
|
||||
|
||||
// Mouse positions
|
||||
QPointF d_currentPos;
|
||||
QwtPlotShapeItem* d_editedItem;
|
||||
|
||||
Mode d_mode;
|
||||
};
|
||||
|
||||
#endif
|
24
examples/itemeditor/itemeditor.pro
Normal file
24
examples/itemeditor/itemeditor.pro
Normal file
|
@ -0,0 +1,24 @@
|
|||
################################################################
|
||||
# Qwt Widget Library
|
||||
# Copyright (C) 1997 Josef Wilgen
|
||||
# Copyright (C) 2002 Uwe Rathmann
|
||||
#
|
||||
# This library is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the Qwt License, Version 1.0
|
||||
################################################################
|
||||
|
||||
include( $${PWD}/../examples.pri )
|
||||
|
||||
TARGET = itemeditor
|
||||
|
||||
HEADERS = \
|
||||
editor.h \
|
||||
shapefactory.h \
|
||||
plot.h
|
||||
|
||||
SOURCES = \
|
||||
editor.cpp \
|
||||
shapefactory.cpp \
|
||||
plot.cpp \
|
||||
main.cpp
|
||||
|
54
examples/itemeditor/main.cpp
Normal file
54
examples/itemeditor/main.cpp
Normal file
|
@ -0,0 +1,54 @@
|
|||
#include "plot.h"
|
||||
#include <qapplication.h>
|
||||
#include <qmainwindow.h>
|
||||
#include <qtoolbar.h>
|
||||
#include <qtoolbutton.h>
|
||||
#include <qcombobox.h>
|
||||
|
||||
class MainWindow: public QMainWindow
|
||||
{
|
||||
public:
|
||||
MainWindow( QWidget * = NULL );
|
||||
};
|
||||
|
||||
MainWindow::MainWindow( QWidget *parent ):
|
||||
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();
|
||||
}
|
120
examples/itemeditor/plot.cpp
Normal file
120
examples/itemeditor/plot.cpp
Normal file
|
@ -0,0 +1,120 @@
|
|||
#include "plot.h"
|
||||
#include "editor.h"
|
||||
#include <qwt_plot_shapeitem.h>
|
||||
#include <qwt_plot_magnifier.h>
|
||||
#include <qwt_plot_canvas.h>
|
||||
#include <qwt_legend.h>
|
||||
#include <qwt_plot_renderer.h>
|
||||
|
||||
class Legend: public QwtLegend
|
||||
{
|
||||
protected:
|
||||
virtual QWidget *createWidget( const QwtLegendData &data ) const
|
||||
{
|
||||
QWidget *w = QwtLegend::createWidget( data );
|
||||
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 < QwtPlot::axisCnt; axis++ )
|
||||
setAxisAutoScale( axis, false );
|
||||
|
||||
d_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 )
|
||||
{
|
||||
d_editor->setMode( static_cast<Editor::Mode>( mode ) );
|
||||
}
|
||||
|
33
examples/itemeditor/plot.h
Normal file
33
examples/itemeditor/plot.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
#ifndef _PLOT_H
|
||||
#define _PLOT_H
|
||||
|
||||
#include <qwt_plot.h>
|
||||
#include "shapefactory.h"
|
||||
|
||||
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 *d_editor;
|
||||
};
|
||||
|
||||
#endif
|
113
examples/itemeditor/shapefactory.cpp
Normal file
113
examples/itemeditor/shapefactory.cpp
Normal file
|
@ -0,0 +1,113 @@
|
|||
#include "shapefactory.h"
|
||||
|
||||
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;
|
||||
}
|
21
examples/itemeditor/shapefactory.h
Normal file
21
examples/itemeditor/shapefactory.h
Normal file
|
@ -0,0 +1,21 @@
|
|||
#ifndef _SHAPE_FACTORY_H_
|
||||
#define _SHAPE_FACTORY_H_
|
||||
|
||||
#include <qpainterpath.h>
|
||||
|
||||
namespace ShapeFactory
|
||||
{
|
||||
enum Shape
|
||||
{
|
||||
Rect,
|
||||
Triangle,
|
||||
Ellipse,
|
||||
Ring,
|
||||
Star,
|
||||
Hexagon
|
||||
};
|
||||
|
||||
QPainterPath path( Shape, const QPointF &, const QSizeF & );
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue