| 1ff0643 by Enrico Ros at 2009-02-20 |
1 |
/*************************************************************************** |
| 0e5745d by Enrico Ros at 2009-05-31 |
2 |
* Copyright (c) 2009 Enrico Ros * |
|
3 |
* 2009 Enrico Ros <enrico.ros@email.it> * |
|
4 |
* 2009 Alberto Scarpa <skaal.sl@gmail.com> * |
| 1ff0643 by Enrico Ros at 2009-02-20 |
5 |
* * |
|
6 |
* Permission is hereby granted, free of charge, to any person * |
|
7 |
* obtaining a copy of this software and associated documentation * |
|
8 |
* files (the "Software"), to deal in the Software without * |
|
9 |
* restriction, including without limitation the rights to use, * |
|
10 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell * |
|
11 |
* copies of the Software, and to permit persons to whom the * |
|
12 |
* Software is furnished to do so, subject to the following * |
|
13 |
* conditions: * |
|
14 |
* * |
|
15 |
* The above copyright notice and this permission notice shall be * |
|
16 |
* included in all copies or substantial portions of the Software. * |
|
17 |
* * |
|
18 |
***************************************************************************/ |
|
19 |
|
| cbdc84f by Enrico Ros at 2009-02-20 |
20 |
#include "AppWidget.h" |
| e45848b by Enrico Ros at 2009-05-31 |
21 |
#include "AbstractGame.h" |
| 6d89023 by Enrico Ros at 2009-02-21 |
22 |
#include "InputUtils.h" |
| e45848b by Enrico Ros at 2009-05-31 |
23 |
#include "ScreenCapture.h" |
|
24 |
#include "WCGame.h" |
| 6336a38 by Enrico Ros at 2009-02-25 |
25 |
#include "ocr/Ocr.h" |
| e45848b by Enrico Ros at 2009-05-31 |
26 |
#include "ui_AppWidget.h" |
| 1ff0643 by Enrico Ros at 2009-02-20 |
27 |
#include <QApplication> |
|
28 |
#include <QDesktopWidget> |
| e45848b by Enrico Ros at 2009-05-31 |
29 |
#include <QPaintEvent> |
| 9d1b99e by Enrico Ros at 2009-02-25 |
30 |
#include <QPainter> |
| 1ff0643 by Enrico Ros at 2009-02-20 |
31 |
#include <QPixmap> |
|
32 |
#include <QPoint> |
|
33 |
#include <QSettings> |
| 6336a38 by Enrico Ros at 2009-02-25 |
34 |
#include <QDirIterator> |
| 6d89023 by Enrico Ros at 2009-02-21 |
35 |
#include <QTimer> |
| 6336a38 by Enrico Ros at 2009-02-25 |
36 |
#include <QDebug> |
| 1ff0643 by Enrico Ros at 2009-02-20 |
37 |
|
| e45848b by Enrico Ros at 2009-05-31 |
38 |
#define DEFAULT_WIDTH 320 |
|
39 |
#define DEFAULT_HEIGHT 41 |
|
40 |
|
|
41 |
|
|
42 |
class RegionWidget : public QWidget { |
|
43 |
public: |
|
44 |
RegionWidget() : QWidget(0, Qt::CustomizeWindowHint | Qt::FramelessWindowHint) |
|
45 |
{ |
|
46 |
setAttribute(Qt::WA_TranslucentBackground); |
|
47 |
setAttribute(Qt::WA_OpaquePaintEvent); |
|
48 |
hide(); |
|
49 |
} |
|
50 |
int rX, rY, rWidth, rHeight; |
|
51 |
void setStartPos( const QPoint & pos ) |
|
52 |
{ |
|
53 |
startPos = pos; |
|
54 |
} |
|
55 |
void setEndPos( const QPoint & pos ) |
|
56 |
{ |
|
57 |
endPos = pos; |
|
58 |
rX = qMin( startPos.x(), endPos.x() ); |
|
59 |
rY = qMin( startPos.y(), endPos.y() ); |
|
60 |
rWidth = qMax( startPos.x(), endPos.x() ) - rX + 1; |
|
61 |
rHeight = qMax( startPos.y(), endPos.y() ) - rY + 1; |
|
62 |
setShown( rWidth > 0 && rHeight > 0 ); |
|
63 |
setGeometry( rX, rY, rWidth, rHeight ); |
|
64 |
} |
|
65 |
protected: |
|
66 |
void paintEvent(QPaintEvent * event) |
|
67 |
{ |
|
68 |
QPainter p(this); |
|
69 |
p.setCompositionMode(QPainter::CompositionMode_Source); |
|
70 |
p.fillRect(event->rect(), QColor(255, 0, 0, 64)); |
|
71 |
p.drawRect(0, 0, width() - 1, height() - 1); |
|
72 |
} |
|
73 |
private: |
|
74 |
QPoint startPos, endPos; |
|
75 |
}; |
|
76 |
|
| cbdc84f by Enrico Ros at 2009-02-20 |
77 |
|
|
78 |
AppWidget::AppWidget(QWidget *parent) |
| 1ff0643 by Enrico Ros at 2009-02-20 |
79 |
: QWidget(parent) |
|
80 |
, ui(new Ui::AppWidgetClass) |
|
81 |
#if defined(Q_OS_WIN) |
| e45848b by Enrico Ros at 2009-05-31 |
82 |
, m_settings( new QSettings( "autogram.ini", QSettings::IniFormat ) ) |
| 1ff0643 by Enrico Ros at 2009-02-20 |
83 |
#else |
|
84 |
, m_settings( new QSettings() ) |
|
85 |
#endif |
| 1e01a22 by Enrico Ros at 2009-02-21 |
86 |
, m_game( 0 ) |
| e45848b by Enrico Ros at 2009-05-31 |
87 |
, m_capture( 0 ) |
| 6336a38 by Enrico Ros at 2009-02-25 |
88 |
, m_ocr( 0 ) |
| e45848b by Enrico Ros at 2009-05-31 |
89 |
, m_pickingRegion( 0 ) |
| cbdc84f by Enrico Ros at 2009-02-20 |
90 |
{ |
| 1ff0643 by Enrico Ros at 2009-02-20 |
91 |
// create ui |
| e45848b by Enrico Ros at 2009-05-31 |
92 |
ui->setupUi( this ); |
| 1ff0643 by Enrico Ros at 2009-02-20 |
93 |
QDesktopWidget dw; |
|
94 |
ui->left->setMaximum( dw.width() ); |
|
95 |
ui->top->setMaximum( dw.height() ); |
|
96 |
ui->width->setMaximum( dw.width() ); |
|
97 |
ui->height->setMaximum( dw.height() ); |
|
98 |
// init defaults.. |
|
99 |
if ( !m_settings->contains( "rect/left" ) ) { |
|
100 |
ui->left->setValue( (dw.width() - DEFAULT_WIDTH) / 3 ); |
|
101 |
ui->top->setValue( (dw.height() - DEFAULT_HEIGHT) / 3 ); |
|
102 |
ui->width->setValue( DEFAULT_WIDTH ); |
|
103 |
ui->height->setValue( DEFAULT_HEIGHT ); |
|
104 |
} |
|
105 |
// ..or reload previous values |
|
106 |
else { |
|
107 |
ui->left->setValue( m_settings->value( "rect/left" ).toInt() ); |
|
108 |
ui->top->setValue( m_settings->value( "rect/top" ).toInt() ); |
|
109 |
ui->width->setValue( m_settings->value( "rect/width" ).toInt() ); |
|
110 |
ui->height->setValue( m_settings->value( "rect/height" ).toInt() ); |
|
111 |
ui->frequency->setValue( m_settings->value( "rect/frequency" ).toInt() ); |
|
112 |
ui->onTop->setChecked( m_settings->value( "rect/ontop" ).toBool() ); |
|
113 |
} |
|
114 |
connect( ui->left, SIGNAL(valueChanged(int)), this, SLOT(slotCapParamsChanged()) ); |
|
115 |
connect( ui->top, SIGNAL(valueChanged(int)), this, SLOT(slotCapParamsChanged()) ); |
|
116 |
connect( ui->width, SIGNAL(valueChanged(int)), this, SLOT(slotCapParamsChanged()) ); |
|
117 |
connect( ui->height, SIGNAL(valueChanged(int)), this, SLOT(slotCapParamsChanged()) ); |
|
118 |
connect( ui->frequency, SIGNAL(valueChanged(int)), this, SLOT(slotCapParamsChanged()) ); |
|
119 |
connect( ui->onTop, SIGNAL(toggled(bool)), this, SLOT(slotOnTopChanged()) ); |
|
120 |
slotOnTopChanged(); |
|
121 |
|
|
122 |
// create the capture |
| e45848b by Enrico Ros at 2009-05-31 |
123 |
m_capture = new ScreenCapture( this ); |
| 1ff0643 by Enrico Ros at 2009-02-20 |
124 |
connect( m_capture, SIGNAL(gotPixmap(const QPixmap &, const QPoint &)), |
|
125 |
this, SLOT(slotProcessPixmap(const QPixmap &, const QPoint &)) ); |
|
126 |
slotCapParamsChanged(); |
| d9a5923 by Enrico Ros at 2009-02-21 |
127 |
|
| 6336a38 by Enrico Ros at 2009-02-25 |
128 |
// create the OCR, train with font and saved glyphs |
|
129 |
m_ocr = new Ocr(); |
| e45848b by Enrico Ros at 2009-05-31 |
130 |
#if 0 |
| 6336a38 by Enrico Ros at 2009-02-25 |
131 |
QFont font( "Arial", 32 ); |
|
132 |
font.setBold( true ); |
|
133 |
m_ocr->trainFont( font ); |
| e45848b by Enrico Ros at 2009-05-31 |
134 |
#endif |
| c03b1c8 by Enrico Ros on Win32 at 2009-06-03 |
135 |
QString dirString = QDir::toNativeSeparators(QCoreApplication::applicationDirPath()) + QDir::separator() + "wc-glyphs"; |
|
136 |
QDirIterator dIt( dirString, QStringList() << "glyph_*.png", QDir::Files ); |
|
137 |
if ( !dIt.hasNext() ) |
|
138 |
qWarning() << "ERROR: can't load glyphs.. something br0ken?" << dirString; |
| 6336a38 by Enrico Ros at 2009-02-25 |
139 |
while ( dIt.hasNext() ) { |
|
140 |
QString fileName = dIt.next(); |
|
141 |
QChar character = fileName.right( 5 ).at( 0 ); |
|
142 |
QImage image( fileName, "PNG" ); |
|
143 |
if ( !image.isNull() ) { |
|
144 |
m_ocr->trainGlyph( image, character ); |
|
145 |
qWarning() << "loaded" << fileName << character; |
| c03b1c8 by Enrico Ros on Win32 at 2009-06-03 |
146 |
} else |
|
147 |
qWarning() << "ERROR loading" << fileName << character; |
| 6336a38 by Enrico Ros at 2009-02-25 |
148 |
} |
|
149 |
|
| d9a5923 by Enrico Ros at 2009-02-21 |
150 |
// ### TEMP |
|
151 |
m_capture->setEnabled( true ); |
| cbdc84f by Enrico Ros at 2009-02-20 |
152 |
} |
|
153 |
|
|
154 |
AppWidget::~AppWidget() |
|
155 |
{ |
| 1ff0643 by Enrico Ros at 2009-02-20 |
156 |
saveSettings(); |
| e45848b by Enrico Ros at 2009-05-31 |
157 |
delete m_pickingRegion; |
| 1ff0643 by Enrico Ros at 2009-02-20 |
158 |
delete m_settings; |
| 6336a38 by Enrico Ros at 2009-02-25 |
159 |
delete m_ocr; |
| 1e01a22 by Enrico Ros at 2009-02-21 |
160 |
delete m_game; |
| 1ff0643 by Enrico Ros at 2009-02-20 |
161 |
delete m_capture; |
| cbdc84f by Enrico Ros at 2009-02-20 |
162 |
delete ui; |
|
163 |
} |
| 1ff0643 by Enrico Ros at 2009-02-20 |
164 |
|
| e45848b by Enrico Ros at 2009-05-31 |
165 |
void AppWidget::mousePressEvent( QMouseEvent * event ) |
|
166 |
{ |
|
167 |
if ( m_pickingRegion ) |
|
168 |
m_pickingRegion->setStartPos( event->globalPos() ); |
|
169 |
} |
|
170 |
|
|
171 |
void AppWidget::mouseMoveEvent( QMouseEvent * event ) |
|
172 |
{ |
|
173 |
if ( m_pickingRegion ) |
|
174 |
m_pickingRegion->setEndPos( event->globalPos() ); |
|
175 |
} |
|
176 |
|
|
177 |
void AppWidget::mouseReleaseEvent( QMouseEvent * event ) |
|
178 |
{ |
|
179 |
if ( m_pickingRegion ) { |
|
180 |
m_pickingRegion->setEndPos( event->globalPos() ); |
|
181 |
ui->left->setValue( m_pickingRegion->rX ); |
|
182 |
ui->top->setValue( m_pickingRegion->rY ); |
|
183 |
///ui->width->setValue( m_pickingRegion->rWidth ); |
|
184 |
///ui->height->setValue( m_pickingRegion->rHeight ); |
|
185 |
slotCapParamsChanged(); |
|
186 |
delete m_pickingRegion; |
|
187 |
m_pickingRegion = 0; |
|
188 |
releaseMouse(); |
|
189 |
} |
|
190 |
} |
|
191 |
|
| 1ff0643 by Enrico Ros at 2009-02-20 |
192 |
void AppWidget::saveSettings() |
|
193 |
{ |
|
194 |
m_settings->setValue( "rect/left", ui->left->value() ); |
|
195 |
m_settings->setValue( "rect/top", ui->top->value() ); |
|
196 |
m_settings->setValue( "rect/width", ui->width->value() ); |
|
197 |
m_settings->setValue( "rect/height", ui->height->value() ); |
|
198 |
m_settings->setValue( "rect/frequency", ui->frequency->value() ); |
|
199 |
m_settings->setValue( "rect/ontop", ui->onTop->isChecked() ); |
|
200 |
} |
|
201 |
|
| e45848b by Enrico Ros at 2009-05-31 |
202 |
void AppWidget::on_gameNo_toggled( bool checked ) |
| 1ff0643 by Enrico Ros at 2009-02-20 |
203 |
{ |
| e45848b by Enrico Ros at 2009-05-31 |
204 |
if ( checked ) { |
|
205 |
delete m_game; |
|
206 |
m_game = 0; |
|
207 |
} |
| 1ff0643 by Enrico Ros at 2009-02-20 |
208 |
} |
|
209 |
|
| e45848b by Enrico Ros at 2009-05-31 |
210 |
void AppWidget::on_gameWc_toggled( bool checked ) |
| 1ff0643 by Enrico Ros at 2009-02-20 |
211 |
{ |
| e45848b by Enrico Ros at 2009-05-31 |
212 |
if ( checked ) { |
|
213 |
delete m_game; |
|
214 |
m_game = new WCGame( this ); |
|
215 |
} |
| 1ff0643 by Enrico Ros at 2009-02-20 |
216 |
} |
|
217 |
|
| e45848b by Enrico Ros at 2009-05-31 |
218 |
void AppWidget::on_gameWcLearn_toggled(bool checked) |
| 1ff0643 by Enrico Ros at 2009-02-20 |
219 |
{ |
| e45848b by Enrico Ros at 2009-05-31 |
220 |
if ( checked ) { |
|
221 |
//delete m_game; |
|
222 |
//m_game = new WCLearn( this ); |
|
223 |
} |
| d9a5923 by Enrico Ros at 2009-02-21 |
224 |
} |
|
225 |
|
| e45848b by Enrico Ros at 2009-05-31 |
226 |
void AppWidget::on_pickRegionButton_clicked() |
| d9a5923 by Enrico Ros at 2009-02-21 |
227 |
{ |
| e45848b by Enrico Ros at 2009-05-31 |
228 |
m_pickingRegion = new RegionWidget(); |
|
229 |
grabMouse( Qt::CrossCursor ); |
| d9a5923 by Enrico Ros at 2009-02-21 |
230 |
} |
|
231 |
|
| e45848b by Enrico Ros at 2009-05-31 |
232 |
void AppWidget::on_trainButton_clicked() |
| d9a5923 by Enrico Ros at 2009-02-21 |
233 |
{ |
| e45848b by Enrico Ros at 2009-05-31 |
234 |
if ( m_game ) |
|
235 |
m_game->train( m_ocr, ui->trainLetters->text(), m_capture->lastPixmap().toImage() ); |
| d9a5923 by Enrico Ros at 2009-02-21 |
236 |
} |
|
237 |
|
| e45848b by Enrico Ros at 2009-05-31 |
238 |
void AppWidget::slotOnTopChanged() |
| d9a5923 by Enrico Ros at 2009-02-21 |
239 |
{ |
| e45848b by Enrico Ros at 2009-05-31 |
240 |
Qt::WindowFlags flags = windowFlags(); |
|
241 |
if ( ui->onTop->isChecked() ) |
|
242 |
flags |= Qt::WindowStaysOnTopHint; |
|
243 |
else |
|
244 |
flags &= ~Qt::WindowStaysOnTopHint; |
|
245 |
setWindowFlags( flags ); |
|
246 |
show(); |
| d9a5923 by Enrico Ros at 2009-02-21 |
247 |
} |
|
248 |
|
| e45848b by Enrico Ros at 2009-05-31 |
249 |
void AppWidget::slotCapParamsChanged() |
| d9a5923 by Enrico Ros at 2009-02-21 |
250 |
{ |
| e45848b by Enrico Ros at 2009-05-31 |
251 |
QRect captureRect( ui->left->value(), ui->top->value(), ui->width->value(), ui->height->value() ); |
|
252 |
m_capture->setGeometry( captureRect ); |
|
253 |
m_capture->setFrequency( ui->frequency->value() ); |
|
254 |
ui->capDisplay->setFixedSize( captureRect.size() ); |
| 1ff0643 by Enrico Ros at 2009-02-20 |
255 |
} |
| 6336a38 by Enrico Ros at 2009-02-25 |
256 |
|
| e45848b by Enrico Ros at 2009-05-31 |
257 |
void AppWidget::slotProcessPixmap( const QPixmap & pixmap, const QPoint & /*cursor*/ ) |
| 6336a38 by Enrico Ros at 2009-02-25 |
258 |
{ |
| e45848b by Enrico Ros at 2009-05-31 |
259 |
// highlight pixmap |
|
260 |
if ( m_game ) |
|
261 |
ui->capDisplay->setPixmap( m_game->highlightPixmap( pixmap ) ); |
|
262 |
else |
|
263 |
ui->capDisplay->setPixmap( pixmap ); |
|
264 |
|
|
265 |
// make da move! |
|
266 |
if ( m_game ) |
|
267 |
m_game->run( ui, m_capture, m_ocr ); |
| 6336a38 by Enrico Ros at 2009-02-25 |
268 |
} |