diff options
Diffstat (limited to 'qtjava/javalib/examples/dragdrop')
-rw-r--r-- | qtjava/javalib/examples/dragdrop/DropSite.java | 167 | ||||
-rw-r--r-- | qtjava/javalib/examples/dragdrop/Main.java | 87 | ||||
-rw-r--r-- | qtjava/javalib/examples/dragdrop/SecretDrag.java | 53 | ||||
-rw-r--r-- | qtjava/javalib/examples/dragdrop/SecretSource.java | 69 | ||||
-rw-r--r-- | qtjava/javalib/examples/dragdrop/trolltech.bmp | bin | 0 -> 30054 bytes | |||
-rw-r--r-- | qtjava/javalib/examples/dragdrop/trolltech.gif | bin | 0 -> 42629 bytes |
6 files changed, 376 insertions, 0 deletions
diff --git a/qtjava/javalib/examples/dragdrop/DropSite.java b/qtjava/javalib/examples/dragdrop/DropSite.java new file mode 100644 index 00000000..3584f1e4 --- /dev/null +++ b/qtjava/javalib/examples/dragdrop/DropSite.java @@ -0,0 +1,167 @@ +/*************************************************************************** +* $Id$ +** +* Drop site example implementation +** +* Created : 979899 +** +* Copyright (C) 1997 by 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 DropSite extends QLabel +{ + SecretSource s; + +void setSecretSource(SecretSource source) +{ + s = source; +} + +DropSite( QWidget parent ) +{ + this(parent, null); +} + +DropSite( QWidget parent, String name ) +{ + super( parent, name ); + setAcceptDrops(true); +} + + +{ + // nothing necessary +} + + +protected void dragMoveEvent( QDragMoveEvent e ) +{ + // Check if you want the drag at e.pos()... + // Give the user some feedback... +} + + +protected void dragEnterEvent( QDragEnterEvent e ) +{ + // Check if you want the drag... + if ( SecretDrag.canDecode( e ) + || QTextDrag.canDecode( e ) + || QImageDrag.canDecode( e ) + || QUriDrag.canDecode( e ) ) { + e.accept(); + } + + // Give the user some feedback... + String t = ""; + for( int i=0; e.format( i ) != null; i++ ) { + t += "\n"; + t += e.format( i ); + } + emit("message", t ); + setBackgroundColor(white()); +} + +protected void dragLeaveEvent( QDragLeaveEvent e ) +{ + // Give the user some feedback... + emit("message", ""); + setBackgroundColor(lightGray()); +} + + +protected void dropEvent( QDropEvent e ) +{ + setBackgroundColor(lightGray()); + + // Try to decode to the data you understand... + + StringBuffer str = new StringBuffer(""); + if ( QTextDrag.decode( e, str ) ) { + setText( str.toString() ); + setMinimumSize( minimumSize().expandedTo( sizeHint() ) ); + return; + } + + QPixmap pm = new QPixmap(); + if ( QImageDrag.decode( e, pm ) ) { + setPixmap( pm ); + setMinimumSize( minimumSize().expandedTo( sizeHint() ) ); + return; + } + + ArrayList strings = new ArrayList(); + if ( QUriDrag.decode( e, strings ) ) { + String m = "Full URLs:\n"; + Iterator it = strings.iterator(); + while (it.hasNext()) + m += " " + (String) it.next() + "\n"; + ArrayList files = new ArrayList(); + if ( QUriDrag.decodeLocalFiles( e, files ) ) { + m = m + "Files:\n"; + Iterator i = strings.iterator(); + while (i.hasNext()) + m = m + " " + (String) i.next() + "\n"; + } + setText( m ); + setMinimumSize( minimumSize().expandedTo( sizeHint() ) ); + return; + } + + if ( SecretDrag.decode( e, str ) ) { + setText( str.toString() ); + setMinimumSize( minimumSize().expandedTo( sizeHint() ) ); + return; + } +} + +protected void mousePressEvent( QMouseEvent e ) +{ + QDragObject drobj; + if ( pixmap() != null ) { + drobj = new QImageDrag( pixmap().convertToImage(), this ); + QPixmap pm = new QPixmap(); + pm.convertFromImage(pixmap().convertToImage().smoothScale( + pixmap().width()/3,pixmap().height()/3)); + drobj.setPixmap(pm,new QPoint(-5,-7)); + // Try it. +// new DragMoviePlayer(drobj); + } else { + drobj = new QTextDrag( text(), this ); + } + drobj.dragCopy(); +} + +class DragMoviePlayer extends QObject { + QDragObject dobj; + QMovie movie; + +DragMoviePlayer( QDragObject p ) +{ +// QObject(p), + dobj = p; + movie = new QMovie("trolltech.gif" ); + movie.connectUpdate(this,SLOT("updatePixmap(QRect)")); +} + +void updatePixmap( QRect rect ) +{ + dobj.setPixmap(movie.framePixmap()); +} + + +} + +void backgroundColorChange( QColor color ) +{ + // Reduce flicker by using repaint() rather than update() + repaint(); +} + +} diff --git a/qtjava/javalib/examples/dragdrop/Main.java b/qtjava/javalib/examples/dragdrop/Main.java new file mode 100644 index 00000000..3297aaec --- /dev/null +++ b/qtjava/javalib/examples/dragdrop/Main.java @@ -0,0 +1,87 @@ +/*************************************************************************** +* $Id$ +** +* Ritual main() for Qt applications +** +* Copyright (C) 1996 by 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 { + + +static DropSite addStuff( QWidget parent, boolean image ) +{ + return addStuff(parent, image, false); +} + +static DropSite addStuff( QWidget parent, boolean image, boolean secret ) +{ + QVBoxLayout tll = new QVBoxLayout( parent, 10 ); + DropSite d = new DropSite( parent ); + d.setFrameStyle( QFrame.Sunken + QFrame.WinPanel ); + tll.addWidget( d ); + if ( image ) { + QPixmap stuff = new QPixmap(); + if ( !stuff.load( "trolltech.bmp" ) ) { + stuff = new QPixmap(20,20); + stuff.fill(Qt.green()); + } + d.setPixmap( stuff ); + } else { + d.setText("Drag and Drop"); + } + d.setFont(new QFont("Helvetica",18)); + if ( secret ) { + SecretSource s = new SecretSource( (byte) 42, parent ); + tll.addWidget( s ); + d.setSecretSource( s); + } + + QLabel format = new QLabel( "\n\n\n\nNone\n\n\n\n", parent ); + tll.addWidget( format ); + tll.activate(); + parent.resize( parent.sizeHint() ); + + QObject.connect( d, Qt.SIGNAL("message(String)"), + format, Qt.SLOT("setText(String)") ); + return d; +} + + + + +public static void main( String[] args ) +{ + QApplication a = new QApplication( args ); + + QWidget mw = new QWidget(); + DropSite d1 = addStuff( mw, true ); + mw.setCaption( "Qt Example - Drag and Drop" ); + mw.show(); + + QWidget mw2 = new QWidget(); + DropSite d2 = addStuff( mw2, false ); + mw2.setCaption( "Qt Example - Drag and Drop" ); + mw2.show(); + + QWidget mw3 = new QWidget(); + DropSite d3 = addStuff( mw3, true, true ); + mw3.setCaption( "Qt Example - Drag and Drop" ); + mw3.show(); + + QObject.connect(Qt.qApp(),Qt.SIGNAL("lastWindowClosed()"),Qt.qApp(),Qt.SLOT("quit()")); + a.exec(); + return; +} + +static { + qtjava.initialize(); +} + +} diff --git a/qtjava/javalib/examples/dragdrop/SecretDrag.java b/qtjava/javalib/examples/dragdrop/SecretDrag.java new file mode 100644 index 00000000..fd12aed3 --- /dev/null +++ b/qtjava/javalib/examples/dragdrop/SecretDrag.java @@ -0,0 +1,53 @@ +/*************************************************************************** +* $Id$ +** +* Custom MIME type implementation example +** +* Created : 979899 +** +* Copyright (C) 1997 by 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.*; + + +class SecretDrag extends QStoredDrag { + +//create the object withe the secret byte +public SecretDrag( byte secret, QWidget parent, String name ) +{ + super( "secret/magic", parent, name ); + byte[] data = { 0 }; + data[0]= secret; + setEncodedData( data ); +} + +public SecretDrag( byte secret, QWidget parent ) +{ + this(secret, parent, null); +} + + +public static boolean canDecode( QDragMoveEvent e ) +{ + return e.provides( "secret/magic" ); +} + +//decode it into a string +public static boolean decode( QDropEvent e, StringBuffer str ) +{ + byte[] payload = e.data( "secret/magic" ); + if ( payload.length > 0 ) { + e.accept(); + String msg = "The secret number is " + payload[0]; + str.setLength(0); + str.append(msg); + return true; + } + return false; +} + +} diff --git a/qtjava/javalib/examples/dragdrop/SecretSource.java b/qtjava/javalib/examples/dragdrop/SecretSource.java new file mode 100644 index 00000000..00824dee --- /dev/null +++ b/qtjava/javalib/examples/dragdrop/SecretSource.java @@ -0,0 +1,69 @@ +/*************************************************************************** +* $Id$ +** +* Custom MIME type implementation example +** +* Created : 979899 +** +* Copyright (C) 1997 by 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.*; + +class SecretSource extends QLabel +{ +private int mySecret; + +SecretSource( byte secret, QWidget parent ) +{ + this(secret, parent, null); +} + +SecretSource( byte secret, QWidget parent, String name ) +{ + super( "Secret", parent, name ); + setBackgroundColor( blue().light() ); + setFrameStyle( Box | Sunken ); + setMinimumHeight( sizeHint().height() * 2 ); + setAlignment( AlignCenter ); + mySecret = secret; +} + +{ +} + +/* XPM */ +static String picture_xpm[] = { +"16 16 3 1", +" c None", +". c #000000", +"X c #FFFF00", +" ..... ", +" ..XXXXX.. ", +" .XXXXXXXXX. ", +" .XXXXXXXXXXX. ", +" .XX..XXX..XX. ", +".XXXXXXXXXXXXX. ", +".XX...XXX...XX. ", +".XXX..XXX..XXX. ", +".XXXXXXXXXXXXX. ", +".XXXXXX.XXXXXX. ", +" .XX.XX.XX.XX. ", +" .XXX..X..XXX. ", +" .XXXXXXXXX. ", +" ..XXXXX.. ", +" ..... ", +" "}; + +protected void mousePressEvent( QMouseEvent e ) +{ + SecretDrag sd = new SecretDrag( (byte) mySecret, this ); + sd.setPixmap(new QPixmap(picture_xpm),new QPoint(8,8)); + sd.dragCopy(); + mySecret++; +} + +} diff --git a/qtjava/javalib/examples/dragdrop/trolltech.bmp b/qtjava/javalib/examples/dragdrop/trolltech.bmp Binary files differnew file mode 100644 index 00000000..220861e2 --- /dev/null +++ b/qtjava/javalib/examples/dragdrop/trolltech.bmp diff --git a/qtjava/javalib/examples/dragdrop/trolltech.gif b/qtjava/javalib/examples/dragdrop/trolltech.gif Binary files differnew file mode 100644 index 00000000..f674369e --- /dev/null +++ b/qtjava/javalib/examples/dragdrop/trolltech.gif |