summaryrefslogtreecommitdiffstats
path: root/qtjava/javalib/examples/application/ApplicationWindow.java
blob: a169454d300ec5349f4f266976d5a7233d4a9479 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import org.kde.qt.*;

public class ApplicationWindow extends TQMainWindow {
	private TQPrinter printer;
	private TQMultiLineEdit e;
	private String filename;

	private final String fileOpenText = "<img source=\"fileopen\"> "+
					"Click this button to open a <em>new file</em>. <br><br>"+
					"You can also select the <b>Open command</b> from the File menu.";
	private final String fileSaveText = "Click this button to save the file you are "+
					"editing.  You will be prompted for a file name.\n\n"+
					"You can also select the Save command from the File menu.\n\n"+
					"Note that implementing this function is left as an exercise for the reader.";
	private final String filePrintText = "Click this button to print the file you "+
					"are editing.\n\n"+
					"You can also select the Print command from the File menu.";

	private final String[] fileopen = {
		"    16    13        5            1",
		". c #040404",
		"# c #808304",
		"a c None",
		"b c #f3f704",
		"c c #f3f7f3",
		"aaaaaaaaa...aaaa",
		"aaaaaaaa.aaa.a.a",
		"aaaaaaaaaaaaa..a",
		"a...aaaaaaaa...a",
		".bcb.......aaaaa",
		".cbcbcbcbc.aaaaa",
		".bcbcbcbcb.aaaaa",
		".cbcb...........",
		".bcb.#########.a",
		".cb.#########.aa",
		".b.#########.aaa",
		"..#########.aaaa",
		"...........aaaaa"
	};

	private final String[] filesave = {
		"    14    14        4            1",
		". c #040404",
		"# c #808304",
		"a c #bfc2bf",
		"b c None",
		"..............",
		".#.aaaaaaaa.a.",
		".#.aaaaaaaa...",
		".#.aaaaaaaa.#.",
		".#.aaaaaaaa.#.",
		".#.aaaaaaaa.#.",
		".#.aaaaaaaa.#.",
		".##........##.",
		".############.",
		".##.........#.",
		".##......aa.#.",
		".##......aa.#.",
		".##......aa.#.",
		"b............."
	};
	
	private final String[] fileprint = {
		"    16    14        6            1",
		". c #000000",
		"# c #848284",
		"a c #c6c3c6",
		"b c #ffff00",
		"c c #ffffff",
		"d c None",
		"ddddd.........dd",
		"dddd.cccccccc.dd",
		"dddd.c.....c.ddd",
		"ddd.cccccccc.ddd",
		"ddd.c.....c....d",
		"dd.cccccccc.a.a.",
		"d..........a.a..",
		".aaaaaaaaaa.a.a.",
		".............aa.",
		".aaaaaa###aa.a.d",
		".aaaaaabbbaa...d",
		".............a.d",
		"d.aaaaaaaaa.a.dd",
		"dd...........ddd"
	};
	
	
	
	
	public ApplicationWindow() {
		super(null, "example application main window", WDestructiveClose);

		// create a printer

		printer = new TQPrinter();


		// create user interface actions

		TQAction fileNewAction, fileOpenAction, fileSaveAction,
				fileSaveAsAction, filePrintAction, fileCloseAction,
				fileQuitAction;

		fileNewAction = new TQAction("New", "&New", new TQKeySequence(CTRL+Key_N), this, "new", false);
		connect(fileNewAction, SIGNAL("activated()"), this, SLOT("newDoc()"));

		fileOpenAction = new TQAction("Open File", new TQIconSet(new TQPixmap(fileopen)), "&Open", new TQKeySequence(CTRL+Key_O), this, "open", false);
		connect(fileOpenAction, SIGNAL("activated()"), this, SLOT("load()"));
		TQMimeSourceFactory.defaultFactory().setPixmap("fileopen", new TQPixmap(fileopen));
		fileOpenAction.setWhatsThis(fileOpenText);

		fileSaveAction = new TQAction("Save File", new TQIconSet(new TQPixmap(filesave)), "&Save", new TQKeySequence(CTRL+Key_S), this, "save", false);
		connect(fileSaveAction, SIGNAL("activated()"), this, SLOT("save()"));
		fileSaveAction.setWhatsThis(fileSaveText);

		fileSaveAsAction = new TQAction("Save File As", "Save &as", new TQKeySequence(), this, "save as", false);
		connect(fileSaveAsAction, SIGNAL("activated()"), this, SLOT("saveAs()"));
		fileSaveAsAction.setWhatsThis(fileSaveText);

		filePrintAction = new TQAction("Print File", new TQIconSet(new TQPixmap(fileprint)), "&Print", new TQKeySequence(CTRL+Key_P), this, "print", false);
		connect(filePrintAction, SIGNAL("activated()"), this, SLOT("print()"));
		filePrintAction.setWhatsThis(filePrintText);

		fileCloseAction = new TQAction("Close", "&Close", new TQKeySequence(CTRL+Key_W), this, "close", false);
		connect(fileCloseAction, SIGNAL("activated()"), this, SLOT("close()"));

		fileQuitAction = new TQAction("Quit", "&Quit", new TQKeySequence(CTRL+Key_Q), this, "quit", false);
		connect(fileQuitAction, SIGNAL("activated()"), tqApp(), SLOT("closeAllWindows()"));


		// populate a tool bar with some actions

		TQToolBar fileTools = new TQToolBar(this, "file operations");
		fileTools.setLabel(tr("File Operations"));
		fileOpenAction.addTo(fileTools);
		fileSaveAction.addTo(fileTools);
		filePrintAction.addTo(fileTools);
		TQWhatsThis.whatsThisButton(fileTools);
		

		// popuplate a menu with all actions
		
		TQPopupMenu file = new TQPopupMenu(this, "file");
		menuBar().insertItem("&File", file);
		fileNewAction.addTo(file);
		fileOpenAction.addTo(file);
		fileSaveAction.addTo(file);
		fileSaveAsAction.addTo(file);
		file.insertSeparator();
		filePrintAction.addTo(file);
		file.insertSeparator();
		fileCloseAction.addTo(file);
		fileQuitAction.addTo(file);


		// add a help menu

		TQPopupMenu help = new TQPopupMenu(this, "help");
		menuBar().insertSeparator();
		menuBar().insertItem("&Help", help);
		help.insertItem("&About", this, SLOT("about()"), new TQKeySequence(Key_F1), -1, -1);
		help.insertItem("About &Qt", this, SLOT("aboutTQt()"));
		help.insertSeparator();
		help.insertItem("What's &This", this, SLOT("whatsThis()"), new TQKeySequence(SHIFT+Key_F1), -1, -1);


		// create and define the central widget

		e = new TQMultiLineEdit(this, "editor");
		e.setFocus();
		setCentralWidget(e);
		statusBar().message("Ready", 2000);
		resize(450, 600);
	}

