lib/HorizontalLine.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 "HorizontalLine.h"
00023 #include "PrefDialog.h"
00024 #include "Config.h"
00025 #include <qpainter.h>
00026 #include <qpointarray.h>
00027 #include <qsettings.h>
00028 
00029 HorizontalLine::HorizontalLine ()
00030 {
00031   defaultColor.setNamedColor("red");
00032   helpFile = "horizontalline.html";
00033   text="";
00034   textLabel = "Text";
00035   type = "HorizontalLine";
00036 
00037   Config config;  
00038   QString s;
00039   config.getData(Config::PlotFont, s);
00040   QStringList l = QStringList::split(",", s, FALSE);
00041   QFont f(l[0], l[1].toInt(), l[2].toInt());
00042   font = f;
00043   
00044   loadDefaults();
00045 }
00046 
00047 HorizontalLine::~HorizontalLine ()
00048 {
00049 }
00050 
00051 void HorizontalLine::draw (QPixmap &buffer, Scaler &scaler, int, int, int)
00052 {
00053   QPainter painter;
00054   painter.begin(&buffer);
00055   painter.setFont(font);
00056   
00057   int y = scaler.convertToY(getValue());
00058   
00059   // if value is off chart then don't draw it
00060   if (getValue() < scaler.getLow())
00061     return;
00062       
00063   painter.setPen(getColor());
00064     
00065   QFontMetrics fm(font);
00066   QString s;
00067   getText(s);
00068   int pixelsWide = fm.width(s);
00069   painter.drawLine (0, y, buffer.width(), y);
00070   painter.drawText(0, y - 1, s, -1);
00071   painter.drawText(0 + pixelsWide + 1, y - 1, QString::number(getValue()), -1);
00072   
00073   clearSelectionArea();
00074   QPointArray array;
00075   array.putPoints(0, 4, 0, y - 4, 0, y + 4, buffer.width(), y + 4, buffer.width(), y - 4);
00076   setSelectionArea(new QRegion(array));
00077     
00078   if (getStatus() == COBase::Selected)
00079   {
00080     clearGrabHandles();
00081     int t = (int) buffer.width() / 4;
00082     
00083     setGrabHandle(new QRegion(0,
00084                               y - (HANDLE_WIDTH / 2),
00085                               HANDLE_WIDTH,
00086                               HANDLE_WIDTH,
00087                               QRegion::Rectangle));
00088     painter.fillRect(0, y - (HANDLE_WIDTH / 2), HANDLE_WIDTH, HANDLE_WIDTH, getColor());
00089   
00090     setGrabHandle(new QRegion(t,
00091                               y - (HANDLE_WIDTH / 2),
00092                               HANDLE_WIDTH,
00093                               HANDLE_WIDTH,
00094                               QRegion::Rectangle));
00095     painter.fillRect(t, y - (HANDLE_WIDTH / 2), HANDLE_WIDTH, HANDLE_WIDTH, getColor());
00096     
00097     setGrabHandle(new QRegion(t * 2,
00098                               y - (HANDLE_WIDTH / 2),
00099                               HANDLE_WIDTH,
00100                               HANDLE_WIDTH,
00101                               QRegion::Rectangle));
00102     painter.fillRect(t * 2, y - (HANDLE_WIDTH / 2), HANDLE_WIDTH, HANDLE_WIDTH, getColor());
00103     
00104     setGrabHandle(new QRegion(t * 3,
00105                               y - (HANDLE_WIDTH / 2),
00106                               HANDLE_WIDTH,
00107                               HANDLE_WIDTH,
00108                               QRegion::Rectangle));
00109     painter.fillRect(t * 3, y - (HANDLE_WIDTH / 2), HANDLE_WIDTH, HANDLE_WIDTH, getColor());
00110     
00111     setGrabHandle(new QRegion(t * 4,
00112                               y - (HANDLE_WIDTH / 2),
00113                               HANDLE_WIDTH,
00114                               HANDLE_WIDTH,
00115                               QRegion::Rectangle));
00116     painter.fillRect(t * 4, y - (HANDLE_WIDTH / 2), HANDLE_WIDTH, HANDLE_WIDTH, getColor());
00117   }
00118 
00119   painter.end();
00120 }
00121 
00122 void HorizontalLine::prefDialog ()
00123 {
00124   QString pl = tr("Details");
00125   QString cl = tr("Color");
00126   QString sd = tr("Set Default");
00127   QString vl = tr("Value");
00128   QString tx = tr("Text");
00129 
00130   PrefDialog *dialog = new PrefDialog();
00131   dialog->setCaption(tr("Edit HorizontalLine"));
00132   dialog->createPage (pl);
00133   dialog->setHelpFile (helpFile);
00134   dialog->addColorPrefItem(cl, pl, color);
00135   dialog->addDoubleItem(vl, pl, getValue());
00136   dialog->addTextItem(tx, pl, text);  //cz odkazy na objekty definovane nahore
00137   dialog->addCheckItem(sd, pl, FALSE);
00138   
00139   int rc = dialog->exec();
00140   
00141   if (rc == QDialog::Accepted)
00142   {
00143     dialog->getColor(cl, color);
00144     value = dialog->getDouble(vl);
00145     dialog->getText(tx, text);
00146     setSaveFlag(TRUE);
00147     
00148     bool f = dialog->getCheck(sd);
00149     if (f)
00150     {
00151       defaultColor = color;
00152       saveDefaults();
00153     }
00154     
00155     emit signalDraw();
00156   }
00157   
00158   delete dialog;
00159 }
00160 
00161 void HorizontalLine::newObject (QString &ind, QString &n)
00162 {
00163   indicator = ind;
00164   plot = ind;
00165   name = n;
00166   status = ClickWait;
00167   emit message(tr("Select point to place HorizontalLine..."));
00168 }
00169 
00170 COBase::Status HorizontalLine::pointerClick (QPoint &point, QDateTime &, double y)
00171 {
00172   switch (status)
00173   {
00174     case None:
00175       if (isSelected(point))
00176       {
00177         status = Selected;
00178         emit signalDraw();
00179       }
00180       break;
00181     case Selected:
00182       if (isGrabSelected(point))
00183         status = Moving;
00184       else
00185       {
00186         if (! isSelected(point))
00187         {
00188           status = None;
00189           emit signalDraw();
00190         }
00191       }
00192       break;
00193     case Moving:
00194       status = Selected;
00195       break;
00196     case ClickWait:
00197       setValue(y);
00198       setSaveFlag(TRUE);
00199       setColor(defaultColor);
00200       emit signalDraw();
00201       status = None;
00202       emit message("");
00203       emit signalSave(name);
00204       break;
00205     default:
00206       break;
00207   }
00208 
00209   return status;    
00210 }
00211 
00212 void HorizontalLine::pointerMoving (QPixmap &, QPoint &, QDateTime &, double y)
00213 {
00214   if (status != Moving)
00215     return;
00216     
00217   setValue(y);
00218   setSaveFlag(TRUE);
00219   emit signalDraw();
00220   QString s = QString::number(y);
00221   emit message(s);
00222 }
00223 
00224 void HorizontalLine::loadDefaults ()
00225 {
00226   QSettings settings;
00227   
00228   QString s = "/Qtstalker/DefaultHorizontalLineColor";
00229   s = settings.readEntry(s);
00230   if (s.length())
00231     defaultColor.setNamedColor(s);
00232 }
00233 
00234 void HorizontalLine::saveDefaults ()
00235 {
00236   QSettings settings;
00237   
00238   QString s = "/Qtstalker/DefaultHorizontalLineColor";
00239   settings.writeEntry(s, defaultColor.name());
00240 }
00241 
00242 void HorizontalLine::getSettings (Setting &set)
00243 {
00244   QString s = QString::number(value);
00245   set.setData(valueLabel, s);
00246   s = color.name();
00247   set.setData(colorLabel, s);
00248   set.setData(plotLabel, plot);
00249   set.setData(nameLabel, name);
00250   set.setData(textLabel, text);
00251   set.setData(typeLabel, type);
00252 }
00253 
00254 void HorizontalLine::setSettings (Setting &set)
00255 {
00256   QString s;
00257   value = set.getDouble(valueLabel);
00258   set.getData(colorLabel, s);
00259   color.setNamedColor(s);
00260   set.getData(plotLabel, plot);
00261   set.getData(nameLabel, name);
00262   set.getData(textLabel, text);
00263 }
00264 
00265 void HorizontalLine::getText (QString &d)
00266 {
00267   d = text;
00268 }
00269 
00270 void HorizontalLine::adjustForSplit (QDateTime &, double d)
00271 {
00272   value = value * d;
00273 }
00274