Mercurial > projects > qtd
annotate examples/mainwindows/sdi/mainwindow.d @ 374:681af90e1d53
Removed debug writelns. Fixed build script.
author | Max Samukha <maxter@spambox.com> |
---|---|
date | Thu, 08 Jul 2010 22:21:56 +0300 |
parents | 31520b2c0b3c |
children |
rev | line source |
---|---|
269 | 1 /**************************************************************************** |
2 ** | |
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). | |
4 ** Contact: Nokia Corporation (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 http://www.qtsoftware.com/contact. | |
38 ** $QT_END_LICENSE$ | |
39 ** | |
40 ****************************************************************************/ | |
41 | |
42 /**************************************************************************** | |
43 ** | |
44 ** Translated from Qt/C++ to QtD/D by Jason Addison | |
45 ** Date: 2009 09 13 | |
46 ** Contact: jraddison@gmail.com | |
47 ** | |
48 ****************************************************************************/ | |
49 | |
50 | |
51 import qt.gui.QMainWindow; | |
52 import qt.gui.QAction; | |
53 import qt.gui.QMenu; | |
54 import qt.gui.QTextEdit; | |
55 import qt.gui.QFileDialog; | |
56 import qt.gui.QMessageBox; | |
57 import qt.core.QSettings; | |
58 import qt.core.QTextStream; | |
59 | |
350
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
60 //import std.string; |
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
61 import std.conv; |
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
62 |
269 | 63 class MainWindow : public QMainWindow |
64 { | |
65 // Q_OBJECT | |
66 | |
67 public: | |
68 this() | |
69 { | |
70 init(); | |
71 setCurrentFile(""); | |
72 } | |
73 | |
74 this(string fileName) | |
75 { | |
76 init(); | |
77 loadFile(fileName); | |
78 } | |
79 | |
80 protected: | |
81 | |
82 void closeEvent(QCloseEvent event) | |
83 { | |
84 if (maybeSave()) { | |
85 writeSettings(); | |
86 event.accept(); | |
87 } else { | |
88 event.ignore(); | |
89 } | |
90 } | |
91 | |
92 private: // slots | |
93 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
94 void slot_newFile() |
269 | 95 { |
96 MainWindow other = new MainWindow; | |
97 other.move(x() + 40, y() + 40); | |
98 other.show(); | |
99 } | |
100 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
101 void slot_open() |
269 | 102 { |
103 scope fileName = QFileDialog.getOpenFileName(this); | |
104 if (fileName) { | |
105 MainWindow existing = findMainWindow(fileName); | |
106 if (existing) { | |
107 existing.show(); | |
108 existing.raise(); | |
109 existing.activateWindow(); | |
110 return; | |
111 } | |
112 if (isUntitled && textEdit.document().isEmpty() | |
113 && !isWindowModified()) { | |
114 loadFile(fileName); | |
115 } else { | |
116 MainWindow other = new MainWindow(fileName); | |
117 if (other.isUntitled) { | |
118 delete other; | |
119 return; | |
120 } | |
121 other.move(x() + 40, y() + 40); | |
122 other.show(); | |
123 } | |
124 } | |
125 } | |
126 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
127 bool slot_save() |
269 | 128 { |
129 if (isUntitled) { | |
130 return saveAs(); | |
131 } else { | |
132 return saveFile(curFile); | |
133 } | |
134 } | |
135 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
136 bool slot_saveAs() |
269 | 137 { |
138 string fileName = QFileDialog.getSaveFileName(this, tr("Save As"), | |
139 curFile); | |
140 if (!fileName) | |
141 return false; | |
142 | |
143 return saveFile(fileName); | |
144 } | |
145 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
146 void slot_about() |
269 | 147 { |
148 QMessageBox.about(this, tr("About SDI"), | |
149 tr("The <b>SDI</b> example demonstrates how to write single " ~ | |
150 "document interface applications using Qt.")); | |
151 } | |
152 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
153 void slot_documentWasModified() |
269 | 154 { |
155 setWindowModified(true); | |
156 } | |
157 | |
158 private: | |
159 | |
160 void init() | |
161 { | |
162 setAttribute(Qt.WA_DeleteOnClose); | |
163 | |
164 isUntitled = true; | |
165 | |
166 textEdit = new QTextEdit; | |
167 setCentralWidget(textEdit); | |
168 | |
169 createActions(); | |
170 createMenus(); | |
171 createToolBars(); | |
172 createStatusBar(); | |
173 | |
174 readSettings(); | |
175 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
176 connect(textEdit.document(), "contentsChanged", this, "documentWasModified"); |
269 | 177 |
178 setUnifiedTitleAndToolBarOnMac(true); | |
179 } | |
180 | |
181 void createActions() | |
182 { | |
183 newAct = new QAction(new QIcon(":/images/new.png"), tr("&New"), this); | |
184 newAct.setShortcuts(QKeySequence.New); | |
185 newAct.setStatusTip(tr("Create a new file")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
186 connect(newAct, "triggered", this, "newFile"); |
269 | 187 |
188 openAct = new QAction(new QIcon(":/images/open.png"), tr("&Open..."), this); | |
189 openAct.setShortcuts(QKeySequence.Open); | |
190 openAct.setStatusTip(tr("Open an existing file")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
191 connect(openAct, "triggered", this, "open"); |
269 | 192 |
193 saveAct = new QAction(new QIcon(":/images/save.png"), tr("&Save"), this); | |
194 saveAct.setShortcuts(QKeySequence.Save); | |
195 saveAct.setStatusTip(tr("Save the document to disk")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
196 connect(saveAct, "triggered", this, "save"); |
269 | 197 |
198 saveAsAct = new QAction(tr("Save &As..."), this); | |
199 saveAsAct.setShortcuts(QKeySequence.SaveAs); | |
200 saveAsAct.setStatusTip(tr("Save the document under a new name")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
201 connect(saveAsAct, "triggered", this, "saveAs"); |
269 | 202 |
203 closeAct = new QAction(tr("&Close"), this); | |
204 closeAct.setShortcut(tr("Ctrl+W")); | |
205 closeAct.setStatusTip(tr("Close this window")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
206 connect(closeAct, "triggered", this, "close"); |
269 | 207 |
208 exitAct = new QAction(tr("E&xit"), this); | |
209 exitAct.setShortcut(tr("Ctrl+Q")); | |
210 exitAct.setStatusTip(tr("Exit the application")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
211 connect(exitAct, "triggered", qApp(), "closeAllWindows"); |
269 | 212 |
213 cutAct = new QAction(new QIcon(":/images/cut.png"), tr("Cu&t"), this); | |
214 cutAct.setShortcuts(QKeySequence.Cut); | |
215 cutAct.setStatusTip(tr("Cut the current selection's contents to the " ~ | |
216 "clipboard")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
217 connect(cutAct, "triggered", textEdit, "cut"); |
269 | 218 |
219 copyAct = new QAction(new QIcon(":/images/copy.png"), tr("&Copy"), this); | |
220 copyAct.setShortcuts(QKeySequence.Copy); | |
221 copyAct.setStatusTip(tr("Copy the current selection's contents to the " ~ | |
222 "clipboard")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
223 connect(copyAct, "triggered", textEdit, "copy"); |
269 | 224 |
225 pasteAct = new QAction(new QIcon(":/images/paste.png"), tr("&Paste"), this); | |
226 pasteAct.setShortcuts(QKeySequence.Paste); | |
227 pasteAct.setStatusTip(tr("Paste the clipboard's contents into the current " ~ | |
228 "selection")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
229 connect(pasteAct, "triggered", textEdit, "paste"); |
269 | 230 |
231 aboutAct = new QAction(tr("&About"), this); | |
232 aboutAct.setStatusTip(tr("Show the application's About box")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
233 connect(aboutAct, "triggered", this, "about"); |
269 | 234 |
235 aboutQtAct = new QAction(tr("About &Qt"), this); | |
236 aboutQtAct.setStatusTip(tr("Show the Qt library's About box")); | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
237 connect(aboutQtAct, "triggered", qApp, "aboutQt"); |
269 | 238 |
239 cutAct.setEnabled(false); | |
240 copyAct.setEnabled(false); | |
241 | |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
242 connect(textEdit, "copyAvailable", cutAct, "setEnabled"); |
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
243 connect(textEdit, "copyAvailable", copyAct, "setEnabled"); |
269 | 244 } |
245 | |
246 void createMenus() | |
247 { | |
248 fileMenu = menuBar.addMenu(tr("&File")); | |
249 fileMenu.addAction(newAct); | |
250 fileMenu.addAction(openAct); | |
251 fileMenu.addAction(saveAct); | |
252 fileMenu.addAction(saveAsAct); | |
253 fileMenu.addSeparator(); | |
254 fileMenu.addAction(closeAct); | |
255 fileMenu.addAction(exitAct); | |
256 | |
257 editMenu = menuBar.addMenu(tr("&Edit")); | |
258 editMenu.addAction(cutAct); | |
259 editMenu.addAction(copyAct); | |
260 editMenu.addAction(pasteAct); | |
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(newAct); | |
273 fileToolBar.addAction(openAct); | |
274 fileToolBar.addAction(saveAct); | |
275 | |
276 editToolBar = addToolBar(tr("Edit")); | |
277 editToolBar.addAction(cutAct); | |
278 editToolBar.addAction(copyAct); | |
279 editToolBar.addAction(pasteAct); | |
280 } | |
281 | |
282 void createStatusBar() | |
283 { | |
284 statusBar.showMessage(tr("Ready")); | |
285 } | |
286 | |
287 void readSettings() | |
288 { | |
289 scope QSettings settings = new QSettings("Trolltech", "SDI Example"); | |
290 scope QPoint pos = settings.value("pos", new QVariant(QPoint(200, 200))).toPoint(); | |
291 scope QSize size = settings.value("size", new QVariant(QSize(400, 400))).toSize(); | |
292 move(pos); | |
293 resize(size); | |
294 } | |
295 | |
296 void writeSettings() | |
297 { | |
298 QSettings settings = new QSettings("Trolltech", "SDI Example"); | |
299 settings.setValue("pos", new QVariant(pos())); | |
300 settings.setValue("size", new QVariant(size())); | |
301 } | |
302 | |
303 bool maybeSave() | |
304 { | |
305 if (textEdit.document().isModified()) { | |
306 QMessageBox.StandardButton ret; | |
307 ret = QMessageBox.warning(this, tr("SDI"), | |
308 tr("The document has been modified.\n" ~ | |
309 "Do you want to save your changes?"), | |
310 QMessageBox.Save | QMessageBox.Discard | |
311 | QMessageBox.Cancel); | |
312 if (ret == QMessageBox.Save) | |
313 return save(); | |
314 else if (ret == QMessageBox.Cancel) | |
315 return false; | |
316 } | |
317 return true; | |
318 } | |
319 | |
320 void loadFile(string fileName) | |
321 { | |
322 scope file = new QFile(fileName); | |
323 if (!file.open(QFile.ReadOnly | QFile.Text)) { | |
324 QMessageBox.warning(this, tr("SDI"), | |
325 tr("Cannot read file " ~ | |
326 fileName ~ ":\n" ~ | |
327 file.errorString ~ ".")); | |
328 return; | |
329 } | |
330 | |
331 scope inStream = new QTextStream(file); | |
332 QApplication.setOverrideCursor(new QCursor(Qt.WaitCursor)); | |
333 textEdit.setPlainText(inStream.readAll()); | |
334 QApplication.restoreOverrideCursor(); | |
335 | |
336 setCurrentFile(fileName); | |
337 statusBar.showMessage(tr("File loaded"), 2000); | |
338 } | |
339 | |
340 bool saveFile(string fileName) | |
341 { | |
342 scope file = new QFile(fileName); | |
343 if (!file.open(QFile.WriteOnly | QFile.Text)) { | |
344 QMessageBox.warning(this, tr("SDI"), | |
345 tr("Cannot write file " ~ | |
346 fileName ~ ":\n" ~ | |
347 file.errorString ~ ".")); | |
348 return false; | |
349 } | |
350 | |
351 scope outStream = new QTextStream(file); | |
352 QApplication.setOverrideCursor(new QCursor(Qt.WaitCursor)); | |
353 outStream.writeString(textEdit.toPlainText()); | |
354 QApplication.restoreOverrideCursor(); | |
355 | |
356 setCurrentFile(fileName); | |
357 statusBar.showMessage("Saved '" ~ fileName ~ "'", 2000); | |
358 return true; | |
359 } | |
360 | |
361 void setCurrentFile(string fileName) | |
362 { | |
363 static int sequenceNumber = 1; | |
364 | |
365 isUntitled = fileName == null; | |
366 if (isUntitled) { | |
367 curFile = tr("document" ~ to!(string)(sequenceNumber++) ~ ".txt"); | |
368 } else { | |
369 scope qfi = new QFileInfo(fileName); | |
370 curFile = qfi.canonicalFilePath; | |
371 } | |
372 | |
373 textEdit.document().setModified(false); | |
374 setWindowModified(false); | |
375 | |
376 setWindowTitle(strippedName(curFile) ~ "[*] - " ~ tr("SDI")); | |
377 } | |
378 | |
379 string strippedName(string fullFileName) | |
380 { | |
381 scope qfi = new QFileInfo(fullFileName); | |
382 return qfi.fileName(); | |
383 } | |
384 | |
385 MainWindow findMainWindow(string fileName) | |
386 { | |
387 scope qfi = new QFileInfo(fileName); | |
388 string canonicalFilePath = qfi.canonicalFilePath(); | |
389 | |
390 foreach (QWidget widget; QApplication.topLevelWidgets) { | |
391 MainWindow mainWin = cast(MainWindow)(widget); | |
392 if (mainWin && mainWin.curFile == canonicalFilePath) | |
393 return mainWin; | |
394 } | |
395 return null; | |
396 } | |
397 | |
398 QTextEdit textEdit; | |
399 string curFile; | |
400 bool isUntitled; | |
401 | |
402 QMenu fileMenu; | |
403 QMenu editMenu; | |
404 QMenu helpMenu; | |
405 QToolBar fileToolBar; | |
406 QToolBar editToolBar; | |
407 | |
408 QAction newAct; | |
409 QAction openAct; | |
410 QAction saveAct; | |
411 QAction saveAsAct; | |
412 QAction closeAct; | |
413 QAction exitAct; | |
414 QAction cutAct; | |
415 QAction copyAct; | |
416 QAction pasteAct; | |
417 QAction aboutAct; | |
418 QAction aboutQtAct; | |
350
31520b2c0b3c
Removed dependency on parent trait and stringof
Max Samukha <maxter@spambox.com>
parents:
323
diff
changeset
|
419 |
323
7a3c43424dca
make all examples compile with new signals/slots
eldar_ins@eldar-laptop
parents:
282
diff
changeset
|
420 mixin Q_OBJECT; |
269 | 421 }; |