src/TestPage.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 "TestPage.h"
00023 #include "HelpWindow.h"
00024 #include "../pics/help.xpm"
00025 #include "../pics/open.xpm"
00026 #include "../pics/newchart.xpm"
00027 #include "../pics/delete.xpm"
00028 #include "../pics/rename.xpm"
00029 #include "../pics/copy.xpm"
00030 #include "SymbolDialog.h"
00031 #include <qinputdialog.h>
00032 #include <qmessagebox.h>
00033 #include <qcursor.h>
00034 #include <stdlib.h>
00035 #include <qaccel.h>
00036 #include <qlayout.h>
00037 #include <qtooltip.h>
00038 
00039 TestPage::TestPage (QWidget *w, MainMenubar *mb, DBIndex *i) : QWidget (w)
00040 {
00041   chartIndex = i;
00042   menubar = mb;
00043   idir.setFilter(QDir::Dirs);
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   list = new QListBox(this);
00056   connect(list, SIGNAL(contextMenuRequested(QListBoxItem *, const QPoint &)), this,
00057           SLOT(rightClick(QListBoxItem *)));
00058   connect(list, SIGNAL(highlighted(const QString &)), this, SLOT(testSelected(const QString &)));
00059   connect(list, SIGNAL(doubleClicked(QListBoxItem *)), this, SLOT(doubleClick(QListBoxItem *)));
00060   vbox->addWidget(list);
00061   
00062   menu = new QPopupMenu(this);
00063   menu->insertItem(QPixmap(newchart), tr("&New Rule             Ctrl+N"), this, SLOT(newTest()));
00064   menu->insertItem(QPixmap(open), tr("&Open Rule                Ctrl+O"), this, SLOT(openTest()));
00065   menu->insertItem(QPixmap(deleteitem), tr("&Delete Rule        Ctrl+D"), this, SLOT(deleteTest()));
00066   menu->insertItem(QPixmap(renam), tr("&Rename Rule             Ctrl+R"), this, SLOT(renameTest()));
00067   menu->insertItem(QPixmap(copypic), tr("&Copy Rule             Ctrl+Y"), this, SLOT(copyTest()));
00068   menu->insertSeparator(-1);
00069   menu->insertItem(QPixmap(help), tr("&Help             Ctrl+H"), this, SLOT(slotHelp()));
00070 
00071   QAccel *a = new QAccel(this);
00072   connect(a, SIGNAL(activated(int)), this, SLOT(slotAccel(int)));
00073   a->insertItem(CTRL+Key_N, NewTest);
00074   a->insertItem(CTRL+Key_O, OpenTest);
00075   a->insertItem(CTRL+Key_D, DeleteTest);
00076   a->insertItem(CTRL+Key_R, RenameTest);
00077   a->insertItem(CTRL+Key_Y, CopyTest);
00078   a->insertItem(CTRL+Key_H, Help);
00079   
00080   updateList();
00081   testNoSelection();
00082 }
00083 
00084 TestPage::~TestPage ()
00085 {
00086 }
00087 
00088 void TestPage::openTest ()
00089 {
00090   Tester *dialog = new Tester(list->currentText(), chartIndex);
00091 //  connect(menubar, SIGNAL(signalScale(bool)), dialog, SLOT(slotScaleToScreen(bool)));
00092   dialog->show();
00093 }
00094 
00095 void TestPage::newTest()
00096 {
00097   Tester *dialog = new Tester;
00098   QString name = dialog->newTest();
00099   delete dialog;
00100   
00101   if (! name.length())
00102     return;
00103   
00104   updateList();
00105   
00106   dialog = new Tester(name, chartIndex);
00107 //  connect(menubar, SIGNAL(signalScale(bool)), dialog, SLOT(slotScaleToScreen(bool)));
00108   dialog->show();
00109 }
00110 
00111 void TestPage::deleteTest()
00112 {
00113   QString s("*");
00114   QString s2;
00115   config.getData(Config::TestPath, s2);
00116   SymbolDialog *dialog = new SymbolDialog(this,
00117                                           s2,
00118                                           s2,
00119                                           s,
00120                                           QFileDialog::DirectoryOnly);
00121   dialog->setCaption(tr("Select Backtest rule To Delete"));
00122 
00123   int rc = dialog->exec();
00124 
00125   if (rc == QDialog::Accepted)
00126   {
00127     rc = QMessageBox::warning(this,
00128                               tr("Qtstalker: Warning"),
00129                               tr("Are you sure you want to delete backtest rule?"),
00130                               QMessageBox::Yes,
00131                               QMessageBox::No,
00132                               QMessageBox::NoButton);
00133 
00134     if (rc == QMessageBox::No)
00135     {
00136       delete dialog;
00137       return;
00138     }
00139 
00140     s = "rm -r " + dialog->selectedFile();
00141     
00142     if (system(s.latin1()) == -1)
00143       qDebug("TestPage::deleteTest:command failed");
00144     
00145     updateList();
00146 
00147     testNoSelection();
00148   }
00149 
00150   delete dialog;
00151 }
00152 
00153 void TestPage::renameTest ()
00154 {
00155   if (list->currentItem() == -1)
00156     return;
00157 
00158   bool ok;
00159   QString s = QInputDialog::getText(tr("Rename Backtest Rule"),
00160                                     tr("Enter new backtest rule name."),
00161                                     QLineEdit::Normal,
00162                                     list->currentText(),
00163                                     &ok,
00164                                     this);
00165 
00166   if ((! ok) || (s.isNull()))
00167     return;
00168 
00169   int loop;
00170   QString selection;
00171   for (loop = 0; loop < (int) s.length(); loop++)
00172   {
00173     QChar c = s.at(loop);
00174     if (c.isLetterOrNumber())
00175       selection.append(c);
00176   }
00177     
00178   config.getData(Config::TestPath, s);
00179   s.append("/" + selection);
00180   QDir dir(s);
00181   if (dir.exists(s, TRUE))
00182   {
00183     QMessageBox::information(this, tr("Qtstalker: Error"), tr("This backtest rule already exists."));
00184     return;
00185   }
00186 
00187   QString s2;
00188   config.getData(Config::TestPath, s2);
00189   s2.append("/" + list->currentText());
00190   dir.rename(s2, s, TRUE);
00191   
00192   list->changeItem(selection, list->currentItem());
00193 }
00194 
00195 void TestPage::copyTest ()
00196 {
00197   if (list->currentItem() == -1)
00198     return;
00199 
00200   bool ok;
00201   QString s = QInputDialog::getText(tr("Copy Backtest Rule"),
00202                                     tr("Enter new name of copy."),
00203                                     QLineEdit::Normal,
00204                                     list->currentText(),
00205                                     &ok,
00206                                     this);
00207 
00208   if ((! ok) || (s.isNull()))
00209     return;
00210 
00211   int loop;
00212   QString selection;
00213   for (loop = 0; loop < (int) s.length(); loop++)
00214   {
00215     QChar c = s.at(loop);
00216     if (c.isLetterOrNumber())
00217       selection.append(c);
00218   }
00219     
00220   config.getData(Config::TestPath, s);
00221   s.append("/" + selection);
00222   QDir dir(s);
00223   if (dir.exists(s, TRUE))
00224   {
00225     QMessageBox::information(this, tr("Qtstalker: Error"), tr("This backtest rule already exists."));
00226     return;
00227   }
00228 
00229   QString ts;
00230   config.getData(Config::TestPath, ts);
00231   s = "cp -R " + ts + "/" + list->currentText() + " " + ts + "/" + selection;
00232 
00233   if (system(s.latin1()) == -1)
00234     qDebug("TestPage::copyTest:command failed");
00235     
00236   updateList();
00237   testNoSelection();
00238 }
00239 
00240 void TestPage::testSelected (const QString &) 
00241 {
00242   menu->setItemEnabled(menu->idAt(1), TRUE);
00243   menu->setItemEnabled(menu->idAt(2), TRUE);
00244   menu->setItemEnabled(menu->idAt(3), TRUE);
00245   menu->setItemEnabled(menu->idAt(4), TRUE);
00246 }
00247 
00248 void TestPage::testNoSelection ()
00249 {
00250   menu->setItemEnabled(menu->idAt(1), FALSE);
00251   menu->setItemEnabled(menu->idAt(2), FALSE);
00252   menu->setItemEnabled(menu->idAt(3), FALSE);
00253   menu->setItemEnabled(menu->idAt(4), FALSE);
00254 }
00255 
00256 void TestPage::rightClick (QListBoxItem *)
00257 {
00258   menu->exec(QCursor::pos());
00259 }
00260 
00261 void TestPage::updateList ()
00262 {
00263   list->clear();
00264 
00265   QString s;
00266   config.getData(Config::TestPath, s);
00267   idir.setPath(s);
00268   int loop;
00269   for (loop = 0; loop < (int) idir.count(); loop++)
00270   {
00271     if (! idir[loop].compare(".") || ! idir[loop].compare(".."))
00272       ;
00273     else
00274       list->insertItem(idir[loop], -1);
00275   }
00276 }
00277 
00278 void TestPage::slotMessage (QString d)
00279 {
00280   emit message(d);
00281 }
00282 
00283 void TestPage::doubleClick (QListBoxItem *item)
00284 {
00285   if (! item)
00286     return;
00287     
00288   Tester *dialog = new Tester(item->text(), chartIndex);
00289   dialog->show();
00290 }
00291 
00292 void TestPage::slotHelp ()
00293 {
00294   QString s = "workwithbacktest.html";
00295   HelpWindow *hw = new HelpWindow(this, s);
00296   hw->show();
00297 }
00298 
00299 void TestPage::keyPressEvent (QKeyEvent *key)
00300 {
00301   doKeyPress(key);
00302 }
00303 
00304 void TestPage::doKeyPress (QKeyEvent *key)
00305 {
00306   key->accept();
00307   
00308   if (key->state() == Qt::ControlButton)
00309   {
00310     switch(key->key())
00311     {
00312       case Qt::Key_N:
00313         slotAccel(NewTest);
00314         break;
00315       case Qt::Key_D:
00316         slotAccel(DeleteTest);
00317         break;
00318       case Qt::Key_O:
00319         slotAccel(OpenTest);
00320         break;
00321       case Qt::Key_R:
00322         slotAccel(RenameTest);
00323         break;
00324       case Qt::Key_Y:
00325         slotAccel(CopyTest);
00326         break;
00327       default:
00328         break;
00329     }
00330   }
00331   else
00332   {
00333     switch (key->key())
00334     {
00335       case Qt::Key_Delete:
00336         deleteTest();
00337         break;
00338       case Qt::Key_Left: // segfaults if we dont trap this
00339       case Qt::Key_Right: // segfaults if we dont trap this
00340         break;      
00341       case Qt::Key_Enter:
00342       case Qt::Key_Return:
00343         openTest();
00344         break;
00345       default:
00346 //        QListBox::keyPressEvent(key);
00347         break;
00348     }
00349   }
00350 }
00351 
00352 void TestPage::slotAccel (int id)
00353 {
00354   switch (id)
00355   {
00356     case NewTest:
00357       newTest();
00358       break;  
00359     case DeleteTest:
00360       deleteTest();
00361       break;  
00362     case RenameTest:
00363       renameTest();
00364       break;  
00365     case OpenTest:
00366       openTest();
00367       break;  
00368     case CopyTest:
00369       copyTest();
00370       break;  
00371     case Help:
00372       slotHelp();
00373       break;  
00374     default:
00375       break;
00376   }
00377 }
00378 
00379 void TestPage::searchChanged (const QString &d)
00380 {
00381   idir.setNameFilter(d);
00382   updateList();
00383 }
00384