1
#define DEBUGLVL 0
2
#include "mydebug.h"
3
4
/**
5
 * @file
6
 * @author Holger Schurig, DH3HS
7
 *
8
 * @section LICENSE
9
 *
10
 * This program is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU General Public License as
12
 * published by the Free Software Foundation; either version 2 of
13
 * the License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful, but
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
 * General Public License for more details at
19
 * http://www.gnu.org/copyleft/gpl.html
20
 *
21
 */
22
23
#include "qextserialport.h"
24
#include "decode_pcl.h"
25
#include "mainwindow.h"
26
27
#include <QSocketNotifier>
28
#include <QPixmap>
29
#include <QFileDialog>
30
#include <QMessageBox>
31
#include <QCloseEvent>
32
#include <QToolBar>
33
34
35
#define APP_NAME "8920 Screen Grabber"
36
37
38
MainWindow::MainWindow()
39
{
40
	MYTRACE("MainWindow::MainWindow");
41
42
	setupUi(this);
43
44
	createActions();
45
	createMenus();
46
	createToolBars();
47
	createStatusBar();
48
49
	readSettings();
50
	// setCurrentFile("");
51
	setUnifiedTitleAndToolBarOnMac(true);
52
53
	QExtSerialPort::PortSettings ps;
54
	ps.BaudRate = QExtSerialPort::BAUD19200;
55
	ps.DataBits = QExtSerialPort::DATA_8;
56
	ps.Parity = QExtSerialPort::PAR_NONE;
57
	ps.StopBits = QExtSerialPort::STOP_1;
58
	ps.FlowControl = QExtSerialPort::FLOW_OFF;
59
	ps.Timeout_Sec = 0;
60
	ps.Timeout_Millisec = 0;
61
	serial = new QExtSerialPort("/dev/ttyUSB0", ps);
62
	if (!serial->open())
63
		qWarning("cannot open serial port");
64
65
	decode = new DecodePCL(serial, this);
66
	connect(decode, SIGNAL(updateImage(const QImage &)),
67
	        this, SLOT(updateImage(const QImage &)) );
68
	connect(decode, SIGNAL(imageDone()),
69
	        this, SLOT(documentWasModified()) );
70
71
	notif = new QSocketNotifier(serial->handle(), QSocketNotifier::Read, this);
72
	connect(notif, SIGNAL(activated(int)),
73
	        decode, SLOT(slotData()) );
74
75
	// Clrear Image
76
	QPixmap pix(QSize(512+1, 256+3));
77
	pix.fill(Qt::white);
78
	pixLabel->setPixmap(pix);
79
}
80
81
82
MainWindow::~MainWindow()
83
{
84
	MYTRACE("MainWindow::~MainWindow");
85
86
	delete serial;
87
}
88
89
90
void MainWindow::updateImage(const QImage &img)
91
{
92
	MYTRACE("MainWindow::updateImage");
93
94
	pixLabel->setPixmap( QPixmap::fromImage(img) );
95
	statusBar()->clearMessage(); // omit flicker
96
	statusBar()->showMessage(tr("Image download in progress ..."), 5000);
97
	saveAct->setEnabled(true);
98
}
99
100
101
void MainWindow::closeEvent(QCloseEvent *event)
102
{
103
	MYTRACE("MainWindow::closeEvent");
104
105
	if (maybeSave()) {
106
		writeSettings();
107
		event->accept();
108
	} else {
109
		event->ignore();
110
	}
111
}
112
113
114
bool MainWindow::save()
115
{
116
	MYTRACE("MainWindow::save");
117
118
	QString fileName = QFileDialog::getSaveFileName(
119
	   this,
120
	   tr("Save screen dump as ..."),
121
	   QString(),
122
	   tr("Portable network graphics (*.png);;"
123
	      "Windows Bitmap (*.bmp);;"
124
	      "Portable Pixmap (*.ppm);;"
125
	      "X11 Pixmap (*.xpm);;"
126
	      "Joint Photographic Experts Group (*.jpg)"));
127
	if (fileName.isEmpty())
128
		return false;
129
130
	return saveFile(fileName);
131
}
132
133
134
void MainWindow::about()
135
{
136
	MYTRACE("MainWindow::about");
137
138
	QMessageBox::about(this, tr("About 8920grabber"),
139
               tr("1. connect the <b>HP 8920 RF Communications Test Set</b> via serial cable to your PC<p>"
140
                  "2. press <b>SHIFT PRINT</b> on the test set<p>"
141
                  "3. the test set will make a screen dump, using some simple HP-PCL commands<p>"
142
                  "4. " APP_NAME " decodes this simply PCL and displays the screen dump as image<p>"
143
                  "5. use the <b>File</b>-><b>Save</b> or the icon to store this as *.png file"));
144
}
145
146
147
void MainWindow::documentWasModified()
148
{
149
	MYTRACE("MainWindow::documentWasModified");
150
151
	setWindowModified(true);
152
	statusBar()->showMessage(tr("Download done!"), 5000);
153
}
154
155
156
void MainWindow::createActions()
157
{
158
	MYTRACE("MainWindow::createActions");
159
160
	saveAct = new QAction(QIcon(":/icons/filesave.png"), tr("&Save ..."), this);
161
	//saveAct->setStatusTip(tr(""));
162
	connect(saveAct, SIGNAL(triggered()), SLOT(save()) );
163
	saveAct->setEnabled(false);
164
165
	aboutAct = new QAction(tr("&About"), this);
166
	connect(aboutAct, SIGNAL(triggered()), SLOT(about()) );
167
168
	exitAct = new QAction(tr("&Exit"), this);
169
	connect(exitAct, SIGNAL(triggered()), SLOT(close()) );
170
}
171
172
173
void MainWindow::createMenus()
174
{
175
	MYTRACE("MainWindow::createMenus");
176
177
	fileMenu = menuBar()->addMenu(tr("&File"));
178
	fileMenu->addAction(saveAct);
179
	fileMenu->addSeparator();
180
	fileMenu->addAction(exitAct);
181
182
	menuBar()->addSeparator();
183
184
	helpMenu = menuBar()->addMenu(tr("&Help"));
185
	helpMenu->addAction(aboutAct);
186
}
187
188
189
void MainWindow::createToolBars()
190
{
191
	MYTRACE("MainWindow::createToolBars");
192
193
	fileToolBar = addToolBar(tr("File"));
194
	fileToolBar->addAction(saveAct);
195
}
196
197
198
void MainWindow::createStatusBar()
199
{
200
	MYTRACE("MainWindow::createStatusBar");
201
202
	//statusBar()->showMessage(tr("Ready"));
203
}
204
205
206
void MainWindow::readSettings()
207
{
208
	MYTRACE("MainWindow::readSettings");
209
210
	// QSettings settings("Trolltech", "Application Example");
211
	// QPoint pos = settings.value("pos", QPoint(200, 200)).toPoint();
212
	// QSize size = settings.value("size", QSize(400, 400)).toSize();
213
	// resize(size);
214
	// move(pos);
215
}
216
217
218
void MainWindow::writeSettings()
219
{
220
	MYTRACE("MainWindow::writeSettings");
221
222
	// QSettings settings("Trolltech", "Application Example");
223
	// settings.setValue("pos", pos());
224
	// settings.setValue("size", size());
225
}
226
227
228
bool MainWindow::maybeSave()
229
{
230
	MYTRACE("MainWindow::maybeSave");
231
232
	if (isWindowModified()) {
233
		QMessageBox::StandardButton ret;
234
		ret = QMessageBox::warning(this, tr(APP_NAME),
235
		                           tr("Received image not yet saved.\n"
236
		                              "Do you want to do that now?"),
237
		                           QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
238
		if (ret == QMessageBox::Save)
239
			return save();
240
		else if (ret == QMessageBox::Cancel)
241
			return false;
242
	}
243
	return true;
244
}
245
246
247
bool MainWindow::saveFile(const QString &fileName)
248
{
249
	MYTRACE("MainWindow::saveFile");
250
251
#ifndef QT_NO_CURSOR
252
	QApplication::setOverrideCursor(Qt::WaitCursor);
253
#endif
254
	bool ok = pixLabel->pixmap()->save(fileName);
255
#ifndef QT_NO_CURSOR
256
	QApplication::restoreOverrideCursor();
257
#endif
258
259
	if (ok) {
260
		statusBar()->showMessage(tr("File saved"), 2000);
261
		setWindowModified(false);
262
	} else {
263
		QMessageBox::warning(this, tr("Application"),
264
		                     tr("Cannot save file %1.")
265
		                     .arg(fileName));
266
	}
267
	return ok;
268
}