From dd963415de36f8e0e61e251d8ecf5c3342e5c0c1 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 8 Nov 2017 14:49:23 +0100 Subject: [PATCH] + found a solution using painter path to fix the text rending in image view --- src/Mod/Image/Gui/GLImageBox.cpp | 4 +- src/Mod/Image/Gui/ImageView.cpp | 4 +- src/Mod/Image/Gui/OpenGLImageBox.cpp | 88 ++++++++++++---------------- 3 files changed, 44 insertions(+), 52 deletions(-) diff --git a/src/Mod/Image/Gui/GLImageBox.cpp b/src/Mod/Image/Gui/GLImageBox.cpp index d7e00cc911..fc6d3c2544 100644 --- a/src/Mod/Image/Gui/GLImageBox.cpp +++ b/src/Mod/Image/Gui/GLImageBox.cpp @@ -84,7 +84,9 @@ GLImageBox::~GLImageBox() // Set up the OpenGL rendering state void GLImageBox::initializeGL() { - qglClearColor( Qt::black ); // Let OpenGL clear to black + QPalette p = this->palette(); + qglClearColor(p.color(this->backgroundRole())); // Let OpenGL clear to background color + //qglClearColor(Qt::black); // Let OpenGL clear to black static bool init = false; if (!init) { init = true; diff --git a/src/Mod/Image/Gui/ImageView.cpp b/src/Mod/Image/Gui/ImageView.cpp index aa9dfb1e2c..9c52551e36 100644 --- a/src/Mod/Image/Gui/ImageView.cpp +++ b/src/Mod/Image/Gui/ImageView.cpp @@ -61,7 +61,7 @@ ImageView::ImageView(QWidget* parent) // In order to fix this problem we directly pass the pointer of the parent // of this ImageView, i.e. the main window. // Note: - // Since Qt the class QGLWidget is marked as deprecated and should be + // Since Qt5 the class QGLWidget is marked as deprecated and should be // replaced by QOpenGLWidget. #if defined(HAVE_QT5_OPENGL) @@ -489,7 +489,7 @@ void ImageView::wheelEvent(QWheelEvent * cEvent) void ImageView::showEvent (QShowEvent *) { - //_pGLImageBox->setFocus(); + _pGLImageBox->setFocus(); } // Update the status bar with the image parameters for the current mouse position diff --git a/src/Mod/Image/Gui/OpenGLImageBox.cpp b/src/Mod/Image/Gui/OpenGLImageBox.cpp index 36c41cad15..8dd1e74886 100644 --- a/src/Mod/Image/Gui/OpenGLImageBox.cpp +++ b/src/Mod/Image/Gui/OpenGLImageBox.cpp @@ -139,7 +139,7 @@ void GLImageBox::initializeGL() connect(logger, &QOpenGLDebugLogger::messageLogged, this, &GLImageBox::handleLoggedMessage); if (logger->initialize()) - logger->startLogging(); + logger->startLogging(QOpenGLDebugLogger::SynchronousLogging); } #endif } @@ -858,70 +858,60 @@ unsigned int GLImageBox::pixValToMapIndex(double PixVal) } } +// https://learnopengl.com/?_escaped_fragment_=In-Practice/Text-Rendering#!In-Practice/Text-Rendering void GLImageBox::renderText(int x, int y, const QString& str, const QFont& fnt) { - // replacement for QGLWidget::renderText - // FIXME: The result looks very poor! - if (str.isEmpty() || !isValid()) return; - QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions(); + //glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS); + //glPushAttrib(GL_ALL_ATTRIB_BITS); - GLint view[4]; - bool use_scissor_testing = f->glIsEnabled(GL_SCISSOR_TEST); - //if (!use_scissor_testing) - f->glGetIntegerv(GL_VIEWPORT, &view[0]); - - - glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS); - glPushAttrib(GL_ALL_ATTRIB_BITS); - glMatrixMode(GL_TEXTURE); - glPushMatrix(); - glLoadIdentity(); - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - - glShadeModel(GL_FLAT); - glDisable(GL_CULL_FACE); - glDisable(GL_LIGHTING); - glDisable(GL_STENCIL_TEST); - glDisable(GL_DEPTH_TEST); +#if 0 glEnable(GL_BLEND); - glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); - - QRect viewport(view[0], view[1], view[2], view[3]); - if (!use_scissor_testing && viewport != rect()) { - // if the user hasn't set a scissor box, we set one that - // covers the current viewport - f->glScissor(view[0], view[1], view[2], view[3]); - f->glEnable(GL_SCISSOR_TEST); - } else if (use_scissor_testing) { - // use the scissor box set by the user - f->glEnable(GL_SCISSOR_TEST); - } + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GLfloat color[4]; - f->glGetFloatv(GL_CURRENT_COLOR, &color[0]); + glGetFloatv(GL_CURRENT_COLOR, &color[0]); QColor col; col.setRgbF(color[0], color[1], color[2],color[3]); - QPainter painter(this); + QFont font(fnt); + font.setStyleHint(QFont::Times, QFont::PreferAntialias); + + QPainter painter; + painter.begin(this); + painter.setRenderHint(QPainter::Antialiasing); + painter.setRenderHint(QPainter::TextAntialiasing); + + painter.setFont(font); painter.setPen(col); - painter.setFont(fnt); painter.drawText(x, y, str); painter.end(); +#else + GLfloat color[4]; + glGetFloatv(GL_CURRENT_COLOR, &color[0]); + QColor col; + col.setRgbF(color[0], color[1], color[2],color[3]); - glMatrixMode(GL_TEXTURE); - glPopMatrix(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - glPopMatrix(); - glPopAttrib(); - glPopClientAttrib(); + QFont font(fnt); + font.setStyleHint(QFont::Times, QFont::PreferAntialias); + + QPainterPath textPath; + textPath.addText(x, y, font, str); + + QPainter painter; + painter.begin(this); + painter.setRenderHint(QPainter::Antialiasing); + painter.setRenderHint(QPainter::TextAntialiasing); + + painter.setBrush(col); + painter.setPen(Qt::NoPen); + painter.drawPath(textPath); + painter.end(); +#endif + //glPopAttrib(); + //glPopClientAttrib(); } #include "moc_OpenGLImageBox.cpp"