annotate examples/draganddrop/dropsite/dropsitewindow.d @ 204:6aeaf24018d7

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