-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmainwindow.cpp
430 lines (351 loc) · 12.3 KB
/
mainwindow.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "imagewidget.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Initializes variables
cellWidth = 0;
rowCount = 0;
items = new QList<ImageWidget*>;
ui->tabWidget->removeTab(1);
ui->tabWidget->removeTab(0);
//ui->tabWidget->addTab(new TargetListWindow, "Target List") ;
noTabs = true ;
// Sets number of columns
setColumnCount(5);
// Get application path
QString exePath = QDir::currentPath();
ui->consoleOutput->append("Current path: " + exePath + "\n");
// Get potential files for classifier exe
QList<QString> files;
listFiles(exePath, "", files);
QString classifierPath;
QString cnnPath;
// Find classifier exe
for (int i = 0; i < files.size(); ++i)
{
if (files[i].contains("SoftmaxRegression.exe"))
{
classifierPath = files[i];
cnnPath = classifierPath;
cnnPath.replace("SoftmaxRegression.exe", "TrainedCNN");
}
}
// Get args
QStringList args;
cnnPath = cnnPath.replace('/', '\\');
args << "-cnn" << cnnPath;
// Start classifier
classifier = new QProcess(this);
classifier->start(classifierPath, args, QProcess::Unbuffered | QProcess::ReadWrite);
classifier->waitForStarted();
// Connect slots for piping the standard output
connect(classifier, SIGNAL(readyReadStandardOutput()), this, SLOT(ReadOut()));
connect(classifier, SIGNAL(readyReadStandardError()), this, SLOT(ReadErr()));
// Set up completer
completer = new QCompleter(prevCommands, this);
ui->consoleCommander->setCompleter(completer);
dataPackage = new LifeSupport(classifier,ui->consoleOutput) ;
}
void MainWindow::ReadOut()
{
QProcess *p = dynamic_cast<QProcess *>(sender());
if (p)
{
ui->consoleOutput->moveCursor(QTextCursor::End);
ui->consoleOutput->insertPlainText(p->readAllStandardOutput());
ui->consoleOutput->moveCursor(QTextCursor::End);
}
}
void MainWindow::ReadErr()
{
QProcess *p = dynamic_cast<QProcess *>(sender());
if (p)
{
//ui->consoleOutput->moveCursor(QTextCursor::End);
//ui->consoleOutput->insertPlainText("ERROR: " + p->readAllStandardError());
//ui->consoleOutput->moveCursor(QTextCursor::End);
}
}
void MainWindow::listFiles(QDir directory, QString indent, QList<QString> &files)
{
indent += "\t";
QDir dir(directory);
QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
foreach(QFileInfo finfo, list) {
files.append(finfo.filePath());
if(finfo.isDir()) {
listFiles(QDir(finfo.absoluteFilePath()), indent, files);
}
}
}
void MainWindow::setColumnCount(int col) {
colCount = col;
ui->photoListTable->setColumnCount(colCount);
}
MainWindow::~MainWindow()
{
classifier->close();
classifier->terminate();
delete items;
delete ui;
delete classifier;
delete completer;
}
void MainWindow::on_MainWindow_destroyed()
{
}
void MainWindow::resizeEvent(QResizeEvent* e)
{
resizeTable();
QWidget::resizeEvent(e);
}
void MainWindow::resizeTable()
{
//return; // Disable resizing
// Gets table width
tableWidth = ui->photoListTable->width()-19;
// Calculates cell width based on table width and number of columns
cellWidth = tableWidth/colCount;
// Calculates cell height based on cell width
cellHeight = cellWidth * 4/5 + 23;
// Rethinks the number of columns
/*
if (tableWidth < 1150) {
if (colCount != 4) {
setColumnCount(4);
refreshTable();
}
}
else {
if (colCount != 5) {
setColumnCount(5);
refreshTable();
}
}
*/
// Resizes each column
for (int i = 0; i < colCount; i++) {
ui->photoListTable->setColumnWidth(i, cellWidth);
}
// Resizes rows
ui->photoListTable->verticalHeader()->setDefaultSectionSize(cellHeight);
}
void MainWindow::refreshTable()
{
resizeTable();
// Makes copy of the items
QList<ImageWidget *> *itemsCopy = new QList<ImageWidget *>;
for (int i = 0; i < items->size(); i++) {
ImageWidget *temp = new ImageWidget(dataPackage, this, false);
// Copy all information over
temp->setImage(items->at(i)->getImage());
temp->setTitle(items->at(i)->getTitle());
temp->setFilePath(items->at(i)->getFilePath());
temp->setFolderPath(items->at(i)->getFolderPath());
temp->setImagePath(items->at(i)->getImagePath());
temp->setNumTargets(items->at(i)->getNumTargets());
temp->setSeen(items->at(i)->getSeen());
// Preserve the old targetList window
temp->deleteTargetListWindow();
temp->changeTargetListWindow(items->at(i)->getTargetList(), items->at(i)->isInitialized());
items->at(i)->changeTargetListWindow(NULL);
itemsCopy->append(temp);
delete &*(items->at(i));
}
// Clears table
ui->photoListTable->clear();
// Recalculates rowCount
rowCount = ceil((double) items->size() / (double) colCount);
ui->photoListTable->setRowCount(rowCount);
// Calculate max col number on the last row
int cMax = (itemsCopy->size() - (rowCount-1)*colCount) % (colCount+1);
// Sets new table contents
int itemCount = 0;
for (int r = 0; r < rowCount; r++) {
for (int c = 0; c < colCount; c++) {
if (!(r == rowCount-1 && c == cMax)) { // ends if the end of the list is hit
items->replace(itemCount, itemsCopy->at(itemCount));
// Sets the widget
ui->photoListTable->setCellWidget(r, c, items->at(itemCount));
itemCount++;
}
else{
break;
}
}
// Disables extra spaces
if (r == rowCount-1) {
for (int c = cMax; c < colCount; c++) {
ui->photoListTable->setItem(r, c, new QTableWidgetItem());
ui->photoListTable->item(r, c)->setFlags(Qt::ItemIsSelectable);
}
}
}
// Take care of memory
delete itemsCopy;
}
QList<ImageWidget*>* MainWindow::getItems()
{
return this->items;
}
void MainWindow::appendItem(QString folderPath, QString filePath, QString imagePath, QString title, int numTargets)
{
// Creates item
ImageWidget *newWidget = new ImageWidget(dataPackage, this);
newWidget->setTitle(title);
newWidget->setImage(imagePath);
newWidget->setFilePath(filePath);
newWidget->setFolderPath(folderPath);
newWidget->setNumTargets(numTargets);
items->append(newWidget);
}
void MainWindow::indexToCoordinates(int index, int *r, int *c)
{
// Row index
*r = ceil((double) (index+1) / (double) colCount) - 1;
// Column index
*c = ((index+1) - *r*colCount) % (colCount+1) - 1;
}
void MainWindow::on_loadButton_clicked()
{
QSettings config(QDir::currentPath()+"config.ini",QSettings::IniFormat);
// Gets the directory from a separate window
QString dir = QFileDialog::getExistingDirectory(this, tr("Load Directory..."), config.value("User Settings/Image Folder","/home").toString(), QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
config.setValue("User Settings/Image Folder",dir);
// Create filter
QStringList nameFilter;
nameFilter.append("*.ini");
// Scans through directory, applying the filter
QDir directory(dir);
QStringList fileList = directory.entryList(nameFilter);
// Goes through each file and opening the image
foreach ( QString file, fileList){
QString filePath = dir+"/"+file ;
QSettings resultFile(filePath,QSettings::IniFormat);
QString imagePath = resultFile.value("Analysis Parameters/IMAGE","").toString() ; //pass directory to image widget
int numTargets = resultFile.value("Crop Info/Number of Crops", "").toInt(); // gets the number of targets
QFileInfo fileInfo(imagePath);
QString filename(fileInfo.fileName());
if ( imagePath != "" ){
appendItem(dir, filePath, imagePath, filename, numTargets);
}
}
/*nameFilter.append("*.png");
nameFilter.append("*.jpg");
// Scans through directory, applying the filter
QDir directory(dir);
QStringList fileList = directory.entryList(nameFilter);
foreach (QString filePath, fileList) {
appendItem(dir+"/"+filePath, filePath);
}*/
refreshTable();
}
void MainWindow::on_addItemButton_clicked()
{
//addItem("");
}
void MainWindow::on_editButton_clicked()
{
QItemSelectionModel *select = ui->photoListTable->selectionModel();
QModelIndexList selected = select->selectedIndexes();
if (select->hasSelection() && selected.length() == 1) {
// Gets the selected index
QList<QModelIndex>::iterator i = selected.begin();
int selectedIndex = (i->row())*colCount + i->column();
// Makes an edit dialog
ItemMaker *editDialog = new ItemMaker();
editDialog->setModal(true);
editDialog->setWindowTitle("Edit");
// Sets initial information
editDialog->setTitle(items->at(selectedIndex)->getTitle());
editDialog->setFilePath(items->at(selectedIndex)->getImagePath());
// Starts the dialog
editDialog->exec();
// If okay was pressed in the edit dialog
if (editDialog->getAccepted()) {
// Gets information from edit dialog
QString title = editDialog->getTitle();
QString filePath = editDialog->getFilePath();
// Sets item information
items->at(selectedIndex)->setTitle(title);
items->at(selectedIndex)->setImage(filePath);
}
delete editDialog;
}
}
void MainWindow::on_deleteItemButton_clicked()
{
QItemSelectionModel *select = ui->photoListTable->selectionModel();
QModelIndexList selected = select->selectedIndexes();
QList<int> deletionOrder;
if (select->hasSelection()) {
// Makes sure everything is deleted in the correct order
for (QList<QModelIndex>::iterator i = selected.begin(); i != selected.end(); i++) {
int index = (i->row()) * colCount + i->column();
deletionOrder.append(index);
}
// Sorts into descending order
qSort(deletionOrder.begin(), deletionOrder.end(), qGreater<int>());
// Deletes items in the table
for (int i = 0; i < deletionOrder.length(); i++) {
int index = deletionOrder.at(i);
items->removeAt(index);
}
// Clears selection
ui->photoListTable->selectionModel()->clearSelection();
refreshTable();
}
}
void MainWindow::addTab(QWidget* newTab, QString title) {
if (noTabs)
ui->tabWidget->removeTab(0);
ui->tabWidget->setCurrentIndex(ui->tabWidget->addTab(newTab, title));
noTabs = false ;
}
// Returns false if not found
bool MainWindow::findTab(QWidget *tab){
int index = ui->tabWidget->indexOf(tab);
if (index == -1) return false;
ui->tabWidget->setCurrentIndex(index);
return true;
}
void MainWindow::on_tabWidget_tabCloseRequested(int index)
{
ui->tabWidget->removeTab(index);
if (ui->tabWidget->count()==0){
noTabs = true ;
//ui->tabWidget->addTab(new TargetListWindow, "Target List") ;
}
}
void MainWindow::on_consoleCommander_returnPressed()
{
// Get command
QString command = ui->consoleCommander->text();
QStringList::iterator it = std::find(prevCommands.begin(), prevCommands.end(), command);
if (it == prevCommands.end())
prevCommands << command;
command += "\n";
// Update autocompleter
QStringListModel *model = (QStringListModel*)(completer->model());
if(model == NULL)
model = new QStringListModel();
model->setStringList(prevCommands);
completer->setModel(model);
// Send command
classifier->write(command.toStdString().c_str());
// Clear commander
ui->consoleCommander->setText("");
}
void MainWindow::on_actionProcess_Image_Set_triggered()
{
ImageSetProcessor *processor = new ImageSetProcessor();
processor->setModal(true);
processor->setWindowTitle("Image Processor");
// Starts the dialog
processor->exec();
delete processor;
}