You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
474 lines
10 KiB
474 lines
10 KiB
15 years ago
|
/***************************************************************************
|
||
|
* Copyright © 2007 by Krzysztof Kundzicz *
|
||
|
* athantor@gmail.com *
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU General Public License as published by *
|
||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||
|
* (at your option) any later version. *
|
||
|
* *
|
||
|
* This program is distributed in the hope that it will be useful, *
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
||
|
* GNU General Public License for more details. *
|
||
|
* *
|
||
|
* You should have received a copy of the GNU General Public License *
|
||
|
* along with this program; if not, write to the *
|
||
|
* Free Software Foundation, Inc., *
|
||
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
|
||
|
***************************************************************************/
|
||
|
|
||
|
#include "ChartDrawer.h"
|
||
|
|
||
|
#ifdef USE_SOLARIS
|
||
|
#include <ieeefp.h>
|
||
|
int isinf(double x) { return !finite(x) && x==x; }
|
||
|
#endif
|
||
|
|
||
|
|
||
|
namespace kt {
|
||
|
|
||
14 years ago
|
ChartDrawer::ChartDrawer(TQWidget *p, wgtsize_t x_max, wgtsize_t y_max, bool autom, const TQString & uname) : TQWidget(p), mXMax(x_max), mYMax(y_max), mAutoMax(autom),
|
||
15 years ago
|
mUnitName(uname), mMMode(MaxModeExact)
|
||
|
{
|
||
|
setBackgroundColor("white");
|
||
|
}
|
||
|
|
||
|
ChartDrawer::~ChartDrawer()
|
||
|
{
|
||
14 years ago
|
TQToolTip::remove(this);
|
||
15 years ago
|
}
|
||
|
|
||
|
ChartDrawer::wgtsize_t ChartDrawer::GetXMax() const
|
||
|
{
|
||
|
return mXMax;
|
||
|
}
|
||
|
|
||
|
ChartDrawer::wgtsize_t ChartDrawer::GetYMax() const
|
||
|
{
|
||
|
return mYMax;
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::SetXMax(const wgtsize_t x)
|
||
|
{
|
||
|
mXMax = x;
|
||
|
|
||
|
for(size_t i = 0; i < mEls.size(); i++)
|
||
|
{
|
||
|
mEls[i].pmVals -> resize(x, 0.0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::SetYMax(const wgtsize_t y)
|
||
|
{
|
||
|
mYMax = y;
|
||
|
}
|
||
|
|
||
|
inline ChartDrawer::wgtsize_t ChartDrawer::GetYScale() const
|
||
|
{
|
||
|
return height() / 8;
|
||
|
}
|
||
|
|
||
|
|
||
|
inline ChartDrawer::wgtunit_t ChartDrawer::TrY(const ChartDrawer::wgtunit_t y) const
|
||
|
{
|
||
|
return height() - y;
|
||
|
}
|
||
|
|
||
14 years ago
|
void ChartDrawer::paintEvent ( TQPaintEvent *)
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPainter pnt( this );
|
||
15 years ago
|
|
||
|
DrawScale(pnt);
|
||
|
DrawFrame(pnt);
|
||
|
DrawChart(pnt);
|
||
|
|
||
|
}
|
||
|
|
||
|
inline ChartDrawer::wgtunit_t ChartDrawer::height() const
|
||
|
{
|
||
14 years ago
|
return TQWidget::height() - 15;
|
||
15 years ago
|
}
|
||
|
|
||
|
inline ChartDrawer::wgtunit_t ChartDrawer::width() const
|
||
|
{
|
||
14 years ago
|
return TQWidget::width() - 65;
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void ChartDrawer::DrawFrame(TQPainter & rPnt )
|
||
15 years ago
|
{
|
||
14 years ago
|
TQPen op = rPnt.pen();
|
||
|
rPnt.setPen(TQPen("#000", 3));
|
||
15 years ago
|
|
||
|
rPnt.drawLine(0, TrY(0), width()+3, TrY(0));
|
||
14 years ago
|
rPnt.drawLine(width()+1, TrY(0), width()+1, TrY(TQWidget::height()));
|
||
15 years ago
|
|
||
14 years ago
|
TQFont oldf(rPnt.font());
|
||
|
TQFont newf(oldf);
|
||
|
newf.setWeight(TQFont::Bold);
|
||
15 years ago
|
newf.setPointSize(10);
|
||
|
newf.setUnderline(1);
|
||
|
|
||
|
rPnt.setFont(newf);
|
||
|
rPnt.drawText(width() + 30, TrY(-7), mUnitName);
|
||
|
rPnt.setFont(oldf);
|
||
|
|
||
|
rPnt.setPen(op);
|
||
|
}
|
||
|
|
||
14 years ago
|
void ChartDrawer::DrawScale(TQPainter & rPnt )
|
||
15 years ago
|
{
|
||
|
|
||
|
if(!mYMax)
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQPen op = rPnt.pen();
|
||
|
TQPen ep("#eee", 1, TQt::DashLine);
|
||
|
TQPen lp("#666", 2, TQt::DotLine);
|
||
|
TQPen tp("#000");
|
||
15 years ago
|
|
||
|
rPnt.setPen(ep);
|
||
|
|
||
|
for(wgtsize_t i = 1; i < width(); i += 10)
|
||
|
{
|
||
|
rPnt.drawLine(i, TrY(0), i, TrY(height()));
|
||
|
}
|
||
|
|
||
|
for(wgtsize_t i = 0; i < height(); i += 10)
|
||
|
{
|
||
|
rPnt.drawLine(0, TrY(i), width(), TrY(i));
|
||
|
}
|
||
|
|
||
|
rPnt.setPen(lp);
|
||
|
rPnt.drawLine(0, TrY(height() - 10), width(), TrY(height() - 10));
|
||
|
rPnt.setPen(tp);
|
||
14 years ago
|
rPnt.drawText(width() + 4, TrY(height() - 10) + 4, TQString::number (mYMax));
|
||
15 years ago
|
|
||
|
for(wgtsize_t i = 0; i < height() - 15 ; i += GetYScale())
|
||
|
{
|
||
|
rPnt.setPen(lp);
|
||
|
rPnt.drawLine(0, TrY(i), width(), TrY(i));
|
||
|
rPnt.setPen(tp);
|
||
14 years ago
|
rPnt.drawText(width() + 4, TrY(i) + 4, TQString::number ( (mYMax / 8.0 ) * ( i / static_cast<double>(GetYScale() )), 'f', 1 ) );
|
||
15 years ago
|
}
|
||
|
|
||
|
rPnt.setPen(op);
|
||
|
}
|
||
|
|
||
14 years ago
|
void ChartDrawer::DrawChart(TQPainter & rPnt)
|
||
15 years ago
|
{
|
||
|
|
||
14 years ago
|
TQPen op = rPnt.pen();
|
||
15 years ago
|
|
||
|
uint32_t skip_max = 0;
|
||
|
|
||
|
for(size_t i = 0; i < mEls.size(); i++)
|
||
|
{
|
||
|
rPnt.setPen( *mEls[i].GetPen() );
|
||
|
|
||
|
for(size_t j = 1; j < mEls[i].pmVals -> size() - 1; j++)
|
||
|
{
|
||
|
rPnt.drawLine(
|
||
|
FindXScreenCoords(j-1),
|
||
|
TrY(FindYScreenCoords(mEls[i].pmVals -> at(j-1))),
|
||
|
FindXScreenCoords(j),
|
||
|
TrY(FindYScreenCoords(mEls[i].pmVals -> at(j)))
|
||
|
);
|
||
|
}
|
||
|
//
|
||
|
rPnt.drawLine(
|
||
|
FindXScreenCoords(mEls[i].pmVals -> size() - 2),
|
||
|
TrY(FindYScreenCoords(mEls[i].pmVals -> at(mEls[i].pmVals -> size() - 2))),
|
||
|
width(),
|
||
|
TrY(FindYScreenCoords(mEls[i].pmVals -> at(mEls[i].pmVals -> size() - 1)))
|
||
|
);
|
||
|
|
||
|
// --------------------
|
||
|
// Line on top
|
||
|
// ------------
|
||
14 years ago
|
TQPen myop(rPnt.pen());
|
||
|
TQPen topl(myop);
|
||
15 years ago
|
topl.setStyle(Qt::DotLine);
|
||
|
rPnt.setPen(topl);
|
||
|
rPnt.drawLine(0, TrY(FindYScreenCoords(mEls[i].pmVals -> at(mEls[i].pmVals -> size() - 1))), width(), TrY(FindYScreenCoords(mEls[i].pmVals -> at(mEls[i].pmVals -> size() - 1))) );
|
||
|
rPnt.setPen(myop);
|
||
|
|
||
14 years ago
|
TQFont oldf(rPnt.font());
|
||
|
TQFont newf(oldf);
|
||
|
newf.setWeight(TQFont::Bold);
|
||
15 years ago
|
newf.setPointSize(8);
|
||
|
|
||
|
rPnt.setFont(newf);
|
||
14 years ago
|
rPnt.drawText(5 + (i * 50), TrY(FindYScreenCoords(mEls[i].pmVals -> at(mEls[i].pmVals -> size() - 1))) + 11, TQString::number (mEls[i].pmVals -> at(mEls[i].pmVals -> size() - 1), 'f', 2 ) );
|
||
15 years ago
|
|
||
|
|
||
|
//------------------
|
||
|
// max
|
||
|
//------------------
|
||
|
|
||
|
if(mMarkMax[i])
|
||
|
{
|
||
|
rPnt.setPen(topl);
|
||
|
std::pair<double, size_t> max = mEls[i] . Max();
|
||
|
|
||
|
rPnt.drawLine(
|
||
|
FindXScreenCoords(max.second), TrY(0), FindXScreenCoords(max.second), TrY(height())
|
||
|
);
|
||
|
|
||
|
rPnt.setPen(myop);
|
||
|
|
||
|
rPnt.setFont(newf);
|
||
14 years ago
|
TQString maxv(TQString::number (max.first, 'f', 2));
|
||
15 years ago
|
|
||
|
if(FindXScreenCoords(max.second) < 35)
|
||
|
{
|
||
|
rPnt.drawText(FindXScreenCoords(max.second) + 5, TrY(height() - (10 * (i - skip_max)) ) + 10, maxv ) ;
|
||
|
} else {
|
||
|
rPnt.drawText(FindXScreenCoords(max.second) - 35 , TrY(height() - (10 * (i - skip_max)) ) + 10, maxv ) ;
|
||
|
}
|
||
|
} else {
|
||
|
skip_max++;
|
||
|
}
|
||
|
|
||
|
rPnt.setFont(oldf);
|
||
|
rPnt.setPen(op);
|
||
|
}
|
||
|
|
||
|
rPnt.setPen(op);
|
||
|
}
|
||
|
|
||
|
inline ChartDrawer::wgtunit_t ChartDrawer::FindXScreenCoords(const double x) const
|
||
|
{
|
||
|
return static_cast<wgtunit_t>((width() / static_cast<double>(mXMax)) * x) ;
|
||
|
}
|
||
|
|
||
|
inline ChartDrawer::wgtunit_t ChartDrawer::FindYScreenCoords(const double y) const
|
||
|
{
|
||
|
return static_cast<wgtunit_t>(((height()) / static_cast<double>(mYMax)) * y) ;
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::EnableAutoMax(bool a)
|
||
|
{
|
||
|
mAutoMax = a;
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::AddValue(const size_t idx, const double val, bool u )
|
||
|
{
|
||
|
|
||
|
if( idx >= mEls.size() )
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
ChartDrawerData::val_t::iterator it = mEls[idx].pmVals -> begin();
|
||
|
|
||
|
while(it != mEls[idx] .pmVals -> end() )
|
||
|
{
|
||
|
*it = *(it + 1);
|
||
|
it++;
|
||
|
}
|
||
|
|
||
|
#ifdef USE_SOLARIS
|
||
|
if(isnand(val) || (isinf(val)))
|
||
|
#else
|
||
|
if(std::isnan(val) || (std::isinf(val)))
|
||
|
#endif
|
||
|
{
|
||
|
*(mEls[idx].pmVals -> end() -1) = 0.0;
|
||
|
} else {
|
||
|
*(mEls[idx].pmVals -> end() -1) = val;
|
||
|
}
|
||
|
|
||
|
if(mAutoMax)
|
||
|
{
|
||
|
if( (mMMode == MaxModeTop) && (val > mYMax) )
|
||
|
{
|
||
|
mYMax = static_cast<wgtsize_t>(val) + 3;
|
||
|
|
||
|
} else if(mMMode == MaxModeExact) {
|
||
|
FindSetMax();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(u)
|
||
|
{
|
||
|
update();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::AddValues(ChartDrawerData Cdd, const bool max)
|
||
|
{
|
||
|
if(Cdd.pmVals -> size() != mXMax)
|
||
|
{
|
||
|
Cdd.pmVals -> resize(mXMax, 0.0);
|
||
|
}
|
||
|
|
||
|
mEls.push_back(Cdd);
|
||
|
mMarkMax.push_back(max);
|
||
|
|
||
|
MakeLegendTooltip();
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::AddValues(ChartDrawerData Cdd, const size_t idx, const bool max)
|
||
|
{
|
||
|
if(Cdd.pmVals -> size() != mXMax)
|
||
|
{
|
||
|
Cdd.pmVals -> resize(mXMax, 0.0);
|
||
|
}
|
||
|
|
||
|
if(idx >= mEls.size())
|
||
|
{
|
||
|
mEls.push_back(Cdd);
|
||
|
} else {
|
||
|
mEls.insert(mEls.begin() + idx, Cdd);
|
||
|
}
|
||
|
|
||
|
if(idx >= mMarkMax.size())
|
||
|
{
|
||
|
mMarkMax.push_back(max);
|
||
|
} else {
|
||
|
mMarkMax.insert(mMarkMax.begin() + idx, max);
|
||
|
}
|
||
|
|
||
|
MakeLegendTooltip();
|
||
|
}
|
||
|
|
||
14 years ago
|
void ChartDrawer::AddValuesCnt(const TQString & rN, const bool max)
|
||
15 years ago
|
{
|
||
|
mEls.push_back(ChartDrawerData(mXMax, rN));
|
||
|
mMarkMax.push_back(max);
|
||
|
|
||
|
MakeLegendTooltip();
|
||
|
}
|
||
|
|
||
14 years ago
|
void ChartDrawer::AddValuesCnt(const TQPen & rP, const TQString & rN, const bool max)
|
||
15 years ago
|
{
|
||
|
mEls.push_back(ChartDrawerData(rP, mXMax, rN));
|
||
|
mMarkMax.push_back(max);
|
||
|
|
||
|
MakeLegendTooltip();
|
||
|
}
|
||
|
|
||
14 years ago
|
void ChartDrawer::SetUnitName(const TQString & rN)
|
||
15 years ago
|
{
|
||
|
mUnitName = rN;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString ChartDrawer::GetUnitName() const
|
||
15 years ago
|
{
|
||
|
return mUnitName;
|
||
|
}
|
||
|
|
||
14 years ago
|
void ChartDrawer::mouseDoubleClickEvent ( TQMouseEvent * evt )
|
||
15 years ago
|
{
|
||
|
FindSetMax();
|
||
|
|
||
|
emit DoubleClicked(evt);
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::EnableMaxDrawAt(const size_t at, const bool e)
|
||
|
{
|
||
|
if(at >= mMarkMax.size())
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
mMarkMax[at] = e;
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::RemoveValuesCnt(const size_t idx)
|
||
|
{
|
||
|
if(idx >= mEls.size())
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
mEls.erase(mEls.begin() + idx);
|
||
|
|
||
|
if(idx <= mMarkMax.size())
|
||
|
{
|
||
|
mMarkMax.erase(mMarkMax.begin() + idx);
|
||
|
}
|
||
|
|
||
|
MakeLegendTooltip();
|
||
|
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::Zero(const size_t idx)
|
||
|
{
|
||
|
if(idx >= mEls.size())
|
||
|
{
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
std::fill(mEls[idx].pmVals -> begin(), mEls[idx].pmVals -> end(), 0.0);
|
||
|
|
||
|
if(mAutoMax)
|
||
|
{
|
||
|
mYMax = 1;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::MakeLegendTooltip()
|
||
|
{
|
||
14 years ago
|
TQToolTip::remove(this);
|
||
15 years ago
|
|
||
13 years ago
|
TQString helpstr(TQString("<b>%1:</b><br><br>").arg(i18n("Legend")));
|
||
14 years ago
|
TQMimeSourceFactory* factory = TQMimeSourceFactory::defaultFactory();
|
||
|
std::vector<TQImage> img;
|
||
15 years ago
|
|
||
|
for(size_t i = 0; i < mEls.size(); i++)
|
||
|
{
|
||
14 years ago
|
img.push_back(TQImage(16,16, 32));
|
||
|
img[i].fill(TQColor(mEls[i].GetPen() -> color()).pixel());
|
||
15 years ago
|
|
||
|
for(uint8_t px = 0; px < 16; px++)
|
||
|
{
|
||
|
img[i].setPixel(px, 0, 0); //t
|
||
|
img[i].setPixel(0, px, 0); //l
|
||
|
img[i].setPixel(px, 15, 0); //b
|
||
|
img[i].setPixel(15, px, 0); //r
|
||
|
}
|
||
|
|
||
14 years ago
|
factory->setImage(mEls[i].GetName().replace(' ', '_') + "-" + TQString::number(i), img[i]);
|
||
13 years ago
|
helpstr += TQString("<img src='%1'> - %2<br>").arg(mEls[i].GetName().replace(" ", "_") + "-" + TQString::number(i)).arg( mEls[i].GetName() );
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
TQToolTip::add(this, helpstr);
|
||
15 years ago
|
}
|
||
|
|
||
|
void ChartDrawer::FindSetMax()
|
||
|
{
|
||
|
wgtsize_t mymax = 1;
|
||
|
|
||
|
for(val_t::const_iterator it = mEls.begin(); it != mEls.end(); ++it)
|
||
|
{
|
||
|
for(ChartDrawerData::val_t::const_iterator subit = it -> pmVals -> begin(); subit != it -> pmVals -> end(); ++subit)
|
||
|
{
|
||
|
if ( (*subit) > mymax )
|
||
|
{
|
||
|
mymax = static_cast<wgtsize_t>(*subit) + 3;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
mYMax = mymax;
|
||
|
}
|
||
|
|
||
|
void ChartDrawer::SetMaxMode(const MaxMode mm)
|
||
|
{
|
||
|
mMMode = mm;
|
||
|
}
|
||
|
|
||
|
ChartDrawer::MaxMode ChartDrawer::GetMaxMode() const
|
||
|
{
|
||
|
return mMMode;
|
||
|
}
|
||
|
|
||
|
} //NS end
|
||
|
|
||
|
#include "ChartDrawer.moc"
|