summaryrefslogtreecommitdiffhomepage
path: root/ftnoir_tracker_fd/ftnoir_tracker_facedetect_dialog.cpp
blob: 71b1924236bf0bfac4b06b2a3f49240dade62733 (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
/* Copyright (c) 2012 Stanislaw Halik
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 */

#include "ftnoir_tracker_fd.h"
#include <Qt>
#include <QPainter>
#include <QPaintEngine>

//*******************************************************************************************************
// faceDetect Settings-dialog.
//*******************************************************************************************************

static void load_settings(struct face_detect_settings* out) {
	qDebug("[!] load_settings()");
	QSettings settings("Abbequerque Inc.", "FaceTrackNoIR");	// Registry settings (in HK_USER)

	QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/Settings/default.ini" ).toString();
	QSettings iniFile( currentFile, QSettings::IniFormat );		// Application settings (in INI-file)

	iniFile.beginGroup ( "FaceDetectTracker" );
	out->redetect_ms = iniFile.value("RedetectMs", 500).toInt();
	out->camera_id = iniFile.value("CameraId", 0).toInt();
	out->quit = 0;
	out->newOutput = 0;
	out->magic = FD_MAGIC;
	out->widgetp = iniFile.value("VideoWidget", true).toBool();
	iniFile.endGroup ();
}

static void save_settings(const struct face_detect_settings* in) {

	QSettings settings("Abbequerque Inc.", "FaceTrackNoIR");	// Registry settings (in HK_USER)

	QString currentFile = settings.value ( "SettingsFile", QCoreApplication::applicationDirPath() + "/Settings/default.ini" ).toString();
	QSettings iniFile( currentFile, QSettings::IniFormat );		// Application settings (in INI-file)

	iniFile.beginGroup ( "FaceDetectTracker" );
	iniFile.setValue("RedetectMs", in->redetect_ms);
	iniFile.setValue("CameraId", in->camera_id);
	iniFile.setValue("VideoWidget", in->widgetp);
	iniFile.endGroup ();
}


//
// Constructor for server-settings-dialog
//
TrackerControls::TrackerControls() :
QWidget()
{
	qDebug("[!] TrackerControls::TrackerControls()");
	hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(struct face_detect_shm), fd_shm_name);
	shm = (struct face_detect_shm*) MapViewOfFile(hMapFile, FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, sizeof(struct face_detect_shm));
	hMutex = CreateMutex(NULL, false, fd_mutex_name);
	ui.setupUi( this );

	load_settings(&shm->settings);

	ui.redetect_ms->setValue(shm->settings.redetect_ms);
	ui.cameraId->setValue(shm->settings.camera_id);
	ui.videoWidget->setChecked(shm->settings.widgetp);

	settingsDirty = false;

	// what a load of boilerplate...
	QObject::connect(ui.okButton, SIGNAL(clicked()), this, SLOT(doOK()));
	QObject::connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(doCancel()));
	QObject::connect(ui.redetect_ms, SIGNAL(valueChanged(int)), this, SLOT(doSetRedetectMs(int)));
	QObject::connect(ui.cameraId, SIGNAL(valueChanged(int)), this, SLOT(doSetCameraId(int)));
	QObject::connect(ui.videoWidget, SIGNAL(toggled(bool)), this, SLOT(doSetVideoWidget(bool)));
}

void TrackerControls::save() {
	save_settings(&shm->settings);
	settingsDirty = false;
}

void TrackerControls::doSetCameraId(int val) {
	settingsDirty = true;
	WaitForSingleObject(hMutex, INFINITE);
	shm->settings.camera_id = val;
	ReleaseMutex(hMutex);
}

void TrackerControls::doSetVideoWidget(bool val) {
	settingsDirty = true;
	WaitForSingleObject(hMutex, INFINITE);
	shm->settings.widgetp = val;
	ReleaseMutex(hMutex);
}

void TrackerControls::doSetRedetectMs(int val) {
	settingsDirty = true;
	WaitForSingleObject(hMutex, INFINITE);
	shm->settings.redetect_ms = val;
	ReleaseMutex(hMutex);
}

//
// Destructor for server-dialog
//
TrackerControls::~TrackerControls() {
	UnmapViewOfFile(shm);
	//CloseHandle(hMutex);
	//CloseHandle(hMapFile);
}

void TrackerControls::Release()
{
    delete this;
}

//
// Initialize tracker-client-dialog
//
void TrackerControls::Initialize(QWidget *parent) {

	QPoint offsetpos(200, 200);
	if (parent) {
		this->move(parent->pos() + offsetpos);
	}
	show();
}

//
// OK clicked on server-dialog
//
void TrackerControls::doOK() {
	save();
	this->close();
}

//
// Cancel clicked on server-dialog
//
void TrackerControls::doCancel() {
	//
	// Ask if changed Settings should be saved
	//
	if (settingsDirty) {
		int ret = QMessageBox::question ( this, "Settings have changed", "Do you want to save the settings?", QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Discard );

		switch (ret) {
			case QMessageBox::Save:
				save();
				this->close();
				break;
			case QMessageBox::Discard:
				this->close();
				break;
			case QMessageBox::Cancel:
				// Cancel was clicked
				break;
			default:
				// should never be reached
			break;
		}
	}
	else {
		this->close();
	}
}

////////////////////////////////////////////////////////////////////////////////
// Factory function that creates instances if the Tracker-settings dialog object.

// Export both decorated and undecorated names.
//   GetTrackerDialog     - Undecorated name, which can be easily used with GetProcAddress
//                          Win32 API function.
//   _GetTrackerDialog@0  - Common name decoration for __stdcall functions in C language.
#pragma comment(linker, "/export:GetTrackerDialog=_GetTrackerDialog@0")

FTNOIR_TRACKER_BASE_EXPORT ITrackerDialogPtr __stdcall GetTrackerDialog( )
{
	return new TrackerControls;
}