lib/PlotLine.cpp

Go to the documentation of this file.
00001 /*
00002  *  Qtstalker stock charter
00003  *
00004  *  Copyright (C) 2001-2007 Stefan S. Stratigakos
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019  *  USA.
00020  */
00021 
00022 #include "PlotLine.h"
00023 #include <qpainter.h>
00024 #include <qpointarray.h>
00025 #include <qobject.h>
00026 
00027 PlotLine::PlotLine ()
00028 {
00029   color.setNamedColor("red");
00030   lineType = PlotLine::Line;
00031   high = -99999999;
00032   low = 99999999;
00033   scaleFlag = FALSE;
00034   colorFlag = FALSE;
00035 }
00036 
00037 PlotLine::~PlotLine ()
00038 {
00039 }
00040 
00041 void PlotLine::copy (PlotLine *d)
00042 {
00043   d->getColor(color);
00044   
00045   setType(d->getType());
00046   
00047   d->getLabel(label);
00048   
00049   setScaleFlag(d->getScaleFlag());
00050 
00051   setColorFlag(d->getColorFlag());
00052 
00053   d->getData(data);
00054 
00055   d->getDateList(dateList);
00056 
00057   setHigh(d->getHigh());
00058   setLow(d->getLow());
00059 }
00060 
00061 void PlotLine::setColor (QString &d)
00062 {
00063   color.setNamedColor(d);
00064 }
00065 
00066 void PlotLine::setColor (QColor &d)
00067 {
00068   color = d;
00069 }
00070 
00071 void PlotLine::getColor (QColor &d)
00072 {
00073   d = color;
00074 }
00075 
00076 void PlotLine::setColorBar (int i, QColor &d)
00077 {
00078   data[i].color = d;
00079 }
00080 
00081 void PlotLine::getColorBar (int i, QColor &d)
00082 {
00083   d = data[i].color;
00084 }
00085 
00086 void PlotLine::setType (PlotLine::LineType d)
00087 {
00088   lineType = d;
00089 }
00090 
00091 void PlotLine::setType (QString &d)
00092 {
00093   if (! d.compare(QObject::tr("Dot")))
00094   {
00095     lineType = Dot;
00096     return;
00097   }
00098 
00099   if (! d.compare(QObject::tr("Dash")))
00100   {
00101     lineType = Dash;
00102     return;
00103   }
00104   
00105   if (! d.compare(QObject::tr("Histogram")))
00106   {
00107     lineType = Histogram;
00108     return;
00109   }
00110   
00111   if (! d.compare(QObject::tr("Dash")))
00112   {
00113     lineType = Dash;
00114     return;
00115   }
00116 
00117   if (! d.compare(QObject::tr("Histogram Bar")))
00118   {
00119     lineType = HistogramBar;
00120     return;
00121   }
00122 
00123   if (! d.compare(QObject::tr("Line")))
00124   {
00125     lineType = Line;
00126     return;
00127   }
00128 
00129   if (! d.compare(QObject::tr("Invisible")))
00130   {
00131     lineType = Invisible;
00132     return;
00133   }
00134 
00135   if (! d.compare(QObject::tr("Horizontal")))
00136   {
00137     lineType = Horizontal;
00138     return;
00139   }
00140 
00141   if (! d.compare(QObject::tr("Bar")))
00142   {
00143     lineType = Bar;
00144     return;
00145   }
00146 
00147   if (! d.compare(QObject::tr("Candle")))
00148   {
00149     lineType = Candle;
00150     return;
00151   }
00152 
00153   if (! d.compare(QObject::tr("PF")))
00154   {
00155     lineType = PF;
00156     return;
00157   }
00158 }
00159 
00160 PlotLine::LineType PlotLine::getType ()
00161 {
00162   return lineType;
00163 }
00164 
00165 void PlotLine::setLabel (QString &d)
00166 {
00167   label = d;
00168 }
00169 
00170 void PlotLine::getLabel (QString &d)
00171 {
00172   d = label;
00173 }
00174 
00175 void PlotLine::append (double d)
00176 {
00177   Val r;
00178   memset(&r, 0, sizeof(Val));
00179   r.color = color;
00180   r.open = d;
00181   r.high = d;
00182   r.low = d;
00183   r.v = d;
00184   data.append(r);
00185   checkHighLow(d);
00186 }
00187 
00188 void PlotLine::prepend (double d)
00189 {
00190   Val r;
00191   memset(&r, 0, sizeof(Val));
00192   r.color = color;
00193   r.open = d;
00194   r.high = d;
00195   r.low = d;
00196   r.v = d;
00197   data.prepend(r);
00198   checkHighLow(d);
00199 }
00200 
00201 double PlotLine::getData (int d)
00202 {
00203   return data[d].v;
00204 }
00205 
00206 void PlotLine::setData (int i, double d)
00207 {
00208   Val r = data[i];
00209   r.open = d;
00210   r.high = d;
00211   r.low = d;
00212   r.v = d;
00213   checkHighLow(d);
00214 }
00215 
00216 int PlotLine::getSize ()
00217 {
00218   return (int) data.count();
00219 }
00220 
00221 double PlotLine::getHigh ()
00222 {
00223   return high;
00224 }
00225 
00226 void PlotLine::setHigh (double d)
00227 {
00228   high = d;
00229 }
00230 
00231 double PlotLine::getLow ()
00232 {
00233   return low;
00234 }
00235 
00236 void PlotLine::setLow (double d)
00237 {
00238   low = d;
00239 }
00240 
00241 void PlotLine::checkHighLow (double d)
00242 {
00243   if (d > high)
00244     high = d;
00245   if (d < low)
00246     low = d;
00247 }
00248 
00249 void PlotLine::setScaleFlag (bool d)
00250 {
00251   scaleFlag = d;
00252 }
00253 
00254 bool PlotLine::getScaleFlag ()
00255 {
00256   return scaleFlag;
00257 }
00258 
00259 void PlotLine::setColorFlag (bool d)
00260 {
00261   colorFlag = d;
00262 }
00263 
00264 bool PlotLine::getColorFlag ()
00265 {
00266   return colorFlag;
00267 }
00268 
00269 void PlotLine::getLineTypes (QStringList &l)
00270 {
00271   l.clear();
00272   l.append(QObject::tr("Dot"));
00273   l.append(QObject::tr("Dash"));
00274   l.append(QObject::tr("Histogram"));
00275   l.append(QObject::tr("Histogram Bar"));
00276   l.append(QObject::tr("Line"));
00277   l.append(QObject::tr("Invisible"));
00278   l.append(QObject::tr("Horizontal"));
00279   l.append(QObject::tr("Candle"));
00280   l.append(QObject::tr("Bar"));
00281   l.append(QObject::tr("PF"));
00282 }
00283 
00284 void PlotLine::append (QColor &c, double o, double h, double l, double cl, bool cf)
00285 {
00286   Val r;
00287   memset(&r, 0, sizeof(Val));
00288   r.color = c;
00289   r.open = o;
00290   r.high = h;
00291   r.low = l;
00292   r.v = cl;
00293   r.candleFill = cf;
00294   data.append(r);
00295   checkHighLow(o);
00296   checkHighLow(h);
00297   checkHighLow(l);
00298   checkHighLow(cl);
00299 }
00300 
00301 void PlotLine::append2 (QColor &c, double o, double h, double l, double cl, bool cf)
00302 {
00303   Val r;
00304   memset(&r, 0, sizeof(Val));
00305   r.color = c;
00306   r.open = o;
00307   r.high = h;
00308   r.low = l;
00309   r.v = cl;
00310   r.candleFill = cf;
00311   data.append(r);
00312   checkHighLow(h);
00313   checkHighLow(l);
00314 }
00315 
00316 void PlotLine::prepend (QColor &c, double o, double h, double l, double cl, bool cf)
00317 {
00318   Val r;
00319   memset(&r, 0, sizeof(Val));
00320   r.color = c;
00321   r.open = o;
00322   r.high = h;
00323   r.low = l;
00324   r.v = cl;
00325   r.candleFill = cf;
00326   data.prepend(r);
00327   checkHighLow(o);
00328   checkHighLow(h);
00329   checkHighLow(l);
00330   checkHighLow(cl);
00331 }
00332 
00333 void PlotLine::getData (int i, QColor &c, double &o, double &h, double &l, double &cl, bool &cf)
00334 {
00335   Val r = data[i];
00336   c = r.color;
00337   o = r.open;
00338   h = r.high;
00339   l = r.low;
00340   cl = r.v;
00341   cf = r.candleFill;
00342 }
00343 
00344 void PlotLine::getHighLowRange (int start, int end, double &h, double &l)
00345 {
00346   int loop;
00347   h = -99999999;
00348   l = 99999999;
00349   for (loop = start; loop <= end; loop++)
00350   {
00351     Val r = data[loop];
00352 
00353     switch (lineType)
00354     {
00355       case Bar:
00356       case Candle:
00357         if (r.open > h)
00358           h = r.open;
00359         if (r.open < l)
00360           l = r.open;
00361 
00362         if (r.high > h)
00363           h = r.high;
00364         if (r.high < l)
00365           l = r.high;
00366 
00367         if (r.low > h)
00368           h = r.low;
00369         if (r.low < l)
00370           l = r.low;
00371 
00372         if (r.v > h)
00373           h = r.v;
00374         if (r.v < l)
00375           l = r.v;
00376         break;
00377       case PF:
00378         if (r.high > h)
00379           h = r.high;
00380         if (r.high < l)
00381           l = r.high;
00382 
00383         if (r.low > h)
00384           h = r.low;
00385         if (r.low < l)
00386           l = r.low;
00387         break;
00388       default:
00389         if (r.v > h)
00390           h = r.v;
00391         if (r.v < l)
00392           l = r.v;
00393       break;
00394     }
00395   }
00396 }
00397 
00398 void PlotLine::getInfo (int i, Setting &r)
00399 {
00400   QString s, k;
00401   double open, high, low, close;
00402   QColor color;
00403   bool ff;
00404 
00405   switch (lineType)
00406   {
00407     case Bar:
00408     case Candle:
00409       getData(i, color, open, high, low, close, ff);
00410 
00411       strip(open, 4, s);
00412       k = "O";
00413       r.setData(k, s);
00414 
00415       strip(high, 4, s);
00416       k = "H";
00417       r.setData(k, s);
00418 
00419       strip(low, 4, s);
00420       k = "L";
00421       r.setData(k, s);
00422 
00423       strip(close, 4, s);
00424       k = "C";
00425       r.setData(k, s);
00426       break;
00427     case PF:
00428       getData(i, color, open, high, low, close, ff);
00429 
00430       strip(high, 4, s);
00431       k = "H";
00432       r.setData(k, s);
00433 
00434       strip(low, 4, s);
00435       k = "L";
00436       r.setData(k, s);
00437       break;
00438     default:
00439       strip(getData(i), 4, s);
00440       r.setData(label, s);
00441       break;
00442   }
00443 }
00444 
00445 void PlotLine::strip (double d, int p, QString &s)
00446 {
00447   s = QString::number(d, 'f', p);
00448 
00449   while (1)
00450   {
00451     if (s.find('.', -1, TRUE) != -1)
00452     {
00453       s.truncate(s.length() - 1);
00454       break;
00455     }
00456     else
00457     {
00458       if (s.find('0', -1, TRUE) != -1)
00459         s.truncate(s.length() - 1);
00460       else
00461         break;
00462     }
00463   }
00464 }
00465 
00466 void PlotLine::getData (int i, QDateTime &dt)
00467 {
00468   if (i >= (int) dateList.count())
00469     return;
00470 
00471   dt = dateList[i];
00472 }
00473 
00474 void PlotLine::append (QDateTime &dt)
00475 {
00476   dateList.append(dt);
00477 }
00478 
00479 void PlotLine::prepend (QDateTime &dt)
00480 {
00481   dateList.prepend(dt);
00482 }
00483 
00484 void PlotLine::getDateList (QValueList<QDateTime> &dl)
00485 {
00486   dl = dateList;
00487 }
00488 
00489 void PlotLine::setDateList (QValueList<QDateTime> &dl)
00490 {
00491   dateList = dl;
00492 }
00493 
00494 void PlotLine::getData (QValueList<Val> &d)
00495 {
00496   d = data;
00497 }
00498 
00499