src/NavigatorTab.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 "NavigatorTab.h"
00023 #include "ChartPage.h"
00024 #include "IndicatorPage.h"
00025 #include "ScannerPage.h"
00026 #include "PortfolioPage.h"
00027 #include "TestPage.h"
00028 #include "GroupPage.h"
00029 #include "ToolBarBtn.h"
00030 #include "RcFile.h"
00031 #include "../pics/indicator.xpm"
00032 #include "../pics/dirclosed.xpm"
00033 #include "../pics/plainitem.xpm"
00034 #include "../pics/portfolio.xpm"
00035 #include "../pics/test.xpm"
00036 #include "../pics/scanner.xpm"
00037 #include <qcursor.h>
00038 #include <qlayout.h>
00039 #include <qtooltip.h>
00040 #include <qaccel.h>
00041 
00042 NavigatorTab::NavigatorTab (QWidget *w, QMainWindow *mw) : QWidget (w)
00043 {
00044   loadSettings();
00045     
00046   QVBoxLayout *vbox = new QVBoxLayout(this);
00047   vbox->setSpacing(0);
00048   vbox->setMargin(2);
00049 
00050   QHBoxLayout *hbox = new QHBoxLayout(vbox);
00051   hbox->setSpacing(1);
00052   hbox->setMargin(1);
00053   
00054   bg = new QButtonGroup(this);
00055   QObject::connect(bg, SIGNAL(clicked(int)), this, SLOT(buttonPressed(int)));
00056   bg->setExclusive(TRUE);
00057   bg->hide();
00058   
00059   ToolBarBtn *button = new ToolBarBtn(this);
00060   QToolTip::add(button, tr("Workwith Charts"));
00061   button->setPixmap(plainitem);
00062   button->setToggleButton(TRUE);
00063   hbox->addWidget(button);
00064   bg->insert(button, 0);
00065   bg->setButton(0);
00066   
00067   button = new ToolBarBtn(this);
00068   QToolTip::add(button, tr("Workwith Groups"));
00069   button->setPixmap(dirclosed);
00070   button->setToggleButton(TRUE);
00071   hbox->addWidget(button);
00072   bg->insert(button, 1);
00073 
00074   button = new ToolBarBtn(this);
00075   QToolTip::add(button, tr("Main Chart Indicators"));
00076   button->setPixmap(indicator);
00077   button->setToggleButton(TRUE);
00078   hbox->addWidget(button);
00079   bg->insert(button, 2);
00080   
00081   button = new ToolBarBtn(this);
00082   QToolTip::add(button, tr("Workwith Portfolios"));
00083   button->setPixmap(portfolio);
00084   button->setToggleButton(TRUE);
00085   hbox->addWidget(button);
00086   bg->insert(button, 3);
00087 
00088   hbox->addStretch(1);
00089     
00090   hbox = new QHBoxLayout(vbox);
00091   hbox->setSpacing(1);
00092   hbox->setMargin(1);  // space around 2nd button row
00093 
00094   button = new ToolBarBtn(this);
00095   QToolTip::add(button, tr("Workwith Backtesting"));
00096   button->setPixmap(test);
00097   button->setToggleButton(TRUE);
00098   hbox->addWidget(button);
00099   bg->insert(button, 4);
00100   
00101   button = new ToolBarBtn(this);
00102   QToolTip::add(button, tr("Workwith Scanner"));
00103   button->setPixmap(scanner);
00104   button->setToggleButton(TRUE);
00105   hbox->addWidget(button);
00106   bg->insert(button, 5);
00107 
00108   hbox->addStretch(1);
00109   
00110   stack = new QWidgetStack(this);
00111   vbox->addWidget(stack);
00112 
00113   menu = new QPopupMenu;
00114   
00115   positionMenu = new QPopupMenu();
00116   connect(positionMenu, SIGNAL(aboutToShow ()), this, SLOT(slotTglPosAboutToShow()));
00117   idMenuLeft = positionMenu->insertItem(tr("Left"), this, SLOT(togglePosition(int)));
00118   positionMenu->setItemParameter(idMenuLeft, 0);
00119   idMenuRight = positionMenu->insertItem(tr("Right"), this, SLOT(togglePosition(int)));
00120   positionMenu->setItemParameter(idMenuRight, 1);
00121   menu->insertItem (tr("Navigator Position"), positionMenu);
00122   
00123   QAccel *a = new QAccel(mw);
00124   connect(a, SIGNAL(activated(int)), this, SLOT(slotAccel(int)));
00125   a->insertItem(CTRL+Key_C, ChartPanelFocus);
00126   a->insertItem(CTRL+Key_G, GroupPanelFocus);
00127   a->insertItem(CTRL+Key_I, IndicatorPanelFocus);
00128   a->insertItem(CTRL+Key_P, PortfolioPanelFocus);
00129   a->insertItem(CTRL+Key_T, TestPanelFocus);
00130   a->insertItem(CTRL+Key_S, ScannerPanelFocus);
00131 }
00132 
00133 NavigatorTab::~NavigatorTab ()
00134 {
00135   saveSettings();
00136   delete menu;
00137 }
00138 
00139 void NavigatorTab::togglePosition (int pos)
00140 {
00141   position = pos;
00142   emit signalPositionChanged(pos);
00143 }
00144 
00145 void NavigatorTab::saveSettings()
00146 {
00147   RcFile rcfile;
00148   rcfile.saveData(RcFile::NavigatorPosition, position);
00149   rcfile.saveData(RcFile::NavigatorActiveButton, activeButton);
00150 }
00151 
00152 void NavigatorTab::loadSettings() 
00153 {
00154   RcFile rcfile;
00155   rcfile.loadData(RcFile::NavigatorPosition, position);
00156   rcfile.loadData(RcFile::NavigatorActiveButton, activeButton);
00157   rcfile.loadData(RcFile::ShowSidePanel,isVisible);
00158 }
00159 
00160 int NavigatorTab::getPosition () 
00161 {
00162   return position;
00163 }
00164 
00165 void NavigatorTab::contextMenuEvent (QContextMenuEvent *event)
00166 {
00167   event->accept();
00168   menu->exec(QCursor::pos());
00169 }
00170 
00171 void NavigatorTab::addWidget (QWidget *w, int id) 
00172 {
00173   stack->addWidget(w, id);
00174 }
00175 
00176 void NavigatorTab::buttonPressed (int id) 
00177 {
00178   activeButton = id;
00179   stack->raiseWidget(id);
00180   QWidget *w = 0;
00181   
00182   switch (id)
00183   {
00184     case 0:
00185       w = (ChartPage *) stack->widget(id);
00186       break;
00187     case 1:
00188       w = (GroupPage *) stack->widget(id);
00189       break;
00190     case 2:
00191       w = (IndicatorPage *) stack->widget(id);
00192       break;
00193     case 3:
00194       w = (PortfolioPage *) stack->widget(id);
00195       break;
00196     case 4:
00197       w = (TestPage *) stack->widget(id);
00198       break;
00199     case 5:
00200       w = (ScannerPage *) stack->widget(id);
00201       break;
00202     default:
00203       break;
00204   }
00205 
00206   if (w)
00207     w->setFocus();
00208 }
00209 
00210 void NavigatorTab::pressButton (int id) 
00211 {
00212   if (bg->id(bg->selected()) == id)
00213   {
00214     buttonPressed(id);
00215     return;
00216   }
00217     
00218   bg->find(id)->toggle();
00219   buttonPressed(id);
00220 }
00221 
00222 void NavigatorTab::doKeyPress (QKeyEvent *key)
00223 {
00224   if (key->state() == Qt::ControlButton)
00225   {
00226     switch (key->key())
00227     {
00228       case Qt::Key_C:
00229         key->accept();
00230         pressButton(0);
00231         break;
00232       case Qt::Key_G:
00233         key->accept();
00234         pressButton(1);
00235         break;
00236       case Qt::Key_I:
00237         key->accept();
00238         pressButton(2);
00239         break;
00240       case Qt::Key_P:
00241         key->accept();
00242         pressButton(3);
00243         break;
00244       case Qt::Key_T:
00245         key->accept();
00246         pressButton(4);
00247         break;
00248       case Qt::Key_S:
00249         key->accept();
00250         pressButton(5);
00251         break;
00252       default:
00253         break;
00254     }
00255   }
00256   else
00257     key->ignore();
00258 }
00259 
00260 void NavigatorTab::slotAccel (int id)
00261 {
00262   switch (id)
00263   {
00264     case ChartPanelFocus:
00265       pressButton(id);
00266       break;
00267     case GroupPanelFocus:
00268       pressButton(id);
00269       break;
00270     case IndicatorPanelFocus:
00271       pressButton(id);
00272       break;
00273     case PortfolioPanelFocus:
00274       pressButton(id);
00275       break;
00276     case TestPanelFocus:
00277       pressButton(id);
00278       break;
00279     case ScannerPanelFocus:
00280       pressButton(id);
00281       break;
00282     default:
00283       break;
00284   }
00285 }
00286 
00287 void NavigatorTab::recentTab(QString chartPath)
00288 {
00289   if (chartPath.startsWith("group/"))
00290     slotAccel(GroupPanelFocus);
00291   else
00292     slotAccel(ChartPanelFocus);
00293 }
00294 
00295 void NavigatorTab::slotTglPosAboutToShow()
00296 {
00297   if(position) 
00298   {
00299     positionMenu->setItemVisible ( idMenuLeft, TRUE );
00300     positionMenu->setItemVisible ( idMenuRight, FALSE );
00301     
00302   }
00303   else 
00304   {
00305     positionMenu->setItemVisible ( idMenuLeft, FALSE );
00306     positionMenu->setItemVisible ( idMenuRight, TRUE );
00307   }
00308 }
00309 
00310 void NavigatorTab::init()
00311 {
00312   pressButton(activeButton);
00313   emit signaVisibilityChanged(isVisible);
00314 
00315 }