Fixed NetSend so it can be used in debug mode.
[qt/compvision.git] / MainWindow.cpp
1 #include "MainWindow.h"\r
2 \r
3 #include <QFileDialog>\r
4 #include <QMouseEvent>\r
5 \r
6 int main(int argc, char **argv)\r
7 {\r
8         QApplication app(argc, argv);\r
9         MainWindow mw;\r
10         mw.show();\r
11 \r
12         return app.exec();\r
13 }\r
14 \r
15 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)\r
16 {\r
17         setupUi(this);\r
18         \r
19         // Set up the ImagePools\r
20         ipDone=new ImagePool();\r
21         ipSrc=new ImagePool();\r
22         ipProc=new ImagePool();\r
23         ipView=new ImagePool();\r
24         \r
25         // Unfortunately, multiple frames can block MainWindow.\r
26         // Need to increase the execution priority of this program; never exceeds 30% CPU.\r
27         ipDone->send(new QImage(320, 240, QImage::Format_RGB32));\r
28         ipDone->send(new QImage(320, 240, QImage::Format_RGB32));\r
29         \r
30         // Set up the dialogs\r
31         cp=new ColorPicker();\r
32         sd=new SourceDialog(ipDone, ipSrc);\r
33         dd=new DestinationDialog(ipView, ipDone);\r
34         is=0;\r
35         running=false;\r
36         \r
37         // Set up the processing\r
38         proc=new ImageProcessor(ipSrc, ipProc);\r
39         proc->bypass=true;\r
40         proc->start();\r
41         \r
42         // Set up the display\r
43         view=new ImageViewer(labelView, fpsLabel, ipProc, ipView);\r
44         view->start();\r
45         QObject::connect(view, SIGNAL(layout(QSemaphore *)), this, SLOT(updateLayout(QSemaphore *)));\r
46         \r
47         QObject::connect(sd, SIGNAL(source(ImageSource *)), this, SLOT(source(ImageSource *)));\r
48         QObject::connect(this, SIGNAL(color(int)), cp, SLOT(setColor(int)));\r
49         \r
50         labelView->installEventFilter(this); // to capture mouse presses\r
51 }\r
52 \r
53 void MainWindow::on_actionLoad_triggered(bool checked)\r
54 {\r
55         QString f=QFileDialog::getOpenFileName(this, "Choose an image to open", path, "Images (*.bmp *.gif *.jpg *.png)");\r
56         if(f.size()<5) return;\r
57         \r
58         // try opening the file\r
59         QImage *image=new QImage();\r
60         if(!image->load(f))\r
61         {\r
62                 delete(image);\r
63                 return;\r
64         }\r
65         \r
66         // Inject the image into the processing stream\r
67         delete(ipDone->receive());\r
68         ipSrc->send(image);\r
69         \r
70         path=f; // remember the last selected image\r
71 }\r
72 \r
73 void MainWindow::on_actionSave_triggered(bool checked)\r
74 {\r
75         const QPixmap *img=labelView->pixmap();\r
76         if(!img || img->isNull()) return;\r
77         \r
78         QString f=QFileDialog::getSaveFileName(this, "Choose where to save the image", path, "Images (*.bmp *.jpg *.png)");\r
79         \r
80         if(f.endsWith(".bmp", Qt::CaseInsensitive))\r
81         {\r
82                 img->save(f, "BMP");\r
83         }\r
84         else if(f.endsWith(".jpg", Qt::CaseInsensitive) || f.endsWith(".jpeg", Qt::CaseInsensitive))\r
85         {\r
86                 img->save(f, "JPG");\r
87         }\r
88         else if(f.endsWith(".png", Qt::CaseInsensitive))\r
89         {\r
90                 img->save(f, "PNG");\r
91         }\r
92 }\r
93 \r
94 void MainWindow::on_actionExit_triggered(bool checked)\r
95 {\r
96         this->close();\r
97 }\r
98 \r
99 void MainWindow::on_actionColor_Picker_triggered(bool checked)\r
100 {\r
101         cp->show();\r
102 }\r
103 \r
104 void MainWindow::on_actionSources_triggered(bool checked)\r
105 {\r
106         sd->show();\r
107 }\r
108 \r
109 void MainWindow::on_actionDestinations_triggered(bool checked)\r
110 {\r
111         dd->show();\r
112 }\r
113 \r
114 void MainWindow::on_buttonStart_clicked()\r
115 {\r
116         if(running)\r
117         {\r
118                 buttonStart->setText("Start");\r
119                 if(is) is->halt();\r
120                 running=false;\r
121         }\r
122         else\r
123         {\r
124                 buttonStart->setText("Stop");\r
125                 if(is) is->start();\r
126                 running=true;\r
127         }\r
128 }\r
129 \r
130 void MainWindow::on_buttonSnap_clicked()\r
131 {\r
132         if(is)\r
133         {\r
134                 is->snap();\r
135         }\r
136         else if(path.size()>0)\r
137         {\r
138                 // try opening the file\r
139                 QImage *image=new QImage();\r
140                 if(!image->load(path))\r
141                 {\r
142                         delete(image);\r
143                         return;\r
144                 }\r
145                 \r
146                 // Inject the image into the processing stream\r
147                 delete(ipDone->receive());\r
148                 ipSrc->send(image);\r
149         }\r
150 }\r
151 \r
152 void MainWindow::on_checkProcess_stateChanged(int state)\r
153 {\r
154         if(state == Qt::Checked)\r
155         {\r
156                 proc->bypass=false;\r
157         }\r
158         else\r
159         {\r
160                 proc->bypass=true;\r
161         }\r
162 }\r
163 \r
164 bool MainWindow::eventFilter(QObject *obj, QEvent *event)\r
165 {\r
166         if(event->type() == QEvent::MouseButtonPress)\r
167         {\r
168                 // Check if it is on a picture\r
169                 if(labelView == (QLabel *)obj)\r
170                 {\r
171                         // Yell out what was pressed.\r
172                         QMouseEvent *me=(QMouseEvent *)event;\r
173                         int mousex=me->x();\r
174                         int mousey=me->y();\r
175                         if(labelView->pixmap())\r
176                         {\r
177                                 QRgb pix=labelView->pixmap()->toImage().pixel(mousex, mousey);\r
178                         \r
179                                 cp->show();\r
180                                 emit color(pix);\r
181                                 \r
182                                 textMessages->append(QString("(%1,%2)=%3").arg(mousex).arg(mousey).arg(pix,8,16,QChar('0')));\r
183                         }\r
184                         \r
185                         return true; // stop propagation of this event\r
186                 }\r
187         }\r
188         \r
189         // standard event processing\r
190         return QObject::eventFilter(obj, event);\r
191 }\r
192 \r
193 void MainWindow::source(ImageSource *is)\r
194 {\r
195         if(this->is)\r
196         {\r
197                 this->is->halt();\r
198         }\r
199         this->is=is;\r
200         if(is)\r
201         {\r
202                 if(running)\r
203                 {\r
204                         is->start();\r
205                 }\r
206                 else\r
207                 {\r
208                         is->halt();\r
209                 }\r
210         }\r
211 }\r
212 \r
213 void MainWindow::updateLayout(QSemaphore *lock)\r
214 {\r
215         // Signal that MainWindow's event loop has run\r
216         lock->release();\r
217 }\r