source: trunk/examples/iconview/simple_dd/main.cpp

Last change on this file 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: 4.1 KB
Line 
1/****************************************************************************
2** $Id: main.cpp 160 2006-12-11 20:15:57Z dmik $
3**
4** Copyright (C) 1992-2001 Trolltech AS. All rights reserved.
5**
6** This file is part of an example program for Qt. This example
7** program may be used, distributed and modified without limitation.
8**
9*****************************************************************************/
10
11#include "main.h"
12
13const char* red_icon[]={
14"16 16 2 1",
15"r c red",
16". c None",
17"................",
18"................",
19"..rrrrrrrrrrrr..",
20"..rrrrrrrrrrrr..",
21"..rrrrrrrrrrrr..",
22"..rrr......rrr..",
23"..rrr......rrr..",
24"..rrr......rrr..",
25"..rrr......rrr..",
26"..rrr......rrr..",
27"..rrr......rrr..",
28"..rrrrrrrrrrrr..",
29"..rrrrrrrrrrrr..",
30"..rrrrrrrrrrrr..",
31"................",
32"................"};
33
34const char* blue_icon[]={
35"16 16 2 1",
36"b c blue",
37". c None",
38"................",
39"................",
40"..bbbbbbbbbbbb..",
41"..bbbbbbbbbbbb..",
42"..bbbbbbbbbbbb..",
43"..bbb......bbb..",
44"..bbb......bbb..",
45"..bbb......bbb..",
46"..bbb......bbb..",
47"..bbb......bbb..",
48"..bbb......bbb..",
49"..bbbbbbbbbbbb..",
50"..bbbbbbbbbbbb..",
51"..bbbbbbbbbbbb..",
52"................",
53"................"};
54
55const char* green_icon[]={
56"16 16 2 1",
57"g c green",
58". c None",
59"................",
60"................",
61"..gggggggggggg..",
62"..gggggggggggg..",
63"..gggggggggggg..",
64"..ggg......ggg..",
65"..ggg......ggg..",
66"..ggg......ggg..",
67"..ggg......ggg..",
68"..ggg......ggg..",
69"..ggg......ggg..",
70"..gggggggggggg..",
71"..gggggggggggg..",
72"..gggggggggggg..",
73"................",
74"................"};
75
76
77// ListBox -- low level drag and drop
78
79DDListBox::DDListBox( QWidget * parent, const char * name, WFlags f ) :
80 QListBox( parent, name, f )
81{
82 setAcceptDrops( TRUE );
83 dragging = FALSE;
84}
85
86
87void DDListBox::dragEnterEvent( QDragEnterEvent *evt )
88{
89 if ( QTextDrag::canDecode( evt ) )
90 evt->accept();
91}
92
93
94void DDListBox::dropEvent( QDropEvent *evt )
95{
96 QString text;
97
98 if ( QTextDrag::decode( evt, text ) )
99 insertItem( text );
100}
101
102
103void DDListBox::mousePressEvent( QMouseEvent *evt )
104{
105 QListBox::mousePressEvent( evt );
106 dragging = TRUE;
107}
108
109
110void DDListBox::mouseMoveEvent( QMouseEvent * )
111{
112 if ( dragging ) {
113 QDragObject *d = new QTextDrag( currentText(), this );
114 d->dragCopy(); // do NOT delete d.
115 dragging = FALSE;
116 }
117}
118
119
120// IconViewIcon -- high level drag and drop
121
122
123bool DDIconViewItem::acceptDrop( const QMimeSource *mime ) const
124{
125 if ( mime->provides( "text/plain" ) )
126 return TRUE;
127 return FALSE;
128}
129
130
131void DDIconViewItem::dropped( QDropEvent *evt, const QValueList<QIconDragItem>& )
132{
133 QString label;
134
135 if ( QTextDrag::decode( evt, label ) )
136 setText( label );
137}
138
139
140// IconView -- high level drag and drop
141
142QDragObject *DDIconView::dragObject()
143{
144 return new QTextDrag( currentItem()->text(), this );
145}
146
147void DDIconView::slotNewItem( QDropEvent *evt, const QValueList<QIconDragItem>& )
148{
149 QString label;
150
151 if ( QTextDrag::decode( evt, label ) ) {
152 DDIconViewItem *item = new DDIconViewItem( this, label );
153 item->setRenameEnabled( TRUE );
154 }
155}
156
157
158
159int main( int argc, char *argv[] )
160{
161 QApplication app( argc, argv );
162
163 // Create and show the widgets
164 QSplitter *split = new QSplitter();
165 DDIconView *iv = new DDIconView( split );
166 (void) new DDListBox( split );
167 app.setMainWidget( split );
168 split->resize( 600, 400 );
169 split->show();
170
171 // Set up the connection so that we can drop items into the icon view
172 QObject::connect(
173 iv, SIGNAL(dropped(QDropEvent*, const QValueList<QIconDragItem>&)),
174 iv, SLOT(slotNewItem(QDropEvent*, const QValueList<QIconDragItem>&)));
175
176 // Populate the QIconView with icons
177 DDIconViewItem *item;
178 item = new DDIconViewItem( iv, "Red", QPixmap( red_icon ) );
179 item->setRenameEnabled( TRUE );
180 item = new DDIconViewItem( iv, "Green", QPixmap( green_icon ) );
181 item->setRenameEnabled( TRUE );
182 item = new DDIconViewItem( iv, "Blue", QPixmap( blue_icon ) );
183 item->setRenameEnabled( TRUE );
184
185 return app.exec();
186}
187
188
Note: See TracBrowser for help on using the repository browser.