-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathControllerDataExporter.cpp
More file actions
253 lines (214 loc) · 8.21 KB
/
Copy pathControllerDataExporter.cpp
File metadata and controls
253 lines (214 loc) · 8.21 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
#include "ControllerDataExporter.h"
#include "Controller/ControllerCommands.h"
#include "Controller/ControllerTrackedComponentCore.h"
#include "Controller/ControllerPlayer.h"
#include "Model/DataExporters/DataExporterCSV.h"
#include "Model/DataExporters/DataExporterSerialize.h"
#include "Model/DataExporters/DataExporterJson.h"
#include "util/types.h"
#include <qmessagebox.h>
#include "QDesktopServices"
using namespace BioTrackerUtilsMisc; // split
ControllerDataExporter::ControllerDataExporter(QObject* parent,
IBioTrackerContext* context,
ENUMS::CONTROLLERTYPE ctr)
: IControllerCfg(parent, context, ctr)
{
}
ControllerDataExporter::~ControllerDataExporter()
{
}
void ControllerDataExporter::cleanup()
{
if (m_Model)
qobject_cast<IModelDataExporter*>(m_Model)->finalize();
}
void ControllerDataExporter::connectControllerToController()
{
// connect to controller commands
IController* ictrcmd = m_BioTrackerContext->requestController(
ENUMS::CONTROLLERTYPE::COMMANDS);
ControllerCommands* ctrcmd = static_cast<ControllerCommands*>(ictrcmd);
QObject::connect(this,
&ControllerDataExporter::emitResetUndoStack,
ctrcmd,
&ControllerCommands::receiveClear,
Qt::DirectConnection);
// connect to view controller
IController* ctr = m_BioTrackerContext->requestController(
ENUMS::CONTROLLERTYPE::TRACKEDCOMPONENTCORE);
ControllerTrackedComponentCore* tccController =
dynamic_cast<ControllerTrackedComponentCore*>(ctr);
QObject::connect(this,
&ControllerDataExporter::emitViewUpdate,
tccController,
&ControllerTrackedComponentCore::receiveUpdateView,
Qt::DirectConnection);
// ControllerPlayer* cPl =
// dynamic_cast<ControllerPlayer*>(m_BioTrackerContext->requestController(ENUMS::CONTROLLERTYPE::PLAYER));
// QObject::connect(cPl, &ControllerPlayer::emitNextMediaInBatch, this,
// &ControllerDataExporter::receiveReset, Qt::DirectConnection);
}
void ControllerDataExporter::createModel()
{
m_Model = nullptr;
}
SourceVideoMetadata ControllerDataExporter::getSourceMetadata()
{
IController* ctrM = m_BioTrackerContext->requestController(
ENUMS::CONTROLLERTYPE::PLAYER);
MediaPlayer* mplay = dynamic_cast<MediaPlayer*>(ctrM->getModel());
SourceVideoMetadata d;
d.name = mplay->getCurrentFileName().toStdString();
d.fps = std::to_string(mplay->getFpsOfSourceFile());
d.fps = d.fps.erase(d.fps.find_last_not_of('0') + 1, std::string::npos);
return d;
}
void ControllerDataExporter::loadFile(std::string file)
{
if (_factory) {
qobject_cast<IModelDataExporter*>(m_Model)->loadFile(file);
} else {
std::cout << "Can not load tracks for this plugin as it does not "
"provide a factory."
<< std::endl;
}
}
void ControllerDataExporter::saveFile(std::string file)
{
if (_factory) {
qobject_cast<IModelDataExporter*>(m_Model)->writeAll(file);
} else {
std::cout << "Can not save tracks for this plugin as it does not "
"provide a factory."
<< std::endl;
}
}
void ControllerDataExporter::createView()
{
m_View = 0;
}
void ControllerDataExporter::setDataStructure(IModel* exp)
{
if (getModel())
delete getModel();
// Grab the codec from config file
std::string exporter = exporterList[_cfg->DataExporter];
if (exporter == "CSV")
m_Model = new DataExporterCSV(this);
else if (exporter == "Serialize")
m_Model = new DataExporterSerialize(this);
else if (exporter == "Json")
m_Model = new DataExporterJson(this);
else
m_Model = nullptr;
qobject_cast<IModelDataExporter*>(m_Model)->open(
static_cast<IModelTrackedTrajectory*>(exp));
IModelDataExporter* model;
if ((model = dynamic_cast<IModelDataExporter*>(getModel())) != nullptr) {
QObject::connect(model,
&IModelDataExporter::fileWritten,
this,
&ControllerDataExporter::receiveFileWritten);
}
}
void ControllerDataExporter::setComponentFactory(
IModelTrackedComponentFactory* exp)
{
_factory = exp;
}
void ControllerDataExporter::receiveTrackingDone(uint frame)
{
if (getModel()) {
dynamic_cast<IModelDataExporter*>(getModel())->write(frame);
}
}
void ControllerDataExporter::receiveFinalizeExperiment()
{
if (getModel()) {
emitResetUndoStack();
dynamic_cast<IModelDataExporter*>(getModel())->finalizeAndReInit();
emitViewUpdate();
}
}
void ControllerDataExporter::receiveReset()
{
if (getModel()) {
emitResetUndoStack();
dynamic_cast<IModelDataExporter*>(getModel())->finalizeAndReInit();
dynamic_cast<IModelDataExporter*>(getModel())->close();
emitViewUpdate();
}
createModel();
}
void ControllerDataExporter::connectModelToController()
{
IController* ctrM = m_BioTrackerContext->requestController(
ENUMS::CONTROLLERTYPE::PLAYER);
MediaPlayer* mplay = dynamic_cast<MediaPlayer*>(ctrM->getModel());
QObject::connect(mplay,
&MediaPlayer::fwdPlayerParameters,
this,
&ControllerDataExporter::rcvPlayerParameters);
}
void ControllerDataExporter::rcvPlayerParameters(
std::shared_ptr<const playerParameters> parameters)
{
if (qobject_cast<IModelDataExporter*>(m_Model) != nullptr) {
qobject_cast<IModelDataExporter*>(m_Model)->setFps(
parameters->m_fpsSourceVideo);
qobject_cast<IModelDataExporter*>(m_Model)->setTitle(
parameters->m_CurrentTitle);
}
}
int ControllerDataExporter::getNumber(bool trial)
{
// Get all existing files of trial or track directory and parse highest
// export number
QString basePath = trial ? _cfg->DirTrials : _cfg->DirTracks;
QStringList allFiles = QDir(basePath).entryList(QDir::NoDotAndDotDot |
QDir::Files);
int maxVal = 0;
foreach (QString s, allFiles) {
int val;
if (sscanf(s.toStdString().c_str(), "Export_%d_*", &val) != EOF) {
maxVal = std::max(maxVal, val);
}
}
return maxVal;
}
QString ControllerDataExporter::generateBasename(bool temporaryFile)
{
QString resultPath = (_trialStarted ? _cfg->DirTrials : _cfg->DirTracks);
QString path = (temporaryFile ? _cfg->DirTemp : resultPath);
int maxVal = getNumber(_trialStarted ? true : false);
std::string current = "Export_" + std::to_string(maxVal + 1) + "_";
return QString(getTimeAndDate(path.toStdString() + current, "").c_str());
}
void ControllerDataExporter::receiveFileWritten(QFileInfo fname)
{
QString str = "Exported file:\n";
str += fname.absoluteFilePath();
QMessageBox msgBox;
msgBox.setText("File saved!");
msgBox.setInformativeText(str);
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
QPushButton* goToFileDirButton = msgBox.addButton(tr("Show in folder"),
QMessageBox::ActionRole);
QPushButton* openFileButton = msgBox.addButton(tr("Open file"),
QMessageBox::ActionRole);
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();
if (msgBox.clickedButton() == goToFileDirButton) {
QUrl fileDirUrl = QUrl::fromLocalFile(fname.absolutePath());
QDesktopServices::openUrl(fileDirUrl);
} else if (msgBox.clickedButton() == openFileButton) {
QUrl fileUrl = QUrl::fromLocalFile(fname.absoluteFilePath());
QDesktopServices::openUrl(fileUrl);
}
}
void ControllerDataExporter::receiveTrialStarted(bool started)
{
_trialStarted = started;
}