def show_settings_dialog(self): settings_dlg = SettingsDialog() settings_dlg.exec_() # apply settings self.remove_menu_buttons() self.build_menu_tree() self.append_menu_buttons()
def openSettings(self): s = QtGui.qApp.settings dlg = SettingsDialog(s) if dlg.exec_() != SettingsDialog.Accepted: return ns = dlg.getData() if ns.connection != s.connection: msg = 'Server connection changed, you should restart to reflect changes' QMessageBox.information(self, 'Major changes', msg) elif ns.camera_type != s.camera_type: msg = 'Camera Type Changed, you should restart to reflect changes' QMessageBox.information(self, 'Major changes', msg) QtGui.qApp.settings = ns QtGui.qApp.saveConfig()
class AdaplinControl: def __init__(self, iface): # Save reference to the QGIS interface self.iface = iface self.canvas = self.iface.mapCanvas() # Create dialog form self.dlg = AdaplinDialog() # Create settings dialog self.settingsDialog = SettingsDialog() def initGui(self): mc = self.canvas layer = mc.currentLayer() # Create action for the plugin icon and the plugin menu self.action = QAction(QIcon(":/plugins/AdaplinTool/icon.png"), "Adaplin", self.iface.mainWindow()) # Button starts disabled and unchecked self.action.setEnabled(False) self.action.setCheckable(True) self.action.setChecked(False) # Add Settings to the [Vector] Menu self.settingsAction = QAction("Settings", self.iface.mainWindow()) #self.iface.addPluginToVectorMenu("&Adaplin Settings", self.settingsAction) self.iface.addPluginToMenu("&Adaplin", self.settingsAction) self.settingsAction.triggered.connect(self.openSettings) # Add the plugin to Plugin Menu and the Plugin Icon to the Toolbar self.iface.addPluginToMenu("&Adaplin", self.action) self.iface.addToolBarIcon(self.action) # Connect signals for button behaviour (map layer change, run the tool and change of QGIS map tool set) self.iface.currentLayerChanged['QgsMapLayer*'].connect(self.toggle) self.action.triggered.connect(self.run) QObject.connect(mc, SIGNAL("mapToolSet(QgsMapTool*)"), self.deactivate) # Connect the change of the Raster Layer in the ComboBox to function trata_combo self.dlg.comboBox.currentIndexChanged.connect(self.trata_combo) def toggle(self): # Get current layer mc = self.canvas layer = mc.currentLayer() #print "Adaplin layer = ", layer.name() # In case the current layer is None we do nothing if layer is None: return #print 'raster type = ', layer.type() #print 'e vector = ', layer.type() == layer.VectorLayer # This is to decide when the plugin button is enabled or disabled # The layer must be a Vector Layer if layer.type() == layer.VectorLayer: # We only care about the Line and Polygon layers if layer.geometryType() == QGis.Line or layer.geometryType( ) == QGis.Polygon: # First we disconnect all possible previous signals associated with this layer # If layer is editable, SIGNAL "editingStopped()" is connected to toggle # If it is not editable, SIGNAL "editingStarted()" is connected to toggle try: layer.editingStarted.disconnect(self.toggle) except: pass try: layer.editingStopped.disconnect(self.toggle) except: pass # If current layer is editable, the button is enabled if layer.isEditable(): self.action.setEnabled(True) # If we stop editing, we run toggle function to disable the button layer.editingStopped.connect(self.toggle) # Layer is not editable else: self.action.setEnabled(False) # In case we start editing, we run toggle function to enable the button layer.editingStarted.connect(self.toggle) else: self.action.setEnabled(False) else: self.action.setEnabled(False) def deactivate(self): self.action.setChecked(False) def unload(self): # Removes item from Plugin Menu, Vector Menu and removes the toolbar icon self.iface.removePluginMenu("&Adaplin", self.action) self.iface.removePluginMenu("&Adaplin", self.settingsAction) self.iface.removeToolBarIcon(self.action) def trata_combo(self): # ComboBox Selected Raster Layer indiceCamada = self.dlg.comboBox.currentIndex() camadaSelecionada = self.camadas_raster[indiceCamada] # Clear ComboBoxs of Bands self.dlg.comboBox_2.clear() self.dlg.comboBox_3.clear() self.dlg.comboBox_4.clear() # Get number of raster bands numBandas = camadaSelecionada.bandCount() # List image bands by numbers and add them to Bands ComboBoxs lista_bandas = [str(b) for b in range(1, numBandas + 1)] self.dlg.comboBox_2.addItems(lista_bandas) self.dlg.comboBox_3.addItems(lista_bandas) self.dlg.comboBox_4.addItems(lista_bandas) def run(self): # Unpress the button if it is pressed if not self.action.isChecked(): print self.action.isChecked() self.action.setChecked(False) return # On-the-fly SRS must be Projected mapCanvasSrs = self.iface.mapCanvas().mapRenderer().destinationCrs() if mapCanvasSrs.geographicFlag(): QMessageBox.information( self.iface.mainWindow(), 'Error', '<h2> Please choose an On-the-Fly Projected Coordinate System</h2>' ) return # Clear ComboBox of Raster Selection. # This command generate a bug sometimes (for example, when using the plugin with a raster, removing this raster and adding it again) # when trying to clear the comboBox when it is triggered with trata_combo # Disconnect and connect the signal is possible but not elegant self.dlg.comboBox.clear() # List rasters of Legend Interface self.camadas_raster = [] camadas = self.iface.legendInterface().layers() for camada in camadas: if camada.type() == QgsMapLayer.RasterLayer: self.camadas_raster.append(camada) self.dlg.comboBox.addItem(camada.name()) # Error in case there are no raster layers in the Legend Interface if not self.camadas_raster: QMessageBox.information( self.iface.mainWindow(), 'Error', '<h2>There are no raster layers in the Legend Interace</h2>') return # Finish the dialog box and run the dialog event loop self.dlg.show() result = self.dlg.exec_() # See if OK was pressed if result: # Get Raster Selected from ComboBox indiceCamada = self.dlg.comboBox.currentIndex() camadaSelecionada = self.camadas_raster[indiceCamada] # Get Bands selected for this Raster numBandas = camadaSelecionada.bandCount() lista_bandas = [str(b) for b in range(1, numBandas + 1)] indiceCombo2 = self.dlg.comboBox_2.currentIndex() indiceCombo3 = self.dlg.comboBox_3.currentIndex() indiceCombo4 = self.dlg.comboBox_4.currentIndex() bandas_selecao = (lista_bandas[indiceCombo2], lista_bandas[indiceCombo3], lista_bandas[indiceCombo4]) # Activate our tool if OK is pressed self.adaplin = Adaplin(self.iface, camadaSelecionada, bandas_selecao) mc = self.canvas layer = mc.currentLayer() mc.setMapTool(self.adaplin) self.action.setChecked(True) else: self.action.setChecked(False) def openSettings(self): # Default settings to reload def valoresPadrao(): self.settingsDialog.doubleSpinBox.setValue(DEFAULT_ESPACAMENTO) self.settingsDialog.spinBox.setValue(DEFAULT_QPONTOS) # Connect button to function that reload default values self.settingsDialog.pushButton.clicked.connect(valoresPadrao) self.settingsDialog.show() result = self.settingsDialog.exec_() # If OK is pressed we update the settings if result: QSettings().setValue(SETTINGS_NAME + "/qpontos", self.settingsDialog.spinBox.value()) QSettings().setValue(SETTINGS_NAME + "/espacamento", self.settingsDialog.doubleSpinBox.value()) # Disconnect signal connected previously self.settingsDialog.pushButton.clicked.disconnect(valoresPadrao)
def show_settings_dialog(self): settings_dlg = SettingsDialog() settings_dlg.exec_() # apply settings # self.remove_menu_buttons() self.build_menu_tree()
def action_settings(self): sett_dialog = SettingsDialog() sett_dialog.show() sett_dialog.exec_() self.reinit_tree()
def settings(self): sett_dialog = SettingsDialog() sett_dialog.show() result = sett_dialog.exec_()
def do_change_settings(self): dlg_settings = SettingsDialog() dlg_settings.show() dlg_settings.projectsDatabaseHasChanged.connect( self.do_load_projects_database) result = dlg_settings.exec_()
class MainWindow(QMainWindow, Ui_MainWindow): docLabelTemplate = "Documents for {}:" docLabelTemplateEmpty = "Documents:" def __init__(self, app, list_manager, parent=None): super(MainWindow, self).__init__(parent) self.app = app # this must be defined before the setupUi is called # cause widgets in the interface need to reference it # in there __init__ methods self.listeners = [] self.list_manager = list_manager self.list_is_dirty = False self.setupUi(self) self.setWindowTitle("Assembly Document Tracker") QCoreApplication.setOrganizationName('Charles Cognato') QCoreApplication.setApplicationName('Assembly Doc Tracker') self.settings = QSettings() open_on_add = self.settings.value('open_on_add', 'false') if open_on_add == "true": self.actionOpen_on_Add.setChecked(True) else: self.actionOpen_on_Add.setChecked(False) # force at least one product to be on the list before documents can be added if self.listWidget.count() <= 0: self.listWidget_2.setEnabled(False) self.listWidget.setContextMenuPolicy(Qt.NoContextMenu) self.productContextActionRename = QAction(QIcon(":/filesaveas.png"), "rename", self) self.listWidget.addAction(self.productContextActionRename) self.connect(self.productContextActionRename, SIGNAL("triggered()"), self.on_assembly_context_action_rename_triggered) self.productContextActionDelete = QAction(QIcon(":/delete.png"), "delete", self) self.productContextActionDelete.setStatusTip(assembly_context_delete_status_tip) self.listWidget.addAction(self.productContextActionDelete) self.connect(self.productContextActionDelete, SIGNAL("triggered()"), self.on_assembly_context_action_delete_triggered) self.connect(self.listWidget, SIGNAL("itemClicked(QListWidgetItem*)"), self.on_list_widget_item_clicked) self.connect(self.listWidget, SIGNAL("currentItemChanged (QListWidgetItem*,QListWidgetItem*)"), self._update_gui) self.connect(self.listWidget, SIGNAL("pdfMoved"), self._transfer_docs) self.listWidget_2.setContextMenuPolicy(Qt.NoContextMenu) self.listWidget_2.addAction(self.action_Open) self.connect(self.action_Open, SIGNAL("triggered()"), self.open_pdf) self.documentContextActionDelete = QAction(QIcon(":/delete.png"), "delete", self) self.listWidget_2.addAction(self.documentContextActionDelete) self.connect(self.documentContextActionDelete, SIGNAL("triggered()"), self.on_document_context_action_delete_activated) self.connect(self.listWidget_2, SIGNAL("itemDoubleClicked (QListWidgetItem*)"), self.on_pdf_double_clicked) self.connect(self.listWidget_2, SIGNAL("pdfAdded"), self.pdf_added) self.connect(self.listWidget_2, SIGNAL("itemSelectionChanged()"), self.on_docs_selection_changed) self.connect(self.listWidget_2, SIGNAL("removeDocs"), self.on_list_widget2_removeDocs) self.connect(self.action_New, SIGNAL("triggered()"), self.on_pbAddAssembly_clicked) self.connect(self.action_Save, SIGNAL("triggered()"), self._save_assembly_data) self.connect(self.actionPreferences, SIGNAL("triggered()"), self.on_preferences_clicked) self.input_dialog = InputDialog(parent=self) self.confirm_dialog = ConfirmDialog(self) self.settings_dialog = SettingsDialog(self) self.usage_dialog = UsageDialog(self) self.show() self.__is_first_run() QTimer.singleShot(0, self._load_assembly_data) # Fixed: bug-0004 def closeEvent(self, QCloseEvent): if self.list_is_dirty: print('saving data...') self._save_assembly_data() self.settings.setValue('open_on_add', self.actionOpen_on_Add.isChecked()) QCloseEvent.accept() def __is_first_run(self): first_run = self.settings.value('first_run', None) if first_run is None: print('first run of the program...') QMessageBox.warning(self, "PDF Viewer", "You need to select a PDF viewer before opening documents.") result = self.settings_dialog.exec_() if result == QDialog.Accepted: self.settings.setValue('first_run', 'complete') @pyqtSignature("") def on_actionUsage_triggered(self): self.usage_dialog.exec_() def on_list_widget2_removeDocs(self, docs): doc_list = self._current_doc_list() """:type doc_list : dict """ for doc in docs: del doc_list[doc] self._update_gui() # Fixed: bug-0001 def on_pbAddAssembly_clicked(self): assembly_text = self._get_input("Enter an assembly part number:") if assembly_text in self.list_manager: QMessageBox.warning(self, "- Warning -", "That assembly is already on the list.") elif assembly_text: self.list_manager[assembly_text] = {} self._add_new_assembly(assembly_text, self.listWidget) # self._update_gui() QTimer.singleShot(0, self._update_gui) self.list_is_dirty = True def on_document_context_action_delete_activated(self): if len(self.listWidget_2.selectedItems()): doc_list = self._current_doc_list() for item in self.listWidget_2.selectedItems(): row = self.listWidget_2.row(item) self.listWidget_2.takeItem(row) del doc_list[item.text()] self._update_gui() self.list_is_dirty = True def on_docs_selection_changed(self): if len(self.listWidget_2.selectedItems()): self.listWidget_2.setContextMenuPolicy(Qt.ActionsContextMenu) else: self.listWidget_2.setContextMenuPolicy(Qt.NoContextMenu) # Fixed: bug-0002 def on_list_widget_item_clicked(self, list_item): self._update_gui() def on_assembly_context_action_delete_triggered(self): if not self._ok_to_continue(): return if self.listWidget.count(): self.remove_assembly() self.list_is_dirty = True # fixed : bug-0005 def on_assembly_context_action_rename_triggered(self): new_assembly = self._get_input("Enter an assembly part number:") if new_assembly: current_assembly, doc_list = self._current_assembly_info() del self.list_manager[current_assembly.text()] self.listWidget.takeItem(self.listWidget.currentRow()) self.list_manager[new_assembly] = doc_list list_item = QListWidgetItem(QIcon(':/assembly.png'), new_assembly, self.listWidget) self.listWidget.setCurrentItem(list_item) self._update_gui() self.list_is_dirty = True def on_pdf_double_clicked(self, *args, **kwargs): print("openng '{}'".format(args[0].get_full_name())) viewer = self.settings.value('viewer/viewer') if viewer: util.open_pdf((args[0].get_full_name(),), viewer) else: QMessageBox.warning(self, '- Warning -', 'PDF Viewer not defined.') def on_preferences_clicked(self): self.settings_dialog.exec_() def open_pdf(self): selected_pdf_list_items = self.listWidget_2.selectedItems() if len(selected_pdf_list_items): pdf_files = [pdf.get_full_name() for pdf in selected_pdf_list_items] util.open_pdf(tuple(pdf_files), self.settings.value('viewer/viewer')) def pdf_added(self, pdf): assembly, doc_list = self._current_assembly_info() short_name = pdf.split('/')[-1] if short_name in doc_list.keys(): return doc_list[short_name] = {'path': pdf, 'short_name': short_name} self._update_gui() # should we open it automatically as well? if self.actionOpen_on_Add.isChecked(): util.open_pdf((pdf,), self.settings.value('viewer/viewer')) self.list_is_dirty = True def remove_assembly(self): assembly, doc_list = self._current_assembly_info() if assembly is not None: # get rid of the assembly del self.list_manager[assembly.text()] self.listWidget.takeItem(self.listWidget.currentRow()) self.listWidget_2.clear() self._update_gui() self.list_is_dirty = True def _add_new_assembly(self, assembly_text, list_widget, make_current=True): list_widget_item = QListWidgetItem(QIcon(':/assembly.png'), assembly_text, list_widget) # list_widget.addItem(list_widget_item) if make_current: list_widget.setCurrentItem(list_widget_item) def _current_assembly(self): return self.listWidget.currentItem() def _current_assembly_info(self): assembly = self._current_assembly() doc_list = self._current_doc_list() return assembly, doc_list def _current_doc_list(self): assembly = self._current_assembly() if assembly is not None: doc_list = self.list_manager[assembly.text()] return doc_list else: return None def _get_input(self, prompt): self.input_dialog.setPrompt(prompt) self.input_dialog.lineEdit.setFocus() self.input_dialog.lineEdit.selectAll() result = self.input_dialog.exec_() if result == QDialog.Accepted: return self.input_dialog.getText() else: return "" def _load_assembly_data(self): path = os.path.join(data_base_path, assembly_data_file) if os.path.isfile(path): with open(path, 'rb') as in_f: self.list_manager = pickle.load(in_f) self._populate_assembly_list(self.list_manager) def _ok_to_continue(self): result = self.confirm_dialog.exec_() if result == QDialog.Rejected: return False else: return True def _populate_assembly_list(self, assemblies): if len(assemblies): for assembly in assemblies.keys(): self._add_new_assembly(assembly, self.listWidget, False) assembly = self.listWidget.item(0) self.listWidget.setCurrentItem(assembly) def _populate_document_list(self, doc_obj_list, assembly): self.listWidget_2.clear() for key in doc_obj_list.keys(): list_item = DocListWidgetItem(QIcon(":/pdf.png"), doc_obj_list[key]['short_name'], doc_obj_list[key]['path'], self.listWidget_2) def _save_assembly_data(self): path = os.path.join(data_base_path, assembly_data_file) with open(path, 'wb') as out_f: pickle.dump(self.list_manager, out_f) def _transfer_docs(self, target_assembly, data): """ transfer documents from one assembly to another :param target_assembly: the assembly to transfer the documents to :param data: a QByteArray containing the documents in the format "file_name:path" :return: returns nothing """ print("moving docs to '{}'...".format(target_assembly)) doc_list = self.list_manager[target_assembly] stream = QDataStream(data, QIODevice.ReadOnly) doc = stream.readQString() while doc != "": file_name, path = doc.split(":") doc_list[file_name] = {'path': path, 'short_name': file_name} doc = stream.readQString() self.list_is_dirty = True def _update_gui(self): if self.listWidget.count(): self.listWidget.setContextMenuPolicy(Qt.ActionsContextMenu) if len(self.listWidget_2.selectedItems()): self.listWidget_2.setContextMenuPolicy(Qt.ActionsContextMenu) self.listWidget_2.setEnabled(True) assembly, doc_list = self._current_assembly_info() if assembly is not None: self._populate_document_list(doc_list, assembly.text()) self.label_2.setText(MainWindow.docLabelTemplate.format(assembly.text())) self.statusbar.showMessage("Doc Count = {}".format(self.listWidget_2.count())) else: self.listWidget.setContextMenuPolicy(Qt.NoContextMenu) self.listWidget_2.setContextMenuPolicy(Qt.NoContextMenu) self.listWidget_2.setEnabled(False) self.label_2.setText(MainWindow.docLabelTemplateEmpty) self.statusbar.clearMessage()