lib/PrefDialog.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 "PrefDialog.h"
00023 #include "HelpWindow.h"
00024 
00025 PrefDialog::PrefDialog (QWidget *w) : QTabDialog (w, "PrefDialog", TRUE)
00026 {
00027   init();
00028 }
00029 
00030 PrefDialog::PrefDialog () : QTabDialog (0, "PrefDialog", TRUE)
00031 {
00032   init();
00033 }
00034 
00035 PrefDialog::~PrefDialog ()
00036 {
00037   widgetList.clear();
00038   gridList.clear();
00039   colorButtonList.clear();
00040   intList.clear();
00041   doubleList.clear();
00042   checkList.clear();
00043   fontButtonList.clear();
00044   textList.clear();
00045   comboList.clear();
00046   dateList.clear();
00047   fileList.clear();
00048   symbolList.clear();
00049   dvList.clear();
00050   labelList.clear();
00051   timeList.clear();
00052 }
00053 
00054 void PrefDialog::init ()
00055 {
00056   widgetList.setAutoDelete(FALSE);
00057   gridList.setAutoDelete(FALSE);
00058   colorButtonList.setAutoDelete(FALSE);
00059   intList.setAutoDelete(FALSE);
00060   doubleList.setAutoDelete(FALSE);
00061   checkList.setAutoDelete(FALSE);
00062   fontButtonList.setAutoDelete(FALSE);
00063   textList.setAutoDelete(FALSE);
00064   comboList.setAutoDelete(FALSE);
00065   dateList.setAutoDelete(FALSE);
00066   fileList.setAutoDelete(FALSE);
00067   symbolList.setAutoDelete(FALSE);
00068   dvList.setAutoDelete(FALSE);
00069   labelList.setAutoDelete(FALSE);
00070   timeList.setAutoDelete(FALSE);
00071   
00072   setOkButton(tr("&OK"));
00073   setCancelButton(tr("&Cancel"));
00074   setHelpButton(tr("&Help"));
00075   
00076   QObject::connect(this, SIGNAL(helpButtonPressed()), this, SLOT(help()));
00077 }
00078 
00079 void PrefDialog::createPage (QString &name)
00080 {
00081   QWidget *w = new QWidget(this);
00082   widgetList.replace(name, w);
00083   
00084   QVBoxLayout *vbox = new QVBoxLayout(w);
00085   
00086   QGridLayout *grid = new QGridLayout(vbox, 1, 2);
00087   grid->setMargin(5);
00088   grid->setSpacing(5);
00089   gridList.replace(name, grid);
00090   grid->setColStretch(1, 1);
00091   
00092   vbox->insertStretch(-1, 1);
00093 
00094   addTab(w, name);
00095 }
00096 
00097 void PrefDialog::deletePage (QString &name)
00098 {
00099   removePage(widgetList[name]);
00100 }
00101 
00102 void PrefDialog::setHelpFile (QString &d)
00103 {
00104   helpFile = d;
00105 }
00106 
00107 void PrefDialog::help ()
00108 {
00109   HelpWindow *hw = new HelpWindow(this, helpFile);
00110   hw->show();
00111 }
00112 
00113 void PrefDialog::addColorItem (QString &name, QString &page, QColor &color)
00114 {
00115   QWidget *w = widgetList[page];
00116   
00117   QGridLayout *grid = gridList[page];
00118   grid->expand(grid->numRows() + 1, grid->numCols());
00119   
00120   QLabel *label = new QLabel(name, w);
00121   grid->addWidget(label, grid->numRows() - 2, 0);
00122   
00123   ColorButton *button = new ColorButton(w, color);
00124   grid->addWidget(button, grid->numRows() - 2, 1);
00125   button->setColorButton();
00126   colorButtonList.replace(name, button);
00127 }
00128 
00129 void PrefDialog::addColorPrefItem (QString &name, QString &page, QColor &color)
00130 {
00131   QWidget *w = widgetList[page];
00132   ColorButton *mainButton = new ColorButton(w, color);
00133   
00134   // add the preset preferred buttons
00135   // for now a fixed quantity of 5 pieces with fixed colors
00136   // may/will later expand with the possibility to make user defineable too
00137   QString prefButtonName = "PrefColor";
00138   ColorButton *prefButton;
00139   QColor prefColor[5]; 
00140   prefColor[0].setNamedColor("white");
00141   prefColor[1].setNamedColor("red");
00142   prefColor[2].setNamedColor("green");
00143   prefColor[3].setRgb(85,170,255); // something like blue
00144   prefColor[4].setRgb(255,170,0); // orange
00145   
00146   // create pref-buttons and arange in one row(hbox)
00147   QHBoxLayout *hbox = new QHBoxLayout(0,0,0,"hbox");
00148   int i;
00149   for (i = 0; i < 5; i++)
00150   {
00151     prefButton = new ColorButton(w, prefColor[i]);
00152     prefButton->setDialogOff();
00153     prefButton->pix.resize(10, 10);
00154     prefButton->setColorButton();
00155     connect (prefButton, SIGNAL(robPressed(QColor)), mainButton, SLOT(setColor(QColor)));
00156     hbox->addWidget(prefButton);
00157     colorButtonList.replace(prefButtonName + QString::number(i, 'f', 0), prefButton);
00158   
00159   }
00160   
00161   QVBoxLayout *vbox = new QVBoxLayout(0,0,0,"vbox");
00162   vbox->addLayout(hbox);
00163   vbox->addWidget(mainButton);
00164   
00165   QGridLayout *grid = gridList[page];
00166   grid->expand(grid->numRows() + 1, grid->numCols());
00167   
00168   QLabel *label = new QLabel(name, w);
00169   grid->addWidget(label, grid->numRows() - 2, 0);
00170   
00171   grid->addLayout(vbox, grid->numRows() - 2, 1);
00172   mainButton->setColorButton();
00173   colorButtonList.replace(name, mainButton);
00174   
00175     
00176 }
00177 
00178 void PrefDialog::getColor (QString &name, QColor &color)
00179 {
00180   ColorButton *button = colorButtonList[name];
00181   if (button)
00182     button->getColor(color);
00183 }
00184 
00185 void PrefDialog::addDoubleItem (QString &name, QString &page, double num)
00186 {
00187   addDoubleItem(name, page, num, -9999999999.0, 9999999999.0);
00188 }
00189 
00190 void PrefDialog::addDoubleItem (QString &name, QString &page, double num, double low, double high)
00191 {
00192   QWidget *w = widgetList[page];
00193   
00194   QGridLayout *grid = gridList[page];
00195   grid->expand(grid->numRows() + 1, grid->numCols());
00196   
00197   QLabel *label = new QLabel(name, w);
00198   grid->addWidget(label, grid->numRows() - 2, 0);
00199   
00200   QDoubleValidator *dv = new QDoubleValidator(low, high, 4, this, 0);
00201   dvList.replace(name, dv);
00202   
00203   QLineEdit *edit = new QLineEdit(QString::number(num), w);
00204   edit->setValidator(dv);
00205   grid->addWidget(edit, grid->numRows() - 2, 1);
00206   doubleList.replace(name, edit);
00207 }
00208 
00209 double PrefDialog::getDouble (QString &name)
00210 {
00211   double num = 0;
00212   QLineEdit *edit = doubleList[name];
00213   if (edit)
00214     num = edit->text().toDouble();
00215   return num;
00216 }
00217 
00218 void PrefDialog::addIntItem (QString &name, QString &page, int num)
00219 {
00220   addIntItem(name, page, num, -999999999, 999999999);
00221 }
00222 
00223 void PrefDialog::addIntItem (QString &name, QString &page, int num, int min, int max)
00224 {
00225   QWidget *w = widgetList[page];
00226   
00227   QGridLayout *grid = gridList[page];
00228   grid->expand(grid->numRows() + 1, grid->numCols());
00229   
00230   QLabel *label = new QLabel(name, w);
00231   grid->addWidget(label, grid->numRows() - 2, 0);
00232   
00233   QSpinBox *spin = new QSpinBox(min, max, 1, w);
00234   spin->setValue(num);
00235   grid->addWidget(spin, grid->numRows() - 2, 1);
00236   intList.replace(name, spin);
00237 }
00238 
00239 int PrefDialog::getInt (QString &name)
00240 {
00241   int num = 0;
00242   QSpinBox *spin = intList[name];
00243   if (spin)
00244     num = spin->value();
00245   return num;
00246 }
00247 
00248 void PrefDialog::addCheckItem (QString &name, QString &page, QString &flag)
00249 {
00250   if (! flag.compare("True"))
00251     addCheckItem(name, page, TRUE);
00252   else
00253     addCheckItem(name, page, FALSE);
00254 }
00255 
00256 void PrefDialog::addCheckItem (QString &name, QString &page, bool flag)
00257 {
00258   QWidget *w = widgetList[page];
00259   
00260   QGridLayout *grid = gridList[page];
00261   grid->expand(grid->numRows() + 1, grid->numCols());
00262   
00263   QLabel *label = new QLabel(name, w);
00264   grid->addWidget(label, grid->numRows() - 2, 0);
00265   
00266   QCheckBox *check = new QCheckBox(w);
00267   check->setChecked(flag);
00268   grid->addWidget(check, grid->numRows() - 2, 1);
00269   checkList.replace(name, check);
00270 }
00271 
00272 bool PrefDialog::getCheck (QString &name)
00273 {
00274   bool flag = FALSE;
00275   QCheckBox *check = checkList[name];
00276   if (check)
00277     flag = check->isChecked();
00278   return flag;
00279 }
00280 
00281 void PrefDialog::getCheckString (QString &name, QString &flag)
00282 {
00283   flag.truncate(0);
00284   QCheckBox *check = checkList[name];
00285   if (check)
00286   {
00287     if (check->isChecked())
00288       flag = "True";
00289     else
00290       flag = "False";
00291   }    
00292 }
00293 
00294 void PrefDialog::addFontItem (QString &name, QString &page, QFont &font)
00295 {
00296   QWidget *w = widgetList[page];
00297   
00298   QGridLayout *grid = gridList[page];
00299   grid->expand(grid->numRows() + 1, grid->numCols());
00300   
00301   QLabel *label = new QLabel(name, w);
00302   grid->addWidget(label, grid->numRows() - 2, 0);
00303   
00304   FontButton *button = new FontButton(w, font);
00305   grid->addWidget(button, grid->numRows() - 2, 1);
00306   fontButtonList.replace(name, button);
00307 }
00308 
00309 void PrefDialog::getFont (QString &name, QFont &font)
00310 {
00311   FontButton *button = fontButtonList[name];
00312   if (button)
00313     button->getFont(font);
00314 }
00315 
00316 void PrefDialog::addTextItem (QString &name, QString &page, QString &t)
00317 {
00318   QWidget *w = widgetList[page];
00319   
00320   QGridLayout *grid = gridList[page];
00321   grid->expand(grid->numRows() + 1, grid->numCols());
00322   
00323   QLabel *label = new QLabel(name, w);
00324   grid->addWidget(label, grid->numRows() - 2, 0);
00325 
00326   QLineEdit *edit = new QLineEdit(t, w);
00327   grid->addWidget(edit, grid->numRows() - 2, 1);
00328   textList.replace(name, edit);
00329 }
00330 
00331 void PrefDialog::getText (QString &name, QString &s)
00332 {
00333   s.truncate(0);
00334   QLineEdit *edit = textList[name];
00335   if (edit)
00336     s = edit->text();
00337 }
00338 
00339 void PrefDialog::addComboItem (QString &name, QString &page, QStringList &l, QString &s)
00340 {
00341   QWidget *w = widgetList[page];
00342   
00343   QGridLayout *grid = gridList[page];
00344   grid->expand(grid->numRows() + 1, grid->numCols());
00345   
00346   QLabel *label = new QLabel(name, w);
00347   grid->addWidget(label, grid->numRows() - 2, 0);
00348 
00349   QComboBox *combo = new QComboBox(w);
00350   combo->insertStringList(l, -1);
00351   if (s.length())
00352   {
00353     int t = l.findIndex(s);
00354     if (t != -1)
00355       combo->setCurrentItem(t);
00356   }
00357   grid->addWidget(combo, grid->numRows() - 2, 1);
00358   comboList.replace(name, combo);
00359 }
00360 
00361 void PrefDialog::addComboItem (QString &name, QString &page, QStringList &l, int s)
00362 {
00363   QWidget *w = widgetList[page];
00364   
00365   QGridLayout *grid = gridList[page];
00366   grid->expand(grid->numRows() + 1, grid->numCols());
00367   
00368   QLabel *label = new QLabel(name, w);
00369   grid->addWidget(label, grid->numRows() - 2, 0);
00370 
00371   QComboBox *combo = new QComboBox(w);
00372   combo->insertStringList(l, -1);
00373   combo->setCurrentItem(s);
00374   grid->addWidget(combo, grid->numRows() - 2, 1);
00375   comboList.replace(name, combo);
00376 }
00377 
00378 void PrefDialog::getCombo (QString &name, QString &s)
00379 {
00380   s.truncate(0);
00381   QComboBox *combo = comboList[name];
00382   if (combo)
00383     s = combo->currentText();
00384 }
00385 
00386 int PrefDialog::getComboIndex (QString &name)
00387 {
00388   int i = 0;
00389   QComboBox *combo = comboList[name];
00390   if (combo)
00391     i = combo->currentItem();
00392   return i;
00393 }
00394 
00395 QComboBox * PrefDialog::getComboWidget (QString &name)
00396 {
00397   return comboList[name];
00398 }
00399 
00400 void PrefDialog::addDateItem (QString &name, QString &page, QDateTime &dt)
00401 {
00402   QWidget *w = widgetList[page];
00403   
00404   QGridLayout *grid = gridList[page];
00405   grid->expand(grid->numRows() + 1, grid->numCols());
00406   
00407   QLabel *label = new QLabel(name, w);
00408   grid->addWidget(label, grid->numRows() - 2, 0);
00409 
00410   QDateEdit *date = new QDateEdit(dt.date(), w);
00411   date->setAutoAdvance(TRUE);
00412   date->setOrder(QDateEdit::YMD);
00413   grid->addWidget(date, grid->numRows() - 2, 1);
00414   dateList.replace(name, date);
00415 }
00416 
00417 void PrefDialog::getDate (QString &name, QDateTime &dt)
00418 {
00419   QDateEdit *date = dateList[name];
00420   if (date)
00421     dt.setDate(date->date());
00422 }
00423 
00424 void PrefDialog::addTimeItem (QString &name, QString &page, QDateTime &dt)
00425 {
00426   QWidget *w = widgetList[page];
00427   
00428   QGridLayout *grid = gridList[page];
00429   grid->expand(grid->numRows() + 1, grid->numCols());
00430   
00431   QLabel *label = new QLabel(name, w);
00432   grid->addWidget(label, grid->numRows() - 2, 0);
00433 
00434   QTimeEdit *time = new QTimeEdit(dt.time(), w);
00435   time->setAutoAdvance(TRUE);
00436   grid->addWidget(time, grid->numRows() - 2, 1);
00437   timeList.replace(name, time);
00438 }
00439 
00440 void PrefDialog::getTime (QString &name, QDateTime &dt)
00441 {
00442   QTimeEdit *time = timeList[name];
00443   if (time)
00444     dt.setTime(time->time());
00445 }
00446 
00447 void PrefDialog::addFileItem (QString &name, QString &page, QStringList &l, QString &p)
00448 {
00449   QWidget *w = widgetList[page];
00450   
00451   QGridLayout *grid = gridList[page];
00452   grid->expand(grid->numRows() + 1, grid->numCols());
00453   
00454   QLabel *label = new QLabel(name, w);
00455   grid->addWidget(label, grid->numRows() - 2, 0);
00456 
00457   FileButton *button = new FileButton(w, l, p);
00458   grid->addWidget(button, grid->numRows() - 2, 1);
00459   fileList.replace(name, button);
00460 }
00461 
00462 void PrefDialog::getFile (QString &name, QStringList &l)
00463 {
00464   l.clear();
00465   FileButton *button = fileList[name];
00466   if (button)
00467     button->getFile(l);
00468 }
00469 
00470 void PrefDialog::addSymbolItem (QString &name, QString &page, QString &path, QString &symbol)
00471 {
00472   QWidget *w = widgetList[page];
00473   
00474   QGridLayout *grid = gridList[page];
00475   grid->expand(grid->numRows() + 1, grid->numCols());
00476   
00477   QLabel *label = new QLabel(name, w);
00478   grid->addWidget(label, grid->numRows() - 2, 0);
00479 
00480   SymbolButton *button = new SymbolButton(w, path, symbol);
00481   grid->addWidget(button, grid->numRows() - 2, 1);
00482   symbolList.replace(name, button);
00483 }
00484 
00485 void PrefDialog::getSymbol (QString &name, QString &s)
00486 {
00487   s.truncate(0);
00488   SymbolButton *button = symbolList[name];
00489   if (button)
00490     button->getPath(s);
00491 }
00492 
00493 void PrefDialog::addLabelItem (QString &name, QString &page, QString &l)
00494 {
00495   QWidget *w = widgetList[page];
00496   
00497   QGridLayout *grid = gridList[page];
00498   grid->expand(grid->numRows() + 1, grid->numCols());
00499   
00500   QLabel *label = new QLabel(name, w);
00501   grid->addWidget(label, grid->numRows() - 2, 0);
00502   
00503   label = new QLabel(l, w);
00504   label->setFrameStyle(QFrame::WinPanel | QFrame::Sunken);
00505   grid->addWidget(label, grid->numRows() - 2, 1);
00506   labelList.replace(name, label);
00507 }
00508 
00509 void PrefDialog::getItem (QString &name, QString &s)
00510 {
00511   s.truncate(0);
00512 
00513   // check color
00514   QColor color;
00515   getColor(name, color);
00516   if (color.isValid())
00517   {
00518     s = color.name();
00519     return;
00520   }
00521 
00522   // check for double
00523   QLineEdit *edit = doubleList[name];
00524   if (edit)
00525   {
00526     s = edit->text();
00527     return;
00528   }
00529 
00530   // check for int
00531   QSpinBox *spin = intList[name];
00532   if (spin)
00533   {
00534     s = spin->text();
00535     return;
00536   }
00537 
00538   getText(name, s);
00539   if (s.length())
00540     return;
00541 
00542   QComboBox *combo = comboList[name];
00543   if (combo)
00544   {
00545     s = QString::number(combo->currentItem());
00546     return;
00547   }
00548 }
00549 
00550