def remove(self): st = self.settings pref = self.app.preferences.ui n = pref.filterList.currentRow() filter = self._filters[self._instances[1][n]] fst = self.filter_settings(filter.id, self._instances[0][n]) if 1 == QMessageBox.question(pref, "Removing %s ..." % filter.name, "Are you sure you want remove filter '%s' (%s)?" % \ (filter.name, filter.instance_description(fst)), 0, 1, 2): pref.filterList.takeItem(n) c = st.value('count', 0).toInt()[0] - 1 if n == c: for i in range(2): self._instances[i].pop() for i in range(n, c): self._instances[0][i] = hs = self._instances[0][i+1] self._instances[1][i] = id = self._instances[1][i+1] st.setValue("id" + str(i), id) st.setValue("hash" + str(i), hs) st.remove("id" + str(c)) st.remove("hash" + str(c)) st.setValue('count', c) if exists(fst.fileName()): fileremove(fst.fileName())
def apply(self): #validate newcachedir = unicode(self.generalCacheLocation.text()) newcachedir = newcachedir.strip() if newcachedir.endswith(os.path.sep): newcachedir = newcachedir[:-1] if not os.path.isdir(newcachedir): QMessageBox.information(self, "QOSM Error", "'%s' is not a directory" % newcachedir) return False if (not len(os.listdir(newcachedir)) == 0) and \ ((newcachedir != s._defaultsetting(s.CACHE_DIRECTORY)) and (newcachedir != s.get(s.CACHE_DIRECTORY))): if QMessageBox.Ok != QMessageBox.question(self, "Question", "Use non-emtpy directory for cache?", QMessageBox.Ok | QMessageBox.Cancel): return False if newcachedir == s._defaultsetting(s.CACHE_DIRECTORY): s.reset(s.CACHE_DIRECTORY) self.generalCacheLocation.setText(s.get(s.CACHE_DIRECTORY)) else: s.put(s.CACHE_DIRECTORY, newcachedir) self.generalCacheLocation.setText(newcachedir) if self.generalMaxTiles.value() == s._defaultsetting(s.MAX_TILES): s.reset(s.MAX_TILES) else: s.put(s.MAX_TILES, self.generalMaxTiles.value()) s.put(s.AUTODOWNLOAD, self.autoDownload.isChecked()) self.apply_customtypes() self.calculate_cachesize() return True
def _regenerateLog(self): if QMessageBox.question(self.viewer_dialog, "Regenerate log", """<P><NOBR>Do you really want to regenerate the entire</NOBR> log? This can be a time-consuming operation.</P>""", QMessageBox.Yes, QMessageBox.No) != QMessageBox.Yes: return self.purrer.save(refresh=True) self._updateViewer()
def restore_from_archive(self, parent=None): """Function to restore a DP from archived copy Asks for confirmation along the way if parent is not None (in which case it will be the parent widget for confirmation dialogs) """ from PyQt4.Qt import QMessageBox exists = os.path.exists(self.sourcepath) if parent: msg = """<P>Do you really want to restore <tt>%s</tt> from this entry's copy of <tt>%s</tt>?</P>""" % (self.sourcepath, self.filename) exists = os.path.exists(self.sourcepath) if exists: msg += """<P>Current file exists, and will be overwritten.</P>""" if QMessageBox.warning(parent, "Restoring from archive", msg, QMessageBox.Yes, QMessageBox.No) != QMessageBox.Yes: return False else: if QMessageBox.question(parent, "Restoring from archive", msg, QMessageBox.Yes, QMessageBox.No) != QMessageBox.Yes: return False busy = Purr.BusyIndicator() # remove file if in the way if exists: if os.system("/bin/rm -fr '%s'" % self.sourcepath): busy = None if parent: QMessageBox.warning(parent, "Error removing file", """<P> There was an error removing %s. Archived copy was not restored. The text console may have more information.</P>""" % self.sourcepath, QMessageBox.Ok, 0) return False # unpack archived file if self.fullpath.endswith('.tgz'): parent_dir = os.path.dirname(self.sourcepath.rstrip('/')) os.system("/bin/rm -fr %s" % self.sourcepath) if os.system("tar zxf '%s' -C '%s'" % (self.fullpath, parent_dir)): busy = None if parent: QMessageBox.warning(parent, "Error unpacking file", """<P> There was an error unpacking the archived version to %s. The text console may have more information.</P>""" % self.sourcepath, QMessageBox.Ok, 0) return False # else simply copy over else: if os.system("/bin/cp -a '%s' '%s'" % (self.fullpath, self.sourcepath)): busy = None if parent: QMessageBox.warning(parent, "Error copying file", """<P> There was an error copying the archived version to %s. The text console may have more information.</P>""" % self.sourcepath, QMessageBox.Ok, 0) return False busy = None if parent: QMessageBox.information(parent, "Restored file", """<P>Restored %s from this entry's archived copy.</P>""" % self.sourcepath, QMessageBox.Ok, 0) return True
def _regenerateLog(self): if QMessageBox.question( self.viewer_dialog, "Regenerate log", """<P><NOBR>Do you really want to regenerate the entire</NOBR> log? This can be a time-consuming operation.</P>""", QMessageBox.Yes, QMessageBox.No) != QMessageBox.Yes: return self.purrer.save(refresh=True) self._updateViewer()
def _deleteSelection(self): unselected = [src for src in self.model.sources if not src.selected] nsel = len(self.model.sources) - len(unselected) if QMessageBox.question(self, "Delete selection", """<P>Really deleted %d selected source(s)? %d unselected sources will remain in the model.</P>""" % (nsel, len(unselected)), QMessageBox.Ok | QMessageBox.Cancel, QMessageBox.Cancel) != QMessageBox.Ok: return self.model.setSources(unselected) self.showMessage("""Deleted %d sources""" % nsel) self.model.emitUpdate(SkyModel.UpdateAll, origin=self)
def _canCloseExistingModel(self): # save model if modified if self.model and self._model_updated: res = QMessageBox.question(self, "Closing sky model", "<P>Model has been modified, would you like to save the changes?</P>", QMessageBox.Save | QMessageBox.Discard | QMessageBox.Cancel, QMessageBox.Save) if res == QMessageBox.Cancel: return False elif res == QMessageBox.Save: if not self.saveFile(confirm=False, overwrite=True): return False # unload model images, unless we are already exiting anyway if not self._exiting: self.imgman.unloadModelImages() return True
def on_generalClearCache_released(self): QMessageBox.information(self, "fish", "fish") if self.generalCacheLocation.text() == s.get(s.CACHE_DIRECTORY): if QMessageBox.Ok == QMessageBox.question(self, "Question", "Clear entire cache directory? This cannot be undone.", QMessageBox.Ok | QMessageBox.Cancel): try: shutil.rmtree(s.get(s.CACHE_DIRECTORY)) os.mkdir(s.get(s.CACHE_DIRECTORY)) self.calculate_cachesize() except Exception as e: log("Error clearing cache directory: %s" % e) QMessageBox.information(self, "Error", "Error cleaning directory: %s" % e) else: QMessageBox.information(self, "Error", "Apply changes to cache directory before clearing.")
def _inputFileSelected(self, filename): if filename: try: header = pyfits.open(filename)[0].header except Exception as err: self.qerrmsg.showMessage("Error reading FITS file %s: %s" % (filename, str(err))) self.wfile_in.setFilename("") return # try to get beam extents gx, gy, grot = [header.get(x, None) for x in ('BMAJ', 'BMIN', 'BPA')] if all([x is not None for x in (gx, gy, grot)]): # if beam size is already set, ask before overwriting print([str(x.text()) for x in (self.wbmaj, self.wbmin, self.wbpa)]) if any([bool(str(x.text())) for x in (self.wbmaj, self.wbmin, self.wbpa)]) and \ QMessageBox.question(self, "Set restoring beam", "Also reset restoring beam size from this FITS file?", QMessageBox.Yes | QMessageBox.No) != QMessageBox.Yes: return self.wbmaj.setText("%.2f" % (gx * 3600)) self.wbmin.setText("%.2f" % (gy * 3600)) self.wbpa.setText("%.2f" % grot)
def removeTagsFromSelection(self): if not hasattr(self, '_remove_tag_dialog'): self._remove_tag_dialog = Widgets.SelectTagsDialog(self, modal=True, caption="Remove Tags", ok_button="Remove") # get set of all tags in selected sources tags = set() for src in self.model.sources: if src.selected: tags.update(src.getTagNames()) if not tags: return tags = list(tags) tags.sort() # show dialog self._remove_tag_dialog.setTags(tags) if self._remove_tag_dialog.exec_() != QDialog.Accepted: return tags = self._remove_tag_dialog.getSelectedTags() if not tags: return # ask for confirmation plural = (len(tags) > 1 and "s") or "" if QMessageBox.question(self, "Removing tags", "<P>Really remove the tag%s '%s' from selected sources?</P>" % ( plural, "', '".join(tags)), QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) != QMessageBox.Yes: return # remove the tags for src in self.model.sources: if src.selected: for tag in tags: src.removeAttribute(tag) # update model self.model.scanTags() self.model.initGroupings() # emit signal what = SkyModel.UpdateSourceContent + SkyModel.UpdateTags + SkyModel.UpdateSelectionOnly self.model.emitUpdate(what, origin=self)
def accept(self): """Tries to export annotations, and closes the dialog if successful.""" try: filename = self.wfile.filename() if os.path.exists(filename) and QMessageBox.question( self, "Exporting Karma annotations", "<P>Overwrite the file %s?</P>" % filename, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) != QMessageBox.Yes: return f = file(self.wfile.filename(), "wt") f.write('COORD W\nPA STANDARD\nCOLOR GREEN\nFONT hershey12\n') # source list if self.wsel.isChecked(): sources = [src for src in self.model.sources if src.selected] else: sources = self.model.sources # calculate basis size for crosses (TODO: replace min_size with something more sensible, as this value is in degrees) brightnesses = [ abs(src.brightness()) for src in sources if src.brightness() != 0 ] min_bright = brightnesses and min(brightnesses) min_size = 0.01 # loop over sources busy = BusyIndicator() for src in sources: ra = src.pos.ra / DEG dec = src.pos.dec / DEG # figure out source size if src.brightness() and min_bright: ysize = (math.log10(abs(src.brightness())) - math.log10(min_bright) + 1) * min_size else: ysize = min_size xsize = ysize / (math.cos(src.pos.dec) or 1) # figure out source style style, label = self.model.getSourcePlotStyle(src) if style: f.write('# %s\n' % src.name) # write symbol for source f.write('COLOR %s\n' % style.symbol_color) if style.symbol == "plus": f.write('CROSS %.12f %.12f %f %f\n' % (ra, dec, xsize, ysize)) elif style.symbol == "cross": f.write('CROSS %.12f %.12f %f %f 45\n' % (ra, dec, ysize, ysize)) elif style.symbol == "circle": f.write('CIRCLE %.12f %.12f %f\n' % (ra, dec, ysize)) elif style.symbol == "dot": f.write('DOT %.12f %.12f\n' % (ra, dec)) elif style.symbol == "square": f.write('CBOX %.12f %.12f %f %f\n' % (ra, dec, xsize, ysize)) elif style.symbol == "diamond": f.write('CBOX %.12f %.12f %f %f 45\n' % (ra, dec, xsize, ysize)) # write label if label: f.write('FONT hershey%d\n' % (style.label_size * 2)) f.write('COLOR %s\n' % style.label_color) f.write('TEXT %.12f %.12f %s\n' % (ra, dec, label)) f.close() except IOError as err: busy = None self.qerrmsg.showMessage( "Error writing Karma annotations file %s: %s" % (filename, str(err))) return busy = None self.parent().showMessage( "Wrote Karma annotations for %d sources to file %s" % (len(sources), filename)) return QDialog.accept(self)
def restore_from_archive(self, parent=None): """Function to restore a DP from archived copy Asks for confirmation along the way if parent is not None (in which case it will be the parent widget for confirmation dialogs) """ from PyQt4.Qt import QMessageBox exists = os.path.exists(self.sourcepath) if parent: msg = """<P>Do you really want to restore <tt>%s</tt> from this entry's copy of <tt>%s</tt>?</P>""" % (self.sourcepath, self.filename) exists = os.path.exists(self.sourcepath) if exists: msg += """<P>Current file exists, and will be overwritten.</P>""" if QMessageBox.warning(parent, "Restoring from archive", msg, QMessageBox.Yes, QMessageBox.No) != QMessageBox.Yes: return False else: if QMessageBox.question(parent, "Restoring from archive", msg, QMessageBox.Yes, QMessageBox.No) != QMessageBox.Yes: return False busy = Purr.BusyIndicator() # remove file if in the way if exists: if os.system("/bin/rm -fr '%s'" % self.sourcepath): busy = None if parent: QMessageBox.warning( parent, "Error removing file", """<P> There was an error removing %s. Archived copy was not restored. The text console may have more information.</P>""" % self.sourcepath, QMessageBox.Ok, 0) return False # unpack archived file if self.fullpath.endswith('.tgz'): parent_dir = os.path.dirname(self.sourcepath.rstrip('/')) os.system("/bin/rm -fr %s" % self.sourcepath) if os.system("tar zxf '%s' -C '%s'" % (self.fullpath, parent_dir)): busy = None if parent: QMessageBox.warning( parent, "Error unpacking file", """<P> There was an error unpacking the archived version to %s. The text console may have more information.</P>""" % self.sourcepath, QMessageBox.Ok, 0) return False # else simply copy over else: if os.system("/bin/cp -a '%s' '%s'" % (self.fullpath, self.sourcepath)): busy = None if parent: QMessageBox.warning( parent, "Error copying file", """<P> There was an error copying the archived version to %s. The text console may have more information.</P>""" % self.sourcepath, QMessageBox.Ok, 0) return False busy = None if parent: QMessageBox.information( parent, "Restored file", """<P>Restored %s from this entry's archived copy.</P>""" % self.sourcepath, QMessageBox.Ok, 0) return True
def reset_defaults(self): if QMessageBox.Ok == QMessageBox.question(self, "Question", "Reset original settings? This cannot be undone.", QMessageBox.Ok | QMessageBox.Cancel): s.reset() self.sync_dialog()
def on_tiletypeRemove_released(self): if QMessageBox.Ok == QMessageBox.question(self, "Question", "Really remove from list?", QMessageBox.Ok | QMessageBox.Cancel): row = [mi.row() for mi in self.tiletypesList.selectedIndexes()][0] del self.customtypes[row] self.tiletypesList.takeItem(row)
def accept(self): """Tries to export annotations, and closes the dialog if successful.""" try: filename = self.wfile.filename() if os.path.exists(filename) and QMessageBox.question(self, "Exporting Karma annotations", "<P>Overwrite the file %s?</P>" % filename, QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes) != QMessageBox.Yes: return f = file(self.wfile.filename(), "wt") f.write('COORD W\nPA STANDARD\nCOLOR GREEN\nFONT hershey12\n') # source list if self.wsel.isChecked(): sources = [src for src in self.model.sources if src.selected] else: sources = self.model.sources # calculate basis size for crosses (TODO: replace min_size with something more sensible, as this value is in degrees) brightnesses = [abs(src.brightness()) for src in sources if src.brightness() != 0] min_bright = brightnesses and min(brightnesses) min_size = 0.01 # loop over sources busy = BusyIndicator() for src in sources: ra = src.pos.ra / DEG dec = src.pos.dec / DEG # figure out source size if src.brightness() and min_bright: ysize = (math.log10(abs(src.brightness())) - math.log10(min_bright) + 1) * min_size else: ysize = min_size xsize = ysize / (math.cos(src.pos.dec) or 1) # figure out source style style, label = self.model.getSourcePlotStyle(src) if style: f.write('# %s\n' % src.name) # write symbol for source f.write('COLOR %s\n' % style.symbol_color) if style.symbol == "plus": f.write('CROSS %.12f %.12f %f %f\n' % (ra, dec, xsize, ysize)) elif style.symbol == "cross": f.write('CROSS %.12f %.12f %f %f 45\n' % (ra, dec, ysize, ysize)) elif style.symbol == "circle": f.write('CIRCLE %.12f %.12f %f\n' % (ra, dec, ysize)) elif style.symbol == "dot": f.write('DOT %.12f %.12f\n' % (ra, dec)) elif style.symbol == "square": f.write('CBOX %.12f %.12f %f %f\n' % (ra, dec, xsize, ysize)) elif style.symbol == "diamond": f.write('CBOX %.12f %.12f %f %f 45\n' % (ra, dec, xsize, ysize)) # write label if label: f.write('FONT hershey%d\n' % (style.label_size * 2)) f.write('COLOR %s\n' % style.label_color) f.write('TEXT %.12f %.12f %s\n' % (ra, dec, label)) f.close() except IOError as err: busy = None self.qerrmsg.showMessage("Error writing Karma annotations file %s: %s" % (filename, str(err))) return busy = None self.parent().showMessage("Wrote Karma annotations for %d sources to file %s" % (len(sources), filename)) return QDialog.accept(self)