<commit_before>#!/usr/env/python
"""
Author: <NAME>
Date: 2010-08-24
Lisa: Pyqt gui for clipf, with extra functionality.

Copyright (C) 2010 <NAME>

This file is part of Lisa.

Lisa is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Lisa is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lisa. If not, see <http://www.gnu.org/licenses/>.
					
"""
import sys, getopt, time
from PyQt4 import QtCore, QtGui
from lisagui import Ui_frmMain
from datetime import datetime
from databaseaccess import DatabaseAccess

class lisa(QtGui.QDialog, Ui_frmMain):

    def __init__(self, parent=None):
        """ Construct basic QApplication, add widgets and start exec_loop """
        # initialize gui
        QtGui.QDialog.__init__(self, parent)
        self.ui = Ui_frmMain()
        self.ui.setupUi(self) 
        self.ConnectSlots(self.ui)
        self.InitGui()
        self.Layout()
        # initialise the command buffer
        self.cmdbuffer = []

    def ConnectSlots(self, parent):
        """ Connect methods to the signals the gui emits """
        self.ui.btnExit.connect(self.ui.btnExit, QtCore.SIGNAL("clicked()"), self.BtnExit_Clicked)
        self.ui.cmbProduct.connect(self.ui.cmbProduct, QtCore.SIGNAL("currentIndexChanged(QString)"), self.CmbProduct_Changed)
        self.ui.cmbProduct.connect(self.ui.tabDetails, QtCore.SIGNAL("currentChanged(int)"), self.TabDetails_Changed)
        self.ui.cmbProduct.connect(self.ui.btnAdd, QtCore.SIGNAL("clicked()"), self.BtnAdd_Clicked)
        self.ui.cmbProduct.connect(self.ui.cmbMarketCode, QtCore.SIGNAL("currentIndexChanged(QString)"), self.CmbMarketCode_Changed)
        self.ui.cmbProduct.connect(self.ui.cmbStockName, QtCore.SIGNAL("currentIndexChanged(QString)"), self.CmbStockName_Changed)

    def BtnExit_Clicked(self):
        """ Exit """
        sys.exit(0)

    def CmbProduct_Changed(self, selstr):
        """ When the account combo selection changes. """
        self.ui.txtComment.setEnabled(True)
        if selstr == 'bet.place':
            self.ui.tabDetails.currentTabName = self.ui.tabDetails.setCurrentIndex(2)
        elif selstr == 'bet.cashin':
            self.ui.tabDetails.currentTabName = self.ui.tabDetails.setCurrentIndex(3)
        elif selstr == 'invest.buystocks' or selstr == 'invest.sellstocks' or selstr == 'invest.changestocks':
            self.ui.tabDetails.currentTabName = self.ui.tabDetails.setCurrentIndex(1)
            self.UpdateInfoDetails()
            if selstr != 'invest.changestocks':
                self.ui.txtComment.setEnabled(False)
        else:
            self.ui.tabDetails.currentTabName = self.ui.tabDetails.setCurrentIndex(0)

    def TabDetails_Changed(self, index):
        """ What to do if you change a tab. """
        self.CmbProduct_Changed(self.ui.cmbProduct.currentText())

    def InitGui(self):
        """ Initialise fields """
        # Dates
        self.ui.dtDate.setDate(QtCore.QDate.currentDate())
        self.ui.dtDateMatch.setDate(QtCore.QDate.currentDate())
        # Info labels
        self.ui.lblInfoFinance.clear()
        self.ui.lblInfoDetails.clear()
        # Fill all combo boxes
        self.FillCombos()

    def FillCombos(self):
        """ Fill in the combo boxes with values. """
        dba = DatabaseAccess()
        # Teams
        for team in dba.GetTeams():
            self.ui.cmbTeamA.addItem(team)
            self.ui.cmbTeamB.addItem(team)
            self.ui.cmbTeamA2.addItem(team)
            self.ui.cmbTeamB2.addItem(team)
        # Products
        for prod in dba.GetProducts():
            self.ui.cmbProduct.addItem(prod)
        # Accounts
        for acc in dba.GetAccounts():
            self.ui.cmbAccount.addItem(acc)
        # Choice
        self.ui.cmbChoice.addItem('A')
        self.ui.cmbChoice.addItem('B')
        # Market codes
        for mcd in dba.GetMcodes():
            self.ui.cmbMarketCode.addItem(mcd)
        # Stock names
        self.FillCmbStockName()
        dba = None

    def FillCmbStockName(self):
        """ Fill cmb function """
        dba = DatabaseAccess()
        self.ui.cmbStockName.clear()
        for name in dba.GetStockNames(self.ui.cmbMarketCode.currentText()):
            self.ui.cmbStockName.addItem(name)
        dba = None
        
    def CmbMarketCode_Changed(self, selstr):
        """ When the marketcode combo selection changes. """
        self.FillCmbStockName()
    
    def CmbStockName_Changed(self, selstr):
        """ When the stock name selection changes. """    
        self.UpdateInfoDetails()        
        
    def UpdateInfoDetails(self):
        """ Update infolabel details. """
        #TODO: find out why changing the market selection gives list index out of bounds
        dba = DatabaseAccess()
        prod = self.ui.cmbProduct.currentText()
        if prod == 'invest.buystocks' or prod == 'invest.sellstocks' or prod == 'invest.changestocks':
            info = dba.GetStockInfo(self.ui.cmbStockName.currentText())
            self.ui.lblInfoDetails.setText('[' + info[1] + '] : ' + info[0])
        dba = None

    def BtnAdd_Clicked(self):
        """ Create the command to send to clipf and add it to the buffer. """
        # parse comment?
        prod = self.ui.cmbProduct.currentText() 
        if prod == 'bet.place':
            comment = self.ui.cmbTeamA.currentText() + ',' + self.ui.cmbTeamB.currentText() + ',' + self.ui.cmbChoice.currentText() + ',' + self.ui.dtDateMatch.date().toString(QtCore.Qt.ISODate)
        elif prod == 'bet.cashin':
            comment = ''
        elif prod == 'invest.buystocks' or prod == 'invest.sellstocks' or prod == 'invest.changestocks':
            comment = self.ui.cmbMarketCode.currentText() + '.' + self.ui.cmbStockName.currentText() + ',' + self.ui.spnQuantity.textFromValue(self.ui.spnQuantity.value()) + ',' + self.ui.spnPrice.textFromValue(self.ui.spnPrice.value())
            if prod == 'invest.changestocks':
                comment = comment + ',' + self.ui.txtComment.text()
        else:
            comment = self.ui.txtComment.text() 
        cmd = 'op add -d ' + self.ui.dtDate.date().toString(QtCore.Qt.ISODate) + ' ' +  self.ui.cmbProduct.currentText() + ' ' + self.ui.spnAmount.textFromValue(self.ui.spnAmount.value()) + ' "' + comment + '"'
        self.cmdbuffer.append(cmd)
        print self.cmdbuffer
        self.ui.txtSummary.append(cmd)
    
    def Layout(self):
        """ Everything about the layout off the application. """
        print 'Layout not implemented yet...'
        # Theming?

