-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddstockwindow.cpp
88 lines (72 loc) · 2.92 KB
/
addstockwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include "addstockwindow.h"
#include "ui_addstockwindow.h"
/* QT libraires */
#include <QMessageBox>
AddStockWindow::AddStockWindow(QWidget *parent, QTableWidget *stockListTableWidget, bool modify) :
QWidget(parent),
ui(new Ui::AddStockWindow),
stockListTableWidget(stockListTableWidget),
modify(modify)
{
ui->setupUi(this);
// Connect to yahoo finance
connect(&yahooFinance, &YahooFinance::transmitExistResult, this, &AddStockWindow::receiveResult);
// Set the name index
ui->selectedIndexComboBox->addItems({"Oil & Gas", "Cars", "Houses", "Green energy", "Manufacturing", "Transport", "Health", "Food", "Technology", "Travel", "Communication", "Banks"});
// If we are going to modify, then display the selected name in the text box
if(modify){
QString nameOfSelectedStock = stockListTableWidget->selectionModel()->selectedRows().at(0).data().toString();
QString nameOfSelectedIndexStock = stockListTableWidget->selectionModel()->selectedRows(1).at(0).data().toString();
ui->stockNameLineEdit->setText(nameOfSelectedStock);
ui->selectedIndexComboBox->setCurrentText(nameOfSelectedIndexStock);
// Disable these
ui->stockNameLineEdit->setEnabled(false);
ui->addToStockListPushButton->setEnabled(false);
// And enable
ui->updateStockPushButton->setEnabled(true);
}else{
ui->updateStockPushButton->setEnabled(false);
}
}
AddStockWindow::~AddStockWindow()
{
delete ui;
}
void AddStockWindow::on_addToStockListPushButton_clicked()
{
// Check if this exist
stockName = ui->stockNameLineEdit->text().toUpper();
stockIndexName = ui->selectedIndexComboBox->currentText();
yahooFinance.stockNameExist(stockName);
}
void AddStockWindow::on_updateStockPushButton_clicked()
{
// Update the stock
int row = stockListTableWidget->currentRow();
stockIndexName = ui->selectedIndexComboBox->currentText();
stockListTableWidget->setItem(row, 1, new QTableWidgetItem(stockIndexName));
}
void AddStockWindow::receiveResult(int result){
// If status error is above 0, then we got an error
if(result > 0){
QMessageBox::warning(this, "Stock name", "No connection to that stock");
}else{
// Check if the stock name exist
int stockAdded = stockListTableWidget->rowCount();
bool exist = false;
for(int i = 0; i < stockAdded; i++){
if(stockName == stockListTableWidget->item(i, 0)->text()){
exist = true;
break;
}
}
// Add new row
if(!exist){
stockListTableWidget->setRowCount(stockAdded + 1);
stockListTableWidget->setItem(stockAdded, 0, new QTableWidgetItem(stockName));
stockListTableWidget->setItem(stockAdded, 1, new QTableWidgetItem(stockIndexName));
}else{
QMessageBox::information(this, "Stock name", "Already exist");
}
}
}