def openPreferences(self): preferencesDialog=Ui_Preferences() gui=QtGui.QDialog(); preferencesDialog.setupUi(gui,self.startUpFlag) gui.setModal(True) reset=gui.exec_() #reset the session when the parameters have been changed if reset==int(True): self.setSession()#preferencesDialog.retVal())
class PreferencesDialog(QtGui.QDialog): nc = 0 # Name column vc = 1 # Value column tc = 2 # Type column gc = 3 # Gross column ac = 4 # Active column def __init__(self, user, session): QtGui.QDialog.__init__(self) self.ui = Ui_Preferences() self.ui.setupUi(self) self.user = user self.session = session # Slots self.ui.add_account.clicked.connect(self.add_account) self.ui.add_deduction.clicked.connect(self.add_deduction) self.ui.delete_deduction.clicked.connect(self.delete_deduction) self.ui.whole_account.clicked.connect(self.whole_account_changed) # Username self.ui.username.setText(self.user.name) # TODO keep track of the identity of each row, either adding # a hidden ID or storing the original values in an internal # data structure to compare the result against # Accounts self.at = self.ui.accountsTable self.active_group = QtGui.QButtonGroup() self.active_group.setExclusive(False) row = 0 for a in self.user.accounts: self.insert_account(row, a.id, a.name, a.affect_gross, a.percentage_or_fixed, a.amount, a.status) row += 1 # Whole Account Actions if self.user.whole_account_actions: self.ui.whole_account.click() # Deductions self.dt = self.ui.deductionsTable row = 0 for a, d in self.user.deductions: self.insert_deduction(row, d, a) row += 1 self.dt.resizeRowsToContents() self.dt.horizontalHeader().setResizeMode( QtGui.QHeaderView.Stretch) # TODO check out doc.trolltech.com/stylesheet-examples.html # for help with styling (e.g., text-align, width, etc) def whole_account_changed(self): hidden = False if self.ui.whole_account.isChecked() else True self.at.setColumnHidden(self.vc, hidden) self.at.setColumnHidden(self.tc, hidden) self.at.setColumnHidden(self.gc, hidden) def add_account(self): self.insert_account(self.at.rowCount(), -1, '', False, budse.Account.PERCENTAGE, 0, True) def insert_account(self, row, id, name, gross, acct_type, amount, active): self.at.setRowCount(self.at.rowCount() + 1) # Account self.at.setItem(row, self.nc, QtGui.QTableWidgetItem(name)) # Affect gross gross_or_net = QtGui.QComboBox() gross_or_net.addItem('Net', 0) gross_or_net.addItem('Gross', 1) if gross: gross_or_net.setCurrentIndex(1) self.at.setCellWidget(row, self.gc, gross_or_net) # Amount and Accont type acct_type = QtGui.QComboBox() acct_type.addItem('Percentage', 0) acct_type.addItem('Fixed', 1) if acct_type == budse.Account.PERCENTAGE: twi = PercentageTableWidgetItem(amount) acct_type.setCurrentIndex(0) else: twi = MonetaryTableWidgetItem(amount) acct_type.setCurrentIndex(1) twi.setTextAlignment(QtCore.Qt.AlignHCenter) self.at.setCellWidget(row, self.tc, acct_type) self.at.setItem(row, self.vc, twi) # Active checkbox cb = QtGui.QCheckBox() if active: cb.setChecked(True) self.at.setCellWidget(row, self.ac, cb) self.active_group.addButton(cb) # TODO: need the button group or the ID? # self.active_group.setId(cb, id) self.at.resizeRowsToContents() def add_deduction(self): self.insert_deduction(self.dt.rowCount(), '', 0) def insert_deduction(self, row, description, amount): dc = 0 # Description column ac = 1 # Amount column # TODO right now each row is identified by its description # is it worth it to make a unique ID so that there can be # multiple instances of a description and value? self.dt.setRowCount(self.dt.rowCount() + 1) # Description twi = QtGui.QTableWidgetItem(description) twi.setTextAlignment(QtCore.Qt.AlignLeft) self.dt.setItem(row, dc, twi) # Amount twi = MonetaryTableWidgetItem(amount) twi.setTextAlignment(QtCore.Qt.AlignHCenter) self.dt.setItem(row, ac, twi) self.dt.resizeRowsToContents() def delete_deduction(self): self.dt.removeRow(self.dt.currentRow()) def save(self): errors = [] # TODO # loop through accounts # if deactivated, set status to False # else if new, insert a new entry # else if changed in any way, update the account # loop through deductions # create a new set for deductions # if removed, do not add # else add info if not errors: QtGui.QWidget.hide(self) else: print('invalid') for e in errors: print(e)
class Preferences(QtWidgets.QDialog): def __init__(self, parent=None): super().__init__(parent) self.parent = parent self.ui = Ui_Preferences() self.ui.setupUi(self) self.setFixedSize(400, 342) self.loadData() self.ui.pref_buttons.accepted.connect(self.accept) self.ui.pref_buttons.button(QtWidgets.QDialogButtonBox.Discard).clicked.connect(self.reject) self.ui.pref_buttons.button(QtWidgets.QDialogButtonBox.RestoreDefaults).clicked.connect(self.restoreDefaults) print("Preferences Window loaded") def loadData(self): abs_path = os.path.abspath(os.path.dirname('__file__')) statistics_dir = abs_path+'\\statistics\\' file_names = os.listdir(statistics_dir) self.statistics_files = [] for file_name in file_names: match = re.search(r'\w+\.html', file_name) if match: self.statistics_files.append(statistics_dir+file_name) def insertPrefs(self): self.ui.pref_map_check.setProperty('value', float(self.parent.settings['MapCheckInterval'])) self.ui.pref_startup.setChecked(int(self.parent.settings['LoadLastOpenedDB'])) self.ui.pref_db_path.setText((self.parent.settings['LastOpenedDB'])) self.ui.pref_statistics.clear() for i, stat_file in enumerate(self.statistics_files): self.ui.pref_statistics.addItem(os.path.basename(stat_file)) if stat_file == self.parent.settings['SelectedStatisticsFile']: self.ui.pref_statistics.setCurrentIndex(i) self.ui.pref_hour.setChecked(int(self.parent.settings['ClockHour12'])) self.ui.pref_millisec.setChecked(int(self.parent.settings['ShowMilliseconds'])) self.ui.pref_zana_level.setProperty('valse', int(self.parent.settings['ZanaLevel'])) self.ui.pref_defualt_zana_mod.clear() for i, zana_mod in enumerate(self.parent.zana_mods): self.ui.pref_defualt_zana_mod.addItem(zana_mod[ZanaMod.Name] + ' (' + zana_mod[ZanaMod.Cost] + ')') if int(self.parent.settings['ZanaDefaultModIndex']) == i: self.ui.pref_defualt_zana_mod.setCurrentIndex(i) # if self.ui.pref_zana_level.property('value') < zana_mod[ZanaMod.Level]: # break self.ui.pref_on_top.setChecked(int(self.parent.settings['AlwaysOnTop'])) def restoreDefaults(self): self.parent.ui_confirm.boxType('confirm') if self.parent.ui_confirm.exec_('Restore Defaults?', 'Are you sure you want to restore the default settings?'): get_defaults = True self.parent.settings = readSettings(get_defaults) self.insertPrefs() def accept(self): self.parent.settings['MapCheckInterval'] = str(self.ui.pref_map_check.property('value')) self.parent.settings['LoadLastOpenedDB'] = str(self.ui.pref_startup.checkState()) self.parent.settings['SelectedStatisticsFile'] = self.statistics_files[self.ui.pref_statistics.currentIndex()] self.parent.settings['ClockHour12'] = str(self.ui.pref_hour.checkState()) self.parent.settings['ShowMilliseconds'] = str(self.ui.pref_millisec.checkState()) self.parent.settings['ZanaLevel'] = str(self.ui.pref_zana_level.property('value')) self.parent.settings['ZanaDefaultModIndex'] = str(self.ui.pref_defualt_zana_mod.currentIndex()) self.parent.settings['AlwaysOnTop'] = str(self.ui.pref_on_top.checkState()) writeSettings(self.parent.settings) super().accept() def exec_(self): self.insertPrefs() return super().exec_()