lib/Cycle.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 "Cycle.h"
00023 #include "PrefDialog.h"
00024 #include <qpainter.h>
00025 #include <qsettings.h>
00026 
00027 Cycle::Cycle ()
00028 {
00029   defaultColor.setNamedColor("red");
00030   defaultInterval = 10;
00031   helpFile = "cycle.html";
00032   interval = 10;
00033   grabPosition = -1;
00034   intervalLabel = "Interval";
00035   type = "Cycle";
00036   
00037   loadDefaults();
00038 }
00039 
00040 Cycle::~Cycle ()
00041 {
00042 }
00043 
00044 void Cycle::draw (QPixmap &buffer, Scaler &, int startIndex, int pixelspace, int startX)
00045 {
00046   QPainter painter;
00047   painter.begin(&buffer);
00048   
00049   int x2 = data->getX(date);
00050   if (x2 == -1)
00051     return;
00052       
00053   int x = startX + (x2 * pixelspace) - (startIndex * pixelspace);
00054   if (x == -1)
00055     return;
00056       
00057   tpixelspace = pixelspace; // save this for adjusting the cycle interval
00058     
00059   int origx = x;
00060       
00061   painter.setPen(getColor());
00062   clearSelectionArea();
00063     
00064   while (x <= buffer.width())
00065   {
00066     if ((x + (interval * pixelspace)) > 0)
00067     {
00068       painter.drawArc(x,
00069                       buffer.height() - ((interval * 4) / 2) - 2,
00070                       interval * pixelspace,
00071                       interval * 4,
00072                       16 * 180,
00073                       16 * -180);
00074       
00075       QPointArray array;
00076       array.putPoints(0,
00077                       4,
00078                       x - (HANDLE_WIDTH / 2), buffer.height(),
00079                       x - (HANDLE_WIDTH / 2), buffer.height() - HANDLE_WIDTH,
00080                       x + (HANDLE_WIDTH / 2), buffer.height() - HANDLE_WIDTH,
00081                       x + (HANDLE_WIDTH / 2), buffer.height());
00082       setSelectionArea(new QRegion(array));
00083     }
00084       
00085     x = x + (interval * pixelspace);
00086   }
00087     
00088   if (getStatus() == COBase::Selected)
00089   {
00090     clearGrabHandles();
00091       
00092     x = origx;
00093       
00094     while (x <= buffer.width())
00095     {
00096       if ((x + (interval * pixelspace)) > 0)
00097       {
00098         setGrabHandle(new QRegion(x - (HANDLE_WIDTH / 2),
00099                                   buffer.height() - HANDLE_WIDTH,
00100                                   HANDLE_WIDTH,
00101                                   HANDLE_WIDTH,
00102                                   QRegion::Rectangle));
00103                                         
00104         painter.fillRect(x - (HANDLE_WIDTH / 2),
00105                          buffer.height() - HANDLE_WIDTH,
00106                          HANDLE_WIDTH,
00107                          HANDLE_WIDTH,
00108                          getColor());
00109       }
00110         
00111       x = x + (interval * pixelspace);
00112     }
00113   }
00114 
00115   painter.end();
00116 }
00117 
00118 void Cycle::prefDialog ()
00119 {
00120   QString pl = tr("Details");
00121   QString cl = tr("Color");
00122   QString il = tr("Interval");
00123   QString sd = tr("Set Default");
00124 
00125   PrefDialog *dialog = new PrefDialog();
00126   dialog->setCaption(tr("Edit Cycle"));
00127   dialog->createPage (pl);
00128   dialog->setHelpFile (helpFile);
00129   dialog->addColorPrefItem(cl, pl, color);
00130   dialog->addIntItem(il, pl, interval, 1, 9999);
00131   dialog->addCheckItem(sd, pl, FALSE);
00132   
00133   int rc = dialog->exec();
00134   
00135   if (rc == QDialog::Accepted)
00136   {
00137     dialog->getColor(cl, color);
00138     interval = dialog->getInt(il);
00139     
00140     bool f = dialog->getCheck(sd);
00141     if (f)
00142     {
00143       defaultColor = color;
00144       saveDefaults();
00145     }
00146     
00147     emit signalDraw();
00148   }
00149   
00150   delete dialog;
00151 }
00152 
00153 void Cycle::newObject (QString &ind, QString &n)
00154 {
00155   indicator = ind;
00156   plot = ind;
00157   name = n;
00158   status = ClickWait;
00159   emit message(tr("Select starting point of Cycle..."));
00160 }
00161 
00162 COBase::Status Cycle::pointerClick (QPoint &point, QDateTime &x, double)
00163 {
00164   switch (status)
00165   {
00166     case None:
00167       if (isSelected(point))
00168       {
00169         status = Selected;
00170         emit signalDraw();
00171       }
00172       break;
00173     case Selected:
00174       if (isGrabSelected(point) && grabPosition < 2 && grabPosition > -1)
00175       {
00176         tpoint = point;
00177         tpoint.setX(point.x() - (interval * tpixelspace));
00178         status = Moving;
00179       }
00180       else
00181       {
00182         if (! isSelected(point))
00183         {
00184           status = None;
00185           emit signalDraw();
00186         }
00187       }
00188       break;
00189     case Moving:
00190       status = Selected;
00191       break;
00192     case ClickWait:
00193       setSaveFlag(TRUE);
00194       setColor(defaultColor);
00195       setDate(x);
00196       emit signalDraw();
00197       status = None;
00198       emit message("");
00199       emit signalSave(name);
00200       break;
00201     default:
00202       break;
00203   }
00204     
00205   return status;    
00206 }
00207 
00208 void Cycle::pointerMoving (QPixmap &, QPoint &p, QDateTime &x, double)
00209 {
00210   if (status != Moving)
00211     return;
00212 
00213   int gp = grabPosition;
00214   if (gp == -1)
00215     return;
00216   
00217   if (gp == 0)
00218   {
00219     setDate(x);
00220     setSaveFlag(TRUE);
00221   
00222     QString s = x.toString("yyyy-MM-dd hh:mm:ss");
00223     emit message(s);
00224     emit signalDraw();
00225   }
00226   else
00227   {
00228     if (p.x() <= tpoint.x())
00229       return;
00230         
00231     int t = (p.x() - tpoint.x()) / tpixelspace;
00232     if (t == 0)
00233       return;
00234           
00235     interval = t;
00236     setSaveFlag(TRUE);
00237         
00238     QString s = tr("Interval") + "=" + QString::number(t);
00239     emit message(s);
00240     emit signalDraw();
00241   }
00242 }
00243 
00244 void Cycle::loadDefaults ()
00245 {
00246   QSettings settings;
00247   
00248   QString s = "/Qtstalker/DefaultCycleColor";
00249   s = settings.readEntry(s);
00250   if (s.length())
00251     defaultColor.setNamedColor(s);
00252 
00253   s = "/Qtstalker/DefaultCycleInterval";
00254   s = settings.readEntry(s);
00255   if (s.length())
00256     defaultInterval = s.toInt();
00257 }
00258 
00259 void Cycle::saveDefaults ()
00260 {
00261   QSettings settings;
00262   
00263   QString s = "/Qtstalker/DefaultCycleColor";
00264   settings.writeEntry(s, defaultColor.name());
00265 
00266   s = "/Qtstalker/DefaultCycleInterval";
00267   settings.writeEntry(s, QString::number(defaultInterval));
00268 }
00269 
00270 double Cycle::getHigh ()
00271 {
00272   return data->getMax();
00273 }
00274 
00275 double Cycle::getLow ()
00276 {
00277   return data->getMin();
00278 }
00279 
00280 void Cycle::getSettings (Setting &set)
00281 {
00282   QString s = date.toString(dateFormat);
00283   set.setData(dateLabel, s);
00284   s = color.name();
00285   set.setData(colorLabel, s);
00286   set.setData(plotLabel, plot);
00287   set.setData(nameLabel, name);
00288   s = QString::number(interval);
00289   set.setData(intervalLabel, s);
00290   set.setData(typeLabel, type);
00291 }
00292 
00293 void Cycle::setSettings (Setting &set)
00294 {
00295   QString s;
00296   set.getData(dateLabel, s);
00297   Bar bar;
00298   bar.setDate(s);
00299   bar.getDate(date);
00300   set.getData(colorLabel, s);
00301   color.setNamedColor(s);
00302   set.getData(plotLabel, plot);
00303   set.getData(nameLabel, name);
00304   interval = set.getInt(intervalLabel);
00305 }
00306 
00307 bool Cycle::isGrabSelected (QPoint point)
00308 {
00309   grabPosition = -1;
00310   int loop;
00311   for (loop = 0; loop < (int) grabHandles.count(); loop++)
00312   {
00313     QRegion *r = grabHandles.at(loop);
00314     if (r->contains(point))
00315     {
00316       grabPosition = loop;
00317       return TRUE;
00318     }
00319   }
00320   
00321   return FALSE;
00322 }
00323