OSCAR-code/Graphs/gXAxis.cpp

324 lines
8.8 KiB
C++
Raw Normal View History

/*
2011-06-26 08:30:44 +00:00
gXAxis Implementation
Copyright (c)2011 Mark Watkins <jedimark@users.sourceforge.net>
License: GPL
*/
2011-06-26 08:30:44 +00:00
#include <math.h>
#include <QDebug>
2011-06-26 08:30:44 +00:00
#include "gXAxis.h"
gXAxis::gXAxis(QColor col)
:gLayer(EmptyChannel)
2011-06-26 08:30:44 +00:00
{
m_line_color=col;
m_text_color=col;
m_major_color=Qt::darkGray;
m_minor_color=Qt::lightGray;
2011-06-26 08:30:44 +00:00
m_show_major_lines=false;
m_show_minor_lines=false;
m_show_minor_ticks=true;
m_show_major_ticks=true;
QDateTime d=QDateTime::currentDateTime();
QTime t1=d.time();
QTime t2=d.toUTC().time();
tz_offset=t2.secsTo(t1)/60L;
tz_offset*=60000L;
//offset=0;
2011-06-26 08:30:44 +00:00
}
gXAxis::~gXAxis()
{
}
void gXAxis::Plot(gGraphWindow & w,float scrx,float scry)
{
float px,py;
2011-08-05 15:01:17 +00:00
int start_px=w.GetLeftMargin();
int start_py=w.GetBottomMargin();
int width=scrx-(w.GetLeftMargin()+w.GetRightMargin());
2011-06-26 08:30:44 +00:00
// float height=scry-(w.GetTopMargin()+w.GetBottomMargin());
2011-08-05 15:01:17 +00:00
if (width<40)
return;
2011-07-27 09:21:53 +00:00
qint64 minx;
qint64 maxx;
2011-06-26 08:30:44 +00:00
if (w.BlockZoom()) {
minx=w.rmin_x;
maxx=w.rmax_x;
} else {
minx=w.min_x;
maxx=w.max_x;
}
2011-08-05 15:01:17 +00:00
qint64 xx=maxx-minx;
2011-06-26 08:30:44 +00:00
if (xx<=0) return;
2011-08-05 15:01:17 +00:00
qint64 rxx=w.rmax_x-w.rmin_x;
2011-06-26 08:30:44 +00:00
2011-08-05 15:01:17 +00:00
QString fd,fmt;
int divisors[]={86400000,3600000,2700000,1800000,1200000,900000,600000,300000,120000,60000,45000,30000,20000,15000,10000,5000,2000,1000,100,50};
int divcnt=sizeof(divisors)/sizeof(int);
int divmax,dividx;
int fitmode;
if (xx>86400000L) { // Day
fd="00 MMM";
fmt="dd MMM";
dividx=0;
divmax=1;
fitmode=0;
} else if (xx>60000) { // Minutes
fd="00:00";
dividx=1;
divmax=10;
fitmode=1;
} else if (xx>5000) { // Seconds
fd="00:00:00";
dividx=9;
divmax=16;
fitmode=2;
} else { // Microseconds
fd="00:00:00:000";
dividx=15;
divmax=divcnt;
fitmode=3;
2011-06-26 08:30:44 +00:00
}
2011-08-05 15:01:17 +00:00
2011-06-26 08:30:44 +00:00
float x,y;
GetTextExtent(fd,x,y);
2011-08-05 15:01:17 +00:00
if (x<=0) { // font size bug
qWarning() << "gXAxis::Plot() x<=0";
return;
}
2011-06-26 08:30:44 +00:00
int max_ticks=width/(x+10); // Max number of ticks that will fit
2011-08-05 15:01:17 +00:00
int fit_ticks=0;
2011-08-05 15:01:17 +00:00
int div=-1;
qint64 closest=0,tmp,tmpft;
2011-08-05 15:01:17 +00:00
for (int i=dividx;i<divmax;i++){
tmpft=xx/divisors[i];
2011-08-05 15:01:17 +00:00
tmp=max_ticks-tmpft;
if (tmp<0) continue;
if (tmpft>closest) { // Find the closest scale to the number
closest=tmpft; // that will fit
div=i;
fit_ticks=tmpft;
}
}
if (fit_ticks==0) {
qDebug() << "FitTicks==0!";
return;
}
if (div<0) {
qDebug() << "div<0";
return;
}
qint64 step=divisors[div];
qint64 zqvx=minx-w.rmin_x; // Amount of time before minx
qint64 zz=zqvx/step; // Number of ticks that fit up to minx
//Align left minimum to divisor
qint64 aligned_start=minx/step;
aligned_start*=step;
/*qint64 rm=w.rmin_x % step; // Offset from rminx of an aligned time
2011-08-05 15:01:17 +00:00
rm=step-rm;
rm+=w.rmin_x;
//qint64 rd=w.rmin_x / divisors[div];
//rd*=divisors[div];
qint64 aligned_start=(zz*step)+rm; // First location of aligned point.
*/
2011-08-05 15:01:17 +00:00
while (aligned_start<minx) {
aligned_start+=step;
}
aligned_start/=step;
aligned_start*=step;
2011-08-05 15:01:17 +00:00
double xmult=double(width)/double(xx);
w.qglColor(Qt::black);
QString tmpstr;
for (qint64 i=aligned_start;i<maxx;i+=step) {
px=double(i-minx)*xmult;
px+=start_px;
glBegin(GL_LINES);
glVertex2f(px,start_py);
glVertex2f(px,start_py-6);
glEnd();
if (fitmode==0) {
} else {
qint64 j=i+tz_offset;
int ms=j % 1000;
int m=(j/60000L) % 60L;
int h=(j/3600000L) % 24L;
int s=(j/1000L) % 60L;
2011-08-05 15:01:17 +00:00
if (fitmode==1) { // minute
tmpstr=QString("%1:%2").arg(h,2,10,QChar('0')).arg(m,2,10,QChar('0'));
} else if (fitmode==2) { // second
tmpstr=QString("%1:%2:%3").arg(h,2,10,QChar('0')).arg(m,2,10,QChar('0')).arg(s,2,10,QChar('0'));
} else if (fitmode==3) { // milli
tmpstr=QString("%1:%2:%3:%4").arg(h,2,10,QChar('0')).arg(m,2,10,QChar('0')).arg(s,2,10,QChar('0')).arg(ms,3,10,QChar('0'));
}
}
DrawText(w,tmpstr,px-(x/2),scry-(w.GetBottomMargin()-12),0);
}
int i=0;
/* return;
2011-08-05 15:01:17 +00:00
// subtract 1.. align to first div unit
float zwv2=zqvx/max_ticks; // Number of ticks that would fit before
float gv;
float res=fmodf(zwv2,gv);
2011-06-26 08:30:44 +00:00
double jj=1/max_ticks;
double minor_tick=max_ticks*double(xx);
2011-06-26 08:30:44 +00:00
double st2=minx; //double(int(frac*1440.0))/1440.0;
double st,q;
bool show_seconds=false;
bool show_milliseconds=false;
bool show_time=true;
double min_tick;
2011-07-27 09:21:53 +00:00
if (xx<86400000) {
//double rounding[16]={12,24,48,72,96,144,288,720,1440,2880,5760,8640,17280,86400,172800,345600}; // time rounding
double rounding[16]={7200000,3600000,1800000,1200000,900000,600000,300000,120000,60000,45000,30000,15000,10000,5000,2000,1000};
2011-06-26 08:30:44 +00:00
int ri;
for (ri=0;ri<16;ri++) {
st=round(st2*rounding[ri])/rounding[ri];
min_tick=round(minor_tick*rounding[ri])/rounding[ri];
q=xx/min_tick; // number of ticks that fits in range
if (q<=jj) break; // compared to number of ticks that fit on screen.
}
2011-07-27 09:21:53 +00:00
if (min_tick<60000) show_seconds=true;
if (min_tick<10000) show_milliseconds=true;
2011-06-26 08:30:44 +00:00
2011-07-27 09:21:53 +00:00
if (min_tick<=1000)
min_tick=1000;
2011-06-26 08:30:44 +00:00
} else { // Day ticks..
show_time=false;
st=st2;
min_tick=1.0;
double mtiks=(x+20.0)/width;
double mt=mtiks*xx;
min_tick=mt;
//if (min_tick<1.0) min_tick=1.0;
//if (min_tick>10) min_tick=10;
}
2011-06-26 11:49:40 +00:00
if (min_tick<=0) {
qWarning() << "gXAxis::Plot() min_tick<=0 :(";
return;
2011-06-26 11:49:40 +00:00
}
2011-06-26 08:30:44 +00:00
double st3=st;
2011-07-27 09:21:53 +00:00
//while (st3>minx) {
// st3-=min_tick/10.0;
//}
//st3+=min_tick/10.0;
2011-06-26 08:30:44 +00:00
py=w.GetBottomMargin();
qint32 vertcnt=0;
GLshort * vertarray=vertex_array[0];
if (vertarray==NULL) {
qWarning() << "VertArray==NULL";
return;
}
2011-06-26 08:30:44 +00:00
if (m_show_minor_ticks) {
for (double i=st3; i<=maxx; i+=min_tick/10.0) {
if (i<minx) continue;
px=(i-minx)*xmult+w.GetLeftMargin();
vertarray[vertcnt++]=px;
vertarray[vertcnt++]=py;
vertarray[vertcnt++]=px;
vertarray[vertcnt++]=py-4;
if (vertcnt>=maxverts) {
qWarning() << "gXAxis::Plot() maxverts exceeded trying to draw minor ticks";
return;
}
2011-06-26 08:30:44 +00:00
}
}
2011-07-27 09:21:53 +00:00
//while (st<minx) {
// st+=min_tick/10.0; // mucking with this changes the scrollyness of the ticker.
//}
2011-06-26 08:30:44 +00:00
int hour,minute,second,millisecond;
QDateTime d;
// Need to use Qpainter clipping..
// messy. :(
2011-06-26 08:30:44 +00:00
glScissor(w.GetLeftMargin()-20,0,width+20,w.GetBottomMargin());
glEnable(GL_SCISSOR_TEST);
//const double extra=min_tick/10.0;
const double extra=0;
2011-07-27 09:21:53 +00:00
for (double i=st; i<=maxx+extra; i+=min_tick) {
2011-07-27 09:21:53 +00:00
d=QDateTime::fromMSecsSinceEpoch(i);
2011-06-26 08:30:44 +00:00
if (show_time) {
minute=d.time().minute();
hour=d.time().hour();
second=d.time().second();
millisecond=d.time().msec();
if (show_milliseconds) {
2011-08-05 07:52:32 +00:00
fd.sprintf("%02i:%02i:%02i:%03i",hour,minute,second,millisecond);
2011-06-26 08:30:44 +00:00
} else if (show_seconds) {
fd.sprintf("%02i:%02i:%02i",hour,minute,second);
} else {
fd.sprintf("%02i:%02i",hour,minute);
}
} else {
2011-07-16 07:28:50 +00:00
fd=d.toString("MMM dd");
2011-06-26 08:30:44 +00:00
}
px=(i-minx)*xmult+w.GetLeftMargin();
if ((i>=st && i<=maxx) && (m_show_major_ticks)) {
vertarray[vertcnt++]=px;
vertarray[vertcnt++]=py;
vertarray[vertcnt++]=px;
vertarray[vertcnt++]=py-6;
if (vertcnt>=maxverts) {
qWarning() << "gXAxis::Plot() maxverts exceeded trying to draw Major ticks";
return;
}
2011-06-26 08:30:44 +00:00
}
GetTextExtent(fd,x,y);
if (!show_time) {
DrawText(w,fd, px+y, scry-(py-(x/2)-8), 90.0);
2011-06-26 08:30:44 +00:00
} else {
DrawText(w,fd, px-(x/2), scry-(py-8-y));
2011-06-26 08:30:44 +00:00
}
}
2011-07-27 09:21:53 +00:00
// Draw the little ticks.
if (vertcnt>=maxverts) {
qWarning() << "maxverts exceeded in gYAxis::Plot()";
return;
}
2011-06-26 08:30:44 +00:00
glLineWidth(1);
glColor3f(0,0,0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_SHORT, 0, vertarray);
glDrawArrays(GL_LINES, 0, vertcnt>>1);
glDisableClientState(GL_VERTEX_ARRAY); // deactivate vertex arrays after drawing
glDisable(GL_SCISSOR_TEST);
*/
2011-06-26 08:30:44 +00:00
}