def save_data(self, filename=None): """Save data""" if filename is None: filename = self.filename if filename is None: filename = getcwd_or_home() filename, _selfilter = getsavefilename(self, _("Save data"), filename, iofunctions.save_filters) if filename: self.filename = filename else: return False QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.processEvents() error_message = self.shellwidget.save_namespace(self.filename) self.shellwidget._kernel_reply = None QApplication.restoreOverrideCursor() QApplication.processEvents() if error_message is not None: QMessageBox.critical(self, _("Save data"), _("<b>Unable to save current workspace</b>" "<br><br>Error message:<br>%s") % error_message) self.save_button.setEnabled(self.filename is not None)
def save_data(self, filename=None): """Save data""" if filename is None: filename = self.filename if filename is None: filename = getcwd_or_home() filename, _selfilter = getsavefilename(self, _("Save data"), filename, iofunctions.save_filters) if filename: self.filename = filename else: return False QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.processEvents() error_message = self.shellwidget.save_namespace(self.filename) self.shellwidget._kernel_reply = None QApplication.restoreOverrideCursor() QApplication.processEvents() if error_message is not None: if 'Some objects could not be saved:' in error_message: save_data_message = ( _('<b>Some objects could not be saved:</b>') + '<br><br><code>{obj_list}</code>'.format( obj_list=error_message.split(': ')[1])) else: save_data_message = _( '<b>Unable to save current workspace</b>' '<br><br>Error message:<br>') + error_message QMessageBox.critical(self, _("Save data"), save_data_message) self.save_button.setEnabled(self.filename is not None)
def eventFilter(self, obj, event): """ Filters events on this object. Params ------ object : QObject The object that is being handled. event : QEvent The event that is happening. Returns ------- bool True to stop the event from being handled further; otherwise return false. """ channel = getattr(self, 'channel', None) if is_channel_valid(channel): status = self._write_access and self._connected if event.type() == QEvent.Leave: QApplication.restoreOverrideCursor() if event.type() == QEvent.Enter and not status: QApplication.setOverrideCursor(QCursor(Qt.ForbiddenCursor)) return PyDMWidget.eventFilter(self, obj, event)
def eventFilter(self, obj, event): status = self._connected if event.type() == QEvent.Leave: QApplication.setOverrideCursor(QCursor(Qt.ArrowCursor)) elif event.type() == QEvent.Enter and not status: QApplication.setOverrideCursor(QCursor(Qt.ForbiddenCursor)) return False
def save_data(self, filename=None): """Save data""" if filename is None: filename = self.filename if filename is None: filename = getcwd() filename, _selfilter = getsavefilename(self, _("Save data"), filename, iofunctions.save_filters) if filename: self.filename = filename else: return False QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.processEvents() if self.is_internal_shell: wsfilter = self.get_internal_shell_filter('picklable', check_all=True) namespace = wsfilter(self.shellwidget.interpreter.namespace).copy() error_message = iofunctions.save(namespace, filename) else: settings = self.get_view_settings() error_message = monitor_save_globals(self._get_sock(), settings, filename) QApplication.restoreOverrideCursor() QApplication.processEvents() if error_message is not None: QMessageBox.critical(self, _("Save data"), _("<b>Unable to save current workspace</b>" "<br><br>Error message:<br>%s") % error_message) self.save_button.setEnabled(self.filename is not None)
def resize_to_contents(self): """Resize cells to contents""" QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) self.resizeColumnsToContents() self.model().fetch_more(columns=True) self.resizeColumnsToContents() QApplication.restoreOverrideCursor()
def load(self): QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) o_table = TableRowHandler(main_window=self.parent) if self.parent.clear_master_table_before_loading: TableHandler.clear_table(self.table_ui) for _row, _key in enumerate(self.json.keys()): _entry = self.json[_key] run_number = _key title = _entry['title'] chemical_formula = _entry['resolved_conflict']['chemical_formula'] # geometry = _entry['resolved_conflict']['geometry'] mass_density = _entry['resolved_conflict']['mass_density'] # sample_env_device = _entry['resolved_conflict']['sample_env_device'] o_table.insert_row(row=_row, title=title, sample_runs=run_number, sample_mass_density=mass_density, sample_chemical_formula=chemical_formula) QApplication.restoreOverrideCursor()
def wrapper(*args, **kwargs): QApplication.setOverrideCursor( QCursor(Qt.WaitCursor)) try: ret_val = func(*args, **kwargs) finally: QApplication.restoreOverrideCursor() return ret_val
def starting_long_process(self, message): """ Showing message in main window's status bar and changing mouse cursor to Qt.WaitCursor """ self.__show_message(message) QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.processEvents()
def override_cursor(self, cursor): """ Set new override cursor. :param cursor: New cursor. """ self._override_cursor = cursor QApplication.restoreOverrideCursor() if cursor is not None: QApplication.setOverrideCursor(cursor)
def from_oncat_config(self, insert_in_table=True): """using only the fields we are looking for (defined in the config.json file, this method retrieves the metadata of either the IPTS or the runs selected, and populate or not the Master table (if insert_in_table is True)""" QApplication.setOverrideCursor(QtCore.Qt.WaitCursor) self.parent.list_of_runs_not_found = [] nexus_json = self.parent.nexus_json_from_template if self.parent.ui.run_radio_button.isChecked(): # remove white space to string to make ONCat happy str_runs = str(self.parent.ui.run_number_lineedit.text()) str_runs = remove_white_spaces(str_runs) # # nexus_json = pyoncatGetNexus(oncat=self.parent.parent.oncat, # instrument=self.parent.parent.instrument['short_name'], # runs=str_runs, # facility=self.parent.parent.facility, # ) result = database_utilities.get_list_of_runs_found_and_not_found(str_runs=str_runs, oncat_result=nexus_json) list_of_runs_not_found = result['not_found'] self.parent.list_of_runs_not_found = list_of_runs_not_found self.parent.list_of_runs_found = result['found'] else: # ipts = str(self.parent.ui.ipts_combobox.currentText()) # # nexus_json = pyoncatGetRunsFromIpts(oncat=self.parent.parent.oncat, # instrument=self.parent.parent.instrument['short_name'], # ipts=ipts, # facility=self.parent.parent.facility) result = database_utilities.get_list_of_runs_found_and_not_found(oncat_result=nexus_json, check_not_found=False) self.parent.list_of_runs_not_found = result['not_found'] self.parent.list_of_runs_found = result['found'] if insert_in_table: self.parent.insert_in_master_table(nexus_json=nexus_json) else: self.isolate_metadata(nexus_json) self.parent.nexus_json = nexus_json QApplication.restoreOverrideCursor() if insert_in_table: self.parent.close()
def mouseMoveEvent(self, event): """Show Pointing Hand Cursor on error messages""" text = self.get_line_at(event.pos()) if get_error_match(text): if not self.__cursor_changed: QApplication.setOverrideCursor(QCursor(Qt.PointingHandCursor)) self.__cursor_changed = True event.accept() return if self.__cursor_changed: QApplication.restoreOverrideCursor() self.__cursor_changed = False self.QT_CLASS.mouseMoveEvent(self, event)
def eventFilter(self, widget, event): """A filter to control the zooming and panning of the figure canvas.""" # ---- Zooming if event.type() == QEvent.Wheel: modifiers = QApplication.keyboardModifiers() if modifiers == Qt.ControlModifier: if event.angleDelta().y() > 0: self.zoom_in() else: self.zoom_out() return True else: return False # ---- Panning # Set ClosedHandCursor: elif event.type() == QEvent.MouseButtonPress: if event.button() == Qt.LeftButton: QApplication.setOverrideCursor(Qt.ClosedHandCursor) self._ispanning = True self.xclick = event.globalX() self.yclick = event.globalY() # Reset Cursor: elif event.type() == QEvent.MouseButtonRelease: QApplication.restoreOverrideCursor() self._ispanning = False # Move ScrollBar: elif event.type() == QEvent.MouseMove: if self._ispanning: dx = self.xclick - event.globalX() self.xclick = event.globalX() dy = self.yclick - event.globalY() self.yclick = event.globalY() scrollBarH = self.horizontalScrollBar() scrollBarH.setValue(scrollBarH.value() + dx) scrollBarV = self.verticalScrollBar() scrollBarV.setValue(scrollBarV.value() + dy) return QWidget.eventFilter(self, widget, event)
def tab_moved(self, event): """Method called when a tab from a QTabBar has been moved.""" # If the left button isn't pressed anymore then return if not event.buttons() & Qt.LeftButton: self.to_index = None return self.to_index = self.dock_tabbar.tabAt(event.pos()) if not self.moving and self.from_index != -1 and self.to_index != -1: QApplication.setOverrideCursor(Qt.ClosedHandCursor) self.moving = True if self.to_index == -1: self.to_index = self.from_index from_index, to_index = self.from_index, self.to_index if from_index != to_index and from_index != -1 and to_index != -1: self.move_tab(from_index, to_index) self._fix_cursor(from_index, to_index) self.from_index = to_index
def mouseMoveEvent(self, event): """ Detect mouser over indicator and highlight the current scope in the editor (up and down decoration arround the foldable text when the mouse is over an indicator). :param event: event """ super(FoldingPanel, self).mouseMoveEvent(event) th = TextHelper(self.editor) line = th.line_nbr_from_position(event.pos().y()) if line >= 0: block = FoldScope.find_parent_scope( self.editor.document().findBlockByNumber(line-1)) if TextBlockHelper.is_fold_trigger(block): if self._mouse_over_line is None: # mouse enter fold scope QApplication.setOverrideCursor( QCursor(Qt.PointingHandCursor)) if self._mouse_over_line != block.blockNumber() and \ self._mouse_over_line is not None: # fold scope changed, a previous block was highlighter so # we quickly update our highlighting self._mouse_over_line = block.blockNumber() self._highlight_block(block) else: # same fold scope, request highlight self._mouse_over_line = block.blockNumber() self._highlight_runner.request_job( self._highlight_block, block) self._highight_block = block else: # no fold scope to highlight, cancel any pending requests self._highlight_runner.cancel_requests() self._mouse_over_line = None QApplication.restoreOverrideCursor() self.repaint()
def import_data(self, filenames=None): """Import data from text file""" title = _("Import data") if filenames is None: if self.filename is None: basedir = getcwd() else: basedir = osp.dirname(self.filename) filenames, _selfilter = getopenfilenames(self, title, basedir, iofunctions.load_filters) if not filenames: return elif is_text_string(filenames): filenames = [filenames] for filename in filenames: self.filename = to_text_string(filename) ext = osp.splitext(self.filename)[1].lower() if ext not in iofunctions.load_funcs: buttons = QMessageBox.Yes | QMessageBox.Cancel answer = QMessageBox.question(self, title, _("<b>Unsupported file extension '%s'</b><br><br>" "Would you like to import it anyway " "(by selecting a known file format)?" ) % ext, buttons) if answer == QMessageBox.Cancel: return formats = list(iofunctions.load_extensions.keys()) item, ok = QInputDialog.getItem(self, title, _('Open file as:'), formats, 0, False) if ok: ext = iofunctions.load_extensions[to_text_string(item)] else: return load_func = iofunctions.load_funcs[ext] # 'import_wizard' (self.setup_io) if is_text_string(load_func): # Import data with import wizard error_message = None try: text, _encoding = encoding.read(self.filename) if self.is_internal_shell: self.editor.import_from_string(text) else: base_name = osp.basename(self.filename) editor = ImportWizard(self, text, title=base_name, varname=fix_reference_name(base_name)) if editor.exec_(): var_name, clip_data = editor.get_data() monitor_set_global(self._get_sock(), var_name, clip_data) except Exception as error: error_message = str(error) else: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.processEvents() if self.is_internal_shell: namespace, error_message = load_func(self.filename) interpreter = self.shellwidget.interpreter for key in list(namespace.keys()): new_key = fix_reference_name(key, blacklist=list(interpreter.namespace.keys())) if new_key != key: namespace[new_key] = namespace.pop(key) if error_message is None: interpreter.namespace.update(namespace) else: error_message = monitor_load_globals(self._get_sock(), self.filename, ext) QApplication.restoreOverrideCursor() QApplication.processEvents() if error_message is not None: QMessageBox.critical(self, title, _("<b>Unable to load '%s'</b>" "<br><br>Error message:<br>%s" ) % (self.filename, error_message)) self.refresh_table()
def overrideCursor(self, cursor): self.restoreCursor() self._cursor = cursor QApplication.setOverrideCursor(cursor)
def mouseMoveEvent(self, _): # pylint: disable=R0201 QApplication.setOverrideCursor(Qt.ArrowCursor)
def resize_to_contents(self): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) self.dataTable.resizeColumnsToContents() self.dataModel.fetch_more(columns=True) self.dataTable.resizeColumnsToContents() QApplication.restoreOverrideCursor()
def initialize(self): """Start pydoc server""" QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.processEvents() self.start_server()
def rehighlight(self): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QSyntaxHighlighter.rehighlight(self) QApplication.restoreOverrideCursor()
def motion_notify_callback(self, event): """Override base class method""" if self.cursor is None: self.cursor = QCursor(Qt.CrossCursor) QApplication.setOverrideCursor(self.cursor)
def import_data(self, filenames=None): """Import data from text file.""" title = _("Import data") if filenames is None: if self.filename is None: basedir = getcwd_or_home() else: basedir = osp.dirname(self.filename) filenames, _selfilter = getopenfilenames(self, title, basedir, iofunctions.load_filters) if not filenames: return elif is_text_string(filenames): filenames = [filenames] for filename in filenames: self.filename = to_text_string(filename) ext = osp.splitext(self.filename)[1].lower() if ext not in iofunctions.load_funcs: buttons = QMessageBox.Yes | QMessageBox.Cancel answer = QMessageBox.question(self, title, _("<b>Unsupported file extension '%s'</b><br><br>" "Would you like to import it anyway " "(by selecting a known file format)?" ) % ext, buttons) if answer == QMessageBox.Cancel: return formats = list(iofunctions.load_extensions.keys()) item, ok = QInputDialog.getItem(self, title, _('Open file as:'), formats, 0, False) if ok: ext = iofunctions.load_extensions[to_text_string(item)] else: return load_func = iofunctions.load_funcs[ext] # 'import_wizard' (self.setup_io) if is_text_string(load_func): # Import data with import wizard error_message = None try: text, _encoding = encoding.read(self.filename) base_name = osp.basename(self.filename) editor = ImportWizard(self, text, title=base_name, varname=fix_reference_name(base_name)) if editor.exec_(): var_name, clip_data = editor.get_data() self.set_value(var_name, clip_data) except Exception as error: error_message = str(error) else: QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.processEvents() error_message = self.shellwidget.load_data(self.filename, ext) self.shellwidget._kernel_reply = None QApplication.restoreOverrideCursor() QApplication.processEvents() if error_message is not None: QMessageBox.critical(self, title, _("<b>Unable to load '%s'</b>" "<br><br>Error message:<br>%s" ) % (self.filename, error_message)) self.refresh_table()
def update_pointer(cursor=None): """Update application pointer.""" if cursor is None: QApplication.restoreOverrideCursor() else: QApplication.setOverrideCursor(cursor)
def initialize(self): """Start pydoc server""" QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) QApplication.processEvents() self.start_server()
def read_mass(self): try: if not _balance.connected: msg = _QCoreApplication.translate( '', 'Balance not connected.') title = _QCoreApplication.translate('', 'Failure') _QMessageBox.critical(self, title, msg, _QMessageBox.Ok) return False msg = _QCoreApplication.translate( '', 'Place the magnet on the balance.') title = _QCoreApplication.translate('', 'Information') reply = _QMessageBox.information( self, title, msg, _QMessageBox.Ok, _QMessageBox.Cancel) if reply == _QMessageBox.Cancel: return False _QApplication.setOverrideCursor(_Qt.WaitCursor) mass_list = [] for i in range(10): mass = _balance.read_mass(wait=0.5) if mass is not None: mass_list.append(mass) _QApplication.restoreOverrideCursor() if len(mass_list) == 0: massA = 0 else: massA = _np.mean(mass_list) self.ui.sbd_block_mass_A.setValue(massA) msg = _QCoreApplication.translate( '', 'Rotate the magnet.') title = _QCoreApplication.translate('', 'Information') reply = _QMessageBox.information( self, title, msg, _QMessageBox.Ok, _QMessageBox.Cancel) if reply == _QMessageBox.Cancel: return False _QApplication.setOverrideCursor(_Qt.WaitCursor) mass_list = [] for i in range(10): mass = _balance.read_mass(wait=0.5) if mass is not None: mass_list.append(mass) _QApplication.restoreOverrideCursor() if len(mass_list) == 0: massB = 0 else: massB = _np.mean(mass_list) self.ui.sbd_block_mass_B.setValue(massB) tol_mass = _utils.MASS_DIFF_TOLERANCE if massA != 0 and massB != 0: if _np.abs(massA - massB) > tol_mass: msg = _QCoreApplication.translate( '', 'Mass diference outside tolerance.') title = _QCoreApplication.translate('', 'Warning') _QMessageBox.warning(self, title, msg, _QMessageBox.Ok) return True except Exception: msg = _QCoreApplication.translate( '', 'Failed to read mass.') title = _QCoreApplication.translate('', 'Failure') _QMessageBox.critical(self, title, msg, _QMessageBox.Ok) _traceback.print_exc(file=_sys.stdout) return False
def resize_to_contents(self): QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) self.dataTable.resizeColumnsToContents() self.dataModel.fetch_more(columns=True) self.dataTable.resizeColumnsToContents() QApplication.restoreOverrideCursor()
def motion_notify_callback(self, event): """Override base class method""" if self.cursor is None: self.cursor = QCursor(Qt.CrossCursor) QApplication.setOverrideCursor(self.cursor)