summaryrefslogtreecommitdiffstats
path: root/examples/dragdrop
diff options
context:
space:
mode:
authorTimothy Pearson <[email protected]>2011-07-10 15:24:15 -0500
committerTimothy Pearson <[email protected]>2011-07-10 15:24:15 -0500
commitbd0f3345a938b35ce6a12f6150373b0955b8dd12 (patch)
tree7a520322212d48ebcb9fbe1087e7fca28b76185c /examples/dragdrop
downloadqt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.tar.gz
qt3-bd0f3345a938b35ce6a12f6150373b0955b8dd12.zip
Add Qt3 development HEAD version
Diffstat (limited to 'examples/dragdrop')
-rw-r--r--examples/dragdrop/dragdrop.doc14
-rw-r--r--examples/dragdrop/dragdrop.pro13
-rw-r--r--examples/dragdrop/dropsite.cpp158
-rw-r--r--examples/dragdrop/dropsite.h55
-rw-r--r--examples/dragdrop/main.cpp73
-rw-r--r--examples/dragdrop/secret.cpp91
-rw-r--r--examples/dragdrop/secret.h42
-rw-r--r--examples/dragdrop/trolltech.bmpbin0 -> 30054 bytes
-rw-r--r--examples/dragdrop/trolltech.gifbin0 -> 42629 bytes
9 files changed, 446 insertions, 0 deletions
diff --git a/examples/dragdrop/dragdrop.doc b/examples/dragdrop/dragdrop.doc
new file mode 100644
index 0000000..25d3170
--- /dev/null
+++ b/examples/dragdrop/dragdrop.doc
@@ -0,0 +1,14 @@
+/*! \page dragdrop-example.html
+
+ \ingroup examples
+ \title Drag and Drop
+
+ This program demonstrates Qt's drag and drop functionality.
+
+ See $QTDIR/examples/dragdrop for the source code.
+
+*/
+
+
+
+
diff --git a/examples/dragdrop/dragdrop.pro b/examples/dragdrop/dragdrop.pro
new file mode 100644
index 0000000..e560fbf
--- /dev/null
+++ b/examples/dragdrop/dragdrop.pro
@@ -0,0 +1,13 @@
+TEMPLATE = app
+TARGET = dragdrop
+
+CONFIG += qt warn_on release
+DEPENDPATH = ../../include
+
+REQUIRES = full-config
+
+HEADERS = dropsite.h \
+ secret.h
+SOURCES = dropsite.cpp \
+ main.cpp \
+ secret.cpp
diff --git a/examples/dragdrop/dropsite.cpp b/examples/dragdrop/dropsite.cpp
new file mode 100644
index 0000000..86e0c27
--- /dev/null
+++ b/examples/dragdrop/dropsite.cpp
@@ -0,0 +1,158 @@
+/****************************************************************************
+**
+** Drop site example implementation
+**
+** Created : 979899
+**
+** Copyright (C) 1997-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include "dropsite.h"
+#include "secret.h"
+#include <qevent.h>
+#include <qpixmap.h>
+#include <qdragobject.h>
+#include <qimage.h>
+#include <qdir.h>
+
+
+DropSite::DropSite( QWidget * parent, const char * name )
+ : QLabel( parent, name )
+{
+ setAcceptDrops(TRUE);
+}
+
+
+DropSite::~DropSite()
+{
+ // nothing necessary
+}
+
+
+void DropSite::dragMoveEvent( QDragMoveEvent *e )
+{
+ // Check if you want the drag at e->pos()...
+ // Give the user some feedback - only copy is possible
+ e->acceptAction( e->action() == QDropEvent::Copy );
+}
+
+
+void DropSite::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...
+ QString t;
+ const char *f;
+ for( int i=0; (f=e->format( i )); i++ ) {
+ if ( *(f) ) {
+ if ( !t.isEmpty() )
+ t += "\n";
+ t += f;
+ }
+ }
+ emit message( t );
+ setBackgroundColor(white);
+}
+
+void DropSite::dragLeaveEvent( QDragLeaveEvent * )
+{
+ // Give the user some feedback...
+ emit message("");
+ setBackgroundColor(lightGray);
+}
+
+
+void DropSite::dropEvent( QDropEvent * e )
+{
+ setBackgroundColor(lightGray);
+
+ // Try to decode to the data you understand...
+ QStrList strings;
+ if ( QUriDrag::decode( e, strings ) ) {
+ QString m("Full URLs:\n");
+ for (const char* u=strings.first(); u; u=strings.next())
+ m = m + " " + u + '\n';
+ QStringList files;
+ if ( QUriDrag::decodeLocalFiles( e, files ) ) {
+ m += "Files:\n";
+ for (QStringList::Iterator i=files.begin(); i!=files.end(); ++i)
+ m = m + " " + QDir::convertSeparators(*i) + '\n';
+ }
+ setText( m );
+ setMinimumSize( minimumSize().expandedTo( sizeHint() ) );
+ return;
+ }
+
+ QString str;
+ if ( QTextDrag::decode( e, str ) ) {
+ setText( str );
+ setMinimumSize( minimumSize().expandedTo( sizeHint() ) );
+ return;
+ }
+
+ QPixmap pm;
+ if ( QImageDrag::decode( e, pm ) ) {
+ setPixmap( pm );
+ setMinimumSize( minimumSize().expandedTo( sizeHint() ) );
+ return;
+ }
+
+ if ( SecretDrag::decode( e, str ) ) {
+ setText( str );
+ setMinimumSize( minimumSize().expandedTo( sizeHint() ) );
+ return;
+ }
+}
+
+DragMoviePlayer::DragMoviePlayer( QDragObject* p ) :
+ QObject(p),
+ dobj(p),
+ movie("trolltech.gif" )
+{
+ movie.connectUpdate(this,SLOT(updatePixmap(const QRect&)));
+}
+
+void DragMoviePlayer::updatePixmap( const QRect& )
+{
+ dobj->setPixmap(movie.framePixmap());
+}
+
+void DropSite::mousePressEvent( QMouseEvent * /*e*/ )
+{
+ QDragObject *drobj;
+ if ( pixmap() ) {
+ drobj = new QImageDrag( pixmap()->convertToImage(), this );
+#if 1
+ QPixmap pm;
+ pm.convertFromImage(pixmap()->convertToImage().smoothScale(
+ pixmap()->width()/3,pixmap()->height()/3));
+ drobj->setPixmap(pm,QPoint(-5,-7));
+#else
+ // Try it.
+ (void)new DragMoviePlayer(drobj);
+#endif
+ } else {
+ drobj = new QTextDrag( text(), this );
+ }
+ drobj->dragCopy();
+}
+
+
+void DropSite::backgroundColorChange( const QColor & )
+{
+ // Reduce flicker by using repaint() rather than update()
+ repaint();
+}
diff --git a/examples/dragdrop/dropsite.h b/examples/dragdrop/dropsite.h
new file mode 100644
index 0000000..00f35e3
--- /dev/null
+++ b/examples/dragdrop/dropsite.h
@@ -0,0 +1,55 @@
+/****************************************************************************
+**
+** Drop site example implementation
+**
+** Created : 979899
+**
+** Copyright (C) 1997-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#ifndef DROPSITE_H
+#define DROPSITE_H
+
+#include <qlabel.h>
+#include <qmovie.h>
+#include "qdropsite.h"
+
+class QDragObject;
+
+class DropSite: public QLabel
+{
+ Q_OBJECT
+public:
+ DropSite( QWidget * parent = 0, const char * name = 0 );
+ ~DropSite();
+
+signals:
+ void message( const QString& );
+
+protected:
+ void dragEnterEvent( QDragEnterEvent * );
+ void dragMoveEvent( QDragMoveEvent * );
+ void dragLeaveEvent( QDragLeaveEvent * );
+ void dropEvent( QDropEvent * );
+ void backgroundColorChange( const QColor& );
+
+ // this is a normal even
+ void mousePressEvent( QMouseEvent * );
+};
+
+class DragMoviePlayer : public QObject {
+ Q_OBJECT
+ QDragObject* dobj;
+ QMovie movie;
+public:
+ DragMoviePlayer(QDragObject*);
+private slots:
+ void updatePixmap( const QRect& );
+};
+
+
+#endif
diff --git a/examples/dragdrop/main.cpp b/examples/dragdrop/main.cpp
new file mode 100644
index 0000000..e318518
--- /dev/null
+++ b/examples/dragdrop/main.cpp
@@ -0,0 +1,73 @@
+/****************************************************************************
+**
+** Ritual main() for Qt applications
+**
+** Copyright (C) 1996-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include <qapplication.h>
+#include "dropsite.h"
+#include "secret.h"
+#include <qlayout.h>
+#include <qcombobox.h>
+#include <qlabel.h>
+#include <qpixmap.h>
+
+static void addStuff( QWidget * parent, bool image, bool secret = FALSE )
+{
+ QVBoxLayout * tll = new QVBoxLayout( parent, 10 );
+ DropSite * d = new DropSite( parent );
+ d->setFrameStyle( QFrame::Sunken + QFrame::WinPanel );
+ tll->addWidget( d );
+ if ( image ) {
+ QPixmap stuff;
+ if ( !stuff.load( "trolltech.bmp" ) ) {
+ stuff = QPixmap(20,20);
+ stuff.fill(Qt::green);
+ }
+ d->setPixmap( stuff );
+ } else {
+ d->setText("Drag and Drop");
+ }
+ d->setFont(QFont("Helvetica",18));
+ if ( secret ) {
+ SecretSource *s = new SecretSource( 42, parent );
+ tll->addWidget( 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, SIGNAL(message(const QString&)),
+ format, SLOT(setText(const QString&)) );
+}
+
+
+int main( int argc, char ** argv )
+{
+ QApplication a( argc, argv );
+
+ QWidget mw;
+ addStuff( &mw, TRUE );
+ mw.setCaption( "Qt Example - Drag and Drop" );
+ mw.show();
+
+ QWidget mw2;
+ addStuff( &mw2, FALSE );
+ mw2.setCaption( "Qt Example - Drag and Drop" );
+ mw2.show();
+
+ QWidget mw3;
+ addStuff( &mw3, TRUE, TRUE );
+ mw3.setCaption( "Qt Example - Drag and Drop" );
+ mw3.show();
+
+ QObject::connect(qApp,SIGNAL(lastWindowClosed()),qApp,SLOT(quit()));
+ return a.exec();
+}
diff --git a/examples/dragdrop/secret.cpp b/examples/dragdrop/secret.cpp
new file mode 100644
index 0000000..c67669a
--- /dev/null
+++ b/examples/dragdrop/secret.cpp
@@ -0,0 +1,91 @@
+/****************************************************************************
+**
+** Custom MIME type implementation example
+**
+** Created : 979899
+**
+** Copyright (C) 1997-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#include "secret.h"
+#include <qevent.h>
+
+
+//create the object withe the secret byte
+SecretDrag::SecretDrag( uchar secret, QWidget * parent, const char * name )
+ : QStoredDrag( "secret/magic", parent, name )
+{
+ QByteArray data(1);
+ data[0]= secret;
+ setEncodedData( data );
+}
+
+
+bool SecretDrag::canDecode( QDragMoveEvent* e )
+{
+ return e->provides( "secret/magic" );
+}
+
+//decode it into a string
+bool SecretDrag::decode( QDropEvent* e, QString& str )
+{
+ QByteArray payload = e->data( "secret/magic" );
+ if ( payload.size() ) {
+ e->accept();
+ QString msg;
+ msg.sprintf("The secret number is %d", payload[0] );
+ str = msg;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+
+SecretSource::SecretSource( int secret, QWidget *parent, const char * name )
+ : QLabel( "Secret", parent, name )
+{
+ setBackgroundColor( blue.light() );
+ setFrameStyle( Box | Sunken );
+ setMinimumHeight( sizeHint().height()*2 );
+ setAlignment( AlignCenter );
+ mySecret = secret;
+}
+
+SecretSource::~SecretSource()
+{
+}
+
+/* XPM */
+static const char * 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.. ",
+" ..... ",
+" "};
+
+void SecretSource::mousePressEvent( QMouseEvent * /*e*/ )
+{
+ SecretDrag *sd = new SecretDrag( mySecret, this );
+ sd->setPixmap(QPixmap(picture_xpm),QPoint(8,8));
+ sd->dragCopy();
+ mySecret++;
+}
diff --git a/examples/dragdrop/secret.h b/examples/dragdrop/secret.h
new file mode 100644
index 0000000..98af540
--- /dev/null
+++ b/examples/dragdrop/secret.h
@@ -0,0 +1,42 @@
+/****************************************************************************
+**
+** Custom MIME type implementation example
+**
+** Created : 979899
+**
+** Copyright (C) 1997-2008 Trolltech ASA. All rights reserved.
+**
+** This file is part of an example program for Qt. This example
+** program may be used, distributed and modified without limitation.
+**
+*****************************************************************************/
+
+#ifndef SECRETDRAG_H
+#define SECRETDRAG_H
+
+#include <qdragobject.h>
+#include <qlabel.h>
+
+class SecretDrag: public QStoredDrag {
+public:
+ SecretDrag( uchar, QWidget * parent = 0, const char * name = 0 );
+ ~SecretDrag() {};
+
+ static bool canDecode( QDragMoveEvent* e );
+ static bool decode( QDropEvent* e, QString& s );
+};
+
+
+class SecretSource: public QLabel
+{
+public:
+ SecretSource( int secret, QWidget *parent = 0, const char * name = 0 );
+ ~SecretSource();
+
+protected:
+ void mousePressEvent( QMouseEvent * );
+private:
+ int mySecret;
+};
+
+#endif
diff --git a/examples/dragdrop/trolltech.bmp b/examples/dragdrop/trolltech.bmp
new file mode 100644
index 0000000..220861e
--- /dev/null
+++ b/examples/dragdrop/trolltech.bmp
Binary files differ
diff --git a/examples/dragdrop/trolltech.gif b/examples/dragdrop/trolltech.gif
new file mode 100644
index 0000000..f674369
--- /dev/null
+++ b/examples/dragdrop/trolltech.gif
Binary files differ