src/ExtraToolbar.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 "ExtraToolbar.h"
00023 #include "PrefDialog.h"
00024 #include <qtooltip.h>
00025 #include <qstringlist.h>
00026 
00027 // FIXME: Why are sometimes two or more entries ticked in recentCombo.
00028 
00029 // FIXME: Make maxItems configurable.
00030 
00031 ExtraToolbar::ExtraToolbar (QMainWindow *mw) : QToolBar ("", mw, mw, TRUE, "extraToolbar")
00032 {
00033   // Recent viewed charts.
00034   recentCombo = new QComboBox(this);
00035   QToolTip::add(recentCombo, tr("Recent charts"));
00036 
00037 // FIXME: Having trouble getting ComboBox to dynamically set its width
00038 // FIXME: Perhaps use setSizeAdjustPolicy(QComboBox::AdjustToContents) when Qt4.
00039   recentCombo->setFixedWidth(260);
00040 
00041   connect(recentCombo, SIGNAL(activated(int)), this, SLOT(slotRecentChartSelected(int)));
00042 
00043   QString s;
00044   rcfile.loadData(RcFile::RecentChartsList, s);
00045   QStringList sl = QStringList::split(",", s);
00046   if (sl.count() > 0)
00047   {
00048     for (int loop = 0; loop < (int) sl.count(); loop++)
00049       recentCombo->insertItem(sl[loop]);
00050   }
00051 
00052   bool b;
00053   rcfile.loadData(RcFile::ShowExtraToolbar, b);
00054   if (!b)
00055     hide();
00056 
00057 // FIXME: Is font set correctly to the appFont?
00058 /*
00059   QFont myFont = recentCombo->font();
00060   QString myFontS = myFont.toString();
00061   qDebug("Combo font=%s", myFontS.latin1());
00062 */
00063 }
00064 
00065 ExtraToolbar::~ExtraToolbar ()
00066 {
00067 }
00068 
00069 void ExtraToolbar::saveSettings ()
00070 {
00071   QString s;
00072   for (int loop = 0; loop < recentCombo->count(); loop++)
00073   {
00074     s += recentCombo->text(loop) + ",";
00075   }
00076   s.remove(s.length()-1,1); // Remove trailing comma.
00077   rcfile.saveData(RcFile::RecentChartsList, s);
00078 }
00079 
00080 void ExtraToolbar::slotSetButtonView ()
00081 {
00082   bool tb;
00083   
00084   rcfile.loadData(RcFile::ShowRecentCharts, tb);
00085   if (tb) recentCombo->show();
00086   else recentCombo->hide();
00087 }
00088 
00089 void ExtraToolbar::slotRecentChartSelected (int)
00090 {
00091   QString chartSelected = recentCombo->currentText();
00092   sortRecentCharts(chartSelected);
00093   QString s;
00094   rcfile.loadData(RcFile::Home, s);
00095   s += "/" + chartSelected;
00096 
00097   QString chartName = chartSelected.section( '/', -1 );
00098   QString chartPath = chartSelected;
00099   int i = chartPath.findRev("/" + chartName, -1, TRUE);
00100   chartPath.remove(i, chartName.length()+1);
00101 
00102   QString chartDir;
00103   rcfile.loadData(RcFile::Home, chartDir);
00104   chartDir += "/" + chartPath;
00105 
00106   emit recentTab(chartSelected);
00107   if (chartPath.startsWith("group/"))
00108     emit signalSetGroupNavItem(chartDir, chartName);
00109   else
00110     emit signalSetChartNavItem(chartDir, chartName);
00111 
00112   emit fileSelected(s);
00113 }
00114 
00115 void ExtraToolbar::slotAddRecentChart (QString selection)
00116 {
00117   QString s;
00118   rcfile.loadData(RcFile::Home, s);
00119   s += "/";
00120   selection.remove(s); // relative Group or Chart path, and the chart name
00121   sortRecentCharts(selection);
00122   if (recentCombo->count() > 10)
00123     recentCombo->removeItem(10);
00124 }
00125 
00126 void ExtraToolbar::slotRemoveRecentCharts (QStringList l)
00127 {
00128   QString s, sl;
00129   rcfile.loadData(RcFile::Home, s);
00130   s += "/";
00131   for (int i = 0; i < (int) l.count(); i++)
00132   {
00133     sl = l[i];
00134     sl.remove(s);
00135     for (int j = 0; j < recentCombo->count(); j++)
00136     {
00137       if (!QString::compare(recentCombo->text(j), sl))
00138         recentCombo->removeItem(j);
00139     }
00140   }
00141 }
00142 
00143 void ExtraToolbar::sortRecentCharts (QString s)
00144 {
00145   // If the item is already in the list then remove it, then insert at top
00146   for (int loop = 0; loop < recentCombo->count(); loop++)
00147   {
00148     if (!QString::compare(recentCombo->text(loop), s))
00149       recentCombo->removeItem(loop);
00150   }
00151   recentCombo->insertItem(s, 0);
00152   recentCombo->setCurrentItem(0);
00153 }