	private void newDoc() {
		ApplicationWindow ed = new ApplicationWindow();
		ed.show();
	}

	private void load() {
		String fn = TQFileDialog.getOpenFileName("", "", this);
		if (!(fn.length()==0))
			load(fn);
		else
			statusBar().message("Loading aborted", 2000);
	}


	private void load(String fileName) {
		TQFile f = new TQFile(fileName);
		if (!f.open(1))
			return;

		filename = fileName;

		e.setAutoUpdate(false);
		e.clear();

		TQTextStream t = new TQTextStream(f);
		while (!t.eof()) {
			String s = t.readLine();
			e.append(s);
		}
		f.close();

		e.setAutoUpdate(true);
		e.repaint();
		e.setEdited(false);
		setCaption(fileName);
		String s = "Loaded document "+fileName;
		statusBar().message(s, 2000);
	}

	private void save() {
		if (filename.length()==0) {
			saveAs();
			return;
		}

		String text = e.text();
		TQFile f = new TQFile(filename);
		if (!f.open(2)) {
			statusBar().message("Could not write to "+filename, 2000);
			return;
		}

		TQTextStream t = new TQTextStream(f);
		t.writeRawBytes(text, text.length());
		f.close();

		e.setEdited(false);

		setCaption(filename);

		statusBar().message("File "+filename+" saved", 2000);
	}


	private void saveAs() {
		String fn = TQFileDialog.getSaveFileName("", "", this);
		if (!(fn.length()==0)) {
			filename = fn;
			save();
		} else {
			statusBar().message("Saving aborted", 2000);
		}
	}

	private void print() {
		final int Margin = 10;
		int pageNo = 1;

		if (printer.setup(this)) {   // printer dialog
			statusBar().message("Printing...");
			TQPainter p = new TQPainter();
			p.begin(printer);     // paint on printer
			p.setFont(e.font());
			int yPos = 0;      // y position for each line
			TQFontMetrics fm = p.fontMetrics();
			
			// need width/height of printer surface
			TQPaintDeviceMetrics metrics = new TQPaintDeviceMetrics(printer);
			
			for(int i = 0 ; i < e.numLines() ; i++) {
				if (Margin + yPos > metrics.height() - Margin) {
					String msg = "Printing (page ";
					msg += ++pageNo;
					msg += ")...";
					statusBar().message(msg);
					printer.newPage();   // no more room on this page
					yPos = 0;     // back to top of page
				}
				p.drawText(Margin, Margin + yPos, metrics.width(),
						fm.lineSpacing(), ExpandTabs | DontClip, e.textLine(i));
				yPos = yPos + fm.lineSpacing();
			}
			p.end();        // send job to printer
			statusBar().message("Printing completed", 2000);
		} else {
			statusBar().message("Printing aborted", 2000);
		}
	}

	protected void closeEvent(TQCloseEvent ce) {
		if (!e.edited()) {
			ce.accept();
			return;
		}

		switch(TQMessageBox.information(this, "Qt Application Example",
					"The document has been changed since the last save.",
					"Save Now", "Cancel", "Leave Anyway", 0, 1)) {
			case 0:
				save();
				ce.accept();
				break;
			case 1:
			default: // just for sanity
				ce.ignore();
				break;
			case 2:
				ce.accept();
				break;
		}
	}

	private void about() {
		TQMessageBox.about(this, "Qt Application Example",
				"This example demonstrates simple use of "+
				"TQMainWindow,\nTQMenuBar and TQToolBar.");
	}


	private void aboutTQt() {
		TQMessageBox.aboutTQt(this, "Qt Application Example");
	}
}