[cairo-commit] cairo-demo/qcairo controlwidgetbase.ui, NONE, 1.1 main.cpp, NONE, 1.1 mainwindow.cpp, NONE, 1.1 mainwindow.h, NONE, 1.1 qcairo.cpp, NONE, 1.1 qcairo.h, NONE, 1.1 qcairo.pro, NONE, 1.1

Carl Worth commit at pdx.freedesktop.org
Tue Jan 25 12:24:25 PST 2005


Committed by: cworth

Update of /cvs/cairo/cairo-demo/qcairo
In directory gabe:/tmp/cvs-serv5635

Added Files:
	controlwidgetbase.ui main.cpp mainwindow.cpp mainwindow.h 
	qcairo.cpp qcairo.h qcairo.pro 
Log Message:
Initial commit of qcairo demo as contributed by Zack Rusin <zack at kde.org>

--- NEW FILE: controlwidgetbase.ui ---
<!DOCTYPE UI><UI version="3.3" stdsetdef="1">
<class>ControlWidgetBase</class>
<widget class="QWidget">
    <property name="name">
        <cstring>ControlWidgetBase</cstring>
    </property>
    <property name="geometry">
        <rect>
            <x>0</x>
            <y>0</y>
            <width>247</width>
            <height>344</height>
        </rect>
    </property>
    <property name="caption">
        <string>Form1</string>
    </property>
    <vbox>
        <property name="name">
            <cstring>unnamed</cstring>
        </property>
        <widget class="QLayoutWidget">
            <property name="name">
                <cstring>layout2</cstring>
            </property>
            <hbox>
                <property name="name">
                    <cstring>unnamed</cstring>
                </property>
                <widget class="QLabel">
                    <property name="name">
                        <cstring>textLabel1_2</cstring>
                    </property>
                    <property name="text">
                        <string>Base length:</string>
                    </property>
                </widget>
                <widget class="QSpinBox">
                    <property name="name">
                        <cstring>m_baseSpin</cstring>
                    </property>
                </widget>
            </hbox>
        </widget>
        <widget class="QLayoutWidget">
            <property name="name">
                <cstring>layout1</cstring>
            </property>
            <hbox>
                <property name="name">
                    <cstring>unnamed</cstring>
                </property>
                <widget class="QLabel">
                    <property name="name">
                        <cstring>textLabel1</cstring>
                    </property>
                    <property name="text">
                        <string>Line width:</string>
                    </property>
                    <property name="buddy" stdset="0">
                        <cstring>m_lineSpin</cstring>
                    </property>
                </widget>
                <widget class="QSpinBox">
                    <property name="name">
                        <cstring>m_lineSpin</cstring>
                    </property>
                </widget>
            </hbox>
        </widget>
        <widget class="QCheckBox">
            <property name="name">
                <cstring>m_dbCB</cstring>
            </property>
            <property name="text">
                <string>Me no like flicker</string>
            </property>
        </widget>
        <widget class="QPushButton">
            <property name="name">
                <cstring>m_updateBtn</cstring>
            </property>
            <property name="text">
                <string>Update widget</string>
            </property>
        </widget>
    </vbox>
</widget>
<layoutdefaults spacing="6" margin="11"/>
</UI>

--- NEW FILE: main.cpp ---
#include "mainwindow.h"

#include <qapplication.h>

int main( int argc, char **argv )
{
    QApplication app( argc, argv );

    MainWindow *w = new MainWindow();

    app.setMainWidget( w );
    w->setMinimumSize( 200, 200 );
    w->show();
    return app.exec();
}

--- NEW FILE: mainwindow.cpp ---
#include "mainwindow.h"

#include "controlwidgetbase.h"
#include "qcairo.h"

#include <qapplication.h>
#include <qdockwindow.h>
#include <qspinbox.h>
#include <qcheckbox.h>
#include <qpushbutton.h>
#include <qpopupmenu.h>
#include <qmenubar.h>
#include <qaction.h>

MainWindow::MainWindow()
    : QMainWindow( 0 )
{
    createGUI();
    createActions();
}

void MainWindow::createGUI()
{
    m_qcairo = new QCairo( this );
    setCentralWidget( m_qcairo );

    QDockWindow *dockWindow = new QDockWindow( QDockWindow::InDock, this,
                                               "controldock" );
    dockWindow->setResizeEnabled( true );
    m_control = new ControlWidgetBase( dockWindow, "controlwidget" );
    dockWindow->setWidget( m_control );
    addDockWindow( dockWindow, Qt::DockLeft );

    m_control->m_baseSpin->setValue( m_qcairo->baseLength() );
    m_control->m_lineSpin->setValue( m_qcairo->lineWidth() );
    m_control->m_dbCB->setChecked( m_qcairo->doubleBuffer() );

    connect( m_control->m_updateBtn, SIGNAL(clicked()),
             SLOT(updateWidget()) );
}

