class NoteEditDialog(QDialog, Ui_Dialog): ready = pyqtSignal(bool, int, name='ready') def __init__(self, model, row=None, *args, **kwargs): super().__init__(*args, **kwargs) self.init_ui() self.init_model(model) if row is None: # работаем в режиме добавления self.__model.insertRow(self.__model.rowCount()) self.__mapper.toLast() else: # работаем в режиме редактирования self.__mapper.setCurrentIndex(row) def init_ui(self): # loadUi('ui/notes_edit_dialog.ui', self) self.setupUi(self) self.plannedDateTimeEdit.setMinimumDate( QDateTime.currentDateTime().date() ) self.plannedDateTimeEdit.setDateTime( QDateTime.currentDateTime() ) def init_model(self, model): self.__model = model self.__mapper = QDataWidgetMapper(self) self.__mapper.setModel(self.__model) self.__mapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit) self.__mapper.addMapping(self.titleEdit, 1) self.__mapper.addMapping(self.plannedDateTimeEdit, 2) self.__mapper.addMapping(self.contentEdit, 3) def accept(self): # это стандартный метод super().accept() self.__mapper.submit() # отправляем данные в модель state = self.__model.submitAll() # отправить изменения в БД self.ready.emit(state, self.__mapper.currentIndex()) def reject(self): # это стандартный метод super().reject() self.__mapper.revert() self.__model.revertAll()
class NoteEditDialog(QDialog, Ui_Dialog): ready = pyqtSignal(bool, int, name='ready') def __init__(self, model, row=None, *args, **kwargs): super().__init__(*args, **kwargs) self.init_ui() self.init_model(model) if row is None: # в режиме добавления self.__model.insertRow(self.__model.rowCount()) self.__mapper.toLast() else: # работаем в режиме редактирования self.__mapper.setCurrentIndex(row) def init_ui(self): self.setupUi(self) self.plannedDateTimeEdit.setMinimumDate( QDateTime.currentDateTime().date()) self.plannedDateTimeEdit.setDateTime(QDateTime.currentDateTime()) def init_model(self, model): self.__model = model self.__mapper = QDataWidgetMapper(self) self.__mapper.setModel(self.__model) self.__mapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit) self.__mapper.addMapping(self.titleEdit, 1) self.__mapper.addMapping(self.plannedDateTimeEdit, 2) self.__mapper.addMapping(self.contentEdit, 3) def accept(self): # это стандартный метод super().accept() self.__mapper.submit() # отправили данные в модель state = self.__model.submitAll() # отправить изменения в БД self.ready.emit(state, self.__mapper.currentIndex()) def reject(self): super().reject() self.__mapper.revert() # отмена изменений self.__model.revertAll()
class DeviceEditDialog(QDialog): def __init__(self, model, row, *args, **kwargs): super(DeviceEditDialog, self).__init__(*args, **kwargs) self.setMinimumWidth(400) self.setWindowTitle("Edit device") self.settings = QSettings() self.settings.beginGroup("Devices") self.mapper = QDataWidgetMapper() self.mapper.setModel(model) self.mapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit) vl = VLayout() gbTopic = QGroupBox("MQTT Topic") self.topic = QLineEdit() self.topic.setPlaceholderText("unique name of your device") self.mapper.addMapping(self.topic, DevMdl.TOPIC) self.full_topic = QLineEdit() self.full_topic.setPlaceholderText("must contain %prefix% and %topic%") self.mapper.addMapping(self.full_topic, DevMdl.FULL_TOPIC) tfl = QFormLayout() tfl.addRow("Topic", self.topic) tfl.addRow("Full topic", self.full_topic) gbTopic.setLayout(tfl) btnSave = QPushButton("Save") btnCancel = QPushButton("Cancel") hl_btns = HLayout() hl_btns.addStretch(1) hl_btns.addWidgets([btnSave, btnCancel]) vl.addWidgets([gbTopic]) vl.addLayout(hl_btns) self.setLayout(vl) self.mapper.setCurrentIndex(row) btnSave.clicked.connect(self.accept) btnCancel.clicked.connect(self.reject) def accept(self): full_topic = self.full_topic.text() if not full_topic.endswith('/'): self.full_topic.setText(full_topic + "/") if not len(self.topic.text()) > 0: QMessageBox.critical(self, "Error", "Topic is required.") elif not "%topic%" in full_topic: QMessageBox.critical(self, "Error", "%topic% is required in FullTopic.") elif not "%prefix%" in full_topic: QMessageBox.critical(self, "Error", "%prefix% is required in FullTopic.") elif self.topic.text() not in self.settings.childGroups(): self.mapper.submit() self.done(QDialog.Accepted) else: QMessageBox.critical(self, "Error", "Device '{}' already on the device list.".format(self.topic.text())) def reject(self): self.mapper.revert() self.done(QDialog.Rejected)