source: trunk/examples/dragdrop/dropsite.cpp@ 168

Last change on this file since 168 was 160, checked in by dmik, 19 years ago

Imported table and iconview modules and a bunch of dependent examples from the official release 3.3.1 from Trolltech.

  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1/****************************************************************************
2** $Id: dropsite.cpp 160 2006-12-11 20:15:57Z dmik $
3**
4** Drop site example implementation
5**
6** Created : 979899
7**
8** Copyright (C) 1997 by Trolltech AS. All rights reserved.
9**
10** This file is part of an example program for Qt. This example
11** program may be used, distributed and modified without limitation.
12**
13*****************************************************************************/
14
15#include "dropsite.h"
16#include "secret.h"
17#include <qevent.h>
18#include <qpixmap.h>
19#include <qdragobject.h>
20#include <qimage.h>
21#include <qdir.h>
22
23
24DropSite::DropSite( QWidget * parent, const char * name )
25 : QLabel( parent, name )
26{
27 setAcceptDrops(TRUE);
28}
29
30
31DropSite::~DropSite()
32{
33 // nothing necessary
34}
35
36
37void DropSite::dragMoveEvent( QDragMoveEvent *e )
38{
39 // Check if you want the drag at e->pos()...
40 // Give the user some feedback - only copy is possible
41 e->acceptAction( e->action() == QDropEvent::Copy );
42}
43
44
45void DropSite::dragEnterEvent( QDragEnterEvent *e )
46{
47 // Check if you want the drag...
48 if ( SecretDrag::canDecode( e )
49 || QTextDrag::canDecode( e )
50 || QImageDrag::canDecode( e )
51 || QUriDrag::canDecode( e ) )
52 {
53 e->accept();
54 }
55
56
57 // Give the user some feedback...
58 QString t;
59 const char *f;
60 for( int i=0; (f=e->format( i )); i++ ) {
61 if ( *(f) ) {
62 if ( !t.isEmpty() )
63 t += "\n";
64 t += f;
65 }
66 }
67 emit message( t );
68 setBackgroundColor(white);
69}
70
71void DropSite::dragLeaveEvent( QDragLeaveEvent * )
72{
73 // Give the user some feedback...
74 emit message("");
75 setBackgroundColor(lightGray);
76}
77
78
79void DropSite::dropEvent( QDropEvent * e )
80{
81 setBackgroundColor(lightGray);
82
83 // Try to decode to the data you understand...
84 QStrList strings;
85 if ( QUriDrag::decode( e, strings ) ) {
86 QString m("Full URLs:\n");
87 for (const char* u=strings.first(); u; u=strings.next())
88 m = m + " " + u + '\n';
89 QStringList files;
90 if ( QUriDrag::decodeLocalFiles( e, files ) ) {
91 m += "Files:\n";
92 for (QStringList::Iterator i=files.begin(); i!=files.end(); ++i)
93 m = m + " " + QDir::convertSeparators(*i) + '\n';
94 }
95 setText( m );
96 setMinimumSize( minimumSize().expandedTo( sizeHint() ) );
97 return;
98 }
99
100 QString str;
101 if ( QTextDrag::decode( e, str ) ) {
102 setText( str );
103 setMinimumSize( minimumSize().expandedTo( sizeHint() ) );
104 return;
105 }
106
107 QPixmap pm;
108 if ( QImageDrag::decode( e, pm ) ) {
109 setPixmap( pm );
110 setMinimumSize( minimumSize().expandedTo( sizeHint() ) );
111 return;
112 }
113
114 if ( SecretDrag::decode( e, str ) ) {
115 setText( str );
116 setMinimumSize( minimumSize().expandedTo( sizeHint() ) );
117 return;
118 }
119}
120
121DragMoviePlayer::DragMoviePlayer( QDragObject* p ) :
122 QObject(p),
123 dobj(p),
124 movie("trolltech.gif" )
125{
126 movie.connectUpdate(this,SLOT(updatePixmap(const QRect&)));
127}
128
129void DragMoviePlayer::updatePixmap( const QRect& )
130{
131 dobj->setPixmap(movie.framePixmap());
132}
133
134void DropSite::mousePressEvent( QMouseEvent * /*e*/ )
135{
136 QDragObject *drobj;
137 if ( pixmap() ) {
138 drobj = new QImageDrag( pixmap()->convertToImage(), this );
139#if 1
140 QPixmap pm;
141 pm.convertFromImage(pixmap()->convertToImage().smoothScale(
142 pixmap()->width()/3,pixmap()->height()/3));
143 drobj->setPixmap(pm,QPoint(-5,-7));
144#else
145 // Try it.
146 (void)new DragMoviePlayer(drobj);
147#endif
148 } else {
149 drobj = new QTextDrag( text(), this );
150 }
151 drobj->dragCopy();
152}
153
154
155void DropSite::backgroundColorChange( const QColor & )
156{
157 // Reduce flicker by using repaint() rather than update()
158 repaint();
159}
Note: See TracBrowser for help on using the repository browser.