lib/DataWindow.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 "DataWindow.h"
00023 #include <qlayout.h>
00024 
00025 DataWindow::DataWindow (QWidget *w) : QDialog (w, "DataWindow", FALSE, WDestructiveClose)
00026 {
00027   resize(750, 550);
00028 
00029   QVBoxLayout *vbox = new QVBoxLayout (this);
00030   vbox->setSpacing(5);
00031   vbox->setMargin(5);
00032 
00033   table = new QTable(this);
00034   table->setSelectionMode(QTable::Single);
00035   table->setReadOnly(TRUE);
00036   hHeader = table->horizontalHeader();
00037   vbox->addWidget (table);
00038 }
00039 
00040 DataWindow::~DataWindow ()
00041 {
00042 }
00043 
00044 void DataWindow::setData (int row, int col, QString &data)
00045 {
00046   if (row > table->numRows() -1)
00047     table->setNumRows(row + 1);
00048   table->setText(row, col, data);
00049 }
00050 
00051 void DataWindow::setHeader (int col, QString &d)
00052 {
00053   if (col >= table->numCols())
00054     table->setNumCols(table->numCols() + 1);
00055   table->setColumnWidth(col, 80);
00056   hHeader->setLabel(col, d);
00057 }
00058 
00059 void DataWindow::setBars (BarData *d)
00060 {
00061   if (! d->count())
00062     return;
00063     
00064   table->setNumCols(6);
00065   table->setNumRows(d->count());
00066   
00067   hHeader->setLabel(0, tr("Date"));
00068   hHeader->setLabel(1, tr("Time"));
00069   hHeader->setLabel(2, tr("Open"));
00070   hHeader->setLabel(3, tr("High"));
00071   hHeader->setLabel(4, tr("Low"));
00072   hHeader->setLabel(5, tr("Close"));
00073   
00074   int loop;
00075   for (loop = 0; loop < (int) d->count(); loop++)
00076   {
00077     QDateTime dt;
00078     d->getDate(loop, dt);
00079     QString s = dt.toString("yyyy-MM-dd");
00080     table->setText(loop, 0, s);
00081     s = dt.toString("hh:mm:ss");
00082     table->setText(loop, 1, s);
00083     table->setText(loop, 2, strip(d->getOpen(loop), 4));
00084     table->setText(loop, 3, strip(d->getHigh(loop), 4));
00085     table->setText(loop, 4, strip(d->getLow(loop), 4));
00086     table->setText(loop, 5, strip(d->getClose(loop), 4));
00087   }
00088   
00089   for (loop = 0; loop < table->numCols(); loop++)
00090     table->adjustColumn(loop);
00091 }
00092 
00093 void DataWindow::setPlot (Plot *d)
00094 {
00095   Indicator *i = d->getIndicator();
00096   int loop2;
00097   QString s;
00098   for (loop2 = 0; loop2 < i->getLines(); loop2++)
00099   {
00100     table->setNumCols(table->numCols() + 1);
00101       
00102     PlotLine *line = i->getLine(loop2);
00103     line->getLabel(s);
00104     hHeader->setLabel(table->numCols() - 1, s);
00105 
00106     int loop3;
00107     int offset = table->numRows() - line->getSize();
00108     for (loop3 = 0; loop3 < line->getSize(); loop3++)
00109       table->setText(loop3 + offset, table->numCols() - 1, strip(line->getData(loop3), 4));
00110     table->adjustColumn(table->numCols() - 1);
00111   }
00112 }
00113 
00114 QString DataWindow::strip (double d, int p)
00115 {
00116   QString s = QString::number(d, 'f', p);
00117 
00118   while (1)
00119   {
00120     if (s.find('.', -1, TRUE) != -1)
00121     {
00122       s.truncate(s.length() - 1);
00123       break;
00124     }
00125     else
00126     {
00127       if (s.find('0', -1, TRUE) != -1)
00128         s.truncate(s.length() - 1);
00129       else
00130         break;
00131     }
00132   }
00133 
00134   return s;
00135 }
00136 
00137