File: mainwindow.cpp

package info (click to toggle)
qwt 6.1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 23,900 kB
  • sloc: cpp: 57,678; xml: 182; makefile: 47
file content (109 lines) | stat: -rw-r--r-- 2,790 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "mainwindow.h"
#include "canvas.h"
#include <qtoolbar.h>
#include <qtoolbutton.h>
#include <qlayout.h>
#include <qlabel.h>
#include <qfiledialog.h>
#include <qbuffer.h>
#include <qpainter.h>
#include <qsvggenerator.h>
#include <qstatusbar.h>

MainWindow::MainWindow()
{
    QWidget *w = new QWidget( this );

    d_canvas[0] = new Canvas( Canvas::Svg, this );
    d_canvas[0]->setAutoFillBackground( true );
    d_canvas[0]->setPalette( Qt::gray );

    d_canvas[1] = new Canvas( Canvas::VectorGraphic, this );
    d_canvas[1]->setAutoFillBackground( true );
    d_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( d_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( d_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();

    d_canvas[0]->setSvg( document );
    d_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();

    d_canvas[0]->setSvg( buf.data() );
    d_canvas[1]->setSvg( buf.data() );
}