src/PortfolioPage.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 "PortfolioPage.h"
00023 #include "PortfolioDialog.h"
00024 #include "SymbolDialog.h"
00025 #include "HelpWindow.h"
00026 #include "../pics/help.xpm"
00027 #include "../pics/open.xpm"
00028 #include "../pics/newchart.xpm"
00029 #include "../pics/delete.xpm"
00030 #include "../pics/rename.xpm"
00031 #include <qinputdialog.h>
00032 #include <qmessagebox.h>
00033 #include <qcursor.h>
00034 #include <qfile.h>
00035 #include <qaccel.h>
00036 
00037 PortfolioPage::PortfolioPage (QWidget *w, DBIndex *i) : QListBox (w)
00038 {
00039   chartIndex = i;
00040 
00041   connect(this, SIGNAL(contextMenuRequested(QListBoxItem *, const QPoint &)), this, SLOT(rightClick(QListBoxItem *)));
00042   connect(this, SIGNAL(highlighted(const QString &)), this, SLOT(portfolioSelected(const QString &)));
00043   connect(this, SIGNAL(doubleClicked(QListBoxItem *)), this, SLOT(doubleClick(QListBoxItem *)));
00044   
00045   menu = new QPopupMenu(this);
00046   menu->insertItem(QPixmap(newchart), tr("&New Portfolio        Ctrl+N"), this, SLOT(newPortfolio()));
00047   menu->insertItem(QPixmap(open), tr("&Open Portfolio           Ctrl+O"), this, SLOT(openPortfolio()));
00048   menu->insertItem(QPixmap(deleteitem), tr("&Delete Portfolio   Ctrl+D"), this, SLOT(deletePortfolio()));
00049   menu->insertItem(QPixmap(renam), tr("&Rename Portfolio        Ctrl+R"), this, SLOT(renamePortfolio()));
00050   menu->insertSeparator(-1);
00051   menu->insertItem(QPixmap(help), tr("&Help             Ctrl+H"), this, SLOT(slotHelp()));
00052 
00053   QAccel *a = new QAccel(this);
00054   connect(a, SIGNAL(activated(int)), this, SLOT(slotAccel(int)));
00055   a->insertItem(CTRL+Key_N, NewPortfolio);
00056   a->insertItem(CTRL+Key_O, OpenPortfolio);
00057   a->insertItem(CTRL+Key_D, DeletePortfolio);
00058   a->insertItem(CTRL+Key_R, RenamePortfolio);
00059   a->insertItem(CTRL+Key_H, Help);
00060   
00061   updateList();
00062   portfolioSelected(QString());
00063 }
00064 
00065 PortfolioPage::~PortfolioPage ()
00066 {
00067 }
00068 
00069 void PortfolioPage::openPortfolio ()
00070 {
00071   PortfolioDialog *dialog = new PortfolioDialog(currentText(), chartIndex);
00072   dialog->show();
00073 }
00074 
00075 void PortfolioPage::openPortfolio (QString d)
00076 {
00077   PortfolioDialog *dialog = new PortfolioDialog(d, chartIndex);
00078   dialog->show();
00079 }
00080 
00081 void PortfolioPage::newPortfolio()
00082 {
00083   bool ok;
00084   QString s = QInputDialog::getText(tr("New Portfolio"),
00085                                     tr("Enter new portfolio name."),
00086                                     QLineEdit::Normal,
00087                                     tr("NewPortfolio"),
00088                                     &ok,
00089                                     this);
00090   if ((ok) && (! s.isNull()))
00091   {
00092     int loop;
00093     QString selection;
00094     for (loop = 0; loop < (int) s.length(); loop++)
00095     {
00096       QChar c = s.at(loop);
00097       if (c.isLetterOrNumber())
00098         selection.append(c);
00099     }
00100   
00101     config.getData(Config::PortfolioPath, s);
00102     s.append("/" + selection);
00103     QDir dir(s);
00104     if (dir.exists(s, TRUE))
00105     {
00106       QMessageBox::information(this, tr("Qtstalker: Error"), tr("This portfolio already exists."));
00107       return;
00108     }
00109 
00110     // create the empty file    
00111     QFile f(s);
00112     if (! f.open(IO_WriteOnly))
00113       return;
00114     f.close();
00115     
00116     updateList();
00117 
00118     openPortfolio(selection);
00119   }
00120 }
00121 
00122 void PortfolioPage::deletePortfolio()
00123 {
00124   QString s("*");
00125   QString s2;
00126   config.getData(Config::PortfolioPath, s2);
00127   SymbolDialog *dialog = new SymbolDialog(this,
00128                                           s2,
00129                                           s2,
00130                                           s,
00131                                           QFileDialog::ExistingFiles);
00132   dialog->setCaption(tr("Select Portfolios To Delete"));
00133 
00134   int rc = dialog->exec();
00135 
00136   if (rc == QDialog::Accepted)
00137   {
00138     rc = QMessageBox::warning(this,
00139                               tr("Qtstalker: Warning"),
00140                               tr("Are you sure you want to delete this portfolio?"),
00141                               QMessageBox::Yes,
00142                               QMessageBox::No,
00143                               QMessageBox::NoButton);
00144 
00145     if (rc == QMessageBox::No)
00146     {
00147       delete dialog;
00148       return;
00149     }
00150 
00151     QStringList l = dialog->selectedFiles();
00152     int loop;
00153     QDir dir;
00154     for (loop = 0; loop < (int) l.count(); loop++)
00155       dir.remove(l[loop], TRUE);
00156 
00157     updateList();
00158     portfolioSelected(QString());
00159   }
00160 
00161   delete dialog;
00162 }
00163 
00164 void PortfolioPage::renamePortfolio ()
00165 {
00166   bool ok;
00167   QString s = QInputDialog::getText(tr("Rename Portfolio"),
00168                                     tr("Enter new portfolio name."),
00169                                     QLineEdit::Normal,
00170                                     currentText(),
00171                                     &ok,
00172                                     this);
00173   if ((ok) && (! s.isNull()))
00174   {
00175     int loop;
00176     QString selection;
00177     for (loop = 0; loop < (int) s.length(); loop++)
00178     {
00179       QChar c = s.at(loop);
00180       if (c.isLetterOrNumber())
00181         selection.append(c);
00182     }
00183   
00184     config.getData(Config::PortfolioPath, s);
00185     s.append("/" + selection);
00186     QDir dir(s);
00187     if (dir.exists(s, TRUE))
00188     {
00189       QMessageBox::information(this, tr("Qtstalker: Error"), tr("This portfolio already exists."));
00190       return;
00191     }
00192 
00193     QString s2;
00194     config.getData(Config::PortfolioPath, s2);
00195     s2.append("/" + currentText());
00196 
00197     dir.rename(s2, s, TRUE);
00198 
00199     updateList();
00200     portfolioSelected(QString());
00201   }
00202 }
00203 
00204 void PortfolioPage::portfolioSelected (const QString &d)
00205 {
00206   if (d.length())
00207   {
00208     menu->setItemEnabled(menu->idAt(1), TRUE);
00209     menu->setItemEnabled(menu->idAt(2), TRUE);
00210     menu->setItemEnabled(menu->idAt(3), TRUE);
00211   }
00212   else
00213   {
00214     menu->setItemEnabled(menu->idAt(1), FALSE);
00215     menu->setItemEnabled(menu->idAt(2), FALSE);
00216     menu->setItemEnabled(menu->idAt(3), FALSE);
00217   }
00218 }
00219 
00220 void PortfolioPage::rightClick (QListBoxItem *)
00221 {
00222   menu->exec(QCursor::pos());
00223 }
00224 
00225 void PortfolioPage::updateList ()
00226 {
00227   clear();
00228 
00229   QString s;
00230   config.getData(Config::PortfolioPath, s);
00231   QDir dir(s);
00232   int loop;
00233   for (loop = 2; loop < (int) dir.count(); loop++)
00234     insertItem(dir[loop], -1);
00235 }
00236 
00237 void PortfolioPage::doubleClick (QListBoxItem *item)
00238 {
00239   if (! item)
00240     return;
00241     
00242   openPortfolio(item->text());
00243 }
00244 
00245 void PortfolioPage::slotHelp ()
00246 {
00247   QString s = "workwithportfolios.html";
00248   HelpWindow *hw = new HelpWindow(this, s);
00249   hw->show();
00250 }
00251 
00252 void PortfolioPage::keyPressEvent (QKeyEvent *key)
00253 {
00254   doKeyPress(key);  
00255 }
00256 
00257 void PortfolioPage::doKeyPress (QKeyEvent *key)
00258 {
00259   key->accept();
00260   
00261   if (key->state() == Qt::ControlButton)
00262   {
00263     switch(key->key())
00264     {
00265       case Qt::Key_N:
00266         slotAccel(NewPortfolio);
00267         break;
00268       case Qt::Key_D:
00269         slotAccel(DeletePortfolio);
00270         break;
00271       case Qt::Key_O:
00272         slotAccel(OpenPortfolio);
00273         break;
00274       case Qt::Key_R:
00275         slotAccel(RenamePortfolio);
00276         break;
00277       default:
00278         break;
00279     }
00280   }
00281   else
00282   {
00283     switch (key->key())
00284     {
00285       case Qt::Key_Delete:
00286         deletePortfolio();
00287         break;
00288       case Qt::Key_Left: // segfaults if we dont trap this
00289       case Qt::Key_Right: // segfaults if we dont trap this
00290         break;      
00291       case Qt::Key_Enter:
00292       case Qt::Key_Return:
00293         openPortfolio();
00294         break;
00295       default:
00296         QListBox::keyPressEvent(key);
00297         break;
00298     }
00299   }
00300 }
00301 
00302 void PortfolioPage::slotAccel (int id)
00303 {
00304   switch (id)
00305   {
00306     case NewPortfolio:
00307       newPortfolio();
00308       break;  
00309     case DeletePortfolio:
00310       deletePortfolio();
00311       break;  
00312     case RenamePortfolio:
00313       renamePortfolio();
00314       break;  
00315     case OpenPortfolio:
00316       openPortfolio();
00317       break;  
00318     case Help:
00319       slotHelp();
00320       break;  
00321     default:
00322       break;
00323   }
00324 }
00325