OSCAR-code/Graphs/glcommon.cpp

152 lines
4.4 KiB
C++
Raw Normal View History

2011-06-26 08:30:44 +00:00
/********************************************************************
glcommon GL code & font stuff
Copyright (c)2011 Mark Watkins <jedimark@users.sourceforge.net>
License: GPL
*********************************************************************/
#include <QApplication>
#include <QFontMetrics>
2011-07-01 10:10:44 +00:00
#include <QDebug>
2011-06-26 08:30:44 +00:00
#include <math.h>
#include "glcommon.h"
#include "SleepLib/profiles.h"
#include <QFile>
bool _graph_init=false;
QFont * defaultfont=NULL;
QFont * mediumfont=NULL;
QFont * bigfont=NULL;
GLshort *vertex_array[num_vert_arrays]={NULL};
2011-06-28 15:25:20 +00:00
bool evil_intel_graphics_chip=false;
2011-06-26 08:30:44 +00:00
// Must be called from a thread inside the application.
void InitGraphs()
2011-06-26 08:30:44 +00:00
{
if (!_graph_init) {
2011-06-26 08:30:44 +00:00
2011-06-27 06:26:29 +00:00
defaultfont=new QFont("FreeSans",10);
bigfont=new QFont("FreeSans",35);
mediumfont=new QFont("FreeSans",18);
for (int i=0;i<num_vert_arrays;i++) {
vertex_array[i]=new GLshort[maxverts];
}
_graph_init=true;
2011-06-26 08:30:44 +00:00
}
}
void DoneGraphs()
2011-06-26 08:30:44 +00:00
{
if (_graph_init) {
delete defaultfont;
2011-06-26 08:30:44 +00:00
delete bigfont;
delete mediumfont;
for (int i=0;i<num_vert_arrays;i++) {
delete [] vertex_array[i];
}
_graph_init=false;
2011-06-26 08:30:44 +00:00
}
}
void GetTextExtent(QString text, float & width, float & height, QFont *font)
{
QFontMetrics fm(*font);
//QRect r=fm.tightBoundingRect(text);
width=fm.width(text); //fm.width(text);
height=fm.xHeight()+2; //fm.ascent();
}
2011-06-27 10:23:24 +00:00
void DrawText(gGraphWindow & wid, QString text, int x, int y, float angle, QColor color,QFont *font)
2011-06-26 08:30:44 +00:00
{
//QFontMetrics fm(*font);
float w,h;
//GetTextExtent(text,w,h,font);
//int a=fm.overlinePos(); //ascent();
//LinedRoundedRectangle(x,wid.GetScrY()-y,w,h,0,1,QColor("black"));
if (!font) {
2011-07-01 10:10:44 +00:00
qDebug() << "Font Problem. Forgot to call GraphInit() ?";
2011-06-26 08:30:44 +00:00
abort();
return;
}
2011-06-27 10:23:24 +00:00
// glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glFlush();
2011-06-26 08:30:44 +00:00
QPainter painter(&wid);
2011-06-27 10:55:55 +00:00
painter.setFont(*font);
2011-07-01 10:10:44 +00:00
painter.setPen(color);
2011-06-27 10:23:24 +00:00
painter.setOpacity(1);
// painter.setCompositionMode(QPainter::CompositionMode_);
if (angle==0) {
painter.drawText(x,y,text);
} else {
GetTextExtent(text, w, h, font);
painter.translate(floor(x),floor(y));
painter.rotate(-90);
painter.drawText(floor(-w/2.0),floor(-h/2.0),text);
painter.translate(floor(-x),floor(-y));
2011-06-26 08:30:44 +00:00
2011-06-27 10:23:24 +00:00
}
2011-06-26 08:30:44 +00:00
painter.end();
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
}
void RoundedRectangle(int x,int y,int w,int h,int radius,const QColor color)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(color.red(),color.green(),color.blue(),color.alpha());
glBegin(GL_POLYGON);
glVertex2i(x+radius,y);
glVertex2i(x+w-radius,y);
for(float i=(float)M_PI*1.5f;i<M_PI*2;i+=0.1f)
glVertex2f(x+w-radius+cos(i)*radius,y+radius+sin(i)*radius);
glVertex2i(x+w,y+radius);
glVertex2i(x+w,y+h-radius);
for(float i=0;i<(float)M_PI*0.5f;i+=0.1f)
glVertex2f(x+w-radius+cos(i)*radius,y+h-radius+sin(i)*radius);
glVertex2i(x+w-radius,y+h);
glVertex2i(x+radius,y+h);
for(float i=(float)M_PI*0.5f;i<M_PI;i+=0.1f)
glVertex2f(x+radius+cos(i)*radius,y+h-radius+sin(i)*radius);
glVertex2i(x,y+h-radius);
glVertex2i(x,y+radius);
for(float i=(float)M_PI;i<M_PI*1.5f;i+=0.1f)
glVertex2f(x+radius+cos(i)*radius,y+radius+sin(i)*radius);
glEnd();
glDisable(GL_BLEND);
}
void LinedRoundedRectangle(int x,int y,int w,int h,int radius,int lw,QColor color)
{
glDisable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glColor4ub(color.red(),color.green(),color.blue(),color.alpha());
glLineWidth((GLfloat)lw);
glBegin(GL_LINE_STRIP);
for(float i=(float)M_PI;i<=1.5f*M_PI;i+=0.1f)
glVertex2f(radius*cos(i)+x+radius,radius*sin(i)+y+radius);
for(float i=1.5f*(float)M_PI;i<=2*M_PI; i+=0.1f)
glVertex2f(radius*cos(i)+x+w-radius,radius*sin(i)+y+radius);
for(float i=0;i<=0.5f*M_PI; i+=0.1f)
glVertex2f(radius*cos(i)+x+w-radius,radius*sin(i)+y+h-radius);
for(float i=0.5f*(float)M_PI;i<=M_PI;i+=0.1f)
glVertex2f(radius*cos(i)+x+radius,radius*sin(i)+y+h-radius);
glVertex2i(x,y+radius);
glEnd();
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
}