2020-06-04 03:39:16 +00:00
|
|
|
#include "scantargetwidget.h"
|
|
|
|
|
2020-07-03 01:53:30 +00:00
|
|
|
#include <QApplication>
|
2020-07-04 04:29:37 +00:00
|
|
|
#include <QCursor>
|
2020-06-05 02:39:32 +00:00
|
|
|
#include <QDebug>
|
2020-07-03 01:53:30 +00:00
|
|
|
#include <QDesktopWidget>
|
|
|
|
#include <QGuiApplication>
|
2020-07-04 04:29:37 +00:00
|
|
|
#include <QKeyEvent>
|
|
|
|
#include <QMouseEvent>
|
2020-06-05 02:39:32 +00:00
|
|
|
#include <QOpenGLContext>
|
2020-07-01 03:03:39 +00:00
|
|
|
#include <QScreen>
|
2020-06-05 02:58:02 +00:00
|
|
|
#include <QTimer>
|
2020-06-05 02:39:32 +00:00
|
|
|
|
2020-06-06 23:47:35 +00:00
|
|
|
#include "../../ClockReceiver/TimeTypes.hpp"
|
|
|
|
|
2020-06-04 03:39:16 +00:00
|
|
|
ScanTargetWidget::ScanTargetWidget(QWidget *parent) : QOpenGLWidget(parent) {}
|
|
|
|
ScanTargetWidget::~ScanTargetWidget() {}
|
|
|
|
|
|
|
|
void ScanTargetWidget::initializeGL() {
|
2020-06-23 02:36:36 +00:00
|
|
|
setDefaultClearColour();
|
2020-06-05 02:39:32 +00:00
|
|
|
|
2020-06-06 23:47:35 +00:00
|
|
|
// Follow each swapped frame with an additional update.
|
2020-06-14 23:26:56 +00:00
|
|
|
connect(this, &QOpenGLWidget::frameSwapped, this, &ScanTargetWidget::vsync);
|
2020-06-04 03:39:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTargetWidget::paintGL() {
|
2020-07-04 04:29:37 +00:00
|
|
|
if(requestedRedrawTime) {
|
2020-07-01 03:03:39 +00:00
|
|
|
const auto now = Time::nanos_now();
|
2020-07-04 04:29:37 +00:00
|
|
|
vsyncPredictor.add_timer_jitter(now - requestedRedrawTime);
|
|
|
|
requestedRedrawTime = 0;
|
2020-06-26 03:59:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 01:53:30 +00:00
|
|
|
// TODO: if Qt 5.14 can be guaranteed, just use window()->screen().
|
|
|
|
const auto screenNumber = QApplication::desktop()->screenNumber(this);
|
|
|
|
QScreen *const screen = QGuiApplication::screens()[screenNumber];
|
|
|
|
|
|
|
|
const float newOutputScale = float(screen->devicePixelRatio());
|
2020-07-01 03:03:39 +00:00
|
|
|
if(outputScale != newOutputScale) {
|
|
|
|
outputScale = newOutputScale;
|
|
|
|
resize();
|
|
|
|
}
|
2020-07-03 01:53:30 +00:00
|
|
|
vsyncPredictor.set_frame_rate(float(screen->refreshRate()));
|
2020-07-01 03:03:39 +00:00
|
|
|
|
2020-06-04 03:39:16 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2020-06-21 22:25:38 +00:00
|
|
|
|
|
|
|
// Gmynastics ahoy: if a producer has been specified or previously connected then:
|
|
|
|
//
|
|
|
|
// (i) if it's a new producer, generate a new scan target and pass it on;
|
|
|
|
// (ii) in any case, check whether the underlyiung framebuffer has changed; and
|
|
|
|
// (iii) draw.
|
|
|
|
//
|
|
|
|
// The slightly convoluted scan target forwarding arrangement works around an issue
|
|
|
|
// with QOpenGLWidget under macOS, which I did not fully diagnose, in which creating
|
|
|
|
// a scan target in ::initializeGL did not work (and no other arrangement really works
|
|
|
|
// with regard to starting up).
|
|
|
|
if(isConnected || producer) {
|
|
|
|
if(producer) {
|
|
|
|
isConnected = true;
|
|
|
|
framebuffer = defaultFramebufferObject();
|
|
|
|
scanTarget = std::make_unique<Outputs::Display::OpenGL::ScanTarget>(framebuffer);
|
|
|
|
producer->set_scan_target(scanTarget.get());
|
|
|
|
producer = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-21 21:25:21 +00:00
|
|
|
// Qt reserves the right to change the framebuffer object due to window resizes or if setParent is called;
|
|
|
|
// therefore check whether it has changed.
|
|
|
|
const auto newFramebuffer = defaultFramebufferObject();
|
|
|
|
if(framebuffer != newFramebuffer) {
|
|
|
|
framebuffer = newFramebuffer;
|
|
|
|
scanTarget->set_target_framebuffer(framebuffer);
|
|
|
|
}
|
|
|
|
|
2020-06-21 21:16:11 +00:00
|
|
|
vsyncPredictor.begin_redraw();
|
2020-07-01 03:03:39 +00:00
|
|
|
scanTarget->update(scaledWidth, scaledHeight);
|
|
|
|
scanTarget->draw(scaledWidth, scaledHeight);
|
2020-06-24 02:59:12 +00:00
|
|
|
glFinish(); // Make sure all costs are properly accounted for in the vsync predictor.
|
2020-06-21 21:16:11 +00:00
|
|
|
vsyncPredictor.end_redraw();
|
2020-06-05 02:39:32 +00:00
|
|
|
}
|
2020-06-04 03:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-14 23:26:56 +00:00
|
|
|
void ScanTargetWidget::vsync() {
|
2020-06-23 02:36:36 +00:00
|
|
|
if(!isConnected) return;
|
|
|
|
|
2020-06-21 21:16:11 +00:00
|
|
|
vsyncPredictor.announce_vsync();
|
2020-06-14 23:26:56 +00:00
|
|
|
|
|
|
|
const auto time_now = Time::nanos_now();
|
2020-07-04 04:29:37 +00:00
|
|
|
requestedRedrawTime = vsyncPredictor.suggested_draw_time();
|
|
|
|
const auto delay_time = (requestedRedrawTime - time_now) / 1'000'000;
|
2020-07-28 01:08:00 +00:00
|
|
|
if(delay_time > 0 && delay_time < vsyncPredictor.frame_duration()) {
|
2020-06-14 23:26:56 +00:00
|
|
|
QTimer::singleShot(delay_time, this, SLOT(repaint()));
|
|
|
|
} else {
|
2020-07-04 04:29:37 +00:00
|
|
|
requestedRedrawTime = 0;
|
2020-06-14 23:26:56 +00:00
|
|
|
repaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-04 03:39:16 +00:00
|
|
|
void ScanTargetWidget::resizeGL(int w, int h) {
|
2020-07-04 04:29:37 +00:00
|
|
|
if(rawWidth != w || rawHeight != h) {
|
|
|
|
rawWidth = w;
|
|
|
|
rawHeight = h;
|
2020-07-01 03:03:39 +00:00
|
|
|
resize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTargetWidget::resize() {
|
2020-07-04 04:29:37 +00:00
|
|
|
const int newScaledWidth = int(float(rawWidth) * outputScale);
|
|
|
|
const int newScaledHeight = int(float(rawHeight) * outputScale);
|
2020-07-01 03:03:39 +00:00
|
|
|
|
|
|
|
if(newScaledWidth != scaledWidth || newScaledHeight != scaledHeight) {
|
|
|
|
scaledWidth = newScaledWidth;
|
|
|
|
scaledHeight = newScaledHeight;
|
|
|
|
glViewport(0, 0, scaledWidth, scaledHeight);
|
|
|
|
}
|
2020-06-04 03:39:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-21 22:25:38 +00:00
|
|
|
void ScanTargetWidget::setScanProducer(MachineTypes::ScanProducer *producer) {
|
|
|
|
this->producer = producer;
|
|
|
|
repaint();
|
2020-06-04 03:39:16 +00:00
|
|
|
}
|
2020-06-23 00:32:44 +00:00
|
|
|
|
|
|
|
void ScanTargetWidget::stop() {
|
|
|
|
makeCurrent();
|
|
|
|
scanTarget.reset();
|
|
|
|
isConnected = false;
|
2020-06-23 02:36:36 +00:00
|
|
|
setDefaultClearColour();
|
|
|
|
vsyncPredictor.pause();
|
2020-07-04 04:29:37 +00:00
|
|
|
requestedRedrawTime = 0;
|
2020-07-03 01:53:30 +00:00
|
|
|
repaint();
|
2020-06-23 02:36:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTargetWidget::setDefaultClearColour() {
|
|
|
|
// Retain the default background colour.
|
|
|
|
const QColor backgroundColour = palette().color(QWidget::backgroundRole());
|
|
|
|
glClearColor(backgroundColour.redF(), backgroundColour.greenF(), backgroundColour.blueF(), 1.0);
|
2020-06-23 00:32:44 +00:00
|
|
|
}
|
2020-07-04 04:29:37 +00:00
|
|
|
|
|
|
|
void ScanTargetWidget::setMouseDelegate(MouseDelegate *delegate) {
|
|
|
|
if(!delegate && mouseIsCaptured) {
|
|
|
|
releaseMouse();
|
|
|
|
}
|
|
|
|
mouseDelegate = delegate;
|
|
|
|
setMouseTracking(delegate);
|
|
|
|
}
|
|
|
|
|
2020-10-03 00:31:47 +00:00
|
|
|
void ScanTargetWidget::keyReleaseEvent(QKeyEvent *event) {
|
|
|
|
// Releasing F8 or F12 needs to be tracked but doesn't actively do anything,
|
|
|
|
// so I'm counting that as a Qt ignore.
|
|
|
|
if(event->key() == Qt::Key_F8) f8State = false;
|
|
|
|
if(event->key() == Qt::Key_F12) f12State = false;
|
|
|
|
event->ignore();
|
|
|
|
}
|
|
|
|
|
2020-07-04 04:29:37 +00:00
|
|
|
void ScanTargetWidget::keyPressEvent(QKeyEvent *event) {
|
2020-10-03 00:31:47 +00:00
|
|
|
// Use either CTRL+Escape or F8+F12 to end mouse captured mode, if currently captured;
|
|
|
|
// otherwise ignore the event.
|
|
|
|
|
|
|
|
if(event->key() == Qt::Key_F8) f8State = true;
|
|
|
|
if(event->key() == Qt::Key_F12) f12State = true;
|
|
|
|
|
|
|
|
if(mouseIsCaptured && (
|
|
|
|
(event->key() == Qt::Key_Escape && event->modifiers()&Qt::ControlModifier) ||
|
|
|
|
(f8State && f12State)
|
|
|
|
)) {
|
2020-07-04 04:29:37 +00:00
|
|
|
releaseMouse();
|
|
|
|
|
|
|
|
QCursor cursor;
|
|
|
|
cursor.setShape(Qt::ArrowCursor);
|
|
|
|
setCursor(cursor);
|
2020-07-04 23:12:34 +00:00
|
|
|
return;
|
2020-07-04 04:29:37 +00:00
|
|
|
}
|
2020-07-04 23:12:34 +00:00
|
|
|
|
|
|
|
event->ignore();
|
2020-07-04 04:29:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTargetWidget::releaseMouse() {
|
|
|
|
QOpenGLWidget::releaseMouse();
|
|
|
|
mouseIsCaptured = false;
|
|
|
|
mouseDelegate->setMouseIsCaptured(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTargetWidget::mousePressEvent(QMouseEvent *event) {
|
|
|
|
if(mouseDelegate) {
|
|
|
|
if(!mouseIsCaptured) {
|
|
|
|
mouseIsCaptured = true;
|
|
|
|
grabMouse();
|
|
|
|
|
|
|
|
QCursor cursor;
|
|
|
|
cursor.setPos(mapToGlobal(QPoint(width() / 2, height() / 2)));
|
|
|
|
cursor.setShape(Qt::BlankCursor);
|
|
|
|
setCursor(cursor);
|
|
|
|
|
|
|
|
mouseDelegate->setMouseIsCaptured(true);
|
|
|
|
} else {
|
|
|
|
setMouseButtonPressed(event->button(), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTargetWidget::mouseReleaseEvent(QMouseEvent *event) {
|
|
|
|
if(mouseDelegate && !mouseIsCaptured) {
|
|
|
|
setMouseButtonPressed(event->button(), false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTargetWidget::setMouseButtonPressed(Qt::MouseButton button, bool isPressed) {
|
|
|
|
switch(button) {
|
|
|
|
default: break;
|
|
|
|
case Qt::LeftButton: mouseDelegate->setButtonPressed(0, isPressed); break;
|
|
|
|
case Qt::RightButton: mouseDelegate->setButtonPressed(1, isPressed); break;
|
|
|
|
case Qt::MiddleButton: mouseDelegate->setButtonPressed(2, isPressed); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ScanTargetWidget::mouseMoveEvent(QMouseEvent *event) {
|
|
|
|
// Recentre the mouse cursor upon every move if it is currently captured.
|
|
|
|
if(mouseDelegate && mouseIsCaptured) {
|
|
|
|
const QPoint centre = QPoint(width() / 2, height() / 2);
|
|
|
|
const QPoint vector = event->pos() - centre;
|
|
|
|
|
|
|
|
mouseDelegate->moveMouse(vector);
|
|
|
|
|
|
|
|
QCursor::setPos(mapToGlobal(centre));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ScanTargetWidget::isMouseCaptured() {
|
|
|
|
return mouseIsCaptured;
|
|
|
|
}
|