20 : QGraphicsView(parent)
22 setOptimizationFlags(QGraphicsView::DontSavePainterState | QGraphicsView::DontAdjustForAntialiasing);
26 setViewportUpdateMode(QGraphicsView::FullViewportUpdate);
28 framesRenderedSinceProfileLogged = 0;
29 renderDurationSinceProfileLogged = 0;
32 viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
34 gestureDetected =
false;
37 ignorePinchesSmallerThanX = settings.value(
"canvas/ignorePinchesSmallerThanX", 0.25).toDouble();
38 ignorePinchesStartedLessThanXSecondsAfterDrag = settings.value(
"canvas/ignorePinchesStartedLessThanXSecondsAfterDrag", 0.5).toDouble();
39 ignorePinchesStartedMoreThanXSecondsAfterTouch = settings.value(
"canvas/ignorePinchesStartedMoreThanXSecondsAfterTouch", 0.25).toDouble();
50 return gestureDetected;
56 bool VuoEditorGraphicsView::event(QEvent * event)
58 if (event->type() == QEvent::KeyPress)
60 QKeyEvent *keyEvent = (QKeyEvent *)(event);
61 if (keyEvent->key() == Qt::Key_Return && (keyEvent->modifiers() & Qt::ControlModifier))
68 if (keyEvent->key() == Qt::Key_Escape)
69 qApp->sendEvent(parentWidget(), keyEvent);
72 else if (event->type() == QEvent::Resize)
75 return QGraphicsView::event(event);
81 void VuoEditorGraphicsView::paintEvent(QPaintEvent *event)
84 QGraphicsView::paintEvent(event);
89 ++framesRenderedSinceProfileLogged;
90 renderDurationSinceProfileLogged += renderDuration;
91 const double profileSeconds = 5;
92 if (t0 > lastProfileLoggedTime + profileSeconds)
94 VUserLog(
"%4d items, average draw time %6.4f s (%6.1f fps), %6.1f draws per second, %4d MB cached, bspDepth %2d",
95 scene()->items().count(),
96 renderDurationSinceProfileLogged/framesRenderedSinceProfileLogged,
97 framesRenderedSinceProfileLogged/renderDurationSinceProfileLogged,
98 framesRenderedSinceProfileLogged/profileSeconds,
99 QPixmapCache::totalUsed()/1024,
100 scene()->bspTreeDepth());
102 foreach (QGraphicsItem *i, scene()->items())
111 VUserLog(
"\t%p %s", i,
typeid(*i).name());
115 lastProfileLoggedTime = t0;
116 framesRenderedSinceProfileLogged = 0;
117 renderDurationSinceProfileLogged = 0;
125 bool VuoEditorGraphicsView::viewportEvent(QEvent *event)
127 QEvent::Type eventType =
event->type();
133 if (eventType == QEvent::MouseButtonPress)
135 auto mbp =
static_cast<QMouseEvent *
>(event);
136 if (mbp->button() == Qt::RightButton)
138 QGraphicsSceneContextMenuEvent contextMenuEvent(QEvent::GraphicsSceneContextMenu);
139 contextMenuEvent.setScreenPos(mbp->globalPos());
140 contextMenuEvent.setScenePos(mapToScene(mbp->pos()));
141 contextMenuEvent.setReason(QGraphicsSceneContextMenuEvent::Mouse);
142 QApplication::sendEvent(scene(), &contextMenuEvent);
149 if (eventType == QEvent::TouchBegin
150 || eventType == QEvent::TouchUpdate
151 || eventType == QEvent::TouchEnd)
153 QTouchEvent *touchEvent =
static_cast<QTouchEvent *
>(event);
154 const QList<QTouchEvent::TouchPoint> &touchPoints = touchEvent->touchPoints();
155 const QTouchEvent::TouchPoint &touchPoint0 = touchPoints.first();
156 const QTouchEvent::TouchPoint &touchPoint1 = touchPoints.last();
158 static double latestTouchBeganTime = 0;
159 if (touchPoint0.state() & Qt::TouchPointPressed
160 || touchPoint1.state() & Qt::TouchPointPressed)
161 latestTouchBeganTime = ((
double)touchEvent->timestamp()) / 1000.;
165 VDebugLog(
"ignoring pinch while scroll in progress");
170 if (secondsSinceLastDrag < ignorePinchesStartedLessThanXSecondsAfterDrag)
172 VDebugLog(
"ignoring pinch that occurred just %.2fs (less than %gs) after the latest drag", secondsSinceLastDrag, ignorePinchesStartedLessThanXSecondsAfterDrag);
178 VDebugLog(
"ignoring pinch while item drag in progress");
184 VDebugLog(
"ignoring pinch while cable drag in progress");
188 if (!rubberBandRect().isNull())
190 VDebugLog(
"ignoring pinch while rubberband in progress");
194 if (touchPoints.count() == 2)
196 double currentLength = QLineF(touchPoint0.pos(), touchPoint1.pos()).length();
198 static double detectedLength = 0;
199 if (!gestureDetected)
202 double startLength = QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length();
203 double scale = currentLength / startLength;
204 if (fabs(scale - 1) < ignorePinchesSmallerThanX)
206 VDebugLog(
"ignoring small pinch (%.2f < %g)", fabs(scale-1), ignorePinchesSmallerThanX);
212 double secondsSinceTouch = ((double)touchEvent->timestamp())/1000. - latestTouchBeganTime;
213 if (secondsSinceTouch > ignorePinchesStartedMoreThanXSecondsAfterTouch)
215 VDebugLog(
"ignoring pinch that started %.2fs (more than %gs) after touch", secondsSinceTouch, ignorePinchesStartedMoreThanXSecondsAfterTouch);
219 detectedLength = QLineF(touchPoint0.pos(), touchPoint1.pos()).length();
222 accumulatedScale = transform().m11();
224 setInteractive(
false);
225 gestureDetected =
true;
228 double scale = currentLength / detectedLength;
230 if (touchEvent->touchPointStates() & Qt::TouchPointReleased)
232 accumulatedScale *= scale;
234 setInteractive(
true);
235 gestureDetected =
false;
242 const double lagLimit = .1;
247 VDebugLog(
"pinch zoomed to scale %g", accumulatedScale * scale);
248 setTransform(QTransform::fromScale(accumulatedScale * scale, accumulatedScale * scale));
251 centerOn(mapToScene(rect().center()));
258 return QGraphicsView::viewportEvent(event);
264 void VuoEditorGraphicsView::updateColor(
bool isDark)