00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "movestock.h"
00019
00020 #include "stockmarketvector.h"
00021 #include "stockmarket.h"
00022 #include "stockcontainer.h"
00023 #include "stock.h"
00024
00025 #include "config.h"
00026
00027 #include <gtkmm/stock.h>
00028
00032 MoveStock::MoveStock(StockMarketVector * const stockMarketVector) {
00033
00034 MoveStock::stockMarketVector = stockMarketVector;
00035 stock = NULL;
00036
00037
00038 set_title("moving stock");
00039 set_border_width(5);
00040 set_resizable(false);
00041 set_modal(true);
00042
00043 vbox = manage(new Gtk::VBox(false, 0));
00044 vbox->set_spacing(5);
00045 add(*vbox);
00046
00047 if (stockMarketVector->getNumberOfStockMarkets() > 0) {
00048 label = manage (new Gtk::Label("Select the old market of the stock:", 0));
00049 vbox->pack_start(*label, Gtk::PACK_SHRINK, 0);
00050 hSep = manage(new Gtk::HSeparator());
00051 vbox->pack_start(*hSep, Gtk::PACK_SHRINK, 0);
00052 vboxChoice = manage(new Gtk::VBox(false, 0));
00053 radioButtons.clear();
00054 Gtk::RadioButton::Group group;
00055 Gtk::RadioButton *rb;
00056 for (int i = 0; i < stockMarketVector->getNumberOfStockMarkets(); i++) {
00057 StockMarket *stockMarket = stockMarketVector->getStockMarket(i);
00058 rb = manage( new Gtk::RadioButton(group, stockMarket->getMarketName()));
00059 radioButtons.push_back(rb);
00060 vboxChoice->pack_start(*rb, Gtk::PACK_SHRINK, 0);
00061 }
00062
00063 vbox->pack_start(*vboxChoice, Gtk::PACK_SHRINK, 0);
00064 hSep = manage(new Gtk::HSeparator());
00065 vbox->pack_start(*hSep, Gtk::PACK_SHRINK, 0);
00066 hButtonBox = manage( new Gtk::HButtonBox());
00067 hButtonBox->set_spacing(5);
00068 vbox->pack_start(*hButtonBox, Gtk::PACK_SHRINK, 0);
00069 buttonCancel = manage( new Gtk::Button(Gtk::Stock::CANCEL));
00070 buttonCancel->signal_clicked().connect(slot(*this,&MoveStock::quit));
00071 hButtonBox->pack_start(*buttonCancel, Gtk::PACK_SHRINK, 0);
00072 buttonNext = manage( new Gtk::Button(Gtk::Stock::GO_FORWARD));
00073 buttonNext->signal_clicked().connect(slot(*this,&MoveStock::selectStock));
00074 hButtonBox->pack_start(*buttonNext, Gtk::PACK_SHRINK, 0);
00075 } else {
00076 set_title("Message");
00077 label = manage (new Gtk::Label("There are no markets!"));
00078 vbox->pack_start(*label, Gtk::PACK_SHRINK, 0);
00079 buttonCancel = manage(new Gtk::Button(Gtk::Stock::OK));
00080 buttonCancel->signal_clicked().connect(slot(*this,&MoveStock::quit));
00081 vbox->pack_start(*buttonCancel, Gtk::PACK_SHRINK, 0);
00082 }
00083 show_all();
00084 }
00085
00089 MoveStock::~MoveStock() {}
00090
00094 void MoveStock::quit() {
00095 delete this;
00096 }
00097
00101 void MoveStock::selectStock() {
00102 Gtk::RadioButton *rb;
00103 for (int i = 0; i < radioButtons.size(); i++) {
00104 rb = radioButtons[i];
00105 if (rb->get_active()) {
00106 stockMarketOld = stockMarketVector->getStockMarket(i);
00107 #ifdef DEBUG
00108 cout << "selected " << stockMarketOld->getMarketName() << endl;
00109 #endif
00110 }
00111 }
00112 for (int i = 0; i < radioButtons.size(); i++) {
00113 rb = radioButtons[i];
00114 delete rb;
00115 }
00116 radioButtons.clear();
00117
00118 delete vbox;
00119 vbox = manage(new Gtk::VBox(false, 0));
00120 vbox->set_spacing(5);
00121 add(*vbox);
00122 if (stockMarketOld->getNumberOfStockContainer() > 0) {
00123 tableEditStockValue = manage(new Gtk::Table(2, 1, false));
00124 tableEditStockValue->set_spacings(5);
00125 vbox->pack_start(*tableEditStockValue, Gtk::PACK_SHRINK, 0);
00126 label = manage (new Gtk::Label("Choose a stock:"));
00127 tableEditStockValue->attach(*label, 0, 1, 0, 1);
00128
00129 comboStockName = manage (new Gtk::Combo());
00130 Gtk::Entry *e;
00131 e = comboStockName->get_entry();
00132 e->set_editable(false);
00133 e->set_max_length(80);
00134 e->set_width_chars(80);
00135 Stock *stock;
00136 char c[80];
00137 for (int i = 0; i < stockMarketOld->getNumberOfStockContainer(); i++) {
00138 stock = stockMarketOld->getStockContainer(i)->getStock();
00139 sprintf(c,"%d: %s - %s - %d pieces - each %0.2f",i+1,(stock->getName()).c_str(), (stock->getBuyingDate()).c_str(), stock->getAmount(), stock->getBuyingRate());
00140 vector_char.push_back(c);
00141 }
00142 comboStockName->set_popdown_strings(vector_char);
00143 comboStockName->set_use_arrows(true);
00144 comboStockName->set_use_arrows_always(true);
00145 tableEditStockValue->attach(*comboStockName, 0, 1, 1, 2);
00146 hButtonBox = manage( new Gtk::HButtonBox());
00147 hButtonBox->set_spacing(5);
00148 vbox->pack_start(*hButtonBox, Gtk::PACK_SHRINK, 0);
00149 buttonCancel = manage( new Gtk::Button(Gtk::Stock::CANCEL));
00150 buttonCancel->signal_clicked().connect(slot(*this,&MoveStock::quit));
00151 hButtonBox->pack_start(*buttonCancel, Gtk::PACK_SHRINK, 0);
00152 buttonNext = manage( new Gtk::Button(Gtk::Stock::GO_FORWARD));
00153 buttonNext->signal_clicked().connect(slot(*this,&MoveStock::moveStock));
00154 hButtonBox->pack_start(*buttonNext, Gtk::PACK_SHRINK, 0);
00155 } else {
00156 set_title("Message");
00157 label = manage (new Gtk::Label("Market is empty!"));
00158 vbox->pack_start(*label, Gtk::PACK_SHRINK, 0);
00159 buttonCancel = manage( new Gtk::Button(Gtk::Stock::CANCEL));
00160 buttonCancel->signal_clicked().connect(slot(*this,&MoveStock::quit));
00161 vbox->pack_start(*buttonCancel, Gtk::PACK_SHRINK, 0);
00162 }
00163 show_all();
00164 }
00165
00166
00170 void MoveStock::moveStock() {
00171 Gtk::Entry *e;
00172 e = comboStockName->get_entry();
00173 string stringVar = e->get_text();
00174 int i = 0;
00175 while ((!(vector_char.empty())) && stock == NULL) {
00176 string s = vector_char.front();
00177 vector_char.pop_front();
00178 if (s == stringVar) {
00179 stock = stockMarketOld->getStockContainer(i)->getStock();
00180 }
00181 i++;
00182 }
00183 if (stock != NULL) {
00184 stockNew = new Stock(stock->getName(), stock->getWkn(), stock->getBuyingDate(), stock->getAmount(), stock->getBuyingRate(),
00185 stock->getPurchaseCost(), stock->getCurrentMarketPrice(), stock->getValuesVector());
00186 bool okay = stockMarketOld->removeStock(stock);
00187 #ifdef DEBUG
00188 if (okay == true) cout << "Removing the stock returned true." << endl;
00189 else cout << "Removing the stock returned false." << endl;
00190 #endif
00191 } else {
00192 #ifdef DEBUG
00193 cout << "Stock wasn't found in this market!" << endl;
00194 #endif
00195 }
00196
00197 delete(vbox);
00198 vbox = manage(new Gtk::VBox(false, 0));
00199 vbox->set_spacing(5);
00200 add(*vbox);
00201 label = manage (new Gtk::Label("Select the new market of the stock:", 0));
00202 vbox->pack_start(*label, Gtk::PACK_SHRINK, 0);
00203 hSep = manage(new Gtk::HSeparator());
00204 vbox->pack_start(*hSep, Gtk::PACK_SHRINK, 0);
00205 vboxChoice = manage(new Gtk::VBox(false, 0));
00206 radioButtons.clear();
00207 Gtk::RadioButton::Group group;
00208 Gtk::RadioButton *rb;
00209 for (int i = 0; i < stockMarketVector->getNumberOfStockMarkets(); i++) {
00210 StockMarket *stockMarket = stockMarketVector->getStockMarket(i);
00211 rb = manage( new Gtk::RadioButton(group, stockMarket->getMarketName()));
00212 radioButtons.push_back(rb);
00213 vboxChoice->pack_start(*rb, Gtk::PACK_SHRINK, 0);
00214 }
00215 vbox->pack_start(*vboxChoice, Gtk::PACK_SHRINK, 0);
00216 hSep = manage(new Gtk::HSeparator());
00217 vbox->pack_start(*hSep, Gtk::PACK_SHRINK, 0);
00218 hButtonBox = manage( new Gtk::HButtonBox());
00219 hButtonBox->set_spacing(5);
00220 vbox->pack_start(*hButtonBox, Gtk::PACK_SHRINK, 0);
00221 buttonCancel = manage( new Gtk::Button(Gtk::Stock::CANCEL));
00222 buttonCancel->signal_clicked().connect(slot(*this,&MoveStock::quit));
00223 hButtonBox->pack_start(*buttonCancel, Gtk::PACK_SHRINK, 0);
00224 buttonNext = manage( new Gtk::Button(Gtk::Stock::GO_FORWARD));
00225 buttonNext->signal_clicked().connect(slot(*this,&MoveStock::finishMoveStock));
00226 hButtonBox->pack_start(*buttonNext, Gtk::PACK_SHRINK, 0);
00227 show_all();
00228 }
00229
00230
00234 void MoveStock::finishMoveStock() {
00235 Gtk::RadioButton *rb;
00236 for (int i = 0; i < radioButtons.size(); i++) {
00237 rb = radioButtons[i];
00238 if (rb->get_active()) {
00239 stockMarketNew = stockMarketVector->getStockMarket(i);
00240 #ifdef DEBUG
00241 cout << "selected " << stockMarketOld->getMarketName() << endl;
00242 #endif
00243 }
00244 }
00245 for (int i = 0; i < radioButtons.size(); i++) {
00246 rb = radioButtons[i];
00247 delete rb;
00248 }
00249 radioButtons.clear();
00250
00251 stockMarketNew->addStock(*stockNew);
00252 quit();
00253 }
00254
00259 bool MoveStock::on_delete_event(GdkEventAny *event) {
00260 quit();
00261 return true;
00262 }