plugins/quote/CSV/CSVRuleDialog.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 "CSVRuleDialog.h"
00023 #include "../../../pics/insert.xpm"
00024 #include "../../../pics/delete.xpm"
00025 #include "Setting.h"
00026 #include "HelpWindow.h"
00027 #include <qdir.h>
00028 #include <qmessagebox.h>
00029 #include <qlayout.h>
00030 #include <qlabel.h>
00031 #include <qtooltip.h>
00032 #include <qinputdialog.h>
00033 #include <qdir.h>
00034 #include <qfile.h>
00035 #include <qtextstream.h>
00036 
00037 CSVRuleDialog::CSVRuleDialog (QWidget *p, QString &d) : QTabDialog (p, "CSVRuleDialog", TRUE)
00038 {
00039   helpFile = "csv.html";
00040   saveFlag = FALSE;
00041   rulePath = d;
00042   
00043   createRulePage();
00044   setOkButton();
00045   setApplyButton(tr("&Save"));  
00046   connect(this, SIGNAL(applyButtonPressed()), this, SLOT(saveRule()));
00047   setCancelButton();
00048   setHelpButton();
00049   QObject::connect(this, SIGNAL(helpButtonPressed()), this, SLOT(help()));
00050   
00051 //  resize(375, 375);
00052   
00053   QFileInfo fi(rulePath);
00054   ruleName->setText(fi.fileName());
00055   
00056   QDir dir;
00057   if (dir.exists(rulePath))
00058     loadRule();
00059 
00060   QString s = tr("Editing CSV Rule: ") + ruleName->text();  
00061   setCaption(s);
00062 }
00063 
00064 CSVRuleDialog::~CSVRuleDialog ()
00065 {
00066 }
00067 
00068 void CSVRuleDialog::createRulePage ()
00069 {
00070   QWidget *w = new QWidget(this);
00071   
00072   QVBoxLayout *vbox = new QVBoxLayout(w);
00073   vbox->setMargin(5);
00074   vbox->setSpacing(0);
00075   
00076   QGridLayout *grid = new QGridLayout(vbox, 5, 2);
00077   grid->setSpacing(5);
00078   grid->setColStretch(1, 1);
00079   
00080   QLabel *label = new QLabel(tr("Rule:"), w);
00081   grid->addWidget(label, 0, 0);
00082   
00083   ruleName = new QLineEdit(w);
00084   grid->addWidget(ruleName, 0, 1);
00085   ruleName->setReadOnly(TRUE);
00086   
00087   label = new QLabel(tr("Chart Type:"), w);
00088   grid->addWidget(label, 1, 0);
00089   
00090   type = new QComboBox(w);
00091   type->insertItem("Stocks", -1);
00092   type->insertItem("Futures", -1);
00093   connect(type, SIGNAL(activated(int)), this, SLOT(comboChanged(int)));
00094   grid->addWidget(type, 1, 1);
00095   
00096   label = new QLabel(tr("Delimiter:"), w);
00097   grid->addWidget(label, 2, 0);
00098   
00099   delimiter = new QComboBox(w);
00100   delimiter->insertItem(tr("Comma"), -1);
00101   delimiter->insertItem(tr("Tab"), -1);
00102   delimiter->insertItem(tr("Space"), -1);
00103   delimiter->insertItem(tr("Semicolon"), -1);
00104   connect(type, SIGNAL(activated(int)), this, SLOT(comboChanged(int)));
00105   grid->addWidget(delimiter, 2, 1);
00106   
00107   label = new QLabel(tr("Data Directory:"), w);
00108   grid->addWidget(label, 3, 0);
00109   
00110   directory = new QLineEdit(w);
00111   connect(directory, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
00112   grid->addWidget(directory, 3, 1);
00113 
00114   label = new QLabel(tr("Symbol Filter:"), w);
00115   grid->addWidget(label, 4, 0);
00116   
00117   symbolFilter = new QLineEdit(w);
00118   connect(symbolFilter, SIGNAL(textChanged(const QString &)), this, SLOT(textChanged(const QString &)));
00119   grid->addWidget(symbolFilter, 4, 1);
00120   
00121   vbox->addSpacing(10);
00122   
00123   QHBoxLayout *hbox = new QHBoxLayout(vbox);
00124   hbox->setSpacing(5);
00125   
00126   fieldList = new QListBox(w);
00127   fieldList->setSelectionMode(QListBox::Extended);
00128   fieldList->insertItem("Symbol", -1);
00129   fieldList->insertItem("Date:YYYYMMDD", -1);
00130   fieldList->insertItem("Date:YYMMDD", -1);
00131   fieldList->insertItem("Date:MMDDYY", -1);
00132   fieldList->insertItem("Date:MMDDYYYY", -1);
00133   fieldList->insertItem("Date:DDMMYYYY", -1);
00134   fieldList->insertItem("Date:MMDDYYYYHHMMSS", -1);
00135   fieldList->insertItem("Date:YYYYMMDDHHMMSS", -1);
00136   fieldList->insertItem("Time", -1);
00137   fieldList->insertItem("Open", -1);
00138   fieldList->insertItem("High", -1);
00139   fieldList->insertItem("Low", -1);
00140   fieldList->insertItem("Close", -1);
00141   fieldList->insertItem("Volume", -1);
00142   fieldList->insertItem("OI", -1);
00143   fieldList->insertItem("Ignore", -1);
00144   fieldList->insertItem("Name", -1);
00145   QObject::connect(fieldList, SIGNAL(highlighted(int)), this, SLOT(fieldListSelected(int)));
00146   hbox->addWidget(fieldList);
00147   
00148   ruleToolbar = new Toolbar(w, Toolbar::Horizontal);
00149   hbox->addWidget(ruleToolbar);
00150 
00151   QString s = "insert";
00152   QString s2 = tr("Insert Field");
00153   ruleToolbar->addButton(s, insert, s2);
00154   QObject::connect(ruleToolbar->getButton(s), SIGNAL(clicked()), this, SLOT(insertField()));
00155   ruleToolbar->setButtonStatus(s, FALSE);
00156   
00157   s = "deleteitem";
00158   s2 = tr("Delete Field");
00159   ruleToolbar->addButton(s, deleteitem, s2);
00160   QObject::connect(ruleToolbar->getButton(s), SIGNAL(clicked()), this, SLOT(deleteField()));
00161   ruleToolbar->setButtonStatus(s, FALSE);
00162   
00163   ruleList = new QListBox(w);
00164   QObject::connect(ruleList, SIGNAL(highlighted(int)), this, SLOT(ruleFieldSelected(int)));
00165   hbox->addWidget(ruleList);
00166   
00167   addTab(w, tr("Rule"));
00168 }
00169 
00170 void CSVRuleDialog::saveRule ()
00171 {
00172   if (! saveFlag)
00173     return;
00174     
00175   if (! directory->text().length())
00176   {
00177     QMessageBox::information(this, tr("Error"), tr("Must inlcude a directory."));
00178     return;
00179   }
00180 
00181   if (directory->text().contains(" "))
00182   {
00183     QMessageBox::information(this, tr("Error"), tr("No spaces allowed in directory name."));
00184     return;
00185   }
00186   
00187   if (! directory->text().right(1).compare("/") ||
00188       ! directory->text().left(1).compare("/"))
00189   {
00190     QMessageBox::information(this, tr("Error"), tr("Invalid directory name."));
00191     return;
00192   }
00193   
00194   QFile f(rulePath);
00195   if (! f.open(IO_WriteOnly))
00196   {
00197     QMessageBox::information(this, tr("Disk Error"), tr("Cannot save file."));
00198     return;
00199   }
00200   QTextStream stream(&f);
00201   
00202   stream << "Delimiter=" << delimiter->currentText() << "\n";
00203   stream << "Type=" << type->currentText() << "\n";
00204   stream << "Directory=" << directory->text() << "\n";
00205   stream << "SymbolFilter=" << symbolFilter->text() << "\n";
00206   
00207   int loop;
00208   QStringList l;
00209   for (loop = 0; loop < (int) ruleList->count(); loop++)
00210     l.append(ruleList->text(loop));
00211   stream << "Rule=" << l.join(",") << "\n";
00212 
00213   f.close();
00214   
00215   saveFlag = FALSE;
00216 }
00217 
00218 void CSVRuleDialog::loadRule ()
00219 {
00220   QFile f(rulePath);
00221   if (! f.open(IO_ReadOnly))
00222   {
00223     QMessageBox::information(this, tr("Disk Error"), tr("Cannot read file."));
00224     return;
00225   }
00226   QTextStream stream(&f);
00227 
00228   while(stream.atEnd() == 0)
00229   {
00230     QString s = stream.readLine();
00231     s = s.stripWhiteSpace();
00232     if (! s.length())
00233       continue;
00234       
00235     QStringList l = QStringList::split("=", s, FALSE);
00236     if (l.count() != 2)
00237       continue;
00238       
00239     if (! l[0].compare("Delimiter"))
00240     {
00241       delimiter->setCurrentText(l[1]);
00242       continue;
00243     }
00244   
00245     if (! l[0].compare("Type"))
00246     {
00247       type->setCurrentText(l[1]);
00248       continue;
00249     }
00250   
00251     if (! l[0].compare("Directory"))
00252     {
00253       directory->setText(l[1]);
00254       continue;
00255     }
00256   
00257     if (! l[0].compare("SymbolFilter"))
00258     {
00259       symbolFilter->setText(l[1]);
00260       continue;
00261     }
00262   
00263     if (! l[0].compare("Rule"))
00264     {
00265       QStringList l2 = QStringList::split(",", l[1], FALSE);
00266       ruleList->insertStringList(l2, -1);
00267     }
00268   }
00269   
00270   f.close();
00271 }
00272 
00273 void CSVRuleDialog::insertField ()
00274 {
00275   int loop;
00276   for (loop = 0; loop < (int) fieldList->count(); loop++)
00277   {
00278     if (fieldList->isSelected(loop))
00279     {
00280       ruleList->insertItem(fieldList->text(loop), ruleList->currentItem());
00281       saveFlag = TRUE;
00282     }
00283   }
00284 }
00285 
00286 void CSVRuleDialog::deleteField ()
00287 {
00288   if (ruleList->currentItem() != -1)
00289   {
00290     ruleList->removeItem(ruleList->currentItem());
00291     saveFlag = TRUE;
00292   }    
00293 }
00294 
00295 void CSVRuleDialog::ruleFieldSelected (int)
00296 {
00297   QString s = "deleteitem";
00298   if (ruleList->currentItem() != -1)
00299     ruleToolbar->setButtonStatus(s, TRUE);
00300   else
00301     ruleToolbar->setButtonStatus(s, FALSE);
00302 }
00303 
00304 void CSVRuleDialog::fieldListSelected (int d)
00305 {
00306   QString s = "insert";
00307   if (d != -1)
00308     ruleToolbar->setButtonStatus(s, TRUE);
00309   else
00310     ruleToolbar->setButtonStatus(s, FALSE);
00311 }
00312 
00313 void CSVRuleDialog::help ()
00314 {
00315   HelpWindow *hw = new HelpWindow(this, helpFile);
00316   hw->show();
00317 }
00318 
00319 void CSVRuleDialog::comboChanged (int)
00320 {
00321   saveFlag = TRUE;
00322 }
00323 
00324 void CSVRuleDialog::textChanged (const QString &)
00325 {
00326   saveFlag = TRUE;
00327 }
00328 
00329