-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
317 lines (281 loc) · 13.2 KB
/
Copy pathMainWindow.cpp
File metadata and controls
317 lines (281 loc) · 13.2 KB
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
/************************************************************************/
/* qt-opencv-multithreaded: */
/* A multithreaded OpenCV application using the Qt framework. */
/* */
/* MainWindow.cpp */
/* */
/* Nick D'Ademo <nickdademo@gmail.com> */
/* */
/* Copyright (c) 2012-2013 Nick D'Ademo */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without restriction, */
/* including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, */
/* and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */
/* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */
/* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/* */
/************************************************************************/
#include "MainWindow.h"
#include "ui_MainWindow.h"
// Qt
#include <QLabel>
#include <QMessageBox>
#include <QtGlobal>
#include <QProcess>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
// Setup UI
ui->setupUi(this);
// Set start tab as blank
QLabel *newTab = new QLabel(ui->tabWidget);
newTab->setText("没有连接摄像头.");
newTab->setAlignment(Qt::AlignCenter);
ui->tabWidget->addTab(newTab, "");
ui->tabWidget->setTabsClosable(false);
// Add "Connect to Camera" button to tab
connectToCameraButton = new QPushButton();
connectToCameraButton->setText("连接到摄像头...");
ui->tabWidget->setCornerWidget(connectToCameraButton, Qt::TopLeftCorner);
connect(connectToCameraButton,SIGNAL(released()),this, SLOT(connectToCamera()));
connect(ui->tabWidget,SIGNAL(tabCloseRequested(int)),this, SLOT(disconnectCamera(int)));
// Set focus on button
connectToCameraButton->setFocus();
// Connect other signals/slots
connect(ui->actionAbout, SIGNAL(triggered()), this, SLOT(showAboutDialog()));
connect(ui->actionQuit, SIGNAL(triggered()), this, SLOT(close()));
connect(ui->actionFullScreen, SIGNAL(toggled(bool)), this, SLOT(setFullScreen(bool)));
// Create SharedImageBuffer object
sharedImageBuffer = new SharedImageBuffer();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectToCamera()
{
// We cannot connect to a camera if devices are already connected and stream synchronization is in progress
if(ui->actionSynchronizeStreams->isChecked() && deviceNumberMap.size()>0 && sharedImageBuffer->getSyncEnabled())
{
// Prompt user
QMessageBox::warning(this, tr("qt-opencv-multithreaded"),
tr("Stream synchronization is in progress.\n\n"
"Please close all currently open streams before attempting to open a new stream."),
QMessageBox::Ok);
}
// Attempt to connect to camera
else
{
// Get next tab index
int nextTabIndex = (deviceNumberMap.size()==0) ? 0 : ui->tabWidget->count();
// Show dialog
CameraConnectDialog *cameraConnectDialog = new CameraConnectDialog(this, ui->actionSynchronizeStreams->isChecked());
if(cameraConnectDialog->exec()==QDialog::Accepted)
{
// Save user-defined device number
int deviceNumber = cameraConnectDialog->getDeviceNumber();
// Check if this camera is already connected
if(!deviceNumberMap.contains(deviceNumber))
{
// Create ImageBuffer with user-defined size
Buffer<Mat> *imageBuffer = new Buffer<Mat>(cameraConnectDialog->getImageBufferSize());
// Add created ImageBuffer to SharedImageBuffer object
sharedImageBuffer->add(deviceNumber, imageBuffer, ui->actionSynchronizeStreams->isChecked());
// Create CameraView
cameraViewMap[deviceNumber] = new CameraView(ui->tabWidget, deviceNumber, sharedImageBuffer);
// Check if stream synchronization is enabled
if(ui->actionSynchronizeStreams->isChecked())
{
// Prompt user
int ret = QMessageBox::question(this, tr("qt-opencv-multithreaded"),
tr("Stream synchronization is enabled.\n\n"
"Do you want to start processing?\n\n"
"Choose 'No' if you would like to open additional streams."),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
// Start processing
if(ret==QMessageBox::Yes)
sharedImageBuffer->setSyncEnabled(true);
// Defer processing
else
sharedImageBuffer->setSyncEnabled(false);
}
// Attempt to connect to camera
if(cameraViewMap[deviceNumber]->connectToCamera(cameraConnectDialog->getDropFrameCheckBoxState(),
cameraConnectDialog->getCaptureThreadPrio(),
cameraConnectDialog->getProcessingThreadPrio(),
cameraConnectDialog->getEnableFrameProcessingCheckBoxState(),
cameraConnectDialog->getResolutionWidth(),
cameraConnectDialog->getResolutionHeight()))
{
// Add to map
deviceNumberMap[deviceNumber] = nextTabIndex;
// Save tab label
QString tabLabel = cameraConnectDialog->getTabLabel();
// Allow tabs to be closed
ui->tabWidget->setTabsClosable(true);
// If start tab, remove
if(nextTabIndex==0)
ui->tabWidget->removeTab(0);
// Add tab
ui->tabWidget->addTab(cameraViewMap[deviceNumber], tabLabel + " [" + QString::number(deviceNumber) + "]");
ui->tabWidget->setCurrentWidget(cameraViewMap[deviceNumber]);
// Set tooltips
setTabCloseToolTips(ui->tabWidget, "Disconnect Camera");
// Prevent user from enabling/disabling stream synchronization after a camera has been connected
ui->actionSynchronizeStreams->setEnabled(false);
localSavePath = cameraConnectDialog->getLocalSavePath();
qDebug() << "localSavePath" << localSavePath;
//connect(this, SIGNAL(setLocalSavePath(QString)), ??, SLOT(setLocalSavePath(QString)));
connect(this, SIGNAL(setLocalSavePath(QString)), cameraViewMap[deviceNumber], SLOT(getLocalSavePath(QString)));
emit setLocalSavePath(localSavePath);
syncThread = new SyncThread(localSavePath);
syncThread->start(QThread::NormalPriority);
}
// Could not connect to camera
else
{
// Display error message
QMessageBox::warning(this,"ERROR:","Could not connect to camera. Please check device number.");
// Explicitly delete widget
delete cameraViewMap[deviceNumber];
cameraViewMap.remove(deviceNumber);
// Remove from shared buffer
sharedImageBuffer->removeByDeviceNumber(deviceNumber);
// Explicitly delete ImageBuffer object
delete imageBuffer;
}
}
// Display error message
else
QMessageBox::warning(this,"ERROR:","Could not connect to camera. Already connected.");
}
// Delete dialog
delete cameraConnectDialog;
}
}
void MainWindow::disconnectCamera(int index)
{
// Local variable(s)
bool doDisconnect=true;
// Check if stream synchronization is enabled, more than 1 camera connected, and frame processing is not in progress
if(ui->actionSynchronizeStreams->isChecked() && cameraViewMap.size()>1 && !sharedImageBuffer->getSyncEnabled())
{
// Prompt user
int ret = QMessageBox::question(this, tr("qt-opencv-multithreaded"),
tr("Stream synchronization is enabled.\n\n"
"Disconnecting this camera will cause frame processing to begin on other streams.\n\n"
"Do you wish to proceed?"),
QMessageBox::Yes | QMessageBox::No,
QMessageBox::Yes);
// Disconnect
if(ret==QMessageBox::Yes)
doDisconnect=true;
// Do not disconnect
else
doDisconnect=false;
}
// Disconnect camera
if(doDisconnect)
{
// Save number of tabs
int nTabs = ui->tabWidget->count();
// Close tab
ui->tabWidget->removeTab(index);
// Delete widget (CameraView) contained in tab
delete cameraViewMap[deviceNumberMap.key(index)];
cameraViewMap.remove(deviceNumberMap.key(index));
// Remove from map
removeFromMapByTabIndex(deviceNumberMap, index);
// Update map (if tab closed is not last)
if(index!=(nTabs-1))
updateMapValues(deviceNumberMap, index);
// If start tab, set tab as blank
if(nTabs==1)
{
QLabel *newTab = new QLabel(ui->tabWidget);
newTab->setText("No camera connected.");
newTab->setAlignment(Qt::AlignCenter);
ui->tabWidget->addTab(newTab, "");
ui->tabWidget->setTabsClosable(false);
ui->actionSynchronizeStreams->setEnabled(true);
}
}
}
void MainWindow::showAboutDialog()
{
QMessageBox::information(this, "About", QString("Thanks to Nick D'Ademo\n\n%1").arg(APP_VERSION));
}
bool MainWindow::removeFromMapByTabIndex(QMap<int, int> &map, int tabIndex)
{
QMutableMapIterator<int, int> i(map);
while (i.hasNext())
{
i.next();
if(i.value()==tabIndex)
{
i.remove();
return true;
}
}
return false;
}
void MainWindow::updateMapValues(QMap<int, int> &map, int tabIndex)
{
QMutableMapIterator<int, int> i(map);
while (i.hasNext())
{
i.next();
if(i.value()>tabIndex)
i.setValue(i.value()-1);
}
}
void MainWindow::setTabCloseToolTips(QTabWidget *tabs, QString tooltip)
{
QList<QAbstractButton*> allPButtons = tabs->findChildren<QAbstractButton*>();
for (int ind = 0; ind < allPButtons.size(); ind++)
{
QAbstractButton* item = allPButtons.at(ind);
if (item->inherits("CloseButton"))
item->setToolTip(tooltip);
}
}
void MainWindow::setFullScreen(bool input)
{
if(input)
this->showFullScreen();
else
this->showNormal();
}
void MainWindow::on_actionSync_triggered()
{
/*
* rsync -auzv --password-file=./pass localSavePath/ citta@192.168.1.5:citta
*/
qDebug() << "rsync images" << localSavePath;
QString cmd;
QString projectPath = QCoreApplication::applicationDirPath();
QString remoteIp = "192.168.1.5";
#if defined(Q_OS_UNIX)
cmd = QString("/usr/bin/rsync -auzv %1/eye_images/ citta@%2::citta").arg(localSavePath).arg(remoteIp);
#else
cmd = QString("%1/rsync -auzv %1/eye_images/ citta@%2::citta").arg(projectPath).arg(remoteIp);
#endif
qDebug() << cmd;
QProcess::execute(cmd);
}