class InformationWindow(QDialog): imageChanged = Signal(int, str) def __init__(self, id_, items, parent=None): super().__init__(parent) itemLabel = QLabel(self.tr("Item: ")) descriptionLabel = QLabel(self.tr("Description: ")) imageFileLabel = QLabel(self.tr("Image file: ")) self.createButtons() self.itemText = QLabel() self.descriptionEditor = QTextEdit() self.imageFileEditor = QComboBox() self.imageFileEditor.setModel(items.relationModel(1)) self.imageFileEditor.setModelColumn( items.relationModel(1).fieldIndex("file")) self.mapper = QDataWidgetMapper(self) self.mapper.setModel(items) self.mapper.setSubmitPolicy(QDataWidgetMapper.ManualSubmit) self.mapper.setItemDelegate(QSqlRelationalDelegate(self.mapper)) self.mapper.addMapping(self.imageFileEditor, 1) self.mapper.addMapping(self.itemText, 2, b"text") self.mapper.addMapping(self.descriptionEditor, 3) self.mapper.setCurrentIndex(id_) self.descriptionEditor.textChanged.connect(self.enableButtons) self.imageFileEditor.currentIndexChanged.connect(self.enableButtons) formLayout = QFormLayout() formLayout.addRow(itemLabel, self.itemText) formLayout.addRow(imageFileLabel, self.imageFileEditor) formLayout.addRow(descriptionLabel, self.descriptionEditor) layout = QVBoxLayout() layout.addLayout(formLayout) layout.addWidget(self.buttonBox) self.setLayout(layout) self.itemId = id_ self.displayedImage = self.imageFileEditor.currentText() self.setWindowFlags(Qt.Window) self.enableButtons(False) self.setWindowTitle(self.itemText.text()) def id(self): return self.itemId @Slot() def revert(self): self.mapper.revert() self.enableButtons(False) @Slot() def submit(self): newImage = self.imageFileEditor.currentText() if self.displayedImage != newImage: self.displayedImage = newImage self.imageChanged.emit(self.itemId, newImage) self.mapper.submit() self.mapper.setCurrentIndex(self.itemId) self.enableButtons(False) def createButtons(self): self.closeButton = QPushButton(self.tr("&Close")) self.revertButton = QPushButton(self.tr("&Revert")) self.submitButton = QPushButton(self.tr("&Submit")) self.closeButton.setDefault(True) self.closeButton.clicked.connect(self.close) self.revertButton.clicked.connect(self.revert) self.submitButton.clicked.connect(self.submit) self.buttonBox = QDialogButtonBox(self) self.buttonBox.addButton(self.submitButton, QDialogButtonBox.AcceptRole) self.buttonBox.addButton(self.revertButton, QDialogButtonBox.ResetRole) self.buttonBox.addButton(self.closeButton, QDialogButtonBox.RejectRole) @Slot() @Slot(bool) def enableButtons(self, enable=True): self.revertButton.setEnabled(enable) self.submitButton.setEnabled(enable)
class BaseSaveWidget(base.BaseWidget, object): def __init__(self, item, settings, temp_path=None, parent=None): # self._item = None self._settings = settings self._temp_path = temp_path self._options_widget = None # super(BaseSaveWidget, self).__init__(parent=parent) # # self.setObjectName('LibrarySaveWidget') # self.set_item(item) def ui(self): super(BaseSaveWidget, self).ui() title_layout = layouts.HorizontalLayout() title_layout.setContentsMargins(2, 2, 0, 0) title_layout.setSpacing(2) self._icon_lbl = QLabel() self._icon_lbl.setMaximumSize(QSize(14, 14)) self._icon_lbl.setMinimumSize(QSize(14, 14)) self._icon_lbl.setScaledContents(True) self._title_lbl = QLabel() title_layout.addWidget(self._icon_lbl) title_layout.addWidget(self._title_lbl) self._folder_widget = directory.SelectFolder('Folder', use_app_browser=True) buttons_layout = layouts.HorizontalLayout() buttons_layout.setContentsMargins(4, 4, 4, 4) buttons_layout.setSpacing(4) buttons_frame = QFrame() buttons_frame.setFrameShape(QFrame.NoFrame) buttons_frame.setFrameShadow(QFrame.Plain) buttons_frame.setLayout(buttons_layout) buttons_layout.addStretch() self.save_btn = buttons.BaseButton('Save') self.cancel_btn = buttons.BaseButton('Cancel') buttons_layout.addWidget(self.save_btn, parent=self) buttons_layout.addWidget(self.cancel_btn, parent=self) buttons_layout.addStretch() self._options_layout = layouts.VerticalLayout() self._options_layout.setContentsMargins(0, 0, 0, 0) self._options_layout.setSpacing(2) self._options_frame = QFrame() self._options_frame.setFrameShape(QFrame.NoFrame) self._options_frame.setFrameShadow(QFrame.Plain) self._options_frame.setLineWidth(0) self._options_frame.setLayout(self._options_layout) self._extra_layout = layouts.VerticalLayout() self._extra_layout.setContentsMargins(0, 0, 0, 0) self._extra_layout.setSpacing(2) self.main_layout.addLayout(title_layout) self.main_layout.addWidget(self._folder_widget) self._extra_layout.addWidget(self._options_frame) self.main_layout.addWidget(dividers.Divider()) self.main_layout.addLayout(self._extra_layout) self.main_layout.addWidget(dividers.Divider()) self.main_layout.addWidget(buttons_frame) def setup_signals(self): self.save_btn.clicked.connect(self._on_save) self.cancel_btn.clicked.connect(self._on_cancel) def settings(self): """ Returns settings object :return: JSONSettings """ return self._settings def set_settings(self, settings): """ Sets save widget settings :param settings: JSONSettings """ self._settings = settings # def item(self): # """ # Returns the library item to be created # :return: LibraryItem # """ # # return self._item def set_item(self, item): """ Sets the base item to be created :param item: LibraryItem """ self._item = item self._title_lbl.setText(item.MenuName) self._icon_lbl.setPixmap(QPixmap(item.type_icon_path())) schema = item.save_schema() if schema: options_widget = formwidget.FormWidget(self) options_widget.set_schema(schema) options_widget.set_validator(item.save_validator) self._options_frame.layout().addWidget(options_widget) self._options_widget = options_widget self.load_settings() options_widget.stateChanged.connect(self._on_options_changed) options_widget.validate() else: self._options_frame.setVisible(False) def library_window(self): """ Returns library widget window for the item :return: LibraryWindow """ return self._item.library_window() def set_library_window(self, library_window): """ Sets the library widget for the item :param library_window: LibraryWindow """ self._item.set_library_window(library_window) def name(self): """ Returns the name of the field :return: str """ return self._title_lbl.text().strip() def description(self): """ Returns the string from the comment field :return: str """ return self._comment.toPlainText().strip() def folder_frame(self): """ Returns the frame that contains the folder edit, label and button :return: QFrame """ return self._folder_frame def folder_path(self): """ Returns the folder path :return: str """ return self._folder_widget.folder_line.text() def set_folder_path(self, path): """ Sets the destination folder path :param path: str """ self._folder_widget.folder_line.setText(path) def default_values(self): """ Returns all the default values for the save fields :return: dict """ values = dict() for option in self.item().save_schema(): values[option.get('name')] = option.get('default') return values def save_settings(self): """ Saves the current state of the widget to disk """ state = self._options_widget.options_state() self.settings().set(self.item().__class__.__name__, {'SaveOptions': state}) def load_settings(self): """ Returns the settings object for saving the state of the widget """ option_settings = self.settings().get(self.item().__class__.__name__, {}) options = option_settings.get('SaveOptions', dict()) values = self.default_values() if options: for option in self.item().save_schema(): name = option.get('name') persistent = option.get('persistent') if not persistent and name in options: options[name] = values[name] self._options_widget.set_state_from_options(options) def save(self, path, icon_path, objects=None): """ Saves the item with the given objects to the given disk location path :param path: list(str) :param icon_path: str :param objects: str """ item = self.item() options = self._options_widget.values() item.save(path=path, objects=objects, icon_path=icon_path, **options) self.close() def _save(self): if not self.library_window(): return try: path = self.folder_path() options = self._options_widget.values() name = options.get('name') objects = dcc.selected_nodes(full_path=True) or list() if not path: raise Exception( 'No folder selected. Please select a destination folder') if not name: raise Exception( 'No name specified. Please set a name before saving') if not os.path.exists(self.icon_path()): btn = self.show_thumbnail_capture_dialog() if btn == QDialogButtonBox.Cancel: return path += '/{}'.format(name) icon_path = self.icon_path() self.save(path=path, icon_path=icon_path, objects=objects) except Exception as e: messagebox.MessageBox.critical(self.library_window(), 'Error while saving', str(e)) LOGGER.error(traceback.format_exc()) raise self.library_window().stack.slide_in_index(0) def _on_options_changed(self): """ Internal callback function that is called when an option value changes """ self.save_settings() def _on_save(self): if not self.library_window(): return self._save() self.library_window().stack.slide_in_index(0) def _on_cancel(self): self.close() self.library_window().stack.slide_in_index(0)