Add files from zip
This commit is contained in:
77
playground/graphicscale/Canvas.cpp
Normal file
77
playground/graphicscale/Canvas.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "Canvas.h"
|
||||
#include <QwtGraphic>
|
||||
#include <QSvgRenderer>
|
||||
|
||||
Canvas::Canvas( Mode mode, QWidget* parent )
|
||||
: QWidget( parent )
|
||||
, m_mode( mode )
|
||||
{
|
||||
const int m = 10;
|
||||
setContentsMargins( m, m, m, m );
|
||||
|
||||
if ( m_mode == Svg )
|
||||
m_renderer = new QSvgRenderer( this );
|
||||
else
|
||||
m_graphic = new QwtGraphic();
|
||||
}
|
||||
|
||||
Canvas::~Canvas()
|
||||
{
|
||||
if ( m_mode == VectorGraphic )
|
||||
delete m_graphic;
|
||||
}
|
||||
|
||||
void Canvas::setSvg( const QByteArray& svgData )
|
||||
{
|
||||
if ( m_mode == VectorGraphic )
|
||||
{
|
||||
m_graphic->reset();
|
||||
|
||||
QSvgRenderer renderer;
|
||||
renderer.load( svgData );
|
||||
|
||||
QPainter p( m_graphic );
|
||||
renderer.render( &p, renderer.viewBoxF() );
|
||||
p.end();
|
||||
}
|
||||
else
|
||||
{
|
||||
m_renderer->load( svgData );
|
||||
}
|
||||
|
||||
update();
|
||||
}
|
||||
|
||||
void Canvas::paintEvent( QPaintEvent* )
|
||||
{
|
||||
QPainter painter( this );
|
||||
|
||||
painter.save();
|
||||
|
||||
painter.setPen( Qt::black );
|
||||
painter.setBrush( Qt::white );
|
||||
painter.drawRect( contentsRect().adjusted( 0, 0, -1, -1 ) );
|
||||
|
||||
painter.restore();
|
||||
|
||||
painter.setPen( Qt::NoPen );
|
||||
painter.setBrush( Qt::NoBrush );
|
||||
render( &painter, contentsRect() );
|
||||
}
|
||||
|
||||
void Canvas::render( QPainter* painter, const QRect& rect ) const
|
||||
{
|
||||
if ( m_mode == Svg )
|
||||
{
|
||||
m_renderer->render( painter, rect );
|
||||
}
|
||||
else
|
||||
{
|
||||
m_graphic->render( painter, rect );
|
||||
}
|
||||
}
|
||||
40
playground/graphicscale/Canvas.h
Normal file
40
playground/graphicscale/Canvas.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*****************************************************************************
|
||||
* 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 <QWidget>
|
||||
|
||||
class QSvgRenderer;
|
||||
class QwtGraphic;
|
||||
|
||||
class Canvas : public QWidget
|
||||
{
|
||||
public:
|
||||
enum Mode
|
||||
{
|
||||
Svg,
|
||||
VectorGraphic
|
||||
};
|
||||
|
||||
Canvas( Mode, QWidget* parent = NULL );
|
||||
virtual ~Canvas();
|
||||
|
||||
void setSvg( const QByteArray& );
|
||||
|
||||
protected:
|
||||
virtual void paintEvent( QPaintEvent* ) QWT_OVERRIDE;
|
||||
|
||||
private:
|
||||
void render( QPainter*, const QRect& ) const;
|
||||
|
||||
const Mode m_mode;
|
||||
union
|
||||
{
|
||||
QSvgRenderer* m_renderer;
|
||||
QwtGraphic* m_graphic;
|
||||
};
|
||||
};
|
||||
117
playground/graphicscale/MainWindow.cpp
Normal file
117
playground/graphicscale/MainWindow.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "MainWindow.h"
|
||||
#include "Canvas.h"
|
||||
|
||||
#include <QToolBar>
|
||||
#include <QToolButton>
|
||||
#include <QLayout>
|
||||
#include <QLabel>
|
||||
#include <QFileDialog>
|
||||
#include <QBuffer>
|
||||
#include <QPainter>
|
||||
#include <QSvgGenerator>
|
||||
#include <QStatusBar>
|
||||
|
||||
MainWindow::MainWindow()
|
||||
{
|
||||
QWidget* w = new QWidget( this );
|
||||
|
||||
m_canvas[0] = new Canvas( Canvas::Svg, this );
|
||||
m_canvas[0]->setAutoFillBackground( true );
|
||||
m_canvas[0]->setPalette( Qt::gray );
|
||||
|
||||
m_canvas[1] = new Canvas( Canvas::VectorGraphic, this );
|
||||
m_canvas[1]->setAutoFillBackground( true );
|
||||
m_canvas[1]->setPalette( Qt::gray );
|
||||
|
||||
QVBoxLayout* vBox1 = new QVBoxLayout();
|
||||
vBox1->setContentsMargins( 0, 0, 0, 0 );
|
||||
vBox1->setSpacing( 5 );
|
||||
vBox1->addWidget( new QLabel( "SVG" ), 0, Qt::AlignCenter );
|
||||
vBox1->addWidget( m_canvas[0], 10 );
|
||||
|
||||
QVBoxLayout* vBox2 = new QVBoxLayout();
|
||||
vBox2->setContentsMargins( 0, 0, 0, 0 );
|
||||
vBox2->setSpacing( 5 );
|
||||
vBox2->addWidget( new QLabel( "Vector Graphic" ), 0, Qt::AlignCenter );
|
||||
vBox2->addWidget( m_canvas[1], 10 );
|
||||
|
||||
QHBoxLayout* layout = new QHBoxLayout( w );
|
||||
layout->addLayout( vBox1 );
|
||||
layout->addLayout( vBox2 );
|
||||
|
||||
setCentralWidget( w );
|
||||
|
||||
QToolBar* toolBar = new QToolBar( this );
|
||||
|
||||
QToolButton* btnLoad = new QToolButton( toolBar );
|
||||
|
||||
btnLoad->setText( "Load SVG" );
|
||||
btnLoad->setToolButtonStyle( Qt::ToolButtonTextUnderIcon );
|
||||
toolBar->addWidget( btnLoad );
|
||||
|
||||
addToolBar( toolBar );
|
||||
|
||||
connect( btnLoad, SIGNAL(clicked()), this, SLOT(loadSVG()) );
|
||||
|
||||
#if 0
|
||||
QPainterPath path;
|
||||
path.addRect( QRectF( 1.0, 1.0, 2.0, 2.0 ) );
|
||||
loadPath( path );
|
||||
#endif
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
}
|
||||
|
||||
void MainWindow::loadSVG()
|
||||
{
|
||||
QString dir = "/home1/uwe/qwt/qwt/tests/svg";
|
||||
const QString fileName = QFileDialog::getOpenFileName( NULL,
|
||||
"Load a Scaleable Vector Graphic (SVG) Document",
|
||||
dir, "SVG Files (*.svg)" );
|
||||
|
||||
if ( !fileName.isEmpty() )
|
||||
loadSVG( fileName );
|
||||
|
||||
statusBar()->showMessage( fileName );
|
||||
}
|
||||
|
||||
void MainWindow::loadSVG( const QString& fileName )
|
||||
{
|
||||
QFile file( fileName );
|
||||
if ( !file.open(QIODevice::ReadOnly | QIODevice::Text) )
|
||||
return;
|
||||
|
||||
const QByteArray document = file.readAll();
|
||||
file.close();
|
||||
|
||||
m_canvas[0]->setSvg( document );
|
||||
m_canvas[1]->setSvg( document );
|
||||
}
|
||||
|
||||
|
||||
void MainWindow::loadPath( const QPainterPath& path )
|
||||
{
|
||||
QBuffer buf;
|
||||
|
||||
QSvgGenerator generator;
|
||||
generator.setOutputDevice( &buf );
|
||||
|
||||
QPainter painter( &generator );
|
||||
painter.setRenderHint( QPainter::Antialiasing, false );
|
||||
painter.setPen( QPen( Qt::blue, 0 ) );
|
||||
painter.setBrush( Qt::darkCyan );
|
||||
painter.drawPath( path );
|
||||
painter.end();
|
||||
|
||||
m_canvas[0]->setSvg( buf.data() );
|
||||
m_canvas[1]->setSvg( buf.data() );
|
||||
}
|
||||
|
||||
#include "moc_MainWindow.cpp"
|
||||
29
playground/graphicscale/MainWindow.h
Normal file
29
playground/graphicscale/MainWindow.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QMainWindow>
|
||||
|
||||
class Canvas;
|
||||
class QPainterPath;
|
||||
|
||||
class MainWindow : public QMainWindow
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
MainWindow();
|
||||
virtual ~MainWindow();
|
||||
|
||||
private Q_SLOTS:
|
||||
void loadSVG();
|
||||
|
||||
private:
|
||||
void loadSVG( const QString& );
|
||||
void loadPath( const QPainterPath& );
|
||||
|
||||
Canvas* m_canvas[2];
|
||||
};
|
||||
25
playground/graphicscale/graphicscale.pro
Normal file
25
playground/graphicscale/graphicscale.pro
Normal 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
|
||||
######################################################################
|
||||
|
||||
include( $${PWD}/../playground.pri )
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4) {
|
||||
!qtHaveModule(svg) {
|
||||
error("Qt has been built without SVG support !")
|
||||
}
|
||||
}
|
||||
|
||||
TARGET = graphicscale
|
||||
QT += svg
|
||||
|
||||
HEADERS = \
|
||||
Canvas.h \
|
||||
MainWindow.h
|
||||
|
||||
SOURCES = \
|
||||
Canvas.cpp \
|
||||
MainWindow.cpp \
|
||||
main.cpp
|
||||
|
||||
18
playground/graphicscale/main.cpp
Normal file
18
playground/graphicscale/main.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
/*****************************************************************************
|
||||
* Qwt Examples - Copyright (C) 2002 Uwe Rathmann
|
||||
* This file may be used under the terms of the 3-clause BSD License
|
||||
*****************************************************************************/
|
||||
|
||||
#include "MainWindow.h"
|
||||
#include <QApplication>
|
||||
|
||||
int main( int argc, char* argv[] )
|
||||
{
|
||||
QApplication app( argc, argv );
|
||||
|
||||
MainWindow window;
|
||||
window.resize( 600, 400 );
|
||||
window.show();
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
Reference in New Issue
Block a user