comparison examples/mainwindows/dockwidgets/mainwindow.d @ 1:e78566595089

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