15 fmt.setDepthBufferSize(24);
17 fmt.setProfile(QSurfaceFormat::CoreProfile);
18 QSurfaceFormat::setDefaultFormat(fmt);
24const char* g_VertexSource =
26 "layout(location = 0) in vec2 position;\n"
27 "layout(location = 1) in vec2 texCoord;\n"
31 " gl_Position = vec4(position, 0.0, 1.0);\n"
32 " texc = vec4(texCoord, 0.0, 1.0);\n"
35const char* g_FragmentSource =
37 "uniform sampler2D texture;\n"
39 "out vec4 fragColor;\n"
42 " fragColor = texture2D(texture, texc.st);\n"
45CFrmViewerOpenGL::CFrmViewerOpenGL(QWidget *parent)
46 : QOpenGLWidget(parent), m_pTexture(nullptr)
48 setAttribute(Qt::WA_DeleteOnClose);
50 SetAdaptWindows(ZoomToWindow);
53 setMouseTracking(
true);
54 setFocusPolicy(Qt::WheelFocus);
58 m_VertexData = { -1.0, 1.0, 0.0, 0.0,
61 -1.0, -1.0, 0.0, 1.0 };
64CFrmViewerOpenGL::~CFrmViewerOpenGL()
66 qDebug() <<
"CFrmViewerOpenGL::~CFrmViewerOpenGL()";
74void CFrmViewerOpenGL::InitVertor()
76 m_ShaderProgram.bind();
79 if(!m_VaoQuad.isCreated())
84 if(!m_VboQuad.isCreated())
87 m_VboQuad.setUsagePattern(QOpenGLBuffer::StaticDraw);
90 m_VboQuad.allocate(m_VertexData.constData(),
91 m_VertexData.count() *
sizeof(GLfloat));
94 m_ShaderProgram.enableAttributeArray(0);
95 m_ShaderProgram.enableAttributeArray(1);
96 m_ShaderProgram.setAttributeBuffer(0, GL_FLOAT, 0, 2, 4 *
sizeof(GLfloat));
97 m_ShaderProgram.setAttributeBuffer(
98 1, GL_FLOAT, 2 *
sizeof(GLfloat), 2, 4 *
sizeof(GLfloat));
100 QString errorLog = m_ShaderProgram.log();
105 m_ShaderProgram.release();
107 GLenum error = glGetError();
111void CFrmViewerOpenGL::initializeGL()
113 qDebug() <<
"CFrmViewerOpenGL::initializeGL()";
114 initializeOpenGLFunctions();
115 glClearColor(0.0, 0.0, 0.0, 1.0);
118 bool success = m_ShaderProgram.addShaderFromSourceCode(QOpenGLShader::Vertex, g_VertexSource);
120 qDebug() <<
"failed!";
122 success = m_ShaderProgram.addShaderFromSourceCode(QOpenGLShader::Fragment, g_FragmentSource);
124 qDebug() <<
"failed!";
128 m_ShaderProgram.bindAttributeLocation(
"position", 0);
129 m_ShaderProgram.link();
133void CFrmViewerOpenGL::resizeGL(
int width,
int height)
136 qDebug() <<
"CFrmViewerOpenGL::resizeGL:" << width << height;
139void CFrmViewerOpenGL::paintGL()
143 if(m_Desktop.isNull() || !m_pTexture || !m_pTexture->isCreated())
return;
145 m_ShaderProgram.bind();
148 glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
149 m_pTexture->release();
151 m_ShaderProgram.release();
153 QString errorLog = m_ShaderProgram.log();
157 GLenum error = glGetError();
163 return m_dbZoomFactor;
166int CFrmViewerOpenGL::SetZoomFactor(
double newZoomFactor)
168 if(newZoomFactor < 0)
return -1;
169 if (qFuzzyCompare(m_dbZoomFactor, newZoomFactor))
171 m_dbZoomFactor = newZoomFactor;
175QSize CFrmViewerOpenGL::GetDesktopSize()
177 return m_Desktop.size();
180int CFrmViewerOpenGL::ReSize(
int width,
int height)
188void CFrmViewerOpenGL::SetAdaptWindows(ADAPT_WINDOWS aw)
191 if(!m_Desktop.isNull())
193 switch (m_AdaptWindows) {
198 ReSize(m_Desktop.width(), m_Desktop.height());
199 m_VertexData = { -1.0, 1.0, 0.0, 0.0,
202 -1.0, -1.0, 0.0, 1.0 };
207 m_VertexData = { -1.0, 1.0, 0.0, 0.0,
210 -1.0, -1.0, 0.0, 1.0 };
216 QRectF r = GetAspectRationRect();
217 m_VertexData = { -((float)r.width()/(float)width()), (
float)r.height()/(float)height(), 0.0, 0.0,
218 (float)r.width()/(float)width(), (float)r.height()/(float)height(), 1.0, 0.0,
219 (float)r.width()/(float)width(), -((float)r.height()/(float)height()), 1.0, 1.0,
220 -((
float)r.width()/(float)width()), -((
float)r.height()/(float)height()), 0.0, 1.0 };
234 return m_AdaptWindows;
237int CFrmViewerOpenGL::Load(QSettings &set)
239 SetZoomFactor(set.value(
"Viewer/ZoomFactor",
GetZoomFactor()).toDouble());
240 SetAdaptWindows(
static_cast<ADAPT_WINDOWS>(set.value(
"Viewer/AdaptType", GetAdaptWindows()).toInt()));
244int CFrmViewerOpenGL::Save(QSettings &set)
247 set.setValue(
"Viewer/AdaptType", GetAdaptWindows());
251QImage CFrmViewerOpenGL::GrabImage(
int x,
int y,
int w,
int h)
253 int width = w, height = h;
255 width = m_Desktop.width();
257 height = m_Desktop.height();
258 return m_Desktop.copy(x, y, width, height);
263 m_Desktop = QImage(width, height, QImage::Format_RGB32);
267 ||
Zoom == m_AdaptWindows)
268 ReSize(width, height);
275 this->setWindowTitle(szName);
280 if(m_Desktop.isNull() || m_Desktop.rect() == r)
286 m_pTexture =
nullptr;
288 m_pTexture =
new QOpenGLTexture(m_Desktop);
303 cursor().setPos(pos);
306void CFrmViewerOpenGL::slotSystemCombination()
309 emit sigKeyPressEvent(Qt::Key_Control, Qt::NoModifier);
310 emit sigKeyPressEvent(Qt::Key_Alt, Qt::NoModifier);
311 emit sigKeyPressEvent(Qt::Key_Delete, Qt::NoModifier);
312 emit sigKeyPressEvent(Qt::Key_Control, Qt::NoModifier);
313 emit sigKeyPressEvent(Qt::Key_Alt, Qt::NoModifier);
314 emit sigKeyPressEvent(Qt::Key_Delete, Qt::NoModifier);
317QRectF CFrmViewerOpenGL::GetAspectRationRect()
319 QRectF dstRect = rect();
320 qreal newW = dstRect.width();
321 qreal newH = dstRect.height();
325 qreal rateW =
static_cast<qreal
>(rect().width())
326 /
static_cast<qreal
>(m_Desktop.width());
327 qreal rateH =
static_cast<qreal
>(rect().height())
328 /
static_cast<qreal
>(m_Desktop.height());
331 newW = m_Desktop.width() * rateW;
332 newH = m_Desktop.height() * rateW;
333 newT = (
static_cast<qreal
>(rect().height()) - newH)
334 /
static_cast<qreal
>(2);
335 }
else if(rateW > rateH) {
336 newW = m_Desktop.width() * rateH;
337 newH = m_Desktop.height() * rateH;
338 newL = (
static_cast<qreal
>(rect().width()) - newW)
339 /
static_cast<qreal
>(2);
341 dstRect = QRectF(newL, newT, newW, newH);
345int CFrmViewerOpenGL::TranslationMousePoint(QPointF inPos, QPointF &outPos)
349 switch (m_AdaptWindows) {
360 outPos.setX(m_Desktop.width() * inPos.x() / width());
361 outPos.setY(m_Desktop.height() * inPos.y() / height());
365 QRectF r = GetAspectRationRect();
366 if(inPos.x() < r.left()
367 || inPos.x() > r.right()
368 || inPos.y() < r.top()
369 || inPos.y() > r.bottom())
371 outPos.setX(m_Desktop.width() * (inPos.x() - r.left()) / r.width());
372 outPos.setY(m_Desktop.height() * (inPos.y() - r.top()) / r.height());
382void CFrmViewerOpenGL::mousePressEvent(QMouseEvent *event)
384 QPointF pos =
event->pos();
385 if(TranslationMousePoint(event->pos(), pos))
return;
387 emit sigMousePressEvent(event->buttons(), QPoint(pos.x(), pos.y()));
391void CFrmViewerOpenGL::mouseReleaseEvent(QMouseEvent *event)
393 QPointF pos =
event->pos();
394 if(TranslationMousePoint(event->pos(), pos))
return;
396 emit sigMouseReleaseEvent(event->button(), QPoint(pos.x(), pos.y()));
400void CFrmViewerOpenGL::mouseMoveEvent(QMouseEvent *event)
402 QPointF pos =
event->pos();
403 if(TranslationMousePoint(event->pos(), pos))
return;
404 emit sigMouseMoveEvent(event->buttons(), QPoint(pos.x(), pos.y()));
405 emit sigMouseMoveEvent(event);
409void CFrmViewerOpenGL::wheelEvent(QWheelEvent *event)
411#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
412 QPointF pos =
event->position();
413 if(TranslationMousePoint(event->position(), pos))
return;
415 QPointF pos =
event->pos();
416 if(TranslationMousePoint(event->pos(), pos))
return;
418 emit sigWheelEvent(event->buttons(), QPoint(pos.x(), pos.y()), event->angleDelta());
422void CFrmViewerOpenGL::keyPressEvent(QKeyEvent *event)
425 emit sigKeyPressEvent(event->key(), event->modifiers());
429void CFrmViewerOpenGL::keyReleaseEvent(QKeyEvent *event)
432 emit sigKeyReleaseEvent(event->key(), event->modifiers());
void slotUpdateCursor(const QCursor &cursor)
Update cursor.
ADAPT_WINDOWS
The ADAPT_WINDOWS enum.
@ Zoom
zoom windows = desktop size * factor
@ OriginalCenter
Original desktop size, the center of the desktop is aligned with the center of the window.
@ KeepAspectRationToWindow
Keep desktop aspectration adapt to windows.
@ Original
Original desktop size, the left-top of the desktop is aligned with the left-top of the window.
@ ZoomToWindow
Desktop adapt to windows.
double GetZoomFactor() const
Adjust the zoom factor.
void slotSetDesktopSize(int width, int height)
Update desktop size.
void slotSetName(const QString &szName)
Update desktop name.
void slotUpdateRect(const QRect &r, const QImage &image)
Update image.
void slotUpdateCursorPosition(const QPoint &pos)
Update cursor position.