Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding=utf-8
- # Form implementation generated from reading ui file 'account_balance.ui'
- #
- # Created: Sat Apr 26 07:21:53 2014
- # by: PyQt4 UI code generator 4.10.4
- #
- # WARNING! All changes made in this file will be lost!
- from PyQt4 import QtCore, QtGui
- import sys
- import pickle
- try:
- _fromUtf8 = QtCore.QString.fromUtf8
- except AttributeError:
- def _fromUtf8(s):
- return s
- try:
- _encoding = QtGui.QApplication.UnicodeUTF8
- def _translate(context, text, disambig):
- return QtGui.QApplication.translate(context, text, disambig, _encoding)
- except AttributeError:
- def _translate(context, text, disambig):
- return QtGui.QApplication.translate(context, text, disambig)
- ############### ACCOUNT CLASS ###############
- class Account():
- """ACCOUNT Instance Class"""
- def __init__(self):
- self.data = pickle.load(open("personal_account.p", "rb"))
- def __getitem__(self, item):
- return self.data[item]
- def __setitem__(self, item, value):
- self.data[item] = value
- def __delitem__(self, key):
- del self.data[key]
- def inserItem(self,name, value, date, reason):
- try:
- total = self.data[name][0] + value
- entries = self.data[name][1]
- if date != None:
- entries.append([value, date, reason])
- self.data.update({name:[total, entries]})
- except:
- self.data.update({name:[value, [[value, date, reason]]]})
- BALANCE.table_send()
- def deleteEntry(self,name, id):
- value = self.data[name][1][id][0]
- self.data[name][1].pop(id)
- self.inserItem(name, -value, None, None)
- if len(self.data[name][1]) == 0:
- self.data.pop(name)
- return True
- return False
- ############### GRAPHIC USER INTERFACE ###############
- class MainFrame(QtGui.QWidget):
- """MainFrame Instance Class"""
- def __init__(self):
- QtGui.QWidget.__init__(self)
- self.setupUi(self)
- def setupUi(self, MainFrame):
- MainFrame.resize(533, 435)
- MainFrame.setMaximumSize(QtCore.QSize(533, 435))
- MainFrame.setMinimumSize(QtCore.QSize(533, 435))
- MainFrame.setWindowTitle("ACCOUNT Balance")
- MainFrame.setWindowIcon(QtGui.QIcon(QtGui.QPixmap("sync.ico")))
- self.tabsFrame = QtGui.QTabWidget(MainFrame)
- self.tabsFrame.setGeometry(QtCore.QRect(0, 0, 541, 401))
- self.i = 0
- self.tabsFrame.currentChanged.connect(self.hidethings)
- self.BalanceTab = QtGui.QWidget()
- self.tabsFrame.addTab(self.BalanceTab, "Balance")
- self.LoansTab = QtGui.QWidget()
- self.tabsFrame.addTab(self.LoansTab, "Loans")
- self.BorrowsTab = QtGui.QWidget()
- self.tabsFrame.addTab(self.BorrowsTab, "Borrows")
- self.tabsFrame.setCurrentIndex(0)
- self.label_balance = QtGui.QLabel(MainFrame)
- self.label_balance.setAlignment(QtCore.Qt.AlignRight | QtCore.Qt.AlignTrailing | QtCore.Qt.AlignVCenter)
- font = QtGui.QFont()
- font.setPointSize(10)
- font.setFamily(_fromUtf8("Batang"))
- self.label_balance.setFont(font)
- self.label_balance.setGeometry(QtCore.QRect(0, 410, 61, 16))
- self.label_balance.setText("Balance:")
- self.box_balance = QtGui.QLineEdit(MainFrame)
- self.box_balance.setDragEnabled(False)
- self.box_balance.setFocusPolicy(QtCore.Qt.NoFocus)
- self.box_balance.setFont(font)
- self.box_balance.setGeometry(QtCore.QRect(65, 410, 71, 20))
- self.box_balance.setMaximumSize(QtCore.QSize(461, 20))
- self.box_balance.setMaxLength(75)
- self.box_balance.setMinimumSize(QtCore.QSize(50, 20))
- self.box_balance.setReadOnly(True)
- def hidethings(self, tab):
- def hideBalance():
- BALANCE.box_name.hide()
- BALANCE.btn_rename.hide()
- BALANCE.table_send2(None)
- BALANCE.label_entrynum.setText("")
- BALANCE.label_balancevalue.setText("")
- def hideLoans():
- LOANS.box_name.setText("")
- LOANS.box_date.setDate(QtCore.QDate.currentDate())
- LOANS.box_value.setText("")
- LOANS.box_reason.setText("")
- def hideBorrows():
- BORROWS.box_name.setText("")
- BORROWS.box_date.setDate(QtCore.QDate.currentDate())
- BORROWS.box_value.setText("")
- BORROWS.box_reason.setText("")
- if self.i != 0:
- BALANCE.table_send()
- if tab == 0:
- hideLoans()
- hideBorrows()
- elif tab == 1:
- hideBalance()
- hideBorrows()
- elif tab == 2:
- hideBalance()
- hideLoans()
- else:
- self.i += 1
- class BalanceFrame(QtGui.QWidget):
- """BalanceFrame Instance Class"""
- def __init__(self, MainFrame):
- QtGui.QWidget.__init__(self)
- self.setupUi(MainFrame)
- def setupUi(self, MainFrame):
- self.box_name = QtGui.QLineEdit(MainFrame.BalanceTab)
- self.box_name.setGeometry(QtCore.QRect(230, 10, 113, 20))
- self.box_name.hide()
- # self.box_name.textChanged.connect(self.rename)
- self.label_balance = QtGui.QLabel(MainFrame.BalanceTab)
- self.label_balance.setGeometry(QtCore.QRect(180, 50, 46, 13))
- self.label_balance.setText("Balance:")
- self.label_balancevalue = QtGui.QLabel(MainFrame.BalanceTab)
- self.label_balancevalue.setGeometry(QtCore.QRect(225, 49, 100, 16))
- self.label_entries = QtGui.QLabel(MainFrame.BalanceTab)
- self.label_entries.setGeometry(QtCore.QRect(180, 78, 100, 13))
- self.label_entries.setText("Entries:")
- self.label_entrynum = QtGui.QLabel(MainFrame.BalanceTab)
- self.label_entrynum.setGeometry(QtCore.QRect(220, 78, 20, 16))
- self.btn_rename = QtGui.QPushButton(MainFrame.BalanceTab)
- self.btn_rename.setGeometry(QtCore.QRect(350, 10, 51, 23))
- self.btn_rename.setText('Rename')
- self.btn_rename.hide()
- self.btn_rename.clicked.connect(self.rename)
- font = QtGui.QFont()
- font.setPointSize(11)
- self.label_name = QtGui.QLabel(MainFrame.BalanceTab)
- self.label_name.setFont(font)
- self.label_name.setGeometry(QtCore.QRect(180, 12, 61, 21))
- self.label_name.setText("Name:")
- self.line_separator = QtGui.QFrame(MainFrame.BalanceTab)
- self.line_separator.setFrameShadow(QtGui.QFrame.Sunken)
- self.line_separator.setFrameShape(QtGui.QFrame.HLine)
- self.line_separator.setGeometry(QtCore.QRect(160, 30, 371, 16))
- self.table_model = QtGui.QStandardItemModel(0,0)
- self.table_name = QtGui.QTableView(MainFrame.BalanceTab)
- self.table_name.horizontalHeader().setDefaultSectionSize(72)
- self.table_name.horizontalHeader().setHighlightSections(False)
- self.table_name.horizontalHeader().setSortIndicatorShown(False)
- self.table_name.horizontalHeader().setStretchLastSection(True)
- self.table_name.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
- self.table_name.setGeometry(QtCore.QRect(10, 10, 151, 361))
- self.table_name.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
- self.table_name.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
- self.table_name.setSortingEnabled(True)
- self.table_name.verticalHeader().setCascadingSectionResizes(False)
- self.table_name.verticalHeader().setDefaultSectionSize(20)
- self.table_name.verticalHeader().setHighlightSections(True)
- self.table_name.verticalHeader().setMinimumSectionSize(20)
- self.table_name.setModel(self.table_model)
- self.table_modelSelection = self.table_name.selectionModel()
- self.table_modelSelection.selectionChanged.connect(self.selection)
- self.table_model2 = QtGui.QStandardItemModel(0, 0)
- self.table_entries = QtGui.QTableView(MainFrame.BalanceTab)
- self.table_entries.setGeometry(QtCore.QRect(180, 110, 341, 241))
- self.table_entries.horizontalHeader().setStretchLastSection(True)
- self.table_entries.verticalHeader().setVisible(False)
- self.table_entries.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
- self.table_entries.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
- self.table_entries.setModel(self.table_model2)
- self.table_entries.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
- self.table_modelSelection2 = self.table_entries.selectionModel()
- self.table_modelSelection2.selectionChanged.connect(self.selection2)
- self.btn_delentry = QtGui.QPushButton(MainFrame.BalanceTab)
- self.btn_delentry.setGeometry(QtCore.QRect(450, 353, 71, 21))
- self.btn_delentry.setText("Delete Entry")
- self.btn_delentry.clicked.connect(self.delentry)
- self.name = None
- self.entry = None
- self.table_send()
- def rename(self):
- oldname = str(self.name)
- newname = str(self.box_name.text())
- ACCOUNT[newname] = ACCOUNT[oldname]
- del ACCOUNT[oldname]
- self.table_send()
- self.table_send2(newname)
- def delentry(self):
- if self.entry != None:
- isEmpty = ACCOUNT.deleteEntry(self.name,self.entry)
- if isEmpty == True:
- self.box_name.setText("")
- self.label_balancevalue.setText("")
- self.label_entrynum.setText("")
- self.table_send2(None)
- self.btn_rename.hide()
- self.box_name.hide()
- else:
- self.label_balancevalue.setText("R$ %.2f" % ACCOUNT.data[self.name][0])
- self.label_entrynum.setText(str(len(ACCOUNT.data[self.name][1])))
- self.box_name.setText(self.name)
- self.table_send2(self.name)
- self.table_send()
- def selection(self, selection):
- self.name = str(selection.indexes()[0].data().toPyObject())
- self.box_name.setText(self.name)
- self.box_name.show()
- def test():
- self.btn_rename.show()
- self.box_name.cursorPositionChanged.connect(test)
- self.label_balancevalue.setText("R$ %.2f" % ACCOUNT.data[self.name][0])
- self.label_entrynum.setText(str(len(ACCOUNT.data[self.name][1])))
- self.table_send2(self.name)
- self.btn_rename.hide()
- def selection2(self, selection, deselected):
- self.entry = int(str(selection.indexes()[0].data().toPyObject())[2:])-1
- def table_send(self):
- balance = 0
- self.table_model.clear()
- for i, n in enumerate(ACCOUNT.data):
- balance += ACCOUNT.data[n][0]
- self.table_model.insertRow(i,[QtGui.QStandardItem(str(n)),QtGui.QStandardItem("R$ %.2f"%(ACCOUNT.data[n][
- 0]))])
- self.table_model.setHeaderData(i, QtCore.Qt.Vertical,str(n))
- MAIN.box_balance.setText("R$ %.2f"%balance)
- self.table_model.setHeaderData(0, QtCore.Qt.Horizontal, "Name")
- self.table_model.setHeaderData(1, QtCore.Qt.Horizontal, "Balance")
- self.table_name.verticalHeader().hide()
- def table_send2(self,name):
- self.table_model2.clear()
- if name != None:
- self.table_entries.horizontalHeader().setResizeMode(QtGui.QHeaderView.ResizeToContents)
- for i,items in enumerate(ACCOUNT.data[name][1]):
- entry = [QtGui.QStandardItem(" "+str(i+1))]
- for item in items:
- if isinstance(item,float):
- entry.append(QtGui.QStandardItem(" R$ %.2f "%item))
- elif item == ACCOUNT.data[name][1][i][2]:
- entry.append(QtGui.QStandardItem(str(item) + " "))
- else:
- entry.append(QtGui.QStandardItem(" "+str(item)+" "))
- self.table_model2.insertRow(i,entry)
- self.table_model2.setHeaderData(0, QtCore.Qt.Horizontal, "ID")
- self.table_model2.setHeaderData(1, QtCore.Qt.Horizontal, "Value")
- self.table_model2.setHeaderData(2, QtCore.Qt.Horizontal, "Date")
- self.table_model2.setHeaderData(3, QtCore.Qt.Horizontal, "Reason")
- self.table_entries.verticalHeader().hide()
- class LoansFrame(QtGui.QWidget):
- """LoansFrame Instance Class"""
- def __init__(self, MainFrame):
- QtGui.QWidget.__init__(self)
- self.setupUi(MainFrame)
- def setupUi(self, MainFrame):
- self.label_from = QtGui.QLabel(MainFrame.LoansTab)
- self.label_from.setGeometry(QtCore.QRect(80, 110, 46, 13))
- self.label_from.setText("From")
- self.box_name = QtGui.QLineEdit(MainFrame.LoansTab)
- self.box_name.setFocusPolicy(QtCore.Qt.ClickFocus)
- self.box_name.setGeometry(QtCore.QRect(130, 110, 113, 20))
- self.box_name.setPlaceholderText("Enter Name...")
- self.label_date = QtGui.QLabel(MainFrame.LoansTab)
- self.label_date.setGeometry(QtCore.QRect(80, 150, 46, 13))
- self.label_date.setText("Date")
- self.box_date = QtGui.QDateEdit(MainFrame.LoansTab)
- self.box_date.setFocusPolicy(QtCore.Qt.ClickFocus)
- self.box_date.setGeometry(QtCore.QRect(130, 150, 113, 20))
- self.box_date.setDate(QtCore.QDate.currentDate())
- self.box_date.setCalendarPopup(True)
- self.label_value = QtGui.QLabel(MainFrame.LoansTab)
- self.label_value.setGeometry(QtCore.QRect(80, 190, 46, 13))
- self.label_value.setText("Value")
- self.box_value = QtGui.QLineEdit(MainFrame.LoansTab)
- self.box_value.setGeometry(QtCore.QRect(130, 190, 113, 20))
- self.label_reason = QtGui.QLabel(MainFrame.LoansTab)
- self.label_reason.setGeometry(QtCore.QRect(80, 260, 46, 13))
- self.label_reason.setText("Reason")
- self.box_reason = QtGui.QLineEdit(MainFrame.LoansTab)
- self.box_reason.setGeometry(QtCore.QRect(130, 260, 113, 20))
- self.btn_return = QtGui.QPushButton(MainFrame.LoansTab)
- self.btn_return.setGeometry(QtCore.QRect(270, 130, 91, 23))
- self.btn_return.setText("Register Return")
- self.btn_lend = QtGui.QPushButton(MainFrame.LoansTab)
- self.btn_lend.setGeometry(QtCore.QRect(270, 260, 91, 23))
- self.btn_lend.setText("Register Lend")
- class BorrowsFrame(QtGui.QWidget):
- """BorrowsFrame Instance Class"""
- def __init__(self, MainFrame):
- QtGui.QWidget.__init__(self)
- self.setupUi(MainFrame)
- def setupUi(self, MainFrame):
- self.label_from = QtGui.QLabel(MainFrame.BorrowsTab)
- self.label_from.setGeometry(QtCore.QRect(80, 110, 46, 13))
- self.label_from.setText("From")
- self.box_name = QtGui.QLineEdit(MainFrame.BorrowsTab)
- self.box_name.setFocusPolicy(QtCore.Qt.ClickFocus)
- self.box_name.setGeometry(QtCore.QRect(130, 110, 113, 20))
- self.box_name.setPlaceholderText("Enter Name...")
- self.label_date = QtGui.QLabel(MainFrame.BorrowsTab)
- self.label_date.setGeometry(QtCore.QRect(80, 150, 46, 13))
- self.label_date.setText("Date")
- self.box_date = QtGui.QDateEdit(MainFrame.BorrowsTab)
- self.box_date.setFocusPolicy(QtCore.Qt.ClickFocus)
- self.box_date.setGeometry(QtCore.QRect(130, 150, 113, 20))
- self.box_date.setDate(QtCore.QDate.currentDate())
- self.box_date.setCalendarPopup(True)
- self.label_value = QtGui.QLabel(MainFrame.BorrowsTab)
- self.label_value.setGeometry(QtCore.QRect(80, 190, 46, 13))
- self.label_value.setText("Value")
- self.box_value = QtGui.QLineEdit(MainFrame.BorrowsTab)
- self.box_value.setGeometry(QtCore.QRect(130, 190, 113, 20))
- self.label_reason = QtGui.QLabel(MainFrame.BorrowsTab)
- self.label_reason.setGeometry(QtCore.QRect(80, 260, 46, 13))
- self.label_reason.setText("Reason")
- self.box_reason = QtGui.QLineEdit(MainFrame.BorrowsTab)
- self.box_reason.setGeometry(QtCore.QRect(130, 260, 113, 20))
- self.btn_return = QtGui.QPushButton(MainFrame.BorrowsTab)
- self.btn_return.setGeometry(QtCore.QRect(270, 130, 91, 23))
- self.btn_return.setText("Register Return")
- self.btn_borrow = QtGui.QPushButton(MainFrame.BorrowsTab)
- self.btn_borrow.setGeometry(QtCore.QRect(270, 260, 91, 23))
- self.btn_borrow.setText("Register Borrow")
- class PopUp(QtGui.QWidget):
- """PopUp Instance Class"""
- def __init__(self, MainFrame, BalanceFrame, LoansFrame, BorrowsFrame):
- QtGui.QWidget.__init__(self)
- self.setupUi(MainFrame, BalanceFrame, LoansFrame, BorrowsFrame)
- def setupUi(self, MainFrame, BalanceFrame, LoansFrame, BorrowsFrame):
- self.table_names = QtCore.QStringList()
- self.createCompleter(LoansFrame, BorrowsFrame)
- self.connections(MainFrame, BalanceFrame, LoansFrame, BorrowsFrame)
- def createCompleter(self, LoansFrame, BorrowsFrame):
- self.table_names = self.suggestions()
- self.completer = QtGui.QCompleter(self.table_names)
- self.regex = QtCore.QRegExp("^(d\.(?:\d\.)?\d{1,3})|((?:\d\.)?\d{1,3}),\d{2}$")
- self.loansvalidator = QtGui.QRegExpValidator(self.regex, LoansFrame.box_value)
- LoansFrame.box_name.setCompleter(self.completer)
- LoansFrame.box_value.setValidator(self.loansvalidator)
- self.borrowsvalidator = QtGui.QRegExpValidator(self.regex, BorrowsFrame.box_value)
- BorrowsFrame.box_name.setCompleter(self.completer)
- BorrowsFrame.box_value.setValidator(self.borrowsvalidator)
- def connections(self, MainFrame, BalanceFrame, LoansFrame, BorrowsFrame):
- QtCore.QObject.connect(LoansFrame.box_name, QtCore.SIGNAL(("textChanged(QString)")), lambda:self.suggestions(MainFrame, BalanceFrame, LoansFrame, BorrowsFrame))
- QtCore.QObject.connect(BorrowsFrame.box_name, QtCore.SIGNAL(("textEdited(QString)")), lambda:self.suggestions(MainFrame, BalanceFrame, LoansFrame, BorrowsFrame))
- LoansFrame.box_value.editingFinished.connect(lambda:LoansFrame.box_value.setText('R$ %s' % (LoansFrame.box_value.text())))
- BorrowsFrame.box_value.editingFinished.connect(lambda:BorrowsFrame.box_value.setText('R$ %s' % (BorrowsFrame.box_value.text())))
- LoansFrame.btn_return.clicked.connect(lambda:self.addEntry(LoansFrame, "return"))
- LoansFrame.btn_lend.clicked.connect(lambda:self.addEntry(LoansFrame))
- BorrowsFrame.btn_return.clicked.connect(lambda:self.addEntry(BorrowsFrame, "return"))
- BorrowsFrame.btn_borrow.clicked.connect(lambda:self.addEntry(BorrowsFrame))
- @staticmethod
- def addEntry(Frame, *returning):
- name = str(Frame.box_name.text())
- date = str(Frame.box_date.text())
- value = float(str(Frame.box_value.text()).strip("R$ ").replace(".", "").replace(",", "."))
- reason = str(Frame.box_reason.text())
- if returning:
- reason = "Return"
- value *= -1
- if str(Frame)[10:].split('Frame')[0] == "Loans":
- ACCOUNT.inserItem(name, value, date, reason)
- else:
- ACCOUNT.inserItem(name, -value, date, reason)
- def suggestions(self, *args):
- list = self.table_names
- [list.append(i) for i in ACCOUNT.data]
- return list
- if __name__ == "__main__":
- app = QtGui.QApplication(sys.argv)
- try:
- acc = pickle.load(open("personal_account.p", "rb"))
- ACCOUNT = Account()
- MAIN = MainFrame()
- BALANCE = BalanceFrame(MAIN)
- LOANS = LoansFrame(MAIN)
- BORROWS = BorrowsFrame(MAIN)
- POPUP = PopUp(MAIN, BALANCE, LOANS, BORROWS)
- MAIN.show()
- sys.exit(app.exec_())
- finally:
- pickle.dump(ACCOUNT.data, open("personal_account.p", "wb"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement