Main Page   Class Hierarchy   Data Structures   File List   Data Fields  

stockgui.cpp

00001 /***************************************************************************
00002                           stockgui.cpp  -  description
00003                              -------------------
00004     begin                : Mon Oct 28 2002
00005     copyright            : (C) 2002 by Michael Otto
00006     email                : Michael.Otto@saskathex.de
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010  *                                                                         *
00011  *   This program is free software; you can redistribute it and/or modify  *
00012  *   it under the terms of the GNU General Public License as published by  *
00013  *   the Free Software Foundation; either version 2 of the License, or     *
00014  *   (at your option) any later version.                                   *
00015  *                                                                         *
00016  ***************************************************************************/
00017 
00018 #include "stockgui.h"
00019 #include "stock.h"
00020 #include "infodialog.h"
00021 #include "chartdialog.h"
00022 
00023 #include "config.h"
00024 
00025 
00029 StockGui::StockGui(Stock * const stock){
00030 #ifdef DEBUG
00031   cout << "creating stockgui" << endl;
00032 #endif
00033   
00034   StockGui::stock = stock;
00035   //labelName = new Gtk::Label(stock->getName(), 0);
00036   this->set_text(StockGui::stock->getName());
00037   labelWkn = new Gtk::Label(StockGui::stock->getWkn(), 0);
00038   labelBuyingDate = new Gtk::Label(StockGui::stock->getBuyingDate(), 0);
00039   labelPurchaseValue = new Gtk::Label("0.00", 0);
00040   labelDayPerformance = new Gtk::Label("0.0 %", 0);
00041   labelTotalMarketValue = new Gtk::Label("0.00", 0);
00042   labelOverallPerformance = new Gtk::Label("0.0 %", 0);
00043 
00044   adjustmentAmount = new Gtk::Adjustment(0, 0, 10000, 1, 10, 10);
00045   adjustmentBuyingRate = new Gtk::Adjustment(0, 0, 10000, 0.01, 10, 10);
00046   adjustmentPurchaseCost = new Gtk::Adjustment(0, 0, 10000, 0.01, 10, 10);
00047   adjustmentCurrentMarketPrice = new Gtk::Adjustment(0, 0, 10000, 0.01, 10, 10);
00048     
00049   spinButtonAmount = new Gtk::SpinButton(*adjustmentAmount, 1, 0);
00050   spinButtonAmount->set_numeric(true);
00051   spinButtonAmount->set_value(StockGui::stock->getAmount());
00052   spinButtonAmount->signal_value_changed().connect(SigC::slot(*this, &StockGui::eventAmountChanged));
00053 
00054   spinButtonBuyingRate = new Gtk::SpinButton(*adjustmentBuyingRate, 1, 0);
00055   spinButtonBuyingRate->set_digits(2);
00056   spinButtonBuyingRate->set_numeric(true);
00057   spinButtonBuyingRate->set_value(StockGui::stock->getBuyingRate());
00058   spinButtonBuyingRate->signal_value_changed().connect(SigC::slot(*this, &StockGui::eventBuyingRateChanged));
00059 
00060   spinButtonPurchaseCost = new Gtk::SpinButton(*adjustmentPurchaseCost, 1, 0);
00061   spinButtonPurchaseCost->set_digits(2);
00062   spinButtonPurchaseCost->set_numeric(true);
00063   spinButtonPurchaseCost->set_value(StockGui::stock->getPurchaseCost());
00064   spinButtonPurchaseCost->signal_value_changed().connect(SigC::slot(*this, &StockGui::eventPurchaseCostChanged));
00065 
00066   spinButtonCurrentMarketPrice = new Gtk::SpinButton(*adjustmentCurrentMarketPrice, 1, 0);
00067   spinButtonCurrentMarketPrice->set_digits(2);
00068   spinButtonCurrentMarketPrice->set_numeric(true);
00069   spinButtonCurrentMarketPrice->set_value(StockGui::stock->getCurrentMarketPrice());
00070   spinButtonCurrentMarketPrice->signal_value_changed().connect(SigC::slot(*this, &StockGui::eventCurrentMarketPriceChanged));
00071 
00072   buttonInfo = new Gtk::Button("Info");
00073   buttonInfo->signal_clicked().connect(slot(*this,&StockGui::eventButtonInfo));
00074   buttonChart = new Gtk::Button("Chart");
00075 //  buttonChart->signal_clicked().connect(slot(*this,&StockGui::eventButtonInfo));
00076   buttonChart->signal_clicked().connect(slot(*this,&StockGui::eventButtonChart));
00077 
00078   stock->attach(this);
00079   this->update(StockGui::stock);
00080 }
00081 
00085 StockGui::~StockGui(){
00086 #ifdef DEBUG
00087   cout << "deleting stockgui";
00088 #endif
00089   // detach the stock from its subject --> doesn't work because the Subject was already deleted!
00090   if (stock != NULL) stock->detach(this);
00091   delete labelWkn, labelBuyingDate;
00092   delete spinButtonAmount, spinButtonBuyingRate;
00093   delete labelPurchaseValue;
00094   delete spinButtonPurchaseCost, spinButtonCurrentMarketPrice;
00095   delete labelDayPerformance, labelTotalMarketValue, labelOverallPerformance;
00096   delete buttonInfo, buttonChart;
00097   delete adjustmentAmount, adjustmentBuyingRate, adjustmentPurchaseCost, adjustmentCurrentMarketPrice;
00098 #ifdef DEBUG
00099   cout << "." << endl;
00100 #endif
00101 }
00102 
00103   /* Getter */
00104 
00108 Gtk::Label *StockGui::getLabelWkn() const{
00109   return labelWkn;
00110 }
00111 
00115 Gtk::Label *StockGui::getLabelBuyingDate() const{
00116   return labelBuyingDate;
00117 }
00118 
00122 Gtk::SpinButton *StockGui::getSpinButtonAmount() const {
00123   return spinButtonAmount;
00124 }
00125 
00129 Gtk::SpinButton *StockGui::getSpinButtonBuyingRate() const {
00130   return spinButtonBuyingRate;
00131 }
00132 
00136 Gtk::Label *StockGui::getLabelPurchaseValue() const {
00137   return labelPurchaseValue;
00138 }
00139 
00143 Gtk::SpinButton *StockGui::getSpinButtonPurchaseCost() const {
00144   return spinButtonPurchaseCost;
00145 }
00146 
00150 Gtk::SpinButton *StockGui::getSpinButtonCurrentMarketPrice() const {
00151   return spinButtonCurrentMarketPrice;
00152 }
00153 
00157 Gtk::Label *StockGui::getLabelDayPerformance() const {
00158   return labelDayPerformance;
00159 }
00160 
00164 Gtk::Label *StockGui::getLabelTotalMarketValue() const {
00165   return labelTotalMarketValue;
00166 }
00167 
00171 Gtk::Label *StockGui::getLabelOverallPerformance() const {
00172   return labelOverallPerformance;
00173 }
00174 
00178 Gtk::Button *StockGui::getButtonInfo() const {
00179   return buttonInfo;
00180 }
00181 
00185 Gtk::Button *StockGui::getButtonChart() const {
00186   return buttonChart;
00187 }
00188 
00189   /* events on the spinbuttons */
00193 void StockGui::eventAmountChanged() {
00194   stock->setAmount(spinButtonAmount->get_value_as_int());
00195 }
00196 
00200 void StockGui::eventBuyingRateChanged() {
00201   stock->setBuyingRate(spinButtonBuyingRate->get_value());
00202   vector <float> values = stock->getValuesVector();
00203   if (values.size() == 0) {
00204     stock->setCurrentMarketPriceDayBefore(spinButtonBuyingRate->get_value());
00205   }
00206 }
00207 
00211 void StockGui::eventPurchaseCostChanged() {
00212   stock->setPurchaseCost(spinButtonPurchaseCost->get_value());
00213 }
00214 
00218 void StockGui::eventCurrentMarketPriceChanged() {
00219   stock->setCurrentMarketPrice(spinButtonCurrentMarketPrice->get_value());
00220 }
00221 
00222   /* update methods for the different labels */
00226 void StockGui::updateLabelPurchaseValue() {
00227   float percent = (stock->getAmount() * stock->getBuyingRate());
00228   char c[20];
00229   sprintf(c,"%.2f", percent);
00230   labelPurchaseValue->set_text(c);
00231 }
00232 
00236 void StockGui::updateLabelDayPerformance() {
00237   float percent;
00238   percent = ((stock->getCurrentMarketPrice() - stock->getCurrentMarketPriceDayBefore()) / stock->getCurrentMarketPriceDayBefore()) * 100;
00239   if (stock->getCurrentMarketPriceDayBefore() <= 0) {
00240     labelDayPerformance->set_text("n/a");
00241   }
00242   else {
00243     char c[20];
00244     sprintf(c,"%.1f %%", percent);
00245     labelDayPerformance->set_text(c);
00246   }
00247 }
00248 
00252 void StockGui::updateLabelTotalMarketValue() {
00253   float percent = (stock->getAmount() * stock->getCurrentMarketPrice());
00254   char c[20];
00255   sprintf(c,"%.2f", percent);
00256   labelTotalMarketValue->set_text(c);
00257 }
00258 
00262 void StockGui::updateLabelOverallPerformance() {
00263   float percent = ((stock->getAmount() * stock->getCurrentMarketPrice()) - (stock->getAmount() * stock->getBuyingRate())) / (stock->getAmount() * stock->getBuyingRate()) * 100 ;
00264   if ((stock->getAmount() * stock->getBuyingRate()) <= 0) {
00265     labelOverallPerformance->set_text("n/a");
00266   }
00267   else {
00268     char c[20];       
00269     sprintf(c,"%.1f %%", percent);
00270     labelOverallPerformance->set_text(c);
00271   }
00272 }
00273 
00274   /* Button events */
00278 void StockGui::eventButtonInfo() {
00279 #ifdef DEBUG
00280   cout << "Info Button" << endl;
00281 #endif
00282   new class InfoDialog(stock);
00283 }
00284  
00288 void StockGui::eventButtonChart() {
00289 #ifdef DEBUG
00290   cout << "Chart Button" << endl;
00291 #endif
00292   new class ChartDialog(stock);
00293 }
00294 
00295 
00299 void StockGui::update(const Subject *const subject) {
00300   adjustmentAmount->set_value(stock->getAmount());
00301   adjustmentBuyingRate->set_value(stock->getBuyingRate());
00302   adjustmentPurchaseCost->set_value(stock->getPurchaseCost());
00303   adjustmentCurrentMarketPrice->set_value(stock->getCurrentMarketPrice());
00304 
00305   this->set_text(stock->getName());
00306   labelWkn->set_text(stock->getWkn());
00307   labelBuyingDate->set_text(stock->getBuyingDate());
00308         
00309   updateLabelPurchaseValue();
00310   updateLabelDayPerformance();
00311   updateLabelTotalMarketValue();
00312   updateLabelOverallPerformance();
00313 }
00314 
00315 

Generated on Tue May 27 21:24:02 2003 for gstockcalc by doxygen1.3-rc3