Repository navigation
Expand file tree
/
Copy pathpreviewscene.cpp
More file actions
314 lines (272 loc) · 11.8 KB
/
Copy pathpreviewscene.cpp
File metadata and controls
314 lines (272 loc) · 11.8 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
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
#include "previewscene.h"
#include "imagegen.h"
#include "mainwindow.h"
#include "interact.h"
#include <QDebug>
#include <QResizeEvent>
#include <QGraphicsEllipseItem>
#include <QGraphicsItem>
#include <QGradient>
#include <QGuiApplication>
#include <QScreen>
/** ****************************************************************************
* @brief PreviewScene::PreviewScene
* @param parent
*/
PreviewScene::PreviewScene(QObject *parent, MainWindow &mainWindowIn) :
QGraphicsScene(parent),
mainWindow(mainWindowIn)
{
}
/** ****************************************************************************
* @brief PreviewScene::mousePressEvent
* @param event
*/
void PreviewScene::mousePressEvent(QGraphicsSceneMouseEvent *event) {
mainWindow.interact.mousePressEvent(event, this);}
void PreviewScene::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
mainWindow.interact.mouseReleaseEvent(event, this);}
void PreviewScene::mouseMoveEvent(QGraphicsSceneMouseEvent *event) {
mainWindow.interact.mouseMoveEvent(event, this);}
/** ****************************************************************************
* @brief PreviewScene::EmittersToGraphItems creates a list of graphics items
* from the emitter arrangements in imgGen. It removes all existing graphics
* items from the scene, and adds these new ones
*/
void PreviewScene::EmittersToGraphItems(ImageGen & imgGen) {
// Get the vector of emitters
QVector<EmitterF> emitters;
if (imgGen.GetEmitterList(emitters)) {
return;
}
const double emDia = imgGen.s.emitterRadius * 2.0; // Simulation/scene coordinates
QPen pen(Qt::black);
pen.setWidthF(imgGen.s.emitterRadius * 0.3);
QBrush brush(Qt::white);
// Delete every existing graphics item from the group
for (QGraphicsItem * item : emItemGroup.childItems()) {
emItemGroup.removeFromGroup(item);
delete item;
}
if (!imgGen.EmittersHidden()) {
// For each emitter, create a graphics item and add to the group
// !@# upgrade to group together each arrangement
// Note that the actual ellipse position is a combination of the QGraphicsItem
// position and the QGraphicsEllipseItem rect position.
// If the item pos is (0,0) and the rect() is centered on (10,20), then
// the ellipse will be centered on (10,20).
QRectF emRect(0., 0., emDia, emDia);
emRect.moveCenter(QPointF(0,0)); // The rect() will always be centered around 0
for (EmitterF e : emitters) {
QGraphicsEllipseItem * item = new QGraphicsEllipseItem(emRect, &emItemGroup);
item->setPos(e.loc);
item->setPen(pen);
item->setBrush(brush);
}
}
// Re-add the group to the scene
this->removeItem(&emItemGroup);
this->addItem(&emItemGroup);
AddAxesLines();
invalidate(this->sceneRect(), QGraphicsScene::ItemLayer);
// The scene and background are automatically redrawn
}
/** ****************************************************************************
* @brief PreviewScene::AddAxesLines
* @param imageGen
*/
void PreviewScene::AddAxesLines() {
Interact & act = mainWindow.interact;
if (yAxisItem.isActive()) {
this->removeItem(&yAxisItem);
}
if (xAxisItem.isActive()) {
this->removeItem(&xAxisItem);
}
// Axes are drawn only when interacting with an arrangement that's mirrored
if (act.IsActive() && act.GetActiveArrangement()) {
EmArrangement* group = act.GetActiveArrangement();
if (group->mirrorHor) {
yAxisItem.setLine(QLineF(0, sceneRect().top(), 0, sceneRect().bottom()));
this->addItem(&yAxisItem);
}
if (group->mirrorVert) {
xAxisItem.setLine(QLineF(sceneRect().left(), 0, sceneRect().right(), 0));
this->addItem(&xAxisItem);
}
}
}
/** ****************************************************************************
* @brief PreviewView::PreviewView constructor
* @param parent
*/
PreviewView::PreviewView(QWidget *parent, MainWindow& mainWindowIn) : QGraphicsView(parent), mainWindow(mainWindowIn)
{
desiredSize = QSize(300,300);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
OnAspectRatioChange();
this->setContentsMargins(0,0,0,0);
}
/** ****************************************************************************
* @brief PreviewView::OnAspectRatioChange should be called when the aspect
* ratio changes
*/
void PreviewView::OnAspectRatioChange()
{
if (imageGen.s.view.aspectRatio < 1.) {
// Portrait orientation. Width determined from height
widthFromHeight = true;
this->setMinimumHeight(300);
this->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding);
desiredSize = QSize((int)((qreal)this->height() * imageGen.s.view.aspectRatio), this->height());
}
else {
// Landscape orientation. Height determined from width
widthFromHeight = false;
this->setMinimumWidth(300);
this->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
desiredSize = QSize(this->width(), int((qreal)this->width() / imageGen.s.view.aspectRatio));
}
this->updateGeometry();
}
/** ****************************************************************************
* @brief PreviewView::resizeEvent
* @param event
*/
void PreviewView::resizeEvent(QResizeEvent *event) {
if (widthFromHeight != (imageGen.s.view.aspectRatio < 1.)) {
OnAspectRatioChange();
}
// Enforce the aspect ratio
// Ensure that the viewable section of the screen stays the same
bool expanded;
if (widthFromHeight) {
qreal newWidth = (qreal)event->size().height() * imageGen.s.view.aspectRatio;
// Limit width to 60% of the screen width
newWidth = std::min(newWidth, 0.60 * (qreal)QGuiApplication::primaryScreen()->geometry().width());
expanded = newWidth > this->minimumWidth();
this->setMinimumWidth(newWidth);
// qDebug("resizeEvent. size=(%dx%d)px. newWidth=%.2f. viewWidth=%d. Expanded=%d",
// event->size().width(), event->size().height(), newWidth, this->width(), expanded);
desiredSize = QSize((int)newWidth, event->size().height());
}
else { // Height from width
qreal newHeight = (qreal)event->size().width() / imageGen.s.view.aspectRatio;
// Limit height to 85% of the screen height
newHeight = std::min(newHeight, 0.85 * (qreal)QGuiApplication::primaryScreen()->geometry().height());
expanded = newHeight > this->minimumHeight();
this->setMinimumHeight(newHeight);
// qDebug("resizeEvent. size=(%dx%d)px. newHeight=%.2f. height=%d. Expanded=%d",
// event->size().width(), event->size().height(), newHeight, this->height(), expanded);
desiredSize = QSize(event->size().width(), (int)newHeight);
}
if (expanded) {
this->fitInView(imageGen.areaSim, Qt::KeepAspectRatioByExpanding);
}else {
this->fitInView(imageGen.areaSim, Qt::KeepAspectRatio);
}
// Set the target image points to the new size
qint32 imgPixCount = this->size().width() * this->size().height();
qreal capMult = 1.0;
if (mainWindow.programMode == ProgramMode::fourBar) {
capMult = 3.0;
}
imageGen.setTargetImgPoints(std::min(imgPixCount, qint32(capMult * GenSettings::dfltImgPointsPreview)), imageGen.genPreview);
// Set the quick image size that the quick preview size is less
imageGen.setTargetImgPoints(std::min(imgPixCount, qint32(capMult * GenSettings::dfltImgPointsQuick)), imageGen.genQuick);
QGraphicsView::resizeEvent(event);
// Background redraw will be triggered
// !@# imageGen.NewImageNeeded();
// TODO reduce the gradual scaling that occurs when generating a new image here
}
/** ****************************************************************************
* @brief PreviewView::sizeHint
* @return
*/
QSize PreviewView::sizeHint() const
{
return desiredSize;
}
/** ****************************************************************************
* @brief PreviewView::OnPatternImageChange
*/
void PreviewView::OnPatternImageChange(QImage & image, qreal imgPerSimUnit, QColor backgroundClr)
{
scene()->invalidate(imageGen.areaSim, QGraphicsScene::BackgroundLayer);
// resetCachedContent(); // Delete previously cached background to force redraw
patternImage = ℑ
patternImgPerSimUnit = imgPerSimUnit;
backgroundColour = backgroundClr;
update(); // Redraws, but not immediately
}
/** ****************************************************************************
* @brief PreviewView::drawBackground
* @param painter
* @param rect
*/
void PreviewView::drawBackground(QPainter *painter, const QRectF &rect)
{
// All painting is done in scene (simulation) coordinates!
// rect is in scene coords
// This function is called to draw partial backgrounds and complete backgrounds.
// 'rect' indicates the background section to draw (in scene units)
// Image coordinates map to the scene / simulation coordinates with
// a factor of imgPerSimUnit. The image itself has dimensions that start at 0,0,
// whilst the scene can start at any point.
painter->fillRect(sceneRect(), backgroundColour);
if (0) { // Draw only the requested portion of the background
// This is somewhat unreliable (Dean R 2021-02-17)
QRectF imgSourceRect((rect.topLeft() - sceneRect().topLeft()) * patternImgPerSimUnit,
rect.size() * patternImgPerSimUnit);
painter->drawImage(rect, *patternImage, imgSourceRect);
// qDebug("drawBackground. rect=(%.2fx%.2f). @(%.2f, %.2f). imgSrcRect=(%.2fx%.2f). viewSz=(%dx%d)",
// rect.width(), rect.height(), rect.x(), rect.y(), imgSourceRect.width(), imgSourceRect.height(),
// this->width(), this->height());
}else { // Redraw the entire background (seems more resiliant with various types of resizing)
painter->drawImage(sceneRect(), *patternImage, patternImage->rect());
// qDebug("drawBackground. rect=(%.2fx%.2f). @(%.2f, %.2f). viewSz=(%dx%d), sceneRect=(%.1fx%.1f)",
// rect.width(), rect.height(), rect.x(), rect.y(),
// this->width(), this->height(),
// sceneRect().width(), sceneRect().height());
}
}
/** ****************************************************************************
* @brief PreviewScene::ListAllItems
*/
void PreviewScene::ListAllItems() {
QStringList strList;
strList.append(QString::asprintf("%d items in scene. Locs = ", items().size()));
for (auto& item : items()) {
QPointF scenePos = item->mapToScene(item->pos());
strList.append(QString::asprintf("(%.2f, %.2f), ", scenePos.x(), scenePos.y()));
}
qDebug() << strList.join("");
}
/** ****************************************************************************
* @brief PreviewScene::OnEmitterArngmtChange
*/
void PreviewScene::OnEmitterArngmtChange()
{
EmittersToGraphItems(imageGen);
}
/** ****************************************************************************
* @brief PreviewScene::OverlayTextSlot displays text on top of the scene
* @param text
*/
void PreviewScene::OverlayTextSlot(QString text)
{
if (text.isEmpty()) {
textOverlay.setVisible(false);
return;
}
textOverlay.setPlainText(text);
textOverlay.setVisible(true);
textOverlay.setTextWidth(sceneRect().width() * 0.9);
textOverlay.setPos(sceneRect().left() + sceneRect().width() * 0.05, -10);
QFont font = textOverlay.font();
font.setPointSize(7);
textOverlay.setFont(font);
textOverlay.setDefaultTextColor(Qt::gray);
this->addItem(&textOverlay);
}