comparison examples/draganddrop/dropsite/dropsitewindow.d @ 19:d54443f1ce1e

add dropsite example
author mandel
date Thu, 14 May 2009 18:20:24 +0000
parents
children e747af2e6b46
comparison
equal deleted inserted replaced
18:e9f0d27a8213 19:d54443f1ce1e
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Qt Software Information (qt-info@nokia.com)
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial Usage
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Nokia.
14 **
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22 **
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26 ** package.
27 **
28 ** GNU General Public License Usage
29 ** Alternatively, this file may be used under the terms of the GNU
30 ** General Public License version 3.0 as published by the Free Software
31 ** Foundation and appearing in the file LICENSE.GPL included in the
32 ** packaging of this file. Please review the following information to
33 ** ensure the GNU General Public License version 3.0 requirements will be
34 ** met: http://www.gnu.org/copyleft/gpl.html.
35 **
36 ** If you are unsure which license is appropriate for your use, please
37 ** contact the sales department at qt-sales@nokia.com.
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 module dropsitewindow;
43
44 import tango.text.Util;
45 import tango.text.Ascii;
46 import tango.text.convert.Format;
47
48 import qt.gui.QWidget;
49 import qt.gui.QLabel;
50 import qt.gui.QTableWidget;
51 import qt.gui.QPushButton;
52 import qt.gui.QVBoxLayout;
53 import qt.gui.QDialogButtonBox;
54
55 import droparea;
56
57
58 class DropSiteWindow : public QWidget
59 {
60 public:
61
62 this()
63 {
64 abstractLabel = new QLabel(tr("This example accepts drags from other "
65 "applications and displays the MIME types "
66 "provided by the drag object."));
67
68 abstractLabel.setWordWrap(true);
69 abstractLabel.adjustSize();
70
71 dropArea = new DropArea;
72 dropArea.changed.connect(&updateFormatsTable);
73
74 char[][] labels;
75 labels ~= tr("Format");
76 labels ~= tr("Content");
77
78 formatsTable = new QTableWidget;
79 formatsTable.setColumnCount(2);
80 formatsTable.setEditTriggers(QAbstractItemView.NoEditTriggers);
81 formatsTable.setHorizontalHeaderLabels(labels);
82 formatsTable.horizontalHeader().setStretchLastSection(true);
83
84 clearButton = new QPushButton(tr("Clear"));
85 quitButton = new QPushButton(tr("Quit"));
86
87 buttonBox = new QDialogButtonBox;
88 buttonBox.addButton(clearButton, QDialogButtonBox.ActionRole);
89 buttonBox.addButton(quitButton, QDialogButtonBox.RejectRole);
90
91 quitButton.pressed.connect(&close);
92 clearButton.pressed.connect(&dropArea.clearArea);
93
94 QVBoxLayout mainLayout = new QVBoxLayout;
95 mainLayout.addWidget(abstractLabel);
96 mainLayout.addWidget(dropArea);
97 mainLayout.addWidget(formatsTable);
98 mainLayout.addWidget(buttonBox);
99 setLayout(mainLayout);
100
101 setWindowTitle(tr("Drop Site"));
102 setMinimumSize(350, 500);
103 }
104
105 void updateFormatsTable(QMimeData mimeData)
106 {
107 formatsTable.setRowCount(0);
108 if (!mimeData)
109 return;
110
111 foreach (char[] format; mimeData.formats()) {
112 QTableWidgetItem formatItem = new QTableWidgetItem(format);
113 formatItem.setFlags(Qt.ItemIsEnabled);
114 formatItem.setTextAlignment(Qt.AlignTop | Qt.AlignLeft);
115
116 char[] text;
117 if (format == "text/plain") {
118 text = trim(mimeData.text());
119 } else if (format == "text/html") {
120 text = trim(mimeData.html());
121 } else if (format == "text/uri-list") {
122 QUrl[] urlList = mimeData.urls();
123 for (int i = 0; i < urlList.length && i < 32; ++i) {
124 char[] url = urlList[i].path();
125 text ~= url ~ " ";
126 }
127 } else {
128 QByteArray data = mimeData.data(format);
129 for (int i = 0; i < data.size() && i < 32; ++i) {
130 char[] hex = toUpper(Format("{0:x}", data.at(i)));
131 text ~= hex ~ " ";
132 }
133 }
134
135 int row = formatsTable.rowCount();
136 formatsTable.insertRow(row);
137 formatsTable.setItem(row, 0, new QTableWidgetItem(format));
138 formatsTable.setItem(row, 1, new QTableWidgetItem(text));
139 }
140
141 formatsTable.resizeColumnToContents(0);
142 }
143
144 private:
145
146 DropArea dropArea;
147 QLabel abstractLabel;
148 QTableWidget formatsTable;
149
150 QPushButton clearButton;
151 QPushButton quitButton;
152 QDialogButtonBox buttonBox;
153 }