diff options
author | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
---|---|---|
committer | toma <toma@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2009-11-25 17:56:58 +0000 |
commit | 90825e2392b2d70e43c7a25b8a3752299a933894 (patch) | |
tree | e33aa27f02b74604afbfd0ea4f1cfca8833d882a /qtjava/javalib/examples/scribble | |
download | tdebindings-90825e2392b2d70e43c7a25b8a3752299a933894.tar.gz tdebindings-90825e2392b2d70e43c7a25b8a3752299a933894.zip |
Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features.
BUG:215923
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdebindings@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'qtjava/javalib/examples/scribble')
-rw-r--r-- | qtjava/javalib/examples/scribble/Main.java | 36 | ||||
-rw-r--r-- | qtjava/javalib/examples/scribble/Scribble.java | 216 |
2 files changed, 252 insertions, 0 deletions
diff --git a/qtjava/javalib/examples/scribble/Main.java b/qtjava/javalib/examples/scribble/Main.java new file mode 100644 index 00000000..01d7a416 --- /dev/null +++ b/qtjava/javalib/examples/scribble/Main.java @@ -0,0 +1,36 @@ +/*************************************************************************** +* $Id$ +** +* Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved. +** +* This file is part of an example program for Qt. This example +* program may be used, distributed and modified without limitation. +** +****************************************************************************/ + +import org.kde.qt.*; + +public class Main { + +public static void main(String[] args) +{ + QApplication a = new QApplication( args ); + + Scribble scribble = new Scribble(); + + scribble.resize( 500, 350 ); + scribble.setCaption("Qt Example - Scribble"); + a.setMainWidget( scribble ); + if ( QApplication.desktop().width() > 550 + && QApplication.desktop().height() > 366 ) + scribble.show(); + else + scribble.showMaximized(); + a.exec(); + return; +} + + static { + qtjava.initialize(); + } +} diff --git a/qtjava/javalib/examples/scribble/Scribble.java b/qtjava/javalib/examples/scribble/Scribble.java new file mode 100644 index 00000000..2adf609c --- /dev/null +++ b/qtjava/javalib/examples/scribble/Scribble.java @@ -0,0 +1,216 @@ +/*************************************************************************** +* $Id$ +** +* Copyright ( C ) 1992-2000 Trolltech AS. All rights reserved. +** +* This file is part of an example program for Qt. This example +* program may be used, distributed and modified without limitation. +** +****************************************************************************/ + +import org.kde.qt.*; +import java.util.*; + +class Scribble extends QMainWindow +{ + +protected Canvas canvas; + +protected QSpinBox bPWidth; +protected QToolButton bPColor, bSave, bClear; + + + +class Canvas extends QWidget +{ + + void setPenColor( QColor c ) + { pen.setColor( c ); } + + void setPenWidth( int w ) + { pen.setWidth( w ); } + + QColor penColor() + { return pen.color(); } + + int penWidth() + { return pen.width(); } + + +protected QPen pen; +protected QPointArray polyline; + +protected boolean mousePressed; + +protected QPixmap buffer = new QPixmap(); + +public boolean no_writing = false; + +Canvas( QWidget parent ) +{ + this(parent, null); +} + +Canvas( QWidget parent, String name ) +{ + super( parent, name, WStaticContents ); + pen = new QPen( Qt.red(), 3 ); + polyline = new QPointArray(3); + + if ((qApp().args().length > 0) && !buffer.load(qApp().args()[0])) + buffer.fill( colorGroup().base() ); + setBackgroundMode( QWidget.PaletteBase ); + setCursor( Qt.crossCursor() ); +} + +void save( String filename, String format ) +{ + if ( !no_writing ) + buffer.save( filename, format.toUpperCase() ); +} + +void clearScreen() +{ + buffer.fill( colorGroup().base() ); + repaint( false ); +} + +protected void mousePressEvent( QMouseEvent e ) +{ + mousePressed = true; + polyline.setPoint(0, e.pos()); + polyline.setPoint(1, e.pos()); + polyline.setPoint(2, e.pos()); +} + +protected void mouseReleaseEvent( QMouseEvent e ) +{ + mousePressed = false; +} + +protected void mouseMoveEvent( QMouseEvent e ) +{ + if ( mousePressed ) { + QPainter painter = new QPainter(); + painter.begin( buffer ); + painter.setPen( pen ); + polyline.setPoint(2, polyline.at(1)); + polyline.setPoint(1, polyline.at(0)); + polyline.setPoint(0, e.pos()); + painter.drawPolyline( polyline ); + painter.end(); + + QRect r = polyline.boundingRect(); + r = r.normalize(); + r.setLeft( r.left() - penWidth() ); + r.setTop( r.top() - penWidth() ); + r.setRight( r.right() + penWidth() ); + r.setBottom( r.bottom() + penWidth() ); + + bitBlt( this, r.x(), r.y(), buffer, r.x(), r.y(), r.width(), r.height() ); + } +} + +protected void resizeEvent( QResizeEvent e ) +{ + super.resizeEvent( e ); + + int w = width() > buffer.width() ? + width() : buffer.width(); + int h = height() > buffer.height() ? + height() : buffer.height(); + + QPixmap tmp = new QPixmap( buffer ); + buffer.resize( w, h ); + buffer.fill( colorGroup().base() ); + bitBlt( buffer, 0, 0, tmp, 0, 0, tmp.width(), tmp.height() ); +} + +protected void paintEvent( QPaintEvent e ) +{ + super.paintEvent( e ); + ArrayList rects = e.region().rects(); + for ( int i = 0; i < rects.size(); i++ ) { + QRect r = (QRect) rects.get(i); + bitBlt( this, r.x(), r.y(), buffer, r.x(), r.y(), r.width(), r.height() ); + } +} + +} + +//------------------------------------------------------ + +Scribble( ) +{ + this(null, null); +} + +Scribble( QWidget parent, String name ) +{ + super( parent, name ); + canvas = new Canvas( this ); + setCentralWidget( canvas ); + + QToolBar tools = new QToolBar( this ); + + bSave = new QToolButton( new QIconSet(), "Save", "Save as PNG image", this, SLOT(" slotSave()"), tools ); + bSave.setText( "Save as..." ); + + tools.addSeparator(); + + bPColor = new QToolButton( new QIconSet(), "Choose Pen Color", "Choose Pen Color", this, SLOT(" slotColor()"), tools ); + bPColor.setText( "Choose Pen Color..." ); + + tools.addSeparator(); + + bPWidth = new QSpinBox( 1, 20, 1, tools ); + QToolTip.add( bPWidth, "Choose Pen Width" ); + connect( bPWidth, SIGNAL(" valueChanged( int )"), this, SLOT(" slotWidth( int )") ); + bPWidth.setValue( 3 ); + + tools.addSeparator(); + + bClear = new QToolButton( new QIconSet(), "Clear Screen", "Clear Screen", this, SLOT(" slotClear()"), tools ); + bClear.setText( "Clear Screen" ); +} + +void slotSave() +{ + QPopupMenu menu = new QPopupMenu( null ); + HashMap formats = new HashMap(); + + for ( int i = 0; i < QImageIO.outputFormats().size(); i++ ) { + String str = (String) QImageIO.outputFormats().get( i ); + formats.put( new Integer(menu.insertItem( str + "..." )), str ); + } + + menu.setMouseTracking( true ); + int id = menu.exec( bSave.mapToGlobal( new QPoint( 0, bSave.height() + 1 ) ) ); + + if ( id != -1 ) { + String format = (String) formats.get( new Integer(id) ); + + String filename = QFileDialog.getSaveFileName( "", "*." + format.toLowerCase(), this ); + if ( !filename.equals("") ) + canvas.save( filename, format ); + } + +} + +void slotColor() +{ + QColor c = QColorDialog.getColor( canvas.penColor(), this ); + if ( c.isValid() ) + canvas.setPenColor( c ); +} + +void slotWidth( int w ) +{ + canvas.setPenWidth( w ); +} + +void slotClear() +{ + canvas.clearScreen(); +} +} |