def calc(self):

        if self.calcTimeStart:
            tdelta = datetime.now() - self.calcTimeStart
            timeSum = self.calcTimeSum + tdelta
        else:
            timeSum = self.calcTimeSum

        self.timeEdit.setTime(self.timedelta2qtime(timeSum))

        startBalance = self.removeFormat(self.startBalance.text())
        if guitools.isInt(startBalance):
            startBalance = int(startBalance)
        else:
            return

        currentBalance = self.removeFormat(self.currentBalance.text())
        if guitools.isInt(currentBalance):
            currentBalance = int(currentBalance)
        else:
            return

        totalProfit = currentBalance - startBalance
        self.totalProfit.setText(self.formatImput(totalProfit))

        if timeSum.total_seconds() <= 0:
            return

        hourProfit = (totalProfit / timeSum.total_seconds()) * 60 * 60
        hourProfit = int(round(hourProfit, 0))

        self.hourProfit.setText(self.formatImput(hourProfit))

        cargoSpace = self.removeFormat(self.cargoSpace.text())
        if guitools.isInt(cargoSpace):
            cargoSpace = int(cargoSpace)
        else:
            return

        cargoProfit = totalProfit / cargoSpace
        self.tonProfit.setText(self.formatImput(int(round(cargoProfit, 0))))

        cargoHourProfit = (cargoProfit / timeSum.total_seconds()) * 60 * 60
        self.tonHourProfit.setText(
            self.formatImput(int(round(cargoHourProfit, 0))))
    def calc(self):

        if self.calcTimeStart:
            tdelta = datetime.now() - self.calcTimeStart
            timeSum = self.calcTimeSum + tdelta
        else:
            timeSum = self.calcTimeSum

        self.timeEdit.setTime( self.timedelta2qtime(timeSum) )

        startBalance = self.removeFormat(self.startBalance.text())
        if guitools.isInt( startBalance ):
            startBalance = int( startBalance )
        else:
            return

        currentBalance = self.removeFormat(self.currentBalance.text())
        if guitools.isInt(currentBalance):
            currentBalance = int(currentBalance)
        else:
            return

        totalProfit = currentBalance - startBalance
        self.totalProfit.setText( self.formatImput(totalProfit) )

        if timeSum.total_seconds() <= 0:
            return

        hourProfit = ( totalProfit / timeSum.total_seconds() ) * 60 * 60
        hourProfit = int(round(hourProfit, 0))

        self.hourProfit.setText( self.formatImput(hourProfit) )

        cargoSpace = self.removeFormat(self.cargoSpace.text())
        if guitools.isInt(cargoSpace):
            cargoSpace = int(cargoSpace)
        else:
            return

        cargoProfit = totalProfit / cargoSpace
        self.tonProfit.setText( self.formatImput( int(round(cargoProfit, 0)) ) )

        cargoHourProfit = ( cargoProfit / timeSum.total_seconds() ) * 60 * 60
        self.tonHourProfit.setText( self.formatImput(int(round(cargoHourProfit, 0))) )
    def saveItems(self):
        self.parent.lockDB()
        failColor = QtGui.QColor(255, 0, 0, 180)
        system = self.systemLineEdit.text()
        systemID = self.mydb.getSystemIDbyName(system)
    
        station = self.stationLineEdit.text()
        stationID = self.mydb.getStationID(systemID, station)
        if not stationID:
            self.parent.unlockDB()
            return
        cur = self.mydb.cursor()
        modifiedDate = datetime.utcnow()
        
        for row in range(0, self.tableWidget.rowCount()):
            itemID = self.getSelectedItemId(row)
            stationBuy = self.tableWidget.item(row, self.headerList.index("Sell")).text()
            stationSell = self.tableWidget.item(row, self.headerList.index("Buy")).text()
            stock = self.tableWidget.item(row, self.headerList.index("Stock")).text()

            if not itemID or not guitools.isInt( stationBuy ) or not guitools.isInt( stationSell ) or not guitools.isInt( stock ):
                #print("fail", stationBuy, stationSell, stock)
                if not guitools.isInt( stationBuy ):
                    self.tableWidget.item(row, self.headerList.index("Sell")).setBackground( failColor )
                if not guitools.isInt( stationSell ):
                    self.tableWidget.item(row, self.headerList.index("Buy")).setBackground( failColor )
                if not guitools.isInt( stock ):
                    self.tableWidget.item(row, self.headerList.index("Stock")).setBackground( failColor )

                self.tableWidget.dataChanged(self.tableWidget.indexFromItem(self.tableWidget.item(row, 0)),
                                             self.tableWidget.indexFromItem(self.tableWidget.item(row, len(self.headerList))))

                msg = "Save Fail"
                msgBox = QtGui.QMessageBox(QtGui.QMessageBox.Information, "Error", msg,
                        QtGui.QMessageBox.NoButton, self)
        
                msgBox.exec_()
                self.parent.unlockDB()
                return

            else:

                cur.execute( "UPDATE price SET StationBuy=?, StationSell=?, Stock=?, modified=?, source=6 where StationID=? AND ItemID=?",
                                            (stationBuy, stationSell, stock, modifiedDate, stationID, itemID))

                if cur.rowcount <= 0:
                    print("no update, insert")
                    cur.execute("insert or IGNORE into price (SystemID, StationID, ItemID, StationBuy, StationSell, Stock, modified, source) values (?,?,?,?,?,?,?,6) ",
                                                            (systemID, stationID, itemID, stationBuy, stationSell, stock, modifiedDate))
            
                cur.execute("select id from price where StationID = ? AND ItemID = ? limit 1", ( stationID, itemID))
                priceID = cur.fetchone()[0]

                ''' blackmarket '''
                blackmarkedCheckBox = guitools.getChildByType(self.tableWidget.cellWidget(row, self.headerList.index("Blackmarket")), QtGui.QCheckBox)
                if blackmarkedCheckBox.isChecked():
                    self.mydb.setBlackmarketPrice(priceID)
                else:
                    self.mydb.removeBlackmarketPrice(priceID)

                ''' ignore '''
                ignoreCheckBox = guitools.getChildByType(self.tableWidget.cellWidget(row, self.headerList.index("Ignore")), QtGui.QCheckBox)
                if ignoreCheckBox.isChecked():
                    self.mydb.setIgnorePriceTemp(priceID)
                else:
                    self.mydb.removeIgnorePrice(priceID)

                ''' fake '''
                fakeCheckBox = guitools.getChildByType(self.tableWidget.cellWidget(row, self.headerList.index("Fake")), QtGui.QCheckBox)
                if fakeCheckBox.isChecked():
                    self.mydb.setFakePrice(priceID)
                else:
                    self.mydb.removeFakePrice(priceID)

                self.mydb.addSystemsInDistanceToDealsInDistancesCacheQueue(systemID, itemID)

        self.mydb.addSystemToDealsInDistancesCacheQueue( [{'id': systemID}] )
                
        self.mydb.con.commit()
        cur.close()
        self.parent.unlockDB()
 def formatImput(self, text):
     if guitools.isInt(self.removeFormat(text)):
         return "{:,}".format(int(self.removeFormat(text)))
     else:
         return text
 def formatImput(self, text):
     if guitools.isInt(self.removeFormat(text)):
         return "{:,}".format( int( self.removeFormat(text)))
     else:
         return text