-
Notifications
You must be signed in to change notification settings - Fork 0
/
QtEvoPic.cpp
169 lines (136 loc) · 4.21 KB
/
QtEvoPic.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
/**
* QtEvoPic.cpp
*
* Created on: Aug 9, 2011
* Author: Jeroen Vlek
* Email: [email protected]
* Website: www.perceptivebits.com
* License: Free Beer (Feel free to use it in every
* way possible and if you like it, make
* sure to give me credit and buy me a drink
* if we ever meet ;) )
*/
#include "Config.h"
#include "QtEvoPic.h"
#include "TargetImage.h"
#include <iostream>
#include <cmath>
#include <QCoreApplication>
#include <QDebug>
#include <QFileDialog>
#include <QLabel>
#include <QPixmap>
#include <QRegExp>
#include <QSizePolicy>
QtEvoPic::QtEvoPic(QWidget *parent) :
QMainWindow(parent) {
ui.setupUi(this);
m_targetImageLabel = new QLabel(ui.gridLayoutWidget);
m_targetImageLabel->setAlignment(Qt::AlignCenter);
m_targetImageLabel->setScaledContents(true);
}
QtEvoPic::~QtEvoPic() {
delete m_targetImageLabel;
QList<QLabel*>::iterator it = m_phenoTypeImageLabels.begin();
for(; it != m_phenoTypeImageLabels.end(); ++it) {
delete *it;
}
}
void QtEvoPic::updateLayout() {
updateLabels();
// remove all current items
QLayoutItem *child;
while ((child = ui.gridLayout->takeAt(0)) != 0) {}
ui.gridLayout->addWidget(m_targetImageLabel, 0, 0);
// add phenotype labels in a square + remainder as an extra row
int rowLength = (int) sqrt(Config::GetDisplaySize() + 1);
for (unsigned int i = 1; i <= Config::GetDisplaySize(); ++i) {
unsigned int rowIndex = (i / rowLength);
unsigned int columnIndex = i % rowLength;
ui.gridLayout->addWidget(m_phenoTypeImageLabels[i - 1], rowIndex,
columnIndex);
}
}
QLabel* QtEvoPic::createLabel() {
QLabel* imageLabel = new QLabel(ui.gridLayoutWidget);
imageLabel->setAlignment(Qt::AlignCenter);
imageLabel->setScaledContents(true);
return imageLabel;
}
void QtEvoPic::updateLabels() {
int diff = Config::GetDisplaySize() - m_phenoTypeImageLabels.size();
if(diff > 0) {
for(int i = 0; i < diff; ++i) {
addLabel();
}
}
else if( diff < 0) {
for(int i = diff; i < 0; ++i) {
removeLabel();
}
}
}
void QtEvoPic::addLabel() {
QLabel* phenotypeImageLabel = createLabel();
m_phenoTypeImageLabels.append(phenotypeImageLabel);
}
void QtEvoPic::removeLabel() {
QLabel* label = m_phenoTypeImageLabels.back();
m_phenoTypeImageLabels.pop_back();
m_phenoTypeImages.pop_back();
ui.gridLayout->removeWidget(label);
delete label;
}
bool QtEvoPic::loadTargetImage() {
// load file
TargetImagePtr targetImage = TargetImage::Instance();
// QString filename = QFileDialog::getOpenFileName(this,
// tr("Open Image"), "", tr("Image Files (*.png *.jpg *.bmp)"));
// targetImage->loadFromFile(qPrintable(filename));
targetImage->loadFromFile(Config::GetTestImageName());
QImage& qTargetImage = dynamic_cast<QImage&>(targetImage->getImageImp());
m_targetImageLabel->setPixmap(QPixmap::fromImage(qTargetImage));
// the target image's size is set for the population as well
Config::SetWidth(qTargetImage.width());
Config::SetHeight(qTargetImage.height());
return true;
}
void QtEvoPic::resizeEvent(QResizeEvent* resizeEvent) {
int newWidth = resizeEvent->size().width();
// control widget stays the same height
int controlHeight = ui.layoutWidget->size().height();
ui.layoutWidget->resize(newWidth, controlHeight);
// rest of the space goes to the grid layout
int newGridHeight = resizeEvent->size().height();
newGridHeight -= controlHeight;
ui.gridLayoutWidget->resize(newWidth, newGridHeight);
}
void QtEvoPic::paintEvent(QPaintEvent*) {
mutex.lock();
if( (int) Config::GetDisplaySize() != m_phenoTypeImageLabels.size()) {
updateLayout();
}
for (int i = 0; i < m_phenoTypeImages.size(); ++i) {
QPixmap pixmap = QPixmap::fromImage(m_phenoTypeImages[i]);
m_phenoTypeImageLabels[i]->setPixmap(pixmap);
}
mutex.unlock();
}
void QtEvoPic::displayPhenotypeImage(unsigned int index,
PhenotypeImage& phenotypeImage) {
QImage& qImage = dynamic_cast<QImage&>(phenotypeImage.getImageImp());
mutex.lock();
if(m_phenoTypeImages.size() <= (int) index) {
m_phenoTypeImages.append(qImage);
} else {
m_phenoTypeImages[index] = qImage;
}
mutex.unlock();
}
void QtEvoPic::keyPressEvent(QKeyEvent* keyEvent) {
switch (keyEvent->key()) {
case Qt::Key_Escape:
QCoreApplication::exit();
break;
}
}