This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
/
generationwidget.cpp
162 lines (137 loc) · 4.41 KB
/
generationwidget.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
// Copyright (c) 2012 Oliver Lau <oliver@von-und-fuer-lau.de>
// All rights reserved.
#include <QPainter>
#include <QUrl>
#include <QMimeData>
#include <QtCore/QDebug>
#include <qmath.h>
#include "generationwidget.h"
#include "breedersettings.h"
GenerationWidget::GenerationWidget(QWidget* parent)
: QWidget(parent)
, mWindowAspectRatio(0)
, mImageAspectRatio(0)
{
QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
sizePolicy.setHeightForWidth(true);
setSizePolicy(sizePolicy);
setAcceptDrops(true);
setMouseTracking(true);
}
void GenerationWidget::setImage(const QImage& image)
{
mImage = image;
mImageAspectRatio = (qreal) mImage.width() / mImage.height();
update();
}
void GenerationWidget::setDNA(const DNA& dna)
{
mDNA = dna;
update();
}
void GenerationWidget::setSplices(const Gene& gene, const QVector<Gene>& offsprings)
{
mSplicedGene = gene;
mSplices = offsprings;
update();
}
bool GenerationWidget::event(QEvent* e)
{
switch (e->type()) {
case QEvent::Leave:
mHighlighted = QPolygonF();
update();
break;
default:
break;
}
return QWidget::event(e);
}
void GenerationWidget::resizeEvent(QResizeEvent* e)
{
mWindowAspectRatio = (qreal) e->size().width() / e->size().height();
}
void GenerationWidget::paintEvent(QPaintEvent*)
{
QPainter p(this);
p.fillRect(rect(), Qt::black);
if (mImage.isNull() || qFuzzyIsNull(mImageAspectRatio) || qFuzzyIsNull(mImageAspectRatio))
return;
if (mWindowAspectRatio < mImageAspectRatio) {
const int h = int(width() / mImageAspectRatio);
mDestRect = QRect(0, (height()-h)/2, width(), h);
}
else {
const int w = int(height() * mImageAspectRatio);
mDestRect = QRect((width()-w)/2, 0, w, height());
}
p.drawImage(mDestRect, mImage);
const qreal invScale = 1 / qSqrt(mDestRect.width() * mDestRect.height());
p.translate(mDestRect.x(), mDestRect.y());
p.scale(mDestRect.width(), mDestRect.height());
p.setBrush(Qt::transparent);
p.setRenderHint(QPainter::Antialiasing);
if (mSplicedGene.color().isValid() && !mSplices.empty()) {
p.setPen(QPen(Qt::green, 1.2 * invScale));
for (QVector<Gene>::const_iterator g = mSplices.constBegin(); g != mSplices.constEnd(); ++g)
p.drawPolygon(g->polygon());
p.setPen(QPen(Qt::red, 1.3 * invScale));
p.drawPolygon(mSplicedGene.polygon());
mSplicedGene = Gene();
mSplices.clear();
}
if (!mHighlighted.isEmpty()) {
p.setPen(QPen(QColor(255, 0, 200), 1.3 * invScale));
p.setBrush(Qt::transparent);
p.drawPolygon(mHighlighted);
}
}
void GenerationWidget::mousePressEvent(QMouseEvent* e)
{
const QPoint& clickPos = e->pos() - mDestRect.topLeft();
emit clickAt(QPointF((qreal)clickPos.x() / mDestRect.width(), (qreal)clickPos.y() / mDestRect.height()));
}
void GenerationWidget::mouseMoveEvent(QMouseEvent* e)
{
const QPoint& clickPos = e->pos() - mDestRect.topLeft();
mHighlighted = mDNA.findPolygonForPoint(QPointF((qreal)clickPos.x() / mDestRect.width(), (qreal)clickPos.y() / mDestRect.height()));
update();
}
void GenerationWidget::dragEnterEvent(QDragEnterEvent* e)
{
const QMimeData* d = e->mimeData();
if (d->hasUrls()) {
if (d->urls().first().toString().contains(QRegExp("\\.(svg|json)$"))) {
e->acceptProposedAction();
setStyleSheet("background-color: #22dd22;");
}
else {
e->ignore();
}
}
else {
e->ignore();
}
}
void GenerationWidget::dragLeaveEvent(QDragLeaveEvent* e)
{
setStyleSheet("background-color: #222222;");
e->accept();
}
void GenerationWidget::dropEvent(QDropEvent* e)
{
const QMimeData* d = e->mimeData();
if (d->hasUrls()) {
QString fileUrl = d->urls().first().toString();
if (fileUrl.contains(QRegExp("file://.*\\.(svg|json)$"))) {
#ifdef WIN32
const QString fileName = fileUrl.remove("file:///");
#else
const QString fileName = fileUrl.remove("file://");
#endif
emit fileDropped(fileName);
gSettings.setCurrentDNAFile(fileName);
}
}
setStyleSheet("background-color: #222222;");
}