Mercurial > projects > qtd
annotate examples/mainwindows/dockwidgets/mainwindow.d @ 282:256ab6cb8e85
Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
author | eldar |
---|---|
date | Fri, 16 Oct 2009 02:43:59 +0000 |
parents | 8aaa84d48451 |
children | 7a3c43424dca |
rev | line source |
---|---|
1 | 1 /**************************************************************************** |
2 ** | |
3 ** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies). | |
4 ** Contact: Qt Software Information (qt-info@nokia.com) | |
5 ** | |
6 ** This file is part of the example classes of the Qt Toolkit. | |
7 ** | |
8 ** Commercial Usage | |
9 ** Licensees holding valid Qt Commercial licenses may use this file in | |
10 ** accordance with the Qt Commercial License Agreement provided with the | |
11 ** Software or, alternatively, in accordance with the terms contained in | |
12 ** a written agreement between you and Nokia. | |
13 ** | |
14 ** | |
15 ** GNU General Public License Usage | |
16 ** Alternatively, this file may be used under the terms of the GNU | |
17 ** General Public License versions 2.0 or 3.0 as published by the Free | |
18 ** Software Foundation and appearing in the file LICENSE.GPL included in | |
19 ** the packaging of this file. Please review the following information | |
20 ** to ensure GNU General Public Licensing requirements will be met: | |
21 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and | |
22 ** http://www.gnu.org/copyleft/gpl.html. In addition, as a special | |
23 ** exception, Nokia gives you certain additional rights. These rights | |
24 ** are described in the Nokia Qt GPL Exception version 1.3, included in | |
25 ** the file GPL_EXCEPTION.txt in this package. | |
26 ** | |
27 ** Qt for Windows(R) Licensees | |
28 ** As a special exception, Nokia, as the sole copyright holder for Qt | |
29 ** Designer, grants users of the Qt/Eclipse Integration plug-in the | |
30 ** right for the Qt/Eclipse Integration to link to functionality | |
31 ** provided by Qt Designer and its related libraries. | |
32 ** | |
33 ** If you are unsure which license is appropriate for your use, please | |
34 ** contact the sales department at qt-sales@nokia.com. | |
35 ** | |
36 ****************************************************************************/ | |
37 | |
38 | |
39 import qt.gui.QMainWindow; | |
40 | |
41 import qt.gui.QListWidget; | |
42 import qt.gui.QMenu; | |
43 import qt.gui.QToolBar; | |
44 import qt.gui.QDockWidget; | |
45 import qt.gui.QListWidget; | |
46 import qt.gui.QMessageBox; | |
47 import qt.gui.QTextEdit; | |
48 import qt.gui.QTextCursor; | |
49 import qt.gui.QTextFrame; | |
50 import qt.gui.QTextFrameFormat; | |
51 import qt.gui.QTextCharFormat; | |
52 import qt.gui.QTextTableFormat; | |
53 import qt.gui.QAction; | |
54 import qt.gui.QStatusBar; | |
55 import qt.gui.QFont; | |
56 import qt.gui.QIcon; | |
215 | 57 import qt.core.QDate; |
1 | 58 |
191 | 59 version(Tango) |
60 import tango.text.Util; | |
61 else | |
62 import std.string; | |
1 | 63 |
64 class MainWindow : public QMainWindow | |
65 { | |
66 // Q_OBJECT | |
67 | |
68 public: | |
69 this() | |
70 { | |
71 textEdit = new QTextEdit; | |
72 setCentralWidget(textEdit); | |
73 | |
74 createActions(); | |
75 createMenus(); | |
76 createToolBars(); | |
77 createStatusBar(); | |
78 createDockWindows(); | |
79 | |
80 setWindowTitle(tr("Dock Widgets")); | |
81 newLetter(); | |
82 } | |
83 | |
84 private : //slots: | |
85 void newLetter() | |
86 { | |
87 textEdit.clear(); | |
88 | |
89 scope cursor = textEdit.textCursor; | |
90 cursor.movePosition(QTextCursor.Start); | |
91 QTextFrame topFrame = cursor.currentFrame(); | |
92 QTextFrameFormat topFrameFormat = topFrame.frameFormat(); | |
93 topFrameFormat.setPadding(16); | |
94 topFrame.setFrameFormat(topFrameFormat); | |
95 | |
96 scope textFormat = new QTextCharFormat; | |
97 scope boldFormat = new QTextCharFormat; | |
98 scope italicFormat = new QTextCharFormat; | |
99 boldFormat.setFontWeight(QFont.Bold); | |
100 italicFormat.setFontItalic(true); | |
101 | |
102 scope tableFormat = new QTextTableFormat; | |
103 tableFormat.setBorder(1); | |
104 tableFormat.setCellPadding(16); | |
105 tableFormat.setAlignment(Qt.AlignRight); | |
106 cursor.insertTable(1, 1, tableFormat); | |
107 cursor.insertText("The Firm", boldFormat); | |
108 cursor.insertBlock(); | |
109 cursor.insertText("321 City Street", textFormat); | |
110 cursor.insertBlock(); | |
111 cursor.insertText("Industry Park"); | |
112 cursor.insertBlock(); | |
113 cursor.insertText("Some Country"); | |
114 cursor.setPosition(topFrame.lastPosition()); | |
115 cursor.insertText(QDate.currentDate.toString("d MMMM yyyy"), textFormat); | |
116 cursor.insertBlock(); | |
117 cursor.insertBlock(); | |
118 cursor.insertText("Dear ", textFormat); | |
119 cursor.insertText("NAME", italicFormat); | |
120 cursor.insertText(",", textFormat); | |
121 for (int i = 0; i < 3; ++i) | |
122 cursor.insertBlock(); | |
123 cursor.insertText(tr("Yours sincerely,"), textFormat); | |
124 for (int i = 0; i < 3; ++i) | |
125 cursor.insertBlock(); | |
126 cursor.insertText("The Boss", textFormat); | |
127 cursor.insertBlock(); | |
128 cursor.insertText("ADDRESS", italicFormat); | |
129 } | |
130 /* | |
131 void save() | |
132 { | |
133 char[] fileName = QFileDialog.getSaveFileName(this, | |
134 tr("Choose a file name"), ".", | |
135 tr("HTML (*.html *.htm)")); | |
136 if (fileName.isEmpty()) | |
137 return; | |
138 scope file = new QFile(fileName); | |
139 if (!file.open(QFile.WriteOnly | QFile.Text)) { | |
140 QMessageBox.warning(this, tr("Dock Widgets"), | |
141 tr(layout("Cannot write file %1:\n%2.", fileName, file.errorString)) | |
142 return; | |
143 } | |
144 | |
145 scope outStream = new QTextStream(file); | |
146 QApplication.setOverrideCursor(Qt.WaitCursor); | |
147 outStream << textEdit.toHtml(); | |
148 QApplication.restoreOverrideCursor(); | |
149 | |
150 statusBar.showMessage(layout("Saved '%1'", fileName), 2000); | |
151 }*/ | |
152 | |
153 void print() { } | |
154 | |
155 void undo() { | |
156 auto document = textEdit.document(); | |
157 document.undo(); | |
158 } | |
159 | |
191 | 160 void insertCustomer(string customer) |
1 | 161 { |
162 if (customer == "") | |
163 return; | |
191 | 164 |
165 string[] customerList = customer.split(", "); | |
1 | 166 auto document = textEdit.document(); |
167 QTextCursor cursor = document.find("NAME"); | |
168 if (!cursor.isNull) { | |
169 cursor.beginEditBlock(); | |
170 cursor.insertText(customerList[0]); | |
171 scope oldcursor = new QTextCursor(cursor); | |
172 cursor = document.find("ADDRESS"); | |
173 if (!cursor.isNull) { | |
174 for (int i = 1; i < customerList.length; ++i) { | |
175 cursor.insertBlock(); | |
176 cursor.insertText(customerList[i]); | |
177 } | |
178 cursor.endEditBlock(); | |
179 } | |
180 else | |
181 oldcursor.endEditBlock(); | |
182 } | |
183 } | |
184 | |
191 | 185 void addParagraph(string paragraph) |
1 | 186 { |
187 if (paragraph == "") | |
188 return; | |
189 auto document = textEdit.document(); | |
190 scope cursor = document.find(tr("Yours sincerely,")); | |
191 if (cursor.isNull) | |
192 return; | |
193 cursor.beginEditBlock(); | |
194 cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor, 2); | |
195 cursor.insertBlock(); | |
196 cursor.insertText(paragraph); | |
197 cursor.insertBlock(); | |
198 cursor.endEditBlock(); | |
199 | |
200 } | |
201 | |
202 void about() | |
203 { | |
204 QMessageBox.about(this, "About Dock Widgets", | |
205 "The <b>Dock Widgets</b> example demonstrates how to " | |
206 "use Qt's dock widgets. You can enter your own text, " | |
207 "click a customer to add a customer name and " | |
208 "address, and click standard paragraphs to add them."); | |
209 } | |
210 | |
211 private: | |
212 void createActions() | |
213 { | |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
1
diff
changeset
|
214 newLetterAct = new QAction(new QIcon(":images/new.png"), tr("&New Letter"), this); |
1 | 215 newLetterAct.setShortcut(tr("Ctrl+N")); |
216 newLetterAct.setStatusTip(tr("Create a new form letter")); | |
282
256ab6cb8e85
Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
eldar
parents:
215
diff
changeset
|
217 connect!("triggered")(newLetterAct, &this.newLetter); |
1 | 218 |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
1
diff
changeset
|
219 saveAct = new QAction(new QIcon(":images/save.png"), tr("&Save..."), this); |
1 | 220 saveAct.setShortcut(tr("Ctrl+S")); |
221 saveAct.setStatusTip(tr("Save the current form letter")); | |
222 // saveAct.triggered.connect(&save); | |
223 | |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
1
diff
changeset
|
224 printAct = new QAction(new QIcon(":images/print.png"), tr("&Print..."), this); |
1 | 225 printAct.setShortcut(tr("Ctrl+P")); |
226 printAct.setStatusTip(tr("Print the current form letter")); | |
227 // printAct.triggered.connect(&print); | |
228 | |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
1
diff
changeset
|
229 undoAct = new QAction(new QIcon(":images/undo.png"), tr("&Undo"), this); |
1 | 230 undoAct.setShortcut(tr("Ctrl+Z")); |
231 undoAct.setStatusTip(tr("Undo the last editing action")); | |
282
256ab6cb8e85
Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
eldar
parents:
215
diff
changeset
|
232 connect!("triggered")(undoAct, &undo); |
1 | 233 |
234 quitAct = new QAction(tr("&Quit"), this); | |
235 quitAct.setShortcut(tr("Ctrl+Q")); | |
236 quitAct.setStatusTip(tr("Quit the application")); | |
282
256ab6cb8e85
Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
eldar
parents:
215
diff
changeset
|
237 connect!("triggered")(quitAct, &this.close); |
1 | 238 |
239 aboutAct = new QAction(tr("&About"), this); | |
240 aboutAct.setStatusTip(tr("Show the application's About box")); | |
282
256ab6cb8e85
Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
eldar
parents:
215
diff
changeset
|
241 connect!("triggered")(aboutAct, &about); |
1 | 242 |
243 aboutQtAct = new QAction(tr("About &Qt"), this); | |
244 aboutQtAct.setStatusTip(tr("Show the Qt library's About box")); | |
245 // aboutQtAct.triggered(&aboutQt); | |
246 } | |
247 | |
248 void createMenus() | |
249 { | |
250 fileMenu = menuBar.addMenu(tr("&File")); | |
251 fileMenu.addAction(newLetterAct); | |
252 fileMenu.addAction(saveAct); | |
253 fileMenu.addAction(printAct); | |
254 fileMenu.addSeparator(); | |
255 fileMenu.addAction(quitAct); | |
256 | |
257 editMenu = menuBar.addMenu(tr("&Edit")); | |
258 editMenu.addAction(undoAct); | |
259 | |
260 viewMenu = menuBar.addMenu(tr("&View")); | |
261 | |
262 menuBar().addSeparator(); | |
263 | |
264 helpMenu = menuBar.addMenu(tr("&Help")); | |
265 helpMenu.addAction(aboutAct); | |
266 helpMenu.addAction(aboutQtAct); | |
267 } | |
268 | |
269 void createToolBars() | |
270 { | |
271 fileToolBar = addToolBar(tr("File")); | |
272 fileToolBar.addAction(newLetterAct); | |
273 fileToolBar.addAction(saveAct); | |
274 fileToolBar.addAction(printAct); | |
275 | |
276 editToolBar = addToolBar(tr("Edit")); | |
277 editToolBar.addAction(undoAct); | |
278 } | |
279 | |
280 void createStatusBar() | |
281 { | |
282 statusBar.showMessage(tr("Ready")); | |
283 } | |
284 | |
285 void createDockWindows() | |
286 { | |
287 QDockWidget dock = new QDockWidget(tr("Customers"), this); | |
288 dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea); | |
289 customerList = new QListWidget(dock); | |
290 customerList.addItems([ | |
291 "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton", | |
292 "Jane Doe, Memorabilia, 23 Watersedge, Beaton", | |
293 "Tammy Shea, Tiblanka, 38 Sea Views, Carlton", | |
294 "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal", | |
295 "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston", | |
296 "Sally Hobart, Tiroli Tea, 67 Long River, Fedula"]); | |
297 dock.setWidget(customerList); | |
298 addDockWidget(Qt.RightDockWidgetArea, dock); | |
299 viewMenu.addAction(dock.toggleViewAction()); | |
300 | |
301 dock = new QDockWidget(tr("Paragraphs"), this); | |
302 paragraphsList = new QListWidget(dock); | |
303 paragraphsList.addItems([ | |
304 "Thank you for your payment which we have received today.", | |
305 "Your order has been dispatched and should be with you " | |
306 "within 28 days.", | |
307 | |
308 "We have dispatched those items that were in stock. The " | |
309 "rest of your order will be dispatched once all the " | |
310 "remaining items have arrived at our warehouse. No " | |
311 "additional shipping charges will be made.", | |
312 | |
313 "You made a small overpayment (less than $5) which we " | |
314 "will keep on account for you, or return at your request.", | |
315 | |
316 "You made a small underpayment (less than $1), but we have " | |
317 "sent your order anyway. We'll add this underpayment to " | |
318 "your next bill.", | |
319 | |
320 "Unfortunately you did not send enough money. Please remit " | |
321 "an additional $. Your order will be dispatched as soon as " | |
322 "the complete amount has been received.", | |
323 | |
324 "You made an overpayment (more than $5). Do you wish to " | |
325 "buy more items, or should we return the excess to you?"]); | |
326 dock.setWidget(paragraphsList); | |
327 addDockWidget(Qt.RightDockWidgetArea, dock); | |
328 viewMenu.addAction(dock.toggleViewAction()); | |
329 | |
282
256ab6cb8e85
Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
eldar
parents:
215
diff
changeset
|
330 connect!("currentTextChanged")(customerList, &this.insertCustomer); |
256ab6cb8e85
Signals look-up andNew syntax for connect. The old one will not work from now on. This will allow for the signals overload. Although changes are done for both D1 and D2 versions, D1 won't work because of compiler bugs. I am tired of waiting for fixes.
eldar
parents:
215
diff
changeset
|
331 connect!("currentTextChanged")(paragraphsList, &this.addParagraph); |
1 | 332 } |
333 | |
334 QTextEdit textEdit; | |
335 QListWidget customerList; | |
336 QListWidget paragraphsList; | |
337 | |
338 QMenu fileMenu; | |
339 QMenu editMenu; | |
340 QMenu viewMenu; | |
341 QMenu helpMenu; | |
342 QToolBar fileToolBar; | |
343 QToolBar editToolBar; | |
344 QAction newLetterAct; | |
345 QAction saveAct; | |
346 QAction printAct; | |
347 QAction undoAct; | |
348 QAction aboutAct; | |
349 QAction aboutQtAct; | |
350 QAction quitAct; | |
351 } | |
352 | |
353 | |
354 //! [9] |