void MainWindow::createActions()
{
    QPopupMenu *file = new QPopupMenu( this );
    menuBar()->insertItem( "&File", file );

    file->insertItem( "&Quit", qApp, SLOT(quit()) );
}

void MainWindow::updateWidget()
{
    m_qcairo->setBaseLength( m_control->m_baseSpin->value() );
    m_qcairo->setLineWidth( m_control->m_lineSpin->value() );
    m_qcairo->setDoubleBuffer( m_control->m_dbCB->isChecked() );
    m_qcairo->update();
}

--- NEW FILE: mainwindow.h ---
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <qmainwindow.h>

class QCairo;
class ControlWidgetBase;

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow();

private slots:
    void updateWidget();
private:
    void createGUI();
    void createActions();

private:
    QCairo *m_qcairo;
    ControlWidgetBase *m_control;
};

#endif

--- NEW FILE: qcairo.cpp ---
#include "qcairo.h"

#include <qpixmap.h>

#define PI 3.1415926535

QCairo::QCairo( QWidget *parent )
    : QWidget( parent ),
      m_lineWidth( 2 ),
      m_baseLength( 10 )
{
    setDoubleBuffer( true );
    m_cr = cairo_create();
}

QCairo::~QCairo()
{
    cairo_destroy( m_cr );
}

void QCairo::triangle()
{
    cairo_move_to( m_cr, m_baseLength, 0 );
    cairo_rel_line_to( m_cr, 2*m_baseLength,  2*m_baseLength );
    cairo_rel_line_to( m_cr, -2*m_baseLength, 0 );
    cairo_close_path( m_cr );
}

void QCairo::square()
{
    cairo_move_to( m_cr, 0, 0);
    cairo_rel_line_to( m_cr,  2*m_baseLength,   0 );
    cairo_rel_line_to( m_cr,   0,  2*m_baseLength );
    cairo_rel_line_to( m_cr, -2*m_baseLength,   0 );
    cairo_close_path( m_cr );
}

void QCairo::bowtie()
{
    cairo_move_to( m_cr, 0, 0 );
    cairo_rel_line_to( m_cr,  2*m_baseLength,  2*m_baseLength );
    cairo_rel_line_to( m_cr, -2*m_baseLength,   0 );
    cairo_rel_line_to( m_cr,  2*m_baseLength, -2*m_baseLength );
    cairo_close_path( m_cr );
}

void QCairo::inf()
{
    cairo_move_to( m_cr, 0, m_baseLength );
    cairo_rel_curve_to( m_cr,
                        0, m_baseLength,
                        m_baseLength, m_baseLength,
                        2*m_baseLength, 0);
    cairo_rel_curve_to( m_cr,
                        m_baseLength, -m_baseLength,
                        2*m_baseLength, -m_baseLength,
                        2*m_baseLength, 0 );
    cairo_rel_curve_to( m_cr,
                        0, m_baseLength,
                        -m_baseLength, m_baseLength,
                        -2*m_baseLength, 0 );
    cairo_rel_curve_to( m_cr,
                        -m_baseLength, -m_baseLength,
                        -2*m_baseLength, -m_baseLength,
                        -2*m_baseLength, 0 );
    cairo_close_path( m_cr );
}

void QCairo::drawShapes( int x, int y, bool fill )
{
    cairo_save( m_cr );

    cairo_new_path( m_cr );
    cairo_translate( m_cr, x+m_baseLength, y+m_baseLength );
    bowtie();
    if ( fill )
	cairo_fill( m_cr );
    else
	cairo_stroke( m_cr );

    cairo_new_path( m_cr );
    cairo_translate( m_cr, 4*m_baseLength, 0 );
    square();

    if ( fill )
	cairo_fill( m_cr );
    else
	cairo_stroke( m_cr );

    cairo_new_path( m_cr );
    cairo_translate( m_cr, 4*m_baseLength, 0 );
    triangle();
    if ( fill )
	cairo_fill( m_cr );
    else
	cairo_stroke( m_cr );

    cairo_new_path( m_cr );
    cairo_translate( m_cr, 4*m_baseLength, 0 );
    inf();
    if ( fill )
	cairo_fill( m_cr );
    else
	cairo_stroke( m_cr );

    cairo_restore( m_cr );
}

void QCairo::fillShapes( int x, int y )
{
    drawShapes( x, y, true );
}

void QCairo::strokeShapes( int x, int y )
{
    drawShapes( x, y, false );
}

void QCairo::paintEvent( QPaintEvent * )
{
#define NUM_DASH 2
    //qDebug( "start" );
    cairo_save( m_cr );

    m_baseLength = m_baseLength;

    Display *dpy = x11AppDisplay();
    Drawable drw = handle();

    if ( m_doubleBuffer ) {
        m_buffer.resize( size() );
        m_buffer.fill( Qt::black );
        dpy = m_buffer.x11AppDisplay();
        drw = m_buffer.handle();
    }

    cairo_set_target_drawable( m_cr, dpy, drw );

    static double dash[NUM_DASH] = {m_baseLength/4.0, m_baseLength/4.0};

    cairo_set_rgb_color( m_cr, 1, 1, 1 );
    cairo_save( m_cr );
    cairo_scale_font( m_cr, 20 );
    cairo_move_to( m_cr, 10, 10 );
    cairo_rotate( m_cr, PI / 2 );
    cairo_show_text( m_cr, (const unsigned char*)"Hello World." );
    cairo_restore( m_cr );

    /* This is handy for examining problems more closely */
    /*cairo_scale(cr, 4, 4);*/

    cairo_set_line_width( m_cr, m_lineWidth );

    cairo_set_tolerance( m_cr, .1 );

    cairo_set_line_join( m_cr, CAIRO_LINE_JOIN_ROUND );
    cairo_set_dash( m_cr, dash, NUM_DASH, 0 );
    strokeShapes( 0, 0 );

    cairo_translate( m_cr, 0, 4*m_baseLength );

    cairo_set_dash( m_cr, NULL, 0, 0 );
    strokeShapes( 0, 0 );

    cairo_translate( m_cr, 0, 4*m_baseLength );

    cairo_set_line_join( m_cr, CAIRO_LINE_JOIN_BEVEL );
    strokeShapes( 0, 0 );

    cairo_translate( m_cr, 0, 4*m_baseLength );

    cairo_set_line_join( m_cr, CAIRO_LINE_JOIN_MITER );
    strokeShapes( 0, 0 );

    cairo_translate( m_cr, 0, 4*m_baseLength );

    fillShapes( 0, 0 );

    cairo_translate( m_cr, 0, 4*m_baseLength );

    cairo_set_line_join( m_cr, CAIRO_LINE_JOIN_BEVEL );
    fillShapes( 0, 0 );
    cairo_set_rgb_color( m_cr, 0, 1, 0 );
    strokeShapes( 0, 0 );

    if ( cairo_status( m_cr ) ) {
	qDebug("Cairo is unhappy: %s\n", cairo_status_string( m_cr ) );
	exit(0);
    }

    cairo_restore( m_cr );

    if ( m_doubleBuffer )
        bitBlt( this, 0, 0, &m_buffer );
}


--- NEW FILE: qcairo.h ---
#ifndef QCAIRO_H
#define QCAIRO_H

#include <qwidget.h>
#include <qpixmap.h>

#include <cairo.h>
#include <cairo-xlib.h>

class QCairo : public QWidget
{
    Q_OBJECT
public:
    QCairo( QWidget *parent );
    ~QCairo();

    int lineWidth() const { return m_lineWidth; }
    int baseLength() const { return m_baseLength; }
    bool doubleBuffer() const { return m_doubleBuffer; }

public slots:
    void setLineWidth( int width ) {
        m_lineWidth = width;
    }

    void setBaseLength( int base ) {
        m_baseLength = base;
    }
    void setDoubleBuffer( bool db ) {
        m_doubleBuffer = db;
        if ( m_doubleBuffer )
            setBackgroundMode( Qt::NoBackground );
        else
            setEraseColor( Qt::black );
    }

protected:
    void paintEvent( QPaintEvent *e );

private:
    void triangle();
    void square();
    void bowtie();
    void inf();
    void drawShapes( int x, int y, bool fill );
    void fillShapes( int x, int y );
    void strokeShapes( int x, int y );

private:
    cairo_t *m_cr;
    QPixmap m_buffer;
    int m_lineWidth;
    int m_baseLength;
    bool m_doubleBuffer;
};

#endif

--- NEW FILE: qcairo.pro ---
TEMPLATE = app

CAIROINCL = $$system(pkg-config cairo --cflags)
CAIROLIBS = $$system(pkg-config cairo --libs)

QMAKE_CXXFLAGS += $$CAIROINCL
LIBS += $$CAIROLIBS

# Input
HEADERS += qcairo.h mainwindow.h
SOURCES += main.cpp qcairo.cpp mainwindow.cpp
FORMS += controlwidgetbase.ui

OBJECTS_DIR     = .tmp
MOC_DIR         = .tmp




More information about the cairo-commit mailing list