# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file '20210503hw.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
import yF_Kbar as kbar



class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(1126, 747)
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(730, 360, 93, 28))
        self.pushButton.setObjectName("pushButton")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(380, 360, 91, 31))
        self.label.setObjectName("label")
        self.tableWidget = QtWidgets.QTableWidget(Dialog)
        self.tableWidget.setGeometry(QtCore.QRect(210, 450, 791, 281))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(220, 50, 791, 261))
        self.label_2.setText("")
        self.label_2.setObjectName("label_2")
        self.lineEdit = QtWidgets.QLineEdit(Dialog)
        self.lineEdit.setGeometry(QtCore.QRect(510, 350, 141, 41))
        self.lineEdit.setObjectName("lineEdit")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)
    
        

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.pushButton.setText(_translate("Dialog", "畫圖"))
        self.label.setText(_translate("Dialog", "讀取筆數"))


        self.pushButton.clicked.connect(self.onclick)


    def onclick(self):
        num = int(self.lineEdit.text()) # 讀取 lineEdit 中的文字
        df = kbar.import_csv(num) 
        self.change_data(df)



    def change_data(self, df):
        # 根據lineEdit 中的文字，呼叫 yF_Kbar 製作 K 線圖
        kbar.draw_candle_chart(df) 
        self.label_2.setPixmap(QtGui.QPixmap("stock_Kbar.png")) # label 置換圖片

        columns_num = df.shape[1] # DataFrame 的 欄位數
        index_num = df.shape[0] # DataFrame 的 列數
        df_columns = df.columns # DataFrame 的 欄位名稱
        df_index = df.index # DataFrame 的 索引列表
        
        self.tableWidget.setColumnCount(columns_num) # 修改 Table Wedget 的欄位數
        self.tableWidget.setRowCount(index_num) # 修改 Table Wedget 的列數
        
        _translate = QtCore.QCoreApplication.translate
        
        # 修改欄位相關資訊
        for c in range(columns_num):
            item = QtWidgets.QTableWidgetItem()
            self.tableWidget.setHorizontalHeaderItem(c, item) # 依據欄位列表依序建構欄位
            
            item = self.tableWidget.horizontalHeaderItem(c) # 選取該欄位
            item.setText(_translate("Dialog", df_columns[c])) # 修改欄位標題文字
            
        for i in range(index_num):
            item = QtWidgets.QTableWidgetItem()
            self.tableWidget.setVerticalHeaderItem(i, item) # 依據索引列表依序建構欄位
            
            item = self.tableWidget.verticalHeaderItem(i) # 選取該索引
            item.setText(_translate("Dialog", str(df_index[i]) )) # 修改索引標題文字
            
        for c in range(columns_num): # 走訪欄位
            for i in range(index_num): # 走訪索引
                item = QtWidgets.QTableWidgetItem()
                self.tableWidget.setItem(i, c, item) # 建構儲存格

                item = self.tableWidget.item(i, c) # 選取儲存格
                item.setText(_translate("Dialog", str(df.iloc[i, c]))) # 修改儲存格文字


if __name__ == "__main__":
    data=[ i.strip('\n').split(',') for i in open('./0050.csv') ]
    stock_id = "0000"
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

