source: trunk/doc/src/snippets/droparea.cpp@ 348

Last change on this file since 348 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 4.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** $QT_BEGIN_LICENSE:LGPL$
7** Commercial Usage
8** Licensees holding valid Qt Commercial licenses may use this file in
9** accordance with the Qt Commercial License Agreement provided with the
10** Software or, alternatively, in accordance with the terms contained in
11** a written agreement between you and Nokia.
12**
13** GNU Lesser General Public License Usage
14** Alternatively, this file may be used under the terms of the GNU Lesser
15** General Public License version 2.1 as published by the Free Software
16** Foundation and appearing in the file LICENSE.LGPL included in the
17** packaging of this file. Please review the following information to
18** ensure the GNU Lesser General Public License version 2.1 requirements
19** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
20**
21** In addition, as a special exception, Nokia gives you certain
22** additional rights. These rights are described in the Nokia Qt LGPL
23** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
24** package.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 3.0 as published by the Free Software
29** Foundation and appearing in the file LICENSE.GPL included in the
30** packaging of this file. Please review the following information to
31** ensure the GNU General Public License version 3.0 requirements will be
32** met: http://www.gnu.org/copyleft/gpl.html.
33**
34** If you are unsure which license is appropriate for your use, please
35** contact the sales department at [email protected].
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include <QtGui>
41
42#include "droparea.h"
43
44DropArea::DropArea(QWidget *parent)
45 : QLabel(parent)
46{
47 setMinimumSize(200, 200);
48 setFrameStyle(QFrame::Sunken | QFrame::StyledPanel);
49 setAlignment(Qt::AlignCenter);
50 setAcceptDrops(true);
51 setAutoFillBackground(true);
52 clear();
53}
54
55void DropArea::dragEnterEvent(QDragEnterEvent *event)
56{
57 setText(tr("<drop content>"));
58 setBackgroundRole(QPalette::Highlight);
59
60 event->acceptProposedAction();
61 emit changed(event->mimeData());
62}
63
64void DropArea::dragMoveEvent(QDragMoveEvent *event)
65{
66 event->acceptProposedAction();
67}
68
69void DropArea::dropEvent(QDropEvent *event)
70{
71 const QMimeData *mimeData = event->mimeData();
72
73 if (mimeData->hasImage()) {
74 setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
75 } else if (mimeData->hasHtml()) {
76 setText(mimeData->html());
77 setTextFormat(Qt::RichText);
78 } else if (mimeData->hasText()) {
79 setText(mimeData->text());
80 setTextFormat(Qt::PlainText);
81 } else {
82 setText(tr("Cannot display data"));
83 }
84
85 setBackgroundRole(QPalette::Dark);
86 event->acceptProposedAction();
87}
88
89//![0]
90void DropArea::paste()
91{
92 const QClipboard *clipboard = QApplication::clipboard();
93 const QMimeData *mimeData = clipboard->mimeData();
94
95 if (mimeData->hasImage()) {
96 setPixmap(qvariant_cast<QPixmap>(mimeData->imageData()));
97 } else if (mimeData->hasHtml()) {
98 setText(mimeData->html());
99 setTextFormat(Qt::RichText);
100 } else if (mimeData->hasText()) {
101 setText(mimeData->text());
102 setTextFormat(Qt::PlainText);
103 } else {
104 setText(tr("Cannot display data"));
105 }
106//![0]
107
108 emit changed(mimeData);
109 setBackgroundRole(QPalette::Dark);
110 //event->acceptProposedAction();
111}
112
113void DropArea::dragLeaveEvent(QDragLeaveEvent *event)
114{
115 clear();
116 event->accept();
117}
118
119void DropArea::clear()
120{
121 setText(tr("<drop content>"));
122 setBackgroundRole(QPalette::Dark);
123
124 emit changed();
125}
126
127QPixmap DropArea::extractPixmap(const QByteArray &data, const QString &format)
128{
129 QList<QByteArray> imageFormats = QImageReader::supportedImageFormats();
130 QPixmap pixmap;
131
132 foreach (QByteArray imageFormat, imageFormats) {
133 if (format.mid(6) == QString(imageFormat)) {
134 pixmap.loadFromData(data, imageFormat);
135 break;
136 }
137 }
138 return pixmap;
139}
Note: See TracBrowser for help on using the repository browser.