Converted to support highdpi devices

This commit is contained in:
Paolo Cignoni 2013-12-16 12:15:20 +00:00
parent eecaf72e1b
commit ad2cb4678a
2 changed files with 42 additions and 107 deletions

View File

@ -29,17 +29,16 @@ $Log: not supported by cvs2svn $
#include <GL/glew.h> #include <GL/glew.h>
#include <wrap/gl/space.h> #include <wrap/gl/space.h>
#include <wrap/gl/picking.h> #include <wrap/gl/picking.h>
#include <wrap/qt/device_to_logical.h>
#include "rubberband.h" #include "rubberband.h"
using namespace vcg; using namespace vcg;
Rubberband::Rubberband(Color4b c) Rubberband::Rubberband(Color4b c)
:color(c),currentphase(RUBBER_BEGIN),qt_cursor(), :color(c)
start(0,0,0),end(0,0,0),have_to_pick(false),font()
{ {
font.setFamily("Helvetica"); this->Reset();
font.setPixelSize(13);
} }
void Rubberband::Render(QGLWidget* gla) void Rubberband::Render(QGLWidget* gla)
@ -47,7 +46,7 @@ void Rubberband::Render(QGLWidget* gla)
if(have_to_pick){ if(have_to_pick){
assert(currentphase!=RUBBER_DRAGGED); assert(currentphase!=RUBBER_DRAGGED);
Point3f pick_point; Point3f pick_point;
bool picked = Pick(qt_cursor.x(), gla->height() - qt_cursor.y(), pick_point); bool picked = Pick(QTLogicalToDevice(gla, qt_cursor.x()), QTLogicalToDevice(gla, gla->height() - qt_cursor.y()), pick_point);
if(picked){ // we have not picked the background if(picked){ // we have not picked the background
have_to_pick=false; have_to_pick=false;
switch(currentphase){ switch(currentphase){
@ -72,7 +71,7 @@ void Rubberband::Render(QGLWidget* gla)
} }
if(currentphase==RUBBER_BEGIN) return; if(currentphase==RUBBER_BEGIN) return;
// Drawing of the current line // Drawing of the current line
glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LINE_BIT | GL_POINT_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT); glPushAttrib(GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LINE_BIT | GL_POINT_BIT | GL_CURRENT_BIT | GL_LIGHTING_BIT | GL_COLOR_BUFFER_BIT);
glDisable(GL_LIGHTING); glDisable(GL_LIGHTING);
@ -81,27 +80,26 @@ void Rubberband::Render(QGLWidget* gla)
glLineWidth(2.5); glLineWidth(2.5);
glPointSize(5.0); glPointSize(5.0);
if(currentphase==RUBBER_DRAGGING ) { if(currentphase==RUBBER_DRAGGING ) {
Point3f qt_start_point; Point2f qt_start_point = DevicePixelConvert(start);
qt_start_point = PixelConvert(start);
glColor(color); glColor(color);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glPushMatrix(); glPushMatrix();
glLoadIdentity(); glLoadIdentity();
gluOrtho2D(0,gla->width(),gla->height(),0); gluOrtho2D(0,QTLogicalToDevice(gla,gla->width()),QTLogicalToDevice(gla,gla->height()),0);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPushMatrix(); glPushMatrix();
glLoadIdentity(); glLoadIdentity();
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glBegin(GL_LINES); glBegin(GL_LINES);
glVertex2f(qt_start_point[0],qt_start_point[1]); glVertex(qt_start_point);
glVertex2f(qt_cursor.x(),qt_cursor.y()); glVertex2f(QTLogicalToDevice(gla, qt_cursor.x()), QTLogicalToDevice(gla, qt_cursor.y()));
glEnd(); glEnd();
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
glPopMatrix(); glPopMatrix();
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glPopMatrix(); glPopMatrix();
} else { } else {
assert(currentphase == RUBBER_DRAGGED); assert(currentphase == RUBBER_DRAGGED);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
@ -115,7 +113,7 @@ void Rubberband::Render(QGLWidget* gla)
glBegin(GL_POINTS); glBegin(GL_POINTS);
glVertex(start); glVertex(start);
glVertex(end); glVertex(end);
glEnd(); glEnd();
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glLineWidth(0.7); glLineWidth(0.7);
glPointSize(1.4); glPointSize(1.4);
@ -167,68 +165,7 @@ void Rubberband::GetPoints(Point3f &s,Point3f &e)
e=end; e=end;
} }
void Rubberband::RenderLabel(QString text,QGLWidget* gla) Point2f Rubberband::DevicePixelConvert(const Point3f p)
{
if(currentphase==RUBBER_BEGIN) return;
int x,y;
if(currentphase==RUBBER_DRAGGING){
x=qt_cursor.x()+16;
y=qt_cursor.y()+16;
} else {
Point3f qt_start = PixelConvert(start);
Point3f qt_end = PixelConvert(end);
if(qt_start[0]>qt_end[0]){
x=int(qt_start[0]+5);
y=int(qt_start[1]);
}else{
x=int(qt_end[0]+5);
y=int(qt_end[1]);
}
}
QFontMetrics fm(font);
QRect brec=fm.boundingRect(text);
glPushAttrib(GL_CURRENT_BIT | GL_DEPTH_BUFFER_BIT | GL_ENABLE_BIT | GL_LINE_BIT );
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0,gla->width(),gla->height(),0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glColor4f(0,0,0,0.5);
glBegin(GL_QUADS);
glVertex2f(x+brec.left(),y+brec.bottom());
glVertex2f(x+brec.right(),y+brec.bottom());
glVertex2f(x+brec.right(),y+brec.top());
glVertex2f(x+brec.left(),y+brec.top());
glEnd();
int offset=2;
glColor4f(0,0,0,0.2);
glBegin(GL_QUADS);
glVertex2f(x+brec.left()-offset,y+brec.bottom()+offset);
glVertex2f(x+brec.right()+offset,y+brec.bottom()+offset);
glVertex2f(x+brec.right()+offset,y+brec.top()-offset);
glVertex2f(x+brec.left()-offset,y+brec.top()-offset);
glEnd();
glColor3f(1,1,1);
gla->renderText(x,y,0.99f,text,font);
glGetError();//Due to buggy glrenderText()
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
}
Point3f Rubberband::PixelConvert(const Point3f p)
{ {
GLint vm[4]; GLint vm[4];
GLdouble mm[16]; GLdouble mm[16];
@ -238,5 +175,5 @@ Point3f Rubberband::PixelConvert(const Point3f p)
glGetDoublev(GL_PROJECTION_MATRIX, pm); glGetDoublev(GL_PROJECTION_MATRIX, pm);
GLdouble wx,wy,wz; GLdouble wx,wy,wz;
gluProject(p[0], p[1], p[2], mm, pm, vm, &wx, &wy, &wz); gluProject(p[0], p[1], p[2], mm, pm, vm, &wx, &wy, &wz);
return Point3f(wx,vm[3]-wy,wz); return Point2f(wx,vm[3]-wy);
} }

View File

@ -45,101 +45,99 @@ class Rubberband
{ {
public: public:
//data: //data:
/// The color of the rubberband /// The color of the rubberband
Color4b color; Color4b color;
// functions: // functions:
/*! /*!
@brief The constructor. @brief The constructor.
Initialize the rubberband data. Initialize the rubberband data.
*/ */
Rubberband(Color4b); Rubberband(Color4b);
/*! /*!
@brief The destructor. @brief The destructor.
The destructor. The destructor.
*/ */
virtual ~Rubberband() {} virtual ~Rubberband() {}
/*! /*!
@brief Render the rubberband and do the picking. @brief Render the rubberband and do the picking.
Is important that this function is called in order to apply the Drag and Pin commands. Is important that this function is called in order to apply the Drag and Pin commands.
@param glw the GL widget. @param glw the GL widget.
*/ */
void Render(QGLWidget* glw); void Render(QGLWidget* glw);
/*! /*!
@brief Set the current rubberband endpoint. @brief Set the current rubberband endpoint.
This function should be called after MouseMove events. This function should be called after MouseMove events.
@param cursor the cursor position. @param cursor the cursor position.
*/ */
void Drag(QPoint cursor); void Drag(QPoint cursor);
/*! /*!
@brief Ask for picking. @brief Ask for picking.
This function should be called after MouseRelease events. This function should be called after MouseRelease events.
The first time is called, if the picking is successful, sets the startpoint. The first time is called, if the picking is successful, sets the startpoint.
The second time sets, if the picking is successful, the endpoint. The second time sets, if the picking is successful, the endpoint.
After the second time this has no effect. After the second time this has no effect.
@param cursor the cursor position. @param cursor the cursor position.
*/ */
void Pin(QPoint cursor); void Pin(QPoint cursor);
/*! /*!
@brief Reset the rubberband. @brief Reset the rubberband.
*/ */
void Reset(); void Reset();
/*! /*!
@brief Return true if the rubberband has been drawn. @brief Return true if the rubberband has been drawn.
@return true if the line has been drawn, false otherwise. @return true if the line has been drawn, false otherwise.
*/ */
bool IsReady(); bool IsReady();
/*! /*!
@brief Get the rubberband start and end points. @brief Get the rubberband start and end points.
@param startpoint is set to the rubberband start point. @param startpoint is set to the rubberband start point.
@param endpoint is set to the rubberband end point. @param endpoint is set to the rubberband end point.
@warning Don't call this function until IsReady() returns true! @warning Don't call this function until IsReady() returns true!
*/ */
void GetPoints(Point3f &startpoint,Point3f &endpoint); void GetPoints(Point3f &startpoint,Point3f &endpoint);
/*! /*!
@brief Render a text label near the endpoint (if it exists). @brief Render a text label near the endpoint (if it exists).
@param text the text to render. @param text the text to render.
@param glw the GL widget. @param glw the GL widget.
*/ */
void RenderLabel(QString text,QGLWidget* glw);
private: private:
// types: // types:
typedef enum { RUBBER_BEGIN = 0, typedef enum { RUBBER_BEGIN = 0,
RUBBER_DRAGGING = 1, RUBBER_DRAGGING = 1,
RUBBER_DRAGGED = 2, RUBBER_DRAGGED = 2,
} RubberPhase; } RubberPhase;
// data: // data:
RubberPhase currentphase; RubberPhase currentphase;
QPoint qt_cursor; QPoint qt_cursor;
Point3f start, end; Point3f start, end;
bool have_to_pick; bool have_to_pick;
QFont font; // QFont font;
// functions: // functions:
Point3f PixelConvert(const Point3f); vcg::Point2f DevicePixelConvert(const Point3f);
}; };
}//namespace }//namespace