-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathView.cpp
544 lines (450 loc) · 15.4 KB
/
View.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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include "View.h"
#include "HelperFunctions.h"
#include <QtWidgets>
#include <QtMath>
GraphicsView::GraphicsView(View &pView, CoreLogic &pCoreLogic):
QGraphicsView(),
mView(pView),
mCoreLogic(pCoreLogic),
mIsLeftMousePressed(false),
mIsAddingWire(false),
mIsDragging(false)
{}
void GraphicsView::wheelEvent(QWheelEvent *pEvent)
{
// Disable zooming while moving a selection
if (!mView.Scene()->selectedItems().empty() && mIsLeftMousePressed)
{
return;
}
Q_ASSERT(pEvent);
if (pEvent->modifiers() & Qt::ControlModifier && !mCoreLogic.IsProcessing())
{
if (pEvent->angleDelta().y() > 0)
{
mView.ZoomIn(canvas::ZOOM_SPEED);
}
else
{
mView.ZoomOut(canvas::ZOOM_SPEED);
}
pEvent->accept();
}
}
void GraphicsView::mousePressEvent(QMouseEvent *pEvent)
{
// Disable canvas interaction while processing
if (mCoreLogic.IsProcessing())
{
return;
}
Q_ASSERT(pEvent);
if (pEvent->button() == Qt::LeftButton) // RMB ignored
{
if (mView.GetPieMenu()->isVisible())
{
mView.GetPieMenu()->HideIfNotPinned();
}
mIsLeftMousePressed = true;
if (pEvent->modifiers() & Qt::ControlModifier)
{
// Start panning
mPanStart = pEvent->pos();
pEvent->accept();
}
else if (pEvent->modifiers() & Qt::ShiftModifier)
{
if (!mCoreLogic.IsSimulationRunning())
{
// Add single components to selection via shift key
auto&& item = scene()->itemAt(mapToScene(pEvent->pos()), QTransform());
if (nullptr != item) {
item->setSelected(!item->isSelected());
}
// Hide clock configurator when another component is added to the selection
emit mCoreLogic.HideClockConfiguratorSignal();
}
}
else
{
mIsDragging = true;
if (mCoreLogic.GetControlMode() == ControlMode::WIRE)
{
mIsAddingWire = true;
}
emit LeftMouseButtonPressedWithoutCtrlEvent(mapToScene(pEvent->pos()), *pEvent);
}
}
}
void GraphicsView::OnMousePressedEventDefault(QMouseEvent &pEvent)
{
QGraphicsView::mousePressEvent(&pEvent);
}
void GraphicsView::mouseReleaseEvent(QMouseEvent *pEvent)
{
Q_ASSERT(pEvent);
mIsDragging = false;
if (mCoreLogic.IsProcessing())
{
return;
}
if (pEvent->button() == Qt::LeftButton)
{
mIsLeftMousePressed = false;
if (!mCoreLogic.IsSimulationRunning())
{
// Snap all potentially moved components to grid
for (auto& comp : scene()->selectedItems())
{
comp->setPos(SnapToGrid(comp->pos()));
}
}
// Add the new wires at the current position
if (mIsAddingWire)
{
mCoreLogic.AddWires(mapToScene(pEvent->pos()));
mIsAddingWire = false;
return;
}
if (pEvent->modifiers() & Qt::ControlModifier)
{
pEvent->accept();
return;
}
}
else if (pEvent->button() == Qt::RightButton)
{
if (!mCoreLogic.IsSimulationRunning())
{
mView.ShowPieMenu(mapFromGlobal(QCursor::pos()));
}
}
QGraphicsView::mouseReleaseEvent(pEvent);
if (pEvent->button() == Qt::LeftButton)
{
if (mCoreLogic.GetControlMode() == ControlMode::COPY)
{
mCoreLogic.FinishPaste();
mCoreLogic.EnterControlMode(ControlMode::EDIT);
}
}
//mView.ZoomIn(0); // To remove buggy line left by the selection rectangle
}
void GraphicsView::mouseMoveEvent(QMouseEvent *pEvent)
{
// Disable canvas interaction while processing
if (mCoreLogic.IsProcessing())
{
return;
}
Q_ASSERT(pEvent);
if (mIsLeftMousePressed)
{
if (mView.GetPieMenu()->isVisible())
{
mView.GetPieMenu()->update();
}
if (mIsAddingWire)
{
mCoreLogic.ShowPreviewWires(mapToScene(pEvent->pos()));
}
else if (pEvent->modifiers() & Qt::ControlModifier && !mIsDragging)
{
// Pan the scene
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (pEvent->position().x() - mPanStart.x()));
verticalScrollBar()->setValue(verticalScrollBar()->value() - (pEvent->position().y() - mPanStart.y()));
mPanStart = pEvent->pos();
pEvent->accept();
return;
}
else if (mCoreLogic.GetControlMode() == ControlMode::ADD && mView.Scene()->selectedItems().size() != 1)
{
/* Only allow dragging of exactly one component (selectedItems().size() == 1),
not selecting (selectedItems().size() == 0) or dragging multiple components */
return;
}
}
QGraphicsView::mouseMoveEvent(pEvent);
}
void GraphicsView::mouseDoubleClickEvent(QMouseEvent *pEvent)
{
Q_UNUSED(pEvent);
return; // Prevent interaction while not in edit mode
}
View::View(QtAwesome &pAwesome, CoreLogic &pCoreLogic):
mGraphicsView(*this, pCoreLogic),
mCoreLogic(pCoreLogic),
mAwesome(pAwesome)
{
mStandardPieMenuIconVariant.insert("color", QColor(0, 0, 0));
mDisabledPieMenuIconVariant.insert("color", QColor(180, 180, 180));
}
void View::Init()
{
setFrameStyle(QFrame::Plain | QFrame::NoFrame);
//mGraphicsView.setCacheMode(QGraphicsView::CacheBackground);
#warning assess if SmoothPixmapTransform is neccessary / impacts performance
mGraphicsView.setRenderHints(QPainter::SmoothPixmapTransform | QPainter::Antialiasing);
mGraphicsView.setDragMode(QGraphicsView::RubberBandDrag);
mGraphicsView.setOptimizationFlags(QGraphicsView::DontSavePainterState);
mGraphicsView.setViewportUpdateMode(QGraphicsView::SmartViewportUpdate);
mGraphicsView.setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
mGraphicsView.setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mGraphicsView.setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
mGraphicsView.setFrameStyle(QGraphicsView::NoFrame);
#warning evaluate if this fixes issue #31
// mGraphicsView.setMouseTracking(true);
mProcessingOverlay = new QWidget(&mGraphicsView);
mProcessingOverlay->setObjectName("mProcessingOverlay");
mProcessingOverlay->setStyleSheet("#mProcessingOverlay {background: rgba(0, 0, 0, 70);}");
mProcessingOverlay->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
mProcessingImage = new QLabel();
mProcessingImage->setPixmap(QPixmap(":/images/logo_processing.png").scaled(518, 200, Qt::KeepAspectRatio));
mProcessingLayout = new QGridLayout();
mProcessingLayout->addWidget(mProcessingImage, 0, 0, Qt::AlignHCenter | Qt::AlignVCenter);
mProcessingOverlay->setLayout(mProcessingLayout);
mProcessingOverlay->hide();
// Initialize pie menu
mPieMenu = new PieMenu(&mGraphicsView);
//mPieMenu->hide();
QObject::connect(mPieMenu, &PieMenu::ButtonClickedSignal, this, &View::OnPieMenuButtonClicked);
mMainLayout = new QGridLayout();
mMainLayout->setContentsMargins(0, 0, 0, 0);
mMainLayout->setSpacing(0);
mMainLayout->addWidget(&mGraphicsView, 1, 0);
mMainLayout->addWidget(mProcessingOverlay, 1, 0);
setLayout(mMainLayout);
mGraphicsView.stackUnder(mProcessingOverlay);
QObject::connect(&mGraphicsView, &GraphicsView::LeftMouseButtonPressedWithoutCtrlEvent, &mCoreLogic, &CoreLogic::OnLeftMouseButtonPressedWithoutCtrl);
QObject::connect(&mCoreLogic, &CoreLogic::MousePressedEventDefaultSignal, &mGraphicsView, &GraphicsView::OnMousePressedEventDefault);
QObject::connect(&mCoreLogic, &CoreLogic::SimulationStartSignal, this, &View::OnSimulationStart);
QObject::connect(&mCoreLogic, &CoreLogic::SimulationStopSignal, this, &View::OnSimulationStop);
SetupMatrix();
}
void View::SetScene(QGraphicsScene &pScene)
{
mScene = &pScene;
mGraphicsView.setScene(&pScene);
mGraphicsView.centerOn(0, 0);
QTimer::singleShot(0, &mGraphicsView, [&]()
{
mGraphicsView.setFocus();
});
}
QGraphicsScene* View::Scene(void) const
{
Q_ASSERT(mScene);
return mScene;
}
QList<QGraphicsItem*> View::Components(void) const
{
Q_ASSERT(mScene);
return mScene->items();
}
void View::OnSimulationStart()
{
mGraphicsView.setDragMode(QGraphicsView::NoDrag);
}
void View::OnSimulationStop()
{
mGraphicsView.setDragMode(QGraphicsView::RubberBandDrag);
}
void View::SetHideGrid(bool pValue)
{
mHideGrid = pValue;
SetupMatrix();
}
void View::SetupMatrix()
{
double scale = std::pow(2, (mZoomLevel - canvas::DEFAULT_ZOOM_LEVEL) / 50.0f);
QTransform matrix;
matrix.scale(scale, scale);
mGraphicsView.setTransform(matrix);
if (!mHideGrid)
{
mGraphicsView.setBackgroundBrush(DrawGridPattern(mZoomLevel));
}
else
{
QPixmap pixmap(canvas::GRID_SIZE, canvas::GRID_SIZE);
pixmap.fill(canvas::BACKGROUND_COLOR);
mGraphicsView.setBackgroundBrush(pixmap);
}
emit ZoomLevelChangedSignal(scale * 100, mZoomLevel);
}
void View::SetZoom(int32_t pZoomLevel)
{
Q_ASSERT(pZoomLevel >= canvas::MIN_ZOOM_LEVEL);
Q_ASSERT(pZoomLevel <= canvas::MAX_ZOOM_LEVEL);
if (!mCoreLogic.IsProcessing())
{
mZoomLevel = pZoomLevel;
SetupMatrix();
emit ZoomLevelChangedSignal(std::pow(2, (mZoomLevel - canvas::DEFAULT_ZOOM_LEVEL) / 50.0f) * 100, mZoomLevel);
}
}
int32_t View::GetZoomLevel() const
{
return mZoomLevel;
}
void View::UpdatePieMenuIcons()
{
mPieMenu->SetButtonIcon(0, mCoreLogic.GetControlMode() == ControlMode::WIRE ? QImage(":/images/icons/material_symbols/arrow_selector_tool_FILL1_wght400_GRAD0_opsz24.svg") :
QImage(":/images/icons/material_symbols/edit_FILL1_wght400_GRAD0_opsz24.svg"));
mPieMenu->SetButtonIcon(1, QImage(":/images/icons/material_symbols/redo_FILL0_wght400_GRAD0_opsz24.svg"));
mPieMenu->SetButtonIcon(2, QImage(":/images/icons/material_symbols/delete_FILL0_wght400_GRAD0_opsz24.svg"));
mPieMenu->SetButtonIcon(3, QImage(":/images/icons/material_symbols/undo_FILL0_wght400_GRAD0_opsz24.svg"));
mPieMenu->SetCloseButtonIcon(QIcon(":/images/icons/material_symbols/close_FILL1_wght400_GRAD0_opsz24.svg"));
mPieMenu->SetPinButtonIcon(QIcon(":/images/icons/pushpin-icon.png"));
}
void View::OnPieMenuButtonClicked(int8_t pButtonIndex)
{
switch (pButtonIndex)
{
case 0:
{
if (mCoreLogic.GetControlMode() == ControlMode::WIRE)
{
mCoreLogic.EnterControlMode(ControlMode::EDIT);
}
else
{
mCoreLogic.EnterControlMode(ControlMode::WIRE);
}
break;
}
case 1:
{
emit RedoFromPieMenuSignal();
break;
}
case 2:
{
if (mCoreLogic.GetControlMode() != ControlMode::COPY)
{
mCoreLogic.DeleteSelectedComponents();
}
else
{
mCoreLogic.AbortPastingIfInCopy();
}
break;
}
case 3:
{
emit UndoFromPieMenuSignal();
break;
}
default:
{
break;
}
}
}
void View::ShowPieMenu(const QPoint &pPos)
{
auto geometry = mPieMenu->geometry();
geometry.setTopLeft(pPos - QPoint(81, 81));
mPieMenu->setGeometry(geometry);
UpdatePieMenuIcons();
FadeInWidget(*mPieMenu, gui::PIE_MENU_ANIMATION_DURATION);
mPieMenu->setFocus();
}
PieMenu* View::GetPieMenu()
{
return mPieMenu;
}
void View::ResetViewport()
{
SetZoom(canvas::DEFAULT_ZOOM_LEVEL);
mGraphicsView.horizontalScrollBar()->setValue((mGraphicsView.horizontalScrollBar()->maximum() + mGraphicsView.horizontalScrollBar()->minimum()) / 2);
mGraphicsView.verticalScrollBar()->setValue((mGraphicsView.verticalScrollBar()->maximum() + mGraphicsView.verticalScrollBar()->minimum()) / 2);
}
void View::ZoomIn(int32_t pAmount)
{
mZoomLevel += pAmount;
mZoomLevel = std::min(mZoomLevel, canvas::MAX_ZOOM_LEVEL);
SetupMatrix();
emit ZoomLevelChangedSignal(std::pow(2, (mZoomLevel - canvas::DEFAULT_ZOOM_LEVEL) / 50.0f) * 100, mZoomLevel);
}
void View::ZoomOut(int32_t pAmount)
{
mZoomLevel -= pAmount;
mZoomLevel = std::max(mZoomLevel, canvas::MIN_ZOOM_LEVEL);
SetupMatrix();
emit ZoomLevelChangedSignal(std::pow(2, (mZoomLevel - canvas::DEFAULT_ZOOM_LEVEL) / 50.0f) * 100, mZoomLevel);
}
QPixmap View::DrawGridPattern(int32_t pZoomLevel)
{
QPixmap pixmap(canvas::GRID_SIZE, canvas::GRID_SIZE);
QPainter painter;
pixmap.fill(canvas::BACKGROUND_COLOR);
if (pZoomLevel < canvas::MIN_GRID_ZOOM_LEVEL)
{
return pixmap;
}
painter.begin(&pixmap);
if (pZoomLevel < canvas::MIN_GRID_ZOOM_LEVEL + 20)
{
QColor color(canvas::GRID_COLOR);
color.setAlpha(((pZoomLevel - canvas::MIN_GRID_ZOOM_LEVEL) * 255) / 20.0f);
painter.setPen(QPen(color, 2 / std::pow(2, (mZoomLevel - canvas::DEFAULT_ZOOM_LEVEL) / 50.0f)));
}
else
{
painter.setPen(QPen(canvas::GRID_COLOR, 2 / std::pow(2, (mZoomLevel - canvas::DEFAULT_ZOOM_LEVEL) / 50.0f)));
}
painter.drawLine(0, 0, canvas::GRID_SIZE - 1, 0);
painter.drawLine(0, 0, 0, canvas::GRID_SIZE - 1);
return pixmap;
}
void View::FadeInProcessingOverlay()
{
bool stoppedCurrentAnimation = false;
if (mProcessingOverlay->graphicsEffect() != nullptr)
{
delete mProcessingOverlay->graphicsEffect();
stoppedCurrentAnimation = true;
}
if (stoppedCurrentAnimation || !mProcessingOverlay->isVisible())
{
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect();
mProcessingOverlay->setGraphicsEffect(effect);
mProcessingOverlay->show();
QPropertyAnimation *anim = new QPropertyAnimation(effect, "opacity");
anim->setDuration(200);
anim->setStartValue(0.0f);
anim->setEndValue(1.0f);
anim->setEasingCurve(QEasingCurve::OutQuad);
QObject::connect(anim, &QPropertyAnimation::finished, this, [&]()
{
delete mProcessingOverlay->graphicsEffect();
});
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
}
void View::FadeOutProcessingOverlay()
{
bool stoppedCurrentAnimation = false;
if (mProcessingOverlay->graphicsEffect() != nullptr)
{
delete mProcessingOverlay->graphicsEffect();
stoppedCurrentAnimation = true;
}
if (stoppedCurrentAnimation || mProcessingOverlay->isVisible())
{
QGraphicsOpacityEffect *effect = new QGraphicsOpacityEffect();
mProcessingOverlay->setGraphicsEffect(effect);
QPropertyAnimation *anim = new QPropertyAnimation(effect, "opacity");
anim->setDuration(200);
anim->setStartValue(1.0f);
anim->setEndValue(0.0f);
anim->setEasingCurve(QEasingCurve::OutQuad);
QObject::connect(anim, &QPropertyAnimation::finished, this, [&]()
{
delete mProcessingOverlay->graphicsEffect();
mProcessingOverlay->hide();
});
anim->start(QAbstractAnimation::DeleteWhenStopped);
}
}