class MainWrapper():
    """ Parsed options get there functionality here, it's seperate from the gui part of the app. """ <commit_msg>Fixed the bug with the list index error on market change.<commit_after>#!/usr/env/python
"""
Author: <NAME>
Date: 2010-08-24
Lisa: Pyqt gui for clipf, with extra functionality.

Copyright (C) 2010 <NAME>

This file is part of Lisa.

Lisa is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Lisa is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lisa. If not, see <http://www.gnu.org/licenses/>.
					
"""
import sys, getopt, time
from PyQt4 import QtCore, QtGui
from lisagui import Ui_frmMain
from datetime import datetime
from databaseaccess import DatabaseAccess

class lisa(QtGui.QDialog, Ui_frmMain):

    def __init__(self, parent=None):
        """ Construct basic QApplication, add widgets and start exec_loop """
        # initialize gui
        QtGui.QDialog.__init__(self, parent)
        self.ui = Ui_frmMain()
        self.ui.setupUi(self) 
        self.ConnectSlots(self.ui)
        self.InitGui()
        self.Layout()
        # initialise the command buffer
        self.cmdbuffer = []

    def ConnectSlots(self, parent):
        """ Connect methods to the signals the gui emits """
        self.ui.btnExit.connect(self.ui.btnExit, QtCore.SIGNAL("clicked()"), self.BtnExit_Clicked)
        self.ui.cmbProduct.connect(self.ui.cmbProduct, QtCore.SIGNAL("currentIndexChanged(QString)"), self.CmbProduct_Changed)
        self.ui.cmbProduct.connect(self.ui.tabDetails, QtCore.SIGNAL("currentChanged(int)"), self.TabDetails_Changed)
        self.ui.cmbProduct.connect(self.ui.btnAdd, QtCore.SIGNAL("clicked()"), self.BtnAdd_Clicked)
        self.ui.cmbProduct.connect(self.ui.cmbMarketCode, QtCore.SIGNAL("currentIndexChanged(QString)"), self.CmbMarketCode_Changed)
        self.ui.cmbProduct.connect(self.ui.cmbStockName, QtCore.SIGNAL("currentIndexChanged(QString)"), self.CmbStockName_Changed)

    def BtnExit_Clicked(self):
        """ Exit """
        sys.exit(0)

    def CmbProduct_Changed(self, selstr):
        """ When the account combo selection changes. """
        self.ui.txtComment.setEnabled(True)
        if selstr == 'bet.place':
            self.ui.tabDetails.currentTabName = self.ui.tabDetails.setCurrentIndex(2)
        elif selstr == 'bet.cashin':
            self.ui.tabDetails.currentTabName = self.ui.tabDetails.setCurrentIndex(3)
        elif selstr == 'invest.buystocks' or selstr == 'invest.sellstocks' or selstr == 'invest.changestocks':
            self.ui.tabDetails.currentTabName = self.ui.tabDetails.setCurrentIndex(1)
            self.UpdateInfoDetails()
            if selstr != 'invest.changestocks':
                self.ui.txtComment.setEnabled(False)
        else:
            self.ui.tabDetails.currentTabName = self.ui.tabDetails.setCurrentIndex(0)

    def TabDetails_Changed(self, index):
        """ What to do if you change a tab. """
        self.CmbProduct_Changed(self.ui.cmbProduct.currentText())

    def InitGui(self):
        """ Initialise fields """
        # Dates
        self.ui.dtDate.setDate(QtCore.QDate.currentDate())
        self.ui.dtDateMatch.setDate(QtCore.QDate.currentDate())
        # Info labels
        self.ui.lblInfoFinance.clear()
        self.ui.lblInfoDetails.clear()
        # Fill all combo boxes
        self.FillCombos()

    def FillCombos(self):
        """ Fill in the combo boxes with values. """
        dba = DatabaseAccess()
        # Teams
        for team in dba.GetTeams():
            self.ui.cmbTeamA.addItem(team)
            self.ui.cmbTeamB.addItem(team)
            self.ui.cmbTeamA2.addItem(team)
            self.ui.cmbTeamB2.addItem(team)
        # Products
        for prod in dba.GetProducts():
            self.ui.cmbProduct.addItem(prod)
        # Accounts
        for acc in dba.GetAccounts():
            self.ui.cmbAccount.addItem(acc)
        # Choice
        self.ui.cmbChoice.addItem('A')
        self.ui.cmbChoice.addItem('B')
        # Market codes
        for mcd in dba.GetMcodes():
            self.ui.cmbMarketCode.addItem(mcd)
        # Stock names
        self.FillCmbStockName()
        dba = None

    def FillCmbStockName(self):
        """ Fill cmb function """
        dba = DatabaseAccess()
        self.ui.cmbStockName.clear()
        for name in dba.GetStockNames(self.ui.cmbMarketCode.currentText()):
            self.ui.cmbStockName.addItem(name)
        dba = None
        
    def CmbMarketCode_Changed(self, selstr):
        """ When the marketcode combo selection changes. """
        self.FillCmbStockName()
    
    def CmbStockName_Changed(self, selstr):
        """ When the stock name selection changes. """    
        self.UpdateInfoDetails()        
        
    def UpdateInfoDetails(self):
        """ Update infolabel details. """
        #TODO: find out why changing the market selection gives list index out of bounds
        dba = DatabaseAccess()
        prod = self.ui.cmbProduct.currentText()
        stock = self.ui.cmbStockName.currentText()
        if (prod == 'invest.buystocks' or prod == 'invest.sellstocks' or prod == 'invest.changestocks') and stock != '':
            info = dba.GetStockInfo(stock)
            self.ui.lblInfoDetails.setText('[' + info[1] + '] : ' + info[0])
        dba = None

    def BtnAdd_Clicked(self):
        """ Create the command to send to clipf and add it to the buffer. """
        # parse comment?
        prod = self.ui.cmbProduct.currentText() 
        if prod == 'bet.place':
            comment = self.ui.cmbTeamA.currentText() + ',' + self.ui.cmbTeamB.currentText() + ',' + self.ui.cmbChoice.currentText() + ',' + self.ui.dtDateMatch.date().toString(QtCore.Qt.ISODate)
        elif prod == 'bet.cashin':
            comment = ''
        elif prod == 'invest.buystocks' or prod == 'invest.sellstocks' or prod == 'invest.changestocks':
            comment = self.ui.cmbMarketCode.currentText() + '.' + self.ui.cmbStockName.currentText() + ',' + self.ui.spnQuantity.textFromValue(self.ui.spnQuantity.value()) + ',' + self.ui.spnPrice.textFromValue(self.ui.spnPrice.value())
            if prod == 'invest.changestocks':
                comment = comment + ',' + self.ui.txtComment.text()
        else:
            comment = self.ui.txtComment.text() 
        cmd = 'op add -d ' + self.ui.dtDate.date().toString(QtCore.Qt.ISODate) + ' ' +  self.ui.cmbProduct.currentText() + ' ' + self.ui.spnAmount.textFromValue(self.ui.spnAmount.value()) + ' "' + comment + '"'
        self.cmdbuffer.append(cmd)
        print self.cmdbuffer
        self.ui.txtSummary.append(cmd)
    
    def Layout(self):
        """ Everything about the layout off the application. """
        print 'Layout not implemented yet...'
        # Theming?

class MainWrapper():
    """ Parsed options get there functionality here, it's seperate from the gui part of the app. """ 
{'fraction_non_alphanumeric': 0.11852994809931267, 'fraction_numerical': 0.0032262589423481553, 'mean_word_length': 2.6552166111253523, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1926399', 'n_tokens_mistral': 4468, 'n_tokens_neox': 4319, 'n_words': 990}