Mercurial > projects > qtd
annotate examples/mainwindows/dockwidgets/mainwindow.d @ 191:a212c97edcd2
one more example
author | eldar |
---|---|
date | Sun, 12 Jul 2009 21:03:57 +0000 |
parents | a2871e6b8b15 |
children | 8aaa84d48451 |
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; | |
57 | |
58 import qt.core.QDate; | |
59 /*import qt.gui. | |
60 import qt.gui. | |
61 import qt.gui. | |
62 import qt.gui. | |
63 import qt.gui. | |
64 import qt.gui. | |
65 */ | |
191 | 66 version(Tango) |
67 import tango.text.Util; | |
68 else | |
69 import std.string; | |
1 | 70 |
71 class MainWindow : public QMainWindow | |
72 { | |
73 // Q_OBJECT | |
74 | |
75 public: | |
76 this() | |
77 { | |
78 textEdit = new QTextEdit; | |
79 setCentralWidget(textEdit); | |
80 | |
81 createActions(); | |
82 createMenus(); | |
83 createToolBars(); | |
84 createStatusBar(); | |
85 createDockWindows(); | |
86 | |
87 setWindowTitle(tr("Dock Widgets")); | |
88 newLetter(); | |
89 } | |
90 | |
91 private : //slots: | |
92 void newLetter() | |
93 { | |
94 textEdit.clear(); | |
95 | |
96 scope cursor = textEdit.textCursor; | |
97 cursor.movePosition(QTextCursor.Start); | |
98 QTextFrame topFrame = cursor.currentFrame(); | |
99 QTextFrameFormat topFrameFormat = topFrame.frameFormat(); | |
100 topFrameFormat.setPadding(16); | |
101 topFrame.setFrameFormat(topFrameFormat); | |
102 | |
103 scope textFormat = new QTextCharFormat; | |
104 scope boldFormat = new QTextCharFormat; | |
105 scope italicFormat = new QTextCharFormat; | |
106 boldFormat.setFontWeight(QFont.Bold); | |
107 italicFormat.setFontItalic(true); | |
108 | |
109 scope tableFormat = new QTextTableFormat; | |
110 tableFormat.setBorder(1); | |
111 tableFormat.setCellPadding(16); | |
112 tableFormat.setAlignment(Qt.AlignRight); | |
113 cursor.insertTable(1, 1, tableFormat); | |
114 cursor.insertText("The Firm", boldFormat); | |
115 cursor.insertBlock(); | |
116 cursor.insertText("321 City Street", textFormat); | |
117 cursor.insertBlock(); | |
118 cursor.insertText("Industry Park"); | |
119 cursor.insertBlock(); | |
120 cursor.insertText("Some Country"); | |
121 cursor.setPosition(topFrame.lastPosition()); | |
122 cursor.insertText(QDate.currentDate.toString("d MMMM yyyy"), textFormat); | |
123 cursor.insertBlock(); | |
124 cursor.insertBlock(); | |
125 cursor.insertText("Dear ", textFormat); | |
126 cursor.insertText("NAME", italicFormat); | |
127 cursor.insertText(",", textFormat); | |
128 for (int i = 0; i < 3; ++i) | |
129 cursor.insertBlock(); | |
130 cursor.insertText(tr("Yours sincerely,"), textFormat); | |
131 for (int i = 0; i < 3; ++i) | |
132 cursor.insertBlock(); | |
133 cursor.insertText("The Boss", textFormat); | |
134 cursor.insertBlock(); | |
135 cursor.insertText("ADDRESS", italicFormat); | |
136 } | |
137 /* | |
138 void save() | |
139 { | |
140 char[] fileName = QFileDialog.getSaveFileName(this, | |
141 tr("Choose a file name"), ".", | |
142 tr("HTML (*.html *.htm)")); | |
143 if (fileName.isEmpty()) | |
144 return; | |
145 scope file = new QFile(fileName); | |
146 if (!file.open(QFile.WriteOnly | QFile.Text)) { | |
147 QMessageBox.warning(this, tr("Dock Widgets"), | |
148 tr(layout("Cannot write file %1:\n%2.", fileName, file.errorString)) | |
149 return; | |
150 } | |
151 | |
152 scope outStream = new QTextStream(file); | |
153 QApplication.setOverrideCursor(Qt.WaitCursor); | |
154 outStream << textEdit.toHtml(); | |
155 QApplication.restoreOverrideCursor(); | |
156 | |
157 statusBar.showMessage(layout("Saved '%1'", fileName), 2000); | |
158 }*/ | |
159 | |
160 void print() { } | |
161 | |
162 void undo() { | |
163 auto document = textEdit.document(); | |
164 document.undo(); | |
165 } | |
166 | |
191 | 167 void insertCustomer(string customer) |
1 | 168 { |
169 if (customer == "") | |
170 return; | |
191 | 171 |
172 string[] customerList = customer.split(", "); | |
1 | 173 auto document = textEdit.document(); |
174 QTextCursor cursor = document.find("NAME"); | |
175 if (!cursor.isNull) { | |
176 cursor.beginEditBlock(); | |
177 cursor.insertText(customerList[0]); | |
178 scope oldcursor = new QTextCursor(cursor); | |
179 cursor = document.find("ADDRESS"); | |
180 if (!cursor.isNull) { | |
181 for (int i = 1; i < customerList.length; ++i) { | |
182 cursor.insertBlock(); | |
183 cursor.insertText(customerList[i]); | |
184 } | |
185 cursor.endEditBlock(); | |
186 } | |
187 else | |
188 oldcursor.endEditBlock(); | |
189 } | |
190 } | |
191 | |
191 | 192 void addParagraph(string paragraph) |
1 | 193 { |
194 if (paragraph == "") | |
195 return; | |
196 auto document = textEdit.document(); | |
197 scope cursor = document.find(tr("Yours sincerely,")); | |
198 if (cursor.isNull) | |
199 return; | |
200 cursor.beginEditBlock(); | |
201 cursor.movePosition(QTextCursor.PreviousBlock, QTextCursor.MoveAnchor, 2); | |
202 cursor.insertBlock(); | |
203 cursor.insertText(paragraph); | |
204 cursor.insertBlock(); | |
205 cursor.endEditBlock(); | |
206 | |
207 } | |
208 | |
209 void about() | |
210 { | |
211 QMessageBox.about(this, "About Dock Widgets", | |
212 "The <b>Dock Widgets</b> example demonstrates how to " | |
213 "use Qt's dock widgets. You can enter your own text, " | |
214 "click a customer to add a customer name and " | |
215 "address, and click standard paragraphs to add them."); | |
216 } | |
217 | |
218 private: | |
219 void createActions() | |
220 { | |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
1
diff
changeset
|
221 newLetterAct = new QAction(new QIcon(":images/new.png"), tr("&New Letter"), this); |
1 | 222 newLetterAct.setShortcut(tr("Ctrl+N")); |
223 newLetterAct.setStatusTip(tr("Create a new form letter")); | |
224 newLetterAct.triggered.connect(&this.newLetter); | |
225 | |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
1
diff
changeset
|
226 saveAct = new QAction(new QIcon(":images/save.png"), tr("&Save..."), this); |
1 | 227 saveAct.setShortcut(tr("Ctrl+S")); |
228 saveAct.setStatusTip(tr("Save the current form letter")); | |
229 // saveAct.triggered.connect(&save); | |
230 | |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
1
diff
changeset
|
231 printAct = new QAction(new QIcon(":images/print.png"), tr("&Print..."), this); |
1 | 232 printAct.setShortcut(tr("Ctrl+P")); |
233 printAct.setStatusTip(tr("Print the current form letter")); | |
234 // printAct.triggered.connect(&print); | |
235 | |
61
a2871e6b8b15
drcc: automatic initialization of resources with program start (disabled with option -no-static-initialize)
SokoL_SD
parents:
1
diff
changeset
|
236 undoAct = new QAction(new QIcon(":images/undo.png"), tr("&Undo"), this); |
1 | 237 undoAct.setShortcut(tr("Ctrl+Z")); |
238 undoAct.setStatusTip(tr("Undo the last editing action")); | |
239 undoAct.triggered.connect(&undo); | |
240 | |
241 quitAct = new QAction(tr("&Quit"), this); | |
242 quitAct.setShortcut(tr("Ctrl+Q")); | |
243 quitAct.setStatusTip(tr("Quit the application")); | |
244 quitAct.triggered.connect(&this.close); | |
245 | |
246 aboutAct = new QAction(tr("&About"), this); | |
247 aboutAct.setStatusTip(tr("Show the application's About box")); | |
248 aboutAct.triggered.connect(&about); | |
249 | |
250 aboutQtAct = new QAction(tr("About &Qt"), this); | |
251 aboutQtAct.setStatusTip(tr("Show the Qt library's About box")); | |
252 // aboutQtAct.triggered(&aboutQt); | |
253 } | |
254 | |
255 void createMenus() | |
256 { | |
257 fileMenu = menuBar.addMenu(tr("&File")); | |
258 fileMenu.addAction(newLetterAct); | |
259 fileMenu.addAction(saveAct); | |
260 fileMenu.addAction(printAct); | |
261 fileMenu.addSeparator(); | |
262 fileMenu.addAction(quitAct); | |
263 | |
264 editMenu = menuBar.addMenu(tr("&Edit")); | |
265 editMenu.addAction(undoAct); | |
266 | |
267 viewMenu = menuBar.addMenu(tr("&View")); | |
268 | |
269 menuBar().addSeparator(); | |
270 | |
271 helpMenu = menuBar.addMenu(tr("&Help")); | |
272 helpMenu.addAction(aboutAct); | |
273 helpMenu.addAction(aboutQtAct); | |
274 } | |
275 | |
276 void createToolBars() | |
277 { | |
278 fileToolBar = addToolBar(tr("File")); | |
279 fileToolBar.addAction(newLetterAct); | |
280 fileToolBar.addAction(saveAct); | |
281 fileToolBar.addAction(printAct); | |
282 | |
283 editToolBar = addToolBar(tr("Edit")); | |
284 editToolBar.addAction(undoAct); | |
285 } | |
286 | |
287 void createStatusBar() | |
288 { | |
289 statusBar.showMessage(tr("Ready")); | |
290 } | |
291 | |
292 void createDockWindows() | |
293 { | |
294 QDockWidget dock = new QDockWidget(tr("Customers"), this); | |
295 dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea); | |
296 customerList = new QListWidget(dock); | |
297 customerList.addItems([ | |
298 "John Doe, Harmony Enterprises, 12 Lakeside, Ambleton", | |
299 "Jane Doe, Memorabilia, 23 Watersedge, Beaton", | |
300 "Tammy Shea, Tiblanka, 38 Sea Views, Carlton", | |
301 "Tim Sheen, Caraba Gifts, 48 Ocean Way, Deal", | |
302 "Sol Harvey, Chicos Coffee, 53 New Springs, Eccleston", | |
303 "Sally Hobart, Tiroli Tea, 67 Long River, Fedula"]); | |
304 dock.setWidget(customerList); | |
305 addDockWidget(Qt.RightDockWidgetArea, dock); | |
306 viewMenu.addAction(dock.toggleViewAction()); | |
307 | |
308 dock = new QDockWidget(tr("Paragraphs"), this); | |
309 paragraphsList = new QListWidget(dock); | |
310 paragraphsList.addItems([ | |
311 "Thank you for your payment which we have received today.", | |
312 "Your order has been dispatched and should be with you " | |
313 "within 28 days.", | |
314 | |
315 "We have dispatched those items that were in stock. The " | |
316 "rest of your order will be dispatched once all the " | |
317 "remaining items have arrived at our warehouse. No " | |
318 "additional shipping charges will be made.", | |
319 | |
320 "You made a small overpayment (less than $5) which we " | |
321 "will keep on account for you, or return at your request.", | |
322 | |
323 "You made a small underpayment (less than $1), but we have " | |
324 "sent your order anyway. We'll add this underpayment to " | |
325 "your next bill.", | |
326 | |
327 "Unfortunately you did not send enough money. Please remit " | |
328 "an additional $. Your order will be dispatched as soon as " | |
329 "the complete amount has been received.", | |
330 | |
331 "You made an overpayment (more than $5). Do you wish to " | |
332 "buy more items, or should we return the excess to you?"]); | |
333 dock.setWidget(paragraphsList); | |
334 addDockWidget(Qt.RightDockWidgetArea, dock); | |
335 viewMenu.addAction(dock.toggleViewAction()); | |
336 | |
337 customerList.currentTextChanged.connect(&this.insertCustomer); | |
338 paragraphsList.currentTextChanged.connect(&this.addParagraph); | |
339 } | |
340 | |
341 QTextEdit textEdit; | |
342 QListWidget customerList; | |
343 QListWidget paragraphsList; | |
344 | |
345 QMenu fileMenu; | |
346 QMenu editMenu; | |
347 QMenu viewMenu; | |
348 QMenu helpMenu; | |
349 QToolBar fileToolBar; | |
350 QToolBar editToolBar; | |
351 QAction newLetterAct; | |
352 QAction saveAct; | |
353 QAction printAct; | |
354 QAction undoAct; | |
355 QAction aboutAct; | |
356 QAction aboutQtAct; | |
357 QAction quitAct; | |
358 } | |
359 | |
360 | |
361 //! [9] |