lib/IndexDialog.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 "IndexDialog.h"
00023 #include "SymbolDialog.h"
00024 #include "PrefDialog.h"
00025 #include "HelpWindow.h"
00026 #include "SymbolButton.h"
00027 #include "Config.h"
00028 #include "DbPlugin.h"
00029 #include <qlayout.h>
00030 #include <qdir.h>
00031 #include <qlabel.h>
00032 #include "../pics/insert.xpm"
00033 #include "../pics/edit.xpm"
00034 #include "../pics/delete.xpm"
00035 
00036 
00037 IndexDialog::IndexDialog (QString &nam, QString &l) : QTabDialog (0, "IndexDialog", TRUE)
00038 {
00039   helpFile = "indexes.html";
00040 
00041   QWidget *w = new QWidget(this);
00042   
00043   QVBoxLayout *vbox = new QVBoxLayout(w);
00044   vbox->setMargin(5);
00045   vbox->setSpacing(5);
00046     
00047   QGridLayout *grid = new QGridLayout(vbox);
00048   grid->setColStretch(1, 1);
00049   grid->setSpacing(5);
00050   
00051   QLabel *label = new QLabel(tr("Name"), w);
00052   grid->addWidget(label, 0, 0);
00053   
00054   name = new QLineEdit(nam, w);
00055   grid->addWidget(name, 0, 1);
00056   
00057   QHBoxLayout *hbox = new QHBoxLayout(vbox);
00058   hbox->setSpacing(2);
00059   
00060   list = new QListView(w);
00061   list->addColumn(tr("Symbol"), 200);
00062   list->addColumn(tr("Weight"), -1);
00063   QObject::connect(list, SIGNAL(selectionChanged()), this, SLOT(buttonStatus()));
00064   hbox->addWidget(list);
00065   
00066   toolbar = new Toolbar(w, Toolbar::Vertical);
00067   hbox->addWidget(toolbar);
00068 
00069   QString s = "add";
00070   QString s2 = tr("Add Item");
00071   toolbar->addButton(s, insert, s2);
00072   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(addItem()));
00073   
00074   s = "edit";
00075   s2 = tr("Edit");
00076   toolbar->addButton(s, edit, s2);
00077   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(editItem()));
00078   
00079   s = "delete";
00080   s2 = tr("Delete");
00081   toolbar->addButton(s, deleteitem, s2);
00082   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(deleteItem()));
00083 
00084   vbox->insertStretch(-1, 1);
00085   
00086   addTab(w, tr("Details"));
00087 
00088   setOkButton(tr("&OK"));
00089   setCancelButton(tr("&Cancel"));
00090   connect(this, SIGNAL(applyButtonPressed()), this, SLOT(accept()));
00091 
00092   setHelpButton();
00093   QObject::connect(this, SIGNAL(helpButtonPressed()), this, SLOT(help()));
00094 
00095   setList(l);
00096     
00097   resize(325, 250);
00098 }
00099 
00100 IndexDialog::IndexDialog () : QTabDialog (0, "IndexDialog", TRUE)
00101 {
00102 }
00103 
00104 IndexDialog::~IndexDialog ()
00105 {
00106 }
00107 
00108 void IndexDialog::buttonStatus ()
00109 {
00110   QString s = "edit";
00111   QString s2 = "delete";
00112   
00113   QListViewItem *item = list->selectedItem();
00114   if (! item)
00115   {
00116     toolbar->setButtonStatus(s, FALSE);
00117     toolbar->setButtonStatus(s2, FALSE);
00118   }
00119   else
00120   {
00121     toolbar->setButtonStatus(s, TRUE);
00122     toolbar->setButtonStatus(s2, TRUE);
00123   }
00124 }
00125 
00126 void IndexDialog::addItem ()
00127 {
00128   double weight = 1;
00129   QString s;
00130   
00131   PrefDialog *dialog = new PrefDialog();
00132   dialog->setCaption(tr("Add Index Item"));
00133   QString pl = tr("Details");
00134   dialog->createPage (pl);
00135   QString t = tr("Symbol");
00136   Config config;
00137   QString t2;
00138   config.getData(Config::DataPath, t2);
00139   dialog->addSymbolItem(t, pl, t2, s);
00140   t = tr("Weight");
00141   dialog->addDoubleItem(t, pl, weight);
00142   int rc = dialog->exec();
00143   
00144   if (rc == QDialog::Accepted)
00145   {
00146     t = tr("Symbol");
00147     dialog->getSymbol(t, s);
00148     if (! s.length())
00149     {
00150       delete dialog;
00151       return;
00152     }
00153     
00154     t = tr("Weight");
00155     weight = dialog->getDouble(t);
00156     
00157     QStringList l = QStringList::split("/", s, FALSE);
00158     symbolDict.insert(l[l.count() - 1], new QString(s));
00159     
00160     new QListViewItem(list, l[l.count() - 1], QString::number(weight));
00161     
00162     buttonStatus();
00163   }
00164   
00165   delete dialog;
00166 }
00167 
00168 void IndexDialog::editItem ()
00169 {
00170   QListViewItem *item = list->selectedItem();
00171   if (! item)
00172     return;
00173 
00174   QString s = item->text(0);
00175   double weight = item->text(1).toFloat();
00176   
00177   PrefDialog *dialog = new PrefDialog();
00178   dialog->setCaption(tr("Edit Index Item"));
00179   QString pl = tr("Details");
00180   dialog->createPage (pl);
00181   QString t = tr("Symbol");
00182   Config config;
00183   QString t2;
00184   config.getData(Config::DataPath, t2);
00185   dialog->addSymbolItem(t, pl, t2, s);
00186   t = tr("Weight");
00187   dialog->addDoubleItem(t, pl, weight);
00188   int rc = dialog->exec();
00189   
00190   if (rc == QDialog::Accepted)
00191   {
00192     t = tr("Symbol");
00193     dialog->getSymbol(t, s);
00194     if (! s.length())
00195     {
00196       delete dialog;
00197       return;
00198     }
00199     
00200     t = tr("Weight");
00201     weight = dialog->getDouble(t);
00202     
00203     symbolDict.remove(item->text(0));
00204     QStringList l = QStringList::split("/", s, FALSE);
00205     symbolDict.insert(l[l.count() - 1], new QString(s));
00206     
00207     item->setText(0, l[l.count() - 1]);
00208     item->setText(1, QString::number(weight));
00209    
00210     buttonStatus();
00211   }
00212   
00213   delete dialog;
00214 }
00215 
00216 void IndexDialog::deleteItem ()
00217 {
00218   QListViewItem *item = list->selectedItem();
00219   if (! item)
00220     return;
00221 
00222   symbolDict.remove(item->text(0));
00223   delete item;
00224  
00225   buttonStatus();
00226 }
00227 
00228 void IndexDialog::setList (QString &d)
00229 {
00230   list->clear();
00231   symbolDict.clear();
00232   
00233   QStringList l = QStringList::split(":", d, FALSE);
00234   
00235   int loop;
00236   for (loop = 0; loop < (int) l.count(); loop = loop + 2)
00237   {
00238     QStringList l2 = QStringList::split("/", l[loop], FALSE);
00239     symbolDict.insert(l2[l2.count() - 1], new QString(l[loop]));
00240     new QListViewItem(list, l2[l2.count() - 1], l[loop + 1]);
00241   }
00242     
00243   buttonStatus();
00244 }
00245 
00246 void IndexDialog::getList (QString &d)
00247 {
00248   d.truncate(0);
00249   
00250   if (! list->childCount())
00251     return;
00252   
00253   QListViewItem *item = list->firstChild();
00254   
00255   while (item)
00256   {
00257     QString *sp = symbolDict[item->text(0)];
00258     d.append(sp->left(sp->length()));
00259     d.append(":");
00260     d.append(item->text(1));
00261     d.append(":");
00262     
00263     item = item->nextSibling();
00264   }
00265 }
00266 
00267 void IndexDialog::getName (QString &d)
00268 {
00269   d = name->text();
00270 }
00271 
00272 void IndexDialog::help ()
00273 {
00274   HelpWindow *hw = new HelpWindow(this, helpFile);
00275   hw->show();
00276 }
00277