lib/FuturesDialog.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 "FuturesDialog.h"
00023 #include "Bar.h"
00024 #include "HelpWindow.h"
00025 #include "DBIndexItem.h"
00026 #include "Exchange.h"
00027 #include <qlabel.h>
00028 #include <qlayout.h>
00029 #include <qmessagebox.h>
00030 #include <qfileinfo.h>
00031 
00032 FuturesDialog::FuturesDialog (QString p, DbPlugin *d, DBIndex *i) : QTabDialog (0, "FuturesDialog", TRUE)
00033 {
00034   helpFile = p;
00035   db = d;
00036   index = i;
00037   reloadFlag = FALSE;
00038   currentDate = QDateTime::currentDateTime();
00039 
00040   setCaption(tr("Qtstalker: Edit Futures"));
00041 
00042   QString s;
00043   d->getSymbol(s);
00044   QFileInfo fi(s);
00045   symbol = fi.fileName();
00046   
00047   createDetailsPage();
00048   createDataPage();
00049   
00050   setOkButton(tr("&OK"));
00051   setCancelButton(tr("&Cancel"));
00052   connect(this, SIGNAL(applyButtonPressed()), this, SLOT(saveChart()));
00053   setHelpButton();
00054   QObject::connect(this, SIGNAL(helpButtonPressed()), this, SLOT(help()));
00055 }
00056 
00057 FuturesDialog::~FuturesDialog ()
00058 {
00059 }
00060 
00061 void FuturesDialog::createDetailsPage ()
00062 {
00063   QWidget *w = new QWidget(this);
00064   
00065   QVBoxLayout *vbox = new QVBoxLayout(w);
00066   vbox->setMargin(5);
00067   vbox->setSpacing(0);
00068     
00069   QGridLayout *grid = new QGridLayout(vbox);
00070   grid->setSpacing(5);
00071   
00072   QLabel *label = new QLabel(tr("Symbol"), w);
00073   grid->addWidget(label, 0, 0);
00074 
00075   QString s;
00076   DBIndexItem item;
00077   index->getIndexItem(symbol, item);
00078   item.getSymbol(s);
00079   label = new QLabel(s, w);
00080   label->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00081   grid->addWidget(label, 0, 1);
00082   
00083   label = new QLabel(tr("Name"), w);
00084   grid->addWidget(label, 1, 0);
00085 
00086   item.getTitle(s);  
00087   title = new QLineEdit(s, w);
00088   grid->addWidget(title, 1, 1);
00089   
00090   label = new QLabel(tr("Exchange"), w);
00091   grid->addWidget(label, 2, 0);
00092 
00093   item.getExchange(s);
00094   Exchange ex;
00095   ex.getExchange(s.toInt(), s);
00096   label = new QLabel(s, w);
00097   label->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00098   grid->addWidget(label, 2, 1);
00099   
00100   label = new QLabel(tr("Type"), w);
00101   grid->addWidget(label, 3, 0);
00102 
00103   item.getType(s);  
00104   label = new QLabel(s, w);
00105   label->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00106   grid->addWidget(label, 3, 1);
00107   
00108   label = new QLabel(tr("Futures Type"), w);
00109   grid->addWidget(label, 4, 0);
00110   
00111   QString s2;
00112   item.getFuturesType(s2);
00113   label = new QLabel(s2, w);
00114   label->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00115   grid->addWidget(label, 4, 1);
00116 
00117   label = new QLabel(tr("Futures Month"), w);
00118   grid->addWidget(label, 5, 0);
00119 
00120   item.getFuturesMonth(s2);  
00121   label = new QLabel(s2, w);
00122   label->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00123   grid->addWidget(label, 5, 1);
00124   
00125   label = new QLabel(tr("First Date"), w);
00126   grid->addWidget(label, 6, 0);
00127   
00128   Bar bar;
00129   db->getFirstBar(bar);
00130   if (! bar.getEmptyFlag())
00131   {
00132     bar.getDateTimeString(TRUE, s);
00133     label = new QLabel(s, w);
00134     label->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00135     grid->addWidget(label, 6, 1);
00136   }
00137   
00138   label = new QLabel(tr("Last Date"), w);
00139   grid->addWidget(label, 7, 0);
00140   
00141 
00142   Bar bar2;
00143   db->getLastBar(bar2);
00144   if (! bar2.getEmptyFlag())
00145   {
00146     bar2.getDateTimeString(TRUE, s);
00147     label = new QLabel(s, w);
00148     label->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00149     grid->addWidget(label, 7, 1);
00150   }
00151   
00152   grid->setColStretch(1, 1);
00153   vbox->insertStretch(-1, 0);
00154   
00155   addTab(w, tr("Details"));  
00156 }
00157 
00158 void FuturesDialog::createDataPage ()
00159 {
00160   QWidget *w = new QWidget(this);
00161     
00162   QVBoxLayout *vbox = new QVBoxLayout(w);
00163   vbox->setMargin(5);
00164   vbox->setSpacing(0);
00165   
00166   barEdit = new BarEdit(w);
00167   QString s = tr("Open");  
00168   QString s2 = "Open";  
00169   barEdit->createField(s, s2, FALSE);
00170   s = tr("High");  
00171   s2 = "High";  
00172   barEdit->createField(s, s2, FALSE);
00173   s = tr("Low");  
00174   s2 = "Low";  
00175   barEdit->createField(s, s2, FALSE);
00176   s = tr("Close");  
00177   s2 = "Close";  
00178   barEdit->createField(s, s2, FALSE);
00179   s = tr("Volume");  
00180   s2 = "Volume";  
00181   barEdit->createField(s, s2, FALSE);
00182   s = tr("OI");  
00183   s2 = "OI";  
00184   barEdit->createField(s, s2, TRUE);
00185   connect(barEdit, SIGNAL(signalDeleteRecord()), this, SLOT(deleteRecord()));
00186   connect(barEdit, SIGNAL(signalSaveRecord()), this, SLOT(saveRecord()));
00187   connect(barEdit, SIGNAL(signalSearch(QDateTime)), this, SLOT(slotDateSearch(QDateTime)));
00188   connect(barEdit, SIGNAL(signalFirstRecord()), this, SLOT(slotFirstRecord()));
00189   connect(barEdit, SIGNAL(signalLastRecord()), this, SLOT(slotLastRecord()));
00190   connect(barEdit, SIGNAL(signalPrevRecord()), this, SLOT(slotPrevRecord()));
00191   connect(barEdit, SIGNAL(signalNextRecord()), this, SLOT(slotNextRecord()));
00192   vbox->addWidget(barEdit);
00193 
00194   addTab(w, tr("Data"));  
00195 }
00196 
00197 void FuturesDialog::deleteRecord ()
00198 {
00199   Bar bar;
00200   QString s;
00201   barEdit->getDate(s);
00202   if (bar.setDate(s))
00203     return;
00204   bar.getDateTimeString(FALSE, s);
00205   db->deleteData(s);
00206 
00207   reloadFlag = TRUE;
00208 }
00209 
00210 void FuturesDialog::saveRecord ()
00211 {
00212   Bar bar;
00213   QString s, s2;
00214   barEdit->getDate(s);
00215   bar.setDate(s);
00216 
00217   s = "Open";
00218   barEdit->getField(s, s2);
00219   bar.setOpen(s2.toDouble());
00220 
00221   s = "High";
00222   barEdit->getField(s, s2);
00223   bar.setHigh(s2.toDouble());
00224 
00225   s = "Low";
00226   barEdit->getField(s, s2);
00227   bar.setLow(s2.toDouble());
00228 
00229   s = "Close";
00230   barEdit->getField(s, s2);
00231   bar.setClose(s2.toDouble());
00232 
00233   s = "Volume";
00234   barEdit->getField(s, s2);
00235   bar.setVolume(s2.toDouble());
00236 
00237   s = "OI";
00238   barEdit->getField(s, s2);
00239   bar.setOI(s2.toInt());
00240 
00241   db->setBar(bar);
00242 
00243   reloadFlag = TRUE;
00244 }
00245 
00246 void FuturesDialog::slotDateSearch (QDateTime dt)
00247 {
00248   Bar record;
00249   db->getSearchBar(dt, record);
00250   if (record.getEmptyFlag())
00251     return;
00252   updateFields(record);
00253 }
00254 
00255 void FuturesDialog::saveChart ()
00256 {
00257   QString s = title->text();
00258   DBIndexItem item;
00259   index->getIndexItem(symbol, item);
00260   item.setTitle(s);
00261   index->setIndexItem(symbol, item);
00262 
00263   if (barEdit->getSaveFlag())
00264   {  
00265     int rc = QMessageBox::warning(this,
00266                                   tr("Warning"),
00267                                   tr("Record has been modified.\nSave changes?"),
00268                                   QMessageBox::Yes,
00269                                   QMessageBox::No,
00270                                   QMessageBox::NoButton);
00271 
00272     if (rc == QMessageBox::Yes)
00273       saveRecord();
00274   }
00275 
00276   reloadFlag = TRUE;
00277   
00278   accept();
00279 }
00280 
00281 void FuturesDialog::help ()
00282 {
00283   HelpWindow *hw = new HelpWindow(this, helpFile);
00284   hw->show();
00285 }
00286 
00287 void FuturesDialog::slotFirstRecord ()
00288 {
00289   Bar record;
00290   db->getFirstBar(record);
00291   if (record.getEmptyFlag())
00292     return;
00293   updateFields(record);
00294 }
00295 
00296 void FuturesDialog::slotLastRecord ()
00297 {
00298   Bar record;
00299   db->getLastBar(record);
00300   if (record.getEmptyFlag())
00301     return;
00302   updateFields(record);
00303 }
00304 
00305 void FuturesDialog::slotNextRecord ()
00306 {
00307   Bar record;
00308   db->getNextBar(currentDate, record);
00309   if (record.getEmptyFlag())
00310     return;
00311   updateFields(record);
00312 }
00313 
00314 void FuturesDialog::slotPrevRecord ()
00315 {
00316   Bar record;
00317   db->getPrevBar(currentDate, record);
00318   if (record.getEmptyFlag())
00319     return;
00320   updateFields(record);
00321 }
00322 
00323 void FuturesDialog::updateFields (Bar &record)
00324 {
00325   record.getDate(currentDate);
00326 
00327   QString s;
00328   record.getDateTimeString(TRUE, s);
00329   barEdit->setDate(s, record.getTickFlag());
00330   
00331   s = "Open";
00332   QString s2 = QString::number(record.getOpen());
00333   barEdit->setField(s, s2);
00334   
00335   s = "High";
00336   s2 = QString::number(record.getHigh());
00337   barEdit->setField(s, s2);
00338   
00339   s = "Low";
00340   s2 = QString::number(record.getLow());
00341   barEdit->setField(s, s2);
00342   
00343   s = "Close";
00344   s2 = QString::number(record.getClose());
00345   barEdit->setField(s, s2);
00346   
00347   s = "Volume";
00348   s2 = QString::number(record.getVolume(), 'f', 0);
00349   barEdit->setField(s, s2);
00350   
00351   s = "OI";
00352   s2 = QString::number(record.getOI(), 'f', 0);
00353   barEdit->setField(s, s2);
00354 
00355   barEdit->clearButtons();
00356 }
00357 
00358 bool FuturesDialog::getReloadFlag ()
00359 {
00360   return reloadFlag;
00361 }
00362