src/ChartPage.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 "ChartPage.h"
00023 #include "Setting.h"
00024 #include "SymbolDialog.h"
00025 #include "DbPlugin.h"
00026 #include "HelpWindow.h"
00027 #include "DBIndex.h"
00028 #include "../pics/edit.xpm"
00029 #include "../pics/delete.xpm"
00030 #include "../pics/export.xpm"
00031 #include "../pics/newchart.xpm"
00032 #include "../pics/help.xpm"
00033 #include "../pics/addgroup.xpm"
00034 #include <qmessagebox.h>
00035 #include <qcursor.h>
00036 #include <qtooltip.h>
00037 #include <qlayout.h>
00038 #include <qaccel.h>
00039 
00040 
00041 ChartPage::ChartPage (QWidget *w, DBIndex *i) : QWidget (w)
00042 {
00043   chartIndex = i;
00044 
00045   QVBoxLayout *vbox = new QVBoxLayout(this);
00046   vbox->setMargin(0);
00047   vbox->setSpacing(5);
00048  
00049   search = new QLineEdit(this);
00050   search->setText("*");
00051   connect(search, SIGNAL(textChanged(const QString &)), this, SLOT(searchChanged(const QString &)));
00052   QToolTip::add(search, tr("List Filter, e.g. s* or sb*"));
00053   vbox->addWidget(search);
00054 
00055   QString s;
00056   rcfile.loadData(RcFile::DataPath, s);
00057   nav = new Navigator(this, s);
00058   connect(nav, SIGNAL(fileOpened(QString)), this, SLOT(chartOpened(QString)));
00059   connect(nav, SIGNAL(contextMenuRequested(QListBoxItem *, const QPoint &)), this,
00060           SLOT(rightClick(QListBoxItem *)));
00061   nav->updateList();
00062   vbox->addWidget(nav);
00063 
00064   newMenu = new QPopupMenu(this);
00065   int id = newMenu->insertItem(QPixmap(newchart), "&CC", this, SLOT(newChart(int)));
00066   newMenu->setItemParameter(id, id);
00067   id = newMenu->insertItem(QPixmap(newchart), "&Index", this, SLOT(newChart(int)));
00068   newMenu->setItemParameter(id, id);
00069   id = newMenu->insertItem(QPixmap(newchart), "&Spread", this, SLOT(newChart(int)));
00070   newMenu->setItemParameter(id, id);
00071   
00072   menu = new QPopupMenu(this);
00073   menu->insertItem(QPixmap(newchart), tr("&New..."), newMenu);
00074   menu->insertItem(QPixmap(edit), tr("&Edit Chart       Ctrl+E"), this, SLOT(editChart()));
00075   menu->insertItem(QPixmap(deleteitem), tr("&Delete Chart       Ctrl+D"), this, SLOT(deleteChart()));
00076   menu->insertItem(QPixmap(exportfile), tr("E&xport Chart CSV   Ctrl+X"), this, SLOT(exportSymbol()));
00077   menu->insertItem(QPixmap(exportfile), tr("D&ump Chart         Ctrl+U"), this, SLOT(dumpSymbol()));
00078   menu->insertItem(QPixmap(addgroup), tr("Add To &Group Ctrl+G"), this, SLOT(addToGroup()));
00079   menu->insertSeparator(-1);
00080   menu->insertItem(QPixmap(help), tr("&Help     Ctrl+H"), this, SLOT(slotHelp()));
00081 
00082   QAccel *a = new QAccel(this);
00083   connect(a, SIGNAL(activated(int)), this, SLOT(slotAccel(int)));
00084   a->insertItem(CTRL+Key_D, DeleteChart);
00085   a->insertItem(CTRL+Key_E, EditChart);
00086   a->insertItem(CTRL+Key_X, ExportSymbol);
00087   a->insertItem(CTRL+Key_U, DumpSymbol);
00088   a->insertItem(CTRL+Key_H, Help);
00089   a->insertItem(CTRL+Key_Tab, Tab);
00090   a->insertItem(Key_Delete, DeleteChartQuick);
00091   a->insertItem(CTRL+Key_G, AddToGroup);
00092   
00093   rcfile.loadData(RcFile::LastChartDir, s);
00094   if (!s.isEmpty()) 
00095     nav->setDirectory(s);
00096   else 
00097     nav->updateList();
00098 }
00099 
00100 ChartPage::~ChartPage ()
00101 {
00102   QString s;
00103   nav->getCurrentPath(s);
00104   rcfile.saveData(RcFile::LastChartDir,s);
00105 }
00106 
00107 void ChartPage::deleteChart ()
00108 {
00109   QString s("*");
00110   QString s2, bp;
00111   nav->getCurrentPath(s2);
00112   rcfile.loadData(RcFile::DataPath, bp);
00113   SymbolDialog *dialog = new SymbolDialog(this, 
00114                                           bp,
00115                                           s2,
00116                                           s,
00117                                           QFileDialog::ExistingFiles);
00118   dialog->setCaption(tr("Select Charts To Delete"));
00119 
00120   int rc = dialog->exec();
00121 
00122   if (rc == QDialog::Accepted)
00123   {
00124     rc = QMessageBox::warning(this,
00125                               tr("Qtstalker: Warning"),
00126                               tr("Are you sure you want to delete selected charts?"),
00127                               QMessageBox::Yes,
00128                               QMessageBox::No,
00129                               QMessageBox::NoButton);
00130 
00131     if (rc == QMessageBox::No)
00132     {
00133       delete dialog;
00134       return;
00135     }
00136 
00137     QStringList l = dialog->selectedFiles();
00138     int loop;
00139     QDir dir;
00140     for (loop = 0; loop < (int) l.count(); loop++)
00141     {
00142       dir.remove(l[loop], TRUE);
00143       chartIndex->deleteChart(l[loop]);
00144     }
00145       
00146     nav->updateList();
00147 
00148     if (l.count())
00149       emit removeRecentCharts(l);
00150   }
00151 
00152   delete dialog;
00153 }
00154 
00155 void ChartPage::editChart ()
00156 {
00157   QString symbol;
00158   nav->getFileSelection(symbol);
00159   if (! symbol.length())
00160     return;
00161 
00162   QFileInfo fi(symbol);
00163   if (! fi.isFile())
00164     return;
00165 
00166   editChart(symbol);
00167 }
00168 
00169 void ChartPage::editChart (QString symbol)
00170 {
00171   if (! symbol.length())
00172     return;
00173 
00174   QFileInfo fi(symbol);
00175   if (! fi.isFile())
00176     return;
00177   QString fn = fi.fileName();
00178 
00179   DbPlugin db;
00180   db.open(symbol, chartIndex);    
00181   int rc = db.dbPrefDialog();
00182   db.close();
00183   if (rc)
00184     emit signalReloadChart();
00185 }
00186 
00187 void ChartPage::exportSymbol ()
00188 {
00189   QString s("*");
00190   QString s2, bp;
00191   nav->getCurrentPath(s2);
00192   rcfile.loadData(RcFile::DataPath, bp);
00193   SymbolDialog *dialog = new SymbolDialog(this,
00194                                           bp,
00195                                           s2,
00196                                           s,
00197                                           QFileDialog::ExistingFiles);
00198   dialog->setCaption(tr("Select Charts"));
00199 
00200   int rc = dialog->exec();
00201 
00202   if (rc == QDialog::Accepted)
00203   {
00204     QString s;
00205     rcfile.loadData(RcFile::Home, s);
00206     s.append("/export");
00207     QDir dir(s);
00208     if (! dir.exists(s, TRUE))
00209     {
00210       if (! dir.mkdir(s, TRUE))
00211       {
00212         qDebug("Unable to create export directory.");
00213         delete dialog;
00214         return;
00215       }
00216     }
00217 
00218     QStringList l = dialog->selectedFiles();
00219     int loop;
00220     for (loop = 0; loop < (int) l.count(); loop++)
00221       exportChart(l[loop], TRUE);
00222   }
00223 
00224   delete dialog;
00225 }
00226 
00227 void ChartPage::dumpSymbol ()
00228 {
00229   QString s("*");
00230   QString s2, bp;
00231   nav->getCurrentPath(s2);
00232   rcfile.loadData(RcFile::DataPath, bp);
00233   SymbolDialog *dialog = new SymbolDialog(this,
00234                                           bp,
00235                                           s2,
00236                                           s,
00237                                           QFileDialog::ExistingFiles);
00238   dialog->setCaption(tr("Select Charts"));
00239 
00240   int rc = dialog->exec();
00241 
00242   if (rc == QDialog::Accepted)
00243   {
00244     rcfile.loadData(RcFile::Home, s);
00245     s.append("/export");
00246     QDir dir(s);
00247     if (! dir.exists(s, TRUE))
00248     {
00249       if (! dir.mkdir(s, TRUE))
00250       {
00251         qDebug("Unable to create export directory.");
00252         delete dialog;
00253         return;
00254       }
00255     }
00256 
00257     QStringList l = dialog->selectedFiles();
00258     int loop;
00259     for (loop = 0; loop < (int) l.count(); loop++)
00260       exportChart(l[loop], FALSE);
00261   }
00262 
00263   delete dialog;
00264 }
00265 
00266 void ChartPage::exportChart (QString &path, bool f)
00267 {
00268   DbPlugin db;
00269   db.open(path, chartIndex);
00270 
00271   QString s;
00272   rcfile.loadData(RcFile::Home, s);
00273   s.append("/export/");
00274   
00275   QString s2;
00276   QFileInfo fi(path);
00277   s2 = fi.fileName();
00278   if (! s2.length())
00279   {
00280     QStringList l = QStringList::split("/", path, FALSE);
00281     s.append(l[l.count() - 1]);
00282   }
00283   else
00284     s.append(s2);
00285 
00286   db.dump(s, f);
00287 
00288   db.close();
00289 }
00290 
00291 void ChartPage::chartOpened (QString d)
00292 {
00293   emit fileSelected(d);
00294   emit addRecentChart(d);
00295 }
00296 
00297 void ChartPage::rightClick (QListBoxItem *)
00298 {
00299   menu->exec(QCursor::pos());
00300 }
00301 
00302 void ChartPage::refreshList ()
00303 {
00304   nav->updateList();
00305 }
00306 
00307 void ChartPage::searchChanged (const QString &d)
00308 {
00309   QString s(d);
00310   nav->setFilter(s);
00311 }
00312 
00313 void ChartPage::newChart (int id)
00314 {
00315   QString type = newMenu->text(id);
00316   type.remove(type.find("&", 0, TRUE), 1);
00317 
00318   DbPlugin db;
00319   QString s;
00320   if (! type.compare("CC"))
00321     db.createNewCC(chartIndex);
00322   else
00323   {
00324     if (! type.compare("Index"))
00325       db.createNewIndex(chartIndex);
00326     else
00327       db.createNewSpread(chartIndex);
00328   }
00329   db.close();
00330   
00331   refreshList();
00332 }
00333 
00334 void ChartPage::slotHelp ()
00335 {
00336   QString s = "workwithcharts.html";
00337   HelpWindow *hw = new HelpWindow(this, s);
00338   hw->show();
00339 }
00340 
00341 void ChartPage::setFocus ()
00342 {
00343   nav->setFocus();
00344 }
00345 
00346 void ChartPage::deleteChartQuick ()
00347 {
00348   if (! nav->isSelected())
00349     return;
00350 
00351   QString s;
00352   nav->getFileSelection(s);
00353   if (! s.length())
00354     return;
00355 
00356   int rc = QMessageBox::warning(this,
00357                             tr("Qtstalker: Warning"),
00358                             tr("Are you sure you want to delete selected chart?"),
00359                             QMessageBox::Yes,
00360                             QMessageBox::No,
00361                             QMessageBox::NoButton);
00362 
00363   if (rc == QMessageBox::No)
00364     return;
00365 
00366   QDir dir;
00367   dir.remove(s, TRUE);
00368   chartIndex->deleteChart(s);
00369   nav->updateList();
00370 }
00371 
00372 void ChartPage::addToGroup ()
00373 {
00374   QString symbol;
00375   nav->getFileSelection(symbol);
00376   if (! symbol.length())
00377     return;
00378 
00379   QFileInfo fi(symbol);
00380   if (! fi.isFile())
00381     return;
00382 
00383   emit signalAddToGroup(symbol);
00384 }
00385 
00386 void ChartPage::doKeyPress (QKeyEvent *key)
00387 {
00388   key->accept();
00389   
00390   if (key->state() == Qt::ControlButton)
00391   {
00392     switch(key->key())
00393     {
00394       case Qt::Key_D:
00395         slotAccel(DeleteChart);
00396         break;
00397       case Qt::Key_E:
00398         slotAccel(EditChart);
00399         break;
00400       case Qt::Key_X:
00401         slotAccel(ExportSymbol);
00402         break;
00403       case Qt::Key_U:
00404         slotAccel(DumpSymbol);
00405         break;
00406       case Qt::Key_Tab:
00407         slotAccel(Tab);
00408         break;
00409       case Qt::Key_G:
00410         slotAccel(AddToGroup);
00411         break;
00412       default:
00413         break;
00414     }
00415   }
00416   else
00417   {
00418     switch(key->key())
00419     {
00420       case Qt::Key_Delete:
00421         deleteChartQuick();
00422         break;
00423       default:
00424         if (! search->hasFocus())
00425           nav->doKeyPress(key);
00426         break;
00427     }
00428   }
00429 }
00430 
00431 void ChartPage::slotAccel (int id)
00432 {
00433   switch (id)
00434   {
00435     case DeleteChartQuick:
00436       deleteChartQuick();
00437       break;  
00438     case DeleteChart:
00439       deleteChart();
00440       break;  
00441     case EditChart:
00442       editChart();
00443       break;  
00444     case ExportSymbol:
00445       exportSymbol();
00446       break;  
00447     case DumpSymbol:
00448       dumpSymbol();
00449       break;  
00450     case Help:
00451       slotHelp();
00452       break;  
00453     case Tab:
00454       if (search->hasFocus())
00455         nav->setFocus();
00456       else
00457         search->setFocus();
00458       break;  
00459     case AddToGroup:
00460       addToGroup();
00461       break;  
00462     default:
00463       break;
00464   }
00465 }
00466 
00467 void ChartPage::setChartNavItem (QString chartDir, QString chartName)
00468 {
00469   nav->setDirectory(chartDir);
00470   search->setText("*"); // perhaps they have recently used the filter, so show all
00471   nav->setNavItem(chartName);
00472 }