-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtargetlistwindow.cpp
329 lines (277 loc) · 11.3 KB
/
targetlistwindow.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
#include "targetlistwindow.h"
#include "ui_targetlistwindow.h"
#include "qevent.h"
#include <QDebug>
#include "imagewidget.h"
TargetListWindow::TargetListWindow(LifeSupport *dataPackage, QWidget *parent) :
QDialog(parent),
parent(parent),
ui(new Ui::TargetListWindow)
{
this->loader = NULL;
colCount = 5;
ui->setupUi(this);
targetList = new TargetList(ui->targetListTable);
connect(ui->targetListTable->horizontalHeader(), SIGNAL(sectionClicked(int)),
this, SLOT(sort(int)));
mainPicWidth = 0;
mainPicHeight = 0;
resultFile = NULL;
this->data=dataPackage;
// Set row size
ui->targetListTable->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
ui->targetListTable->horizontalHeader()->setDefaultSectionSize(100);
ui->targetListTable->horizontalHeader()->setMinimumSectionSize(100);
ui->targetListTable->verticalHeader()->setDefaultSectionSize(100);
//ui->targetListTable->setStyleSheet("QTableWidget {selection-background-color: #00000000; selection-color: white}");
}
TargetListWindow::~TargetListWindow()
{
if (loader != 0 && loader->isRunning())
{
loader->requestInterruption();
loader->wait();
}
// Not sure if loader needs to be deleted. Could cause memory leak if not
delete loader;
delete ui;
delete targetList;
delete resultFile;
}
void TargetListWindow::on_newItem_clicked()
{
TargetMaker* targetMaker = new TargetMaker(this);
targetMaker->setModal(true);
targetMaker->setWindowTitle("Create New Target");
targetMaker->exec();
if (targetMaker->getAccepted()) {
targetList->addNewRow(targetMaker->getImageFilePath(), targetMaker->getName(), targetMaker->getCoord(), targetMaker->getDesc(), 0, 0);
}
delete targetMaker;
}
void TargetListWindow::on_edit_clicked()
{
QItemSelectionModel *select = ui->targetListTable->selectionModel();
QModelIndexList selectedRows = select->selectedRows();
if (select->hasSelection() && selectedRows.length() == 1) { // only 1 item can be selected
QList<QModelIndex>::iterator i = selectedRows.begin();
int selectedRow = i->row(); // gets the selected row number
// Creates edit window
targetEditor = new TargetMaker(this);
targetEditor->setModal(true);
targetEditor->setWindowTitle("Edit Target");
// Sets default values
targetEditor->setDefaultFileInput(targetList->getRows()->at(selectedRow)->imageFilePath);
targetEditor->setDefaultNameInput(targetList->getRows()->at(selectedRow)->name->text());
targetEditor->setDefaultCoordInput(targetList->getRows()->at(selectedRow)->coord->text());
targetEditor->setDefaultDescInput(targetList->getRows()->at(selectedRow)->desc->text());
targetEditor->setDefaultInputs();
// Opens edit window
targetEditor->exec();
if (targetEditor->getAccepted()) {
targetList->editRow(selectedRow, targetEditor->getImageFilePath(), targetEditor->getName(), targetEditor->getCoord(), targetEditor->getDesc());
}
delete targetEditor;
}
}
void TargetListWindow::on_deleteButton_clicked()
{
QItemSelectionModel *select = ui->targetListTable->selectionModel();
QModelIndexList selectedRows = select->selectedRows();
QList<int> deletionOrder;
if (select->hasSelection()) {
// Makes sure everything is deleted in the correct order
for (QList<QModelIndex>::iterator i = selectedRows.begin(); i != selectedRows.end(); i++) {
deletionOrder.append(i->row());
}
qSort(deletionOrder.begin(), deletionOrder.end());
for (int i = 0, decrement = 0; i < deletionOrder.length(); i++, decrement++) {
targetList->deleteRow(deletionOrder[i] - decrement);
}
ui->targetListTable->selectionModel()->clearSelection(); // Deselects all rows
}
}
void TargetListWindow::on_upButton_clicked()
{
QItemSelectionModel *select = ui->targetListTable->selectionModel();
QModelIndexList selectedRows = select->selectedRows();
QList<int> moveOrder;
if (select->hasSelection()) {
// Makes sure everything is moved in the correct order
for (QList<QModelIndex>::iterator i = selectedRows.begin(); i != selectedRows.end(); i++) {
moveOrder.append(i->row());
}
qSort(moveOrder.begin(), moveOrder.end());
ui->targetListTable->selectionModel()->clearSelection(); // Deselects all rows
QItemSelection selectedItems = ui->targetListTable->selectionModel()->selection();
if (moveOrder[0] != 0) {
for (int i = 0; i < moveOrder.length(); i++) {
ui->targetListTable->selectRow(moveOrder[i]-1);
selectedItems.merge(ui->targetListTable->selectionModel()->selection(), QItemSelectionModel::Select);
targetList->moveUp(moveOrder[i]);
}
ui->targetListTable->selectionModel()->select(selectedItems, QItemSelectionModel::Select); // Reselects new rows
}
}
}
void TargetListWindow::on_downButton_clicked()
{
QItemSelectionModel *select = ui->targetListTable->selectionModel();
QModelIndexList selectedRows = select->selectedRows();
QList<int> moveOrder;
if (select->hasSelection()) {
// Makes sure everything is moved in the correct order
for (QList<QModelIndex>::iterator i = selectedRows.begin(); i != selectedRows.end(); i++) {
moveOrder.append(i->row());
}
qSort(moveOrder.begin(), moveOrder.end(), qGreater<int>());
ui->targetListTable->selectionModel()->clearSelection(); // Deselects all rows
QItemSelection selectedItems = ui->targetListTable->selectionModel()->selection();
if (moveOrder[0] != targetList->getRows()->length()-1) {
for (int i = 0; i < moveOrder.length(); i++) {
ui->targetListTable->selectRow(moveOrder[i]+1);
selectedItems.merge(ui->targetListTable->selectionModel()->selection(), QItemSelectionModel::Select);
targetList->moveDown(moveOrder[i]);
}
ui->targetListTable->selectionModel()->select(selectedItems, QItemSelectionModel::Select); // Reselects new rows
}
}
}
void TargetListWindow::sort(int col)
{
if (col == 1) {
targetList->sortName(0, targetList->getRows()->length()-1);
targetList->refreshTable();
}
}
void TargetListWindow::setMainPic (QString imagePath) {
if (imagePath != "") {
QPixmap pix;
pix.load(imagePath);
mainpic = pix.scaled(381, 381, Qt::KeepAspectRatioByExpanding, Qt::FastTransformation) ;
ui->mainpic->setPixmap(mainpic);
}
}
void TargetListWindow::loadTargets(QString folderPath, QString filePath)
{
// Starts a new thread to load it
loader = new Loader(targetList, dynamic_cast<ImageWidget *>(this->parent), folderPath, filePath);
loader->start();
// Get height
resultFile = new QSettings(filePath, QSettings::IniFormat);
mainPicWidth = resultFile->value("Analysis Parameters/Width").toInt();
mainPicHeight = resultFile->value("Analysis Parameters/Height").toInt();
// Get info about image
QString altitude = resultFile->value("Position/altitude").toString();
QString latitude = resultFile->value("Position/latitude").toString();
QString longitude = resultFile->value("Position/longitude").toString();
QString heading = resultFile->value("Position/heading").toString();
QString headingDegrees = resultFile->value("Position/headingDegrees").toString();
ui->locationText->setText("<center><b>Latitude:</b> " + latitude +
" | <b>Longitude:</b> " + longitude +
" | <b>Altitude:</b> " + altitude +
" | <b>Heading:</b> " + heading +
" | <b>Heading (Degrees):</b> " + headingDegrees +
"</center>");
}
void Loader::run()
{
QSettings resultFile(filePath, QSettings::IniFormat);
for (int i = 0; i < resultFile.value("Crop Info/Number of Crops").toInt(); ++i)
{
if (!this->isInterruptionRequested())
{
QString imagePath = imageWidget->getTargetData()[i].imagePath;
QString name = imagePath;
QString coord = imageWidget->getTargetData()[i].coord;
int x = imageWidget->getTargetData()[i].x;
int y = imageWidget->getTargetData()[i].y;
QString desc = imageWidget->getTargetData()[i].desc;
try
{
targetList->addNewRow(folderPath+"/"+imagePath,name,coord,desc, x, y);
}
catch (std::exception& e)
{
return;
}
}
}
if (!this->isInterruptionRequested())
{
try
{
targetList->refreshTable();
}
catch (std::exception& e)
{
return;
}
}
}
void TargetListWindow::on_targetListTable_doubleClicked(const QModelIndex &index)
{
int rowNum = index.row();
TargetWindow *targetWindow = new TargetWindow(data, targetList->getRows()->at(rowNum), this);
targetWindow->setModal(true);
targetWindow->setWindowTitle("Target");
data->imagePath = targetList->getRows()->at(rowNum)->name->text() ;
// Opens edit window
targetWindow->exec();
delete targetWindow;
}
void TargetListWindow::on_targetListTable_clicked(const QModelIndex &index)
{
int rowNum = index.row();
TargetListItem* rowItem = targetList->getRows()->at(rowNum);
int x = rowItem->x;
int y = rowItem->y;
QPixmap pixmap;
QPainter painter;
QPen white(Qt::white);
pixmap = this->mainpic;
painter.begin(&pixmap);
painter.setPen(white);
// Draw Box around target
QRect rect;
int boxSize = pixmap.width()/15;
rect.setX((double) x/mainPicWidth*pixmap.width()-boxSize/2);
rect.setY((double) y/mainPicHeight*pixmap.height()-boxSize/2);
rect.setHeight(boxSize);
rect.setWidth(boxSize);
painter.drawRect(rect);
painter.end();
ui->mainpic->setPixmap(pixmap);
}
/*QString TargetListWindow::selected(){
QItemSelectionModel *select = ui->targetListTable->selectionModel();
QModelIndexList selectedRows = select->selectedRows();
if (select->hasSelection() && selectedRows.length() == 1) { // only 1 item can be selected
QList<QModelIndex>::iterator i = selectedRows.begin();
int selectedRow = i->row(); // gets the selected row number
return targetList->getRows()->at(selectedRow)->name->text() ;
}
else
return NULL ;
}*/
/*
void TargetListWindow::changeDesc ( QString desc ) {
QItemSelectionModel *select = ui->targetListTable->selectionModel();
QModelIndexList selectedRows = select->selectedRows();
if (select->hasSelection() && selectedRows.length() == 1)
{ // only 1 item can be selected
QList<QModelIndex>::iterator i = selectedRows.begin();
int selectedRow = i->row(); // gets the selected row number
targetList->getRows()->at(selectedRow)->desc->setText(desc);
}
}
*/
CustomLabel::CustomLabel( QWidget* parent, Qt::WindowFlags f )
: QLabel( parent, f ) {}
CustomLabel::CustomLabel( const QString& text, QWidget* parent, Qt::WindowFlags f )
: QLabel( text, parent, f ) {}
void CustomLabel::mousePressedEvent( QMouseEvent* ev )
{
const QPoint p = ev->pos();
emit mousePressed(p);
}