def edit( cls, record, parent = None, uifile = '', commit = True ): """ Prompts the user to edit the inputed record. :param record | <orb.Table> parent | <QWidget> :return <bool> | accepted """ # create the dialog dlg = QDialog(parent) dlg.setWindowTitle('Edit %s' % record.schema().name()) # create the widget cls = record.schema().property('widgetClass', cls) widget = cls(dlg) if ( uifile ): widget.setUiFile(uifile) widget.setRecord(record) widget.layout().setContentsMargins(0, 0, 0, 0) # create buttons opts = QDialogButtonBox.Save | QDialogButtonBox.Cancel btns = QDialogButtonBox(opts, Qt.Horizontal, dlg) # create layout layout = QVBoxLayout() layout.addWidget(widget) layout.addWidget(btns) dlg.setLayout(layout) dlg.adjustSize() # create connections #btns.accepted.connect(widget.save) btns.rejected.connect(dlg.reject) widget.saved.connect(dlg.accept) if ( dlg.exec_() ): if commit: result = widget.record().commit() if 'errored' in result: QMessageBox.information(self.window(), 'Error Committing to Database', result['errored']) return False return True return False
def accept( self ): """ Saves the data to the profile before closing. """ if ( not self.uiNameTXT.text() ): QMessageBox.information(self, 'Invalid Name', 'You need to supply a name for your layout.') return prof = self.profile() if ( not prof ): prof = XViewProfile() prof.setName(nativestring(self.uiNameTXT.text())) prof.setVersion(self.uiVersionSPN.value()) prof.setDescription(nativestring(self.uiDescriptionTXT.toPlainText())) prof.setIcon(self.uiIconBTN.filepath()) super(XViewProfileDialog, self).accept()
def accept(self): """ Saves the data to the profile before closing. """ if (not self.uiNameTXT.text()): QMessageBox.information( self, 'Invalid Name', 'You need to supply a name for your layout.') return prof = self.profile() if (not prof): prof = XViewProfile() prof.setName(nativestring(self.uiNameTXT.text())) prof.setVersion(self.uiVersionSPN.value()) prof.setDescription(nativestring(self.uiDescriptionTXT.toPlainText())) prof.setIcon(self.uiIconBTN.filepath()) super(XViewProfileDialog, self).accept()
def saveProfileLayout(self, profile): """ Saves the profile layout to the inputed profile. :param profile | <projexui.widgets.xviewwidget.XViewProfile> """ if not self.viewWidget(): text = self.profileText() QMessageBox.information(self.window(), 'Could Not Save {0}'.format(text), 'Error saving layout, '\ 'No View Widget Assigned') return # save the profile from the view widget prof = self.viewWidget().saveProfile() profile.setXmlElement(prof.xmlElement()) if not self.signalsBlocked(): self.profileChanged.emit(profile)
def saveProfileLayout(self, profile): """ Saves the profile layout to the inputed profile. :param profile | <projexui.widgets.xviewwidget.XViewProfile> """ if not self.viewWidget(): text = self.profileText() QMessageBox.information(self.window(), 'Could Not Save {0}'.format(text), 'Error saving layout, '\ 'No View Widget Assigned') return # save the profile from the view widget prof = self.viewWidget().saveProfile() profile.setXmlElement(prof.xmlElement()) if not self.signalsBlocked(): self.profileChanged.emit(profile)
def save( self ): """ Saves the values from the editor to the system. """ schema = self.schema() if ( not schema ): self.saved.emit() return record = self.record() if not record: record = self._model() # validate the information save_data = [] column_edits = self.findChildren(XOrbColumnEdit) for widget in column_edits: columnName = widget.columnName() column = schema.column(columnName) if ( not column ): logger.warning('%s is not a valid column of %s.' % \ (columnName, schema.name())) continue value = widget.value() if ( value == IGNORED ): continue # check for required columns if ( column.required() and not value ): name = column.displayName() QMessageBox.information(self, 'Missing Required Field', '%s is a required field.' % name) return # check for unique columns elif ( column.unique() ): # check for uniqueness query = Q(column.name()) == value if ( record.isRecord() ): query &= Q(self._model) != record columns = self._model.schema().primaryColumns() result = self._model.select(columns = columns, where = query) if ( result.total() ): QMessageBox.information(self, 'Duplicate Entry', '%s already exists.' % value) return save_data.append((column, value)) # record the properties for the record for column, value in save_data: record.setRecordValue(column.name(), value) self._record = record self.saved.emit()
def save(self): """ Saves the changes from the ui to this widgets record instance. """ record = self.record() if not record: logger.warning('No record has been defined for %s.' % self) return False if not self.signalsBlocked(): self.aboutToSaveRecord.emit(record) self.aboutToSave.emit() values = self.saveValues() # ignore columns that are the same (fixes bugs in encrypted columns) check = values.copy() for column_name, value in check.items(): try: equals = value == record.recordValue(column_name) except UnicodeWarning: equals = False if equals: check.pop(column_name) # check to see if nothing has changed if not check and record.isRecord(): if not self.signalsBlocked(): self.recordSaved.emit(record) self.saved.emit() self._saveSignalBlocked = False else: self._saveSignalBlocked = True if self.autoCommitOnSave(): status, result = record.commit() if status == 'errored': if 'db_error' in result: msg = nativestring(result['db_error']) else: msg = 'An unknown database error has occurred.' QMessageBox.information(self, 'Commit Error', msg) return False return True # validate the modified values success, msg = record.validateValues(check) if ( not success ): QMessageBox.information(None, 'Could Not Save', msg) return False record.setRecordValues(**values) success, msg = record.validateRecord() if not success: QMessageBox.information(None, 'Could Not Save', msg) return False if ( self.autoCommitOnSave() ): result = record.commit() if 'errored' in result: QMessageBox.information(None, 'Could Not Save', msg) return False if ( not self.signalsBlocked() ): self.recordSaved.emit(record) self.saved.emit() self._saveSignalBlocked = False else: self._saveSignalBlocked = True return True
def showProfileMenu(self, point): """ Prompts the user for profile menu options. Editing needs to be enabled for this to work. """ if not self.isEditingEnabled(): return trigger = self.actionAt(point) if (isinstance(trigger, XViewProfileAction)): prof = trigger.profile() else: prof = None # define the menu menu = QMenu(self) acts = {} text = self.profileText() # user right clicked on a profile if prof: acts['edit'] = menu.addAction('Edit {0}...'.format(text)) acts['save'] = menu.addAction('Save Layout') menu.addSeparator() acts['copy'] = menu.addAction('Copy {0}'.format(text)) acts['export'] = menu.addAction('Export {0}...'.format(text)) menu.addSeparator() acts['remove'] = menu.addAction('Delete {0}'.format(text)) # show toolbar options else: acts['new'] = menu.addAction('New Layout'.format(text)) menu.addSeparator() acts['save_as'] = menu.addAction('Save Layout as...') if QApplication.clipboard().text(): acts['paste'] = menu.addAction('Paste {0}'.format(text)) acts['import'] = menu.addAction('Import {0}...'.format(text)) for key, act in acts.items(): act.setIcon(QIcon(resources.find('img/{0}.png'.format(key)))) # run the menu act = menu.exec_(QCursor.pos()) # create a new profile if act is None: return elif act == acts.get('new'): self.clearActive() # create a new clear profile elif act == acts.get('save_as'): self.saveProfileAs() # edit an existing profile elif act == acts.get('edit'): self.editProfile(prof) # save or create a new profile elif act == acts.get('save'): self.saveProfileLayout(prof) # copy profile elif act == acts.get('copy'): QApplication.clipboard().setText(prof.toString()) # export elif act == acts.get('export'): self.exportProfile(prof) # export elif act == acts.get('import'): self.importProfile() # paste profile elif act == acts.get('paste'): text = QApplication.clipboard().text() try: prof = XViewProfile.fromString(text) except: prof = None QMessageBox.information(self.window(), 'Invalid {0}'.format(text), 'The clipboard text does not contain '\ 'a properly formated {0}'.format(text)) if prof and not prof.isEmpty(): self.createProfile(profile=prof) # paste as profile elif act == acts.get('paste_as'): text = QApplication.clipboard().text() prof = XViewProfile.fromString(text) if not prof.isEmpty(): if XViewProfileDialog.edit(self, prof): self.createProfile(profile=prof) # remove the profile elif act == acts.get('remove'): self.removeProfile(prof)
def showProfileMenu(self, point): """ Prompts the user for profile menu options. Editing needs to be enabled for this to work. """ if not self.isEditingEnabled(): return trigger = self.actionAt(point) if (isinstance(trigger, XViewProfileAction)): prof = trigger.profile() else: prof = None # define the menu menu = QMenu(self) acts = {} text = self.profileText() # user right clicked on a profile if prof: acts['edit'] = menu.addAction('Edit {0}...'.format(text)) acts['save'] = menu.addAction('Save Layout') menu.addSeparator() acts['copy'] = menu.addAction('Copy {0}'.format(text)) acts['export'] = menu.addAction('Export {0}...'.format(text)) menu.addSeparator() acts['remove'] = menu.addAction('Delete {0}'.format(text)) # show toolbar options else: acts['new'] = menu.addAction('New Layout'.format(text)) menu.addSeparator() acts['save_as'] = menu.addAction('Save Layout as...') if QApplication.clipboard().text(): acts['paste'] = menu.addAction('Paste {0}'.format(text)) acts['import'] = menu.addAction('Import {0}...'.format(text)) for key, act in acts.items(): act.setIcon(QIcon(resources.find('img/{0}.png'.format(key)))) # run the menu act = menu.exec_(QCursor.pos()) # create a new profile if act is None: return elif act == acts.get('new'): self.clearActive() # create a new clear profile elif act == acts.get('save_as'): self.saveProfileAs() # edit an existing profile elif act == acts.get('edit'): self.editProfile(prof) # save or create a new profile elif act == acts.get('save'): self.saveProfileLayout(prof) # copy profile elif act == acts.get('copy'): QApplication.clipboard().setText(prof.toString()) # export elif act == acts.get('export'): self.exportProfile(prof) # export elif act == acts.get('import'): self.importProfile() # paste profile elif act == acts.get('paste'): text = QApplication.clipboard().text() try: prof = XViewProfile.fromString(text) except: prof = None QMessageBox.information(self.window(), 'Invalid {0}'.format(text), 'The clipboard text does not contain '\ 'a properly formated {0}'.format(text)) if prof and not prof.isEmpty(): self.createProfile(profile=prof) # paste as profile elif act == acts.get('paste_as'): text = QApplication.clipboard().text() prof = XViewProfile.fromString(text) if not prof.isEmpty(): if XViewProfileDialog.edit(self, prof): self.createProfile(profile=prof) # remove the profile elif act == acts.get('remove'): self.removeProfile(prof)