def outputIndexAs(self): widget = QApplication.focusWidget() extensions = [] for extension, desc in EXPORT_EXTENSIONS.items(): extensions.append("{} (*{})".format(desc, extension)) with Lib.Qt.DisableUI(*self.window.widgets(), forModalDialog=True): form = QFileDialog( self.window, "Output As — {}".format(QApplication.applicationName())) form.setNameFilters(extensions) form.setAcceptMode(QFileDialog.AcceptSave) form.setDirectory(self.state.outputPath) form.selectFile(str(pathlib.Path(self.state.model.filename).stem)) if form.exec_(): filename = form.selectedFiles()[0] extension = form.selectedNameFilter() if filename: # Must have some extension if not re.match(r"^.*[.].+$", filename): if extension: filename += EXTENSION_EXTRACT_RX.sub( r"\1", extension) else: filename += ".rtf" self._outputIndex(filename, widget) Lib.restoreFocus(widget)
def open_file(parent, filter): dialog = QFileDialog() dialog.setAcceptMode(QFileDialog.AcceptOpen) dialog.setFilter(QDir.Files) dialog.setFileMode(QFileDialog.ExistingFile) dialog.setNameFilter(filter) res = dialog.exec() if res: return dialog.selectedFiles()[0]
def open_file(parent, filter): dialog = QFileDialog() dialog.setAcceptMode(QFileDialog.AcceptOpen) dialog.setFilter(QDir.Files) dialog.setFileMode(QFileDialog.ExistingFile) dialog.setNameFilter(filter) res = dialog.exec() if res: return dialog.selectedFiles()[0]
def save(self): dialog = QFileDialog(self) dialog.setFileMode(QFileDialog.AnyFile) dialog.setAcceptMode(QFileDialog.AcceptSave) dialog.setDefaultSuffix("avi") dialog.setDirectory(os.getcwd()) dialog.setNameFilter(self.tr("Video Files [.avi] (*.avi)")) if dialog.exec_(): output_file_name = dialog.selectedFiles()[0] correlate(self.clip_filename, self.audio_filename, output_file_name)
def save(self): dialog = QFileDialog(self) dialog.setFileMode(QFileDialog.AnyFile) dialog.setAcceptMode(QFileDialog.AcceptSave) dialog.setDefaultSuffix("avi") dialog.setDirectory(os.getcwd()) dialog.setNameFilter(self.tr("Video Files [.avi] (*.avi)")) if dialog.exec_(): output_file_name = dialog.selectedFiles()[0] correlate(self.clip_filename, self.audio_filename, output_file_name)
def seleccionarPY(self): dialogo = QFileDialog(None) dialogo.setFileMode(QFileDialog.AnyFile) dialogo.setNameFilter('Archivo Python(*.py)') dialogo.setAcceptMode(QFileDialog.AcceptSave) dialogo.setDefaultSuffix('py') if dialogo.exec_(): global Ruta_archivoPY Ruta_archivoPY = dialogo.selectedFiles()[0] self.rutaSalida_lineEdit.setText(Ruta_archivoPY)
def _create_control ( self, parent ): # If the caller provided a default path instead of a default directory # and filename, split the path into it directory and filename # components. if ((len( self.default_path ) != 0) and (len( self.default_directory ) == 0) and (len( self.default_filename ) == 0)): default_directory, default_filename = \ os.path.split( self.default_path ) else: default_directory = self.default_directory default_filename = self.default_filename # Convert the filter: keep = True filters = [] for f in self.wildcard.split( '|' ): if keep and f: filters.append( f ) keep = not keep # Set the default directory: if not default_directory: default_directory = QDir.currentPath() dlg = QFileDialog( parent, self.title, default_directory ) dlg.setViewMode( QFileDialog.Detail ) dlg.selectFile( default_filename ) dlg.setFilters( filters ) if self.wildcard_index < filters.count(): dlg.selectFilter( filters[ self.wildcard_index ] ) if self.action == 'open': dlg.setAcceptMode( QFileDialog.AcceptOpen ) dlg.setFileMode( QFileDialog.ExistingFile ) elif self.action == 'open files': dlg.setAcceptMode( QFileDialog.AcceptOpen ) dlg.setFileMode( QFileDialog.ExistingFiles ) else: dlg.setAcceptMode( QFileDialog.AcceptSave ) dlg.setFileMode( QFileDialog.AnyFile ) return dlg
class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle("Cobaya input generator for Cosmology") self.setGeometry(0, 0, 1500, 1000) self.move( QApplication.desktop().screen().rect().center() - self.rect().center()) self.show() # Main layout self.layout = QHBoxLayout() self.setLayout(self.layout) self.layout_left = QVBoxLayout() self.layout.addLayout(self.layout_left) self.layout_output = QVBoxLayout() self.layout.addLayout(self.layout_output) # LEFT: Options self.options = QWidget() self.layout_options = QVBoxLayout() self.options.setLayout(self.layout_options) self.options_scroll = QScrollArea() self.options_scroll.setWidget(self.options) self.options_scroll.setWidgetResizable(True) self.layout_left.addWidget(self.options_scroll) self.atoms = odict() titles = odict([ ["preset", "Presets"], ["theory", "Theory code"], ["primordial", "Primordial perturbations"], ["geometry", "Geometry"], ["hubble", "Constaint on hubble parameter"], ["baryons", "Baryon sector"], ["dark_matter", "Dark matter"], ["dark_energy", "Lambda / Dark energy"], ["neutrinos", "Neutrinos and other extra matter"], ["bbn", "BBN"], ["reionization", "Reionization history"], ["cmb_lensing", "CMB lensing"], ["cmb", "CMB experiments"], ["sampler", "Samplers"]]) for a in titles: self.atoms[a] = { "group": QGroupBox(titles[a]), "combo": QComboBox()} self.layout_options.addWidget(self.atoms[a]["group"]) self.atoms[a]["layout"] = QVBoxLayout(self.atoms[a]["group"]) self.atoms[a]["layout"].addWidget(self.atoms[a]["combo"]) self.atoms[a]["combo"].addItems( [text(k,v) for k,v in getattr(input_database, a).items()]) # Connect to refreshers -- needs to be after adding all elements for a in self.atoms: if a == "preset": self.atoms["preset"]["combo"].currentIndexChanged.connect( self.refresh_preset) continue self.atoms[a]["combo"].currentIndexChanged.connect(self.refresh) # Add Planck-naming checkbox and connect to refresher too self.planck_names = QCheckBox("Keep common names") self.atoms["theory"]["layout"].addWidget(self.planck_names) self.planck_names.stateChanged.connect(self.refresh) # RIGHT: Output + buttons self.display_tabs = QTabWidget() self.display = {} for k in ["yaml", "python"]: self.display[k] = QTextEdit() self.display[k].setLineWrapMode(QTextEdit.NoWrap) self.display[k].setFontFamily("mono") self.display[k].setCursorWidth(0) self.display[k].setReadOnly(True) self.display_tabs.addTab(self.display[k], k) self.layout_output.addWidget(self.display_tabs) # Buttons self.buttons = QHBoxLayout() self.save_button = QPushButton('Save', self) self.copy_button = QPushButton('Copy to clipboard', self) self.buttons.addWidget(self.save_button) self.buttons.addWidget(self.copy_button) self.save_button.released.connect(self.save_file) self.copy_button.released.connect(self.copy_clipb) self.layout_output.addLayout(self.buttons) self.save_dialog = QFileDialog() self.save_dialog.setFileMode(QFileDialog.AnyFile) self.save_dialog.setAcceptMode(QFileDialog.AcceptSave) # Select first preset, by default # print(self.atoms["preset"]["combo"].itemText(0)) @Slot() def refresh(self): info = create_input(planck_names=self.planck_names.isChecked(), **{ k:str(self.atoms[k]["combo"].currentText().split(_separator)[0]) for k in self.atoms if k is not "preset"}) self.refresh_display(info) @Slot() def refresh_preset(self): preset = self.atoms["preset"]["combo"].currentText().split(_separator)[0] info = create_input(preset=preset) self.refresh_display(info) # Update combo boxes to reflect the preset values, without triggering update for k,v in input_database.preset[preset].items(): if k in [input_database._desc, "derived"]: continue self.atoms[k]["combo"].blockSignals(True) self.atoms[k]["combo"].setCurrentIndex( self.atoms[k]["combo"].findText( text(v,getattr(input_database, k).get(v)))) self.atoms[k]["combo"].blockSignals(False) def refresh_display(self, info): self.display["python"].setText( "from collections import OrderedDict\n\ninfo = " + pformat(info)) self.display["yaml"].setText(yaml_dump(info)) @Slot() def save_file(self): ftype = next(k for k,w in self.display.items() if w is self.display_tabs.currentWidget()) ffilter = {"yaml": "Yaml files (*.yaml *.yml)", "python": "(*.py)"}[ftype] fsuffix = {"yaml": ".yaml", "python": ".py"}[ftype] fname, path = self.save_dialog.getSaveFileName( self.save_dialog, "Save input file", fsuffix, ffilter, os.getcwd()) if not fname.endswith(fsuffix): fname += fsuffix with open(fname, "w+") as f: f.write(self.display_tabs.currentWidget().toPlainText()) @Slot() def copy_clipb(self): self.clipboard.setText(self.display_tabs.currentWidget().toPlainText())
class MainWindow(QWidget): def __init__(self): super(MainWindow, self).__init__() self.setWindowTitle("Cobaya input generator for Cosmology") self.setGeometry(0, 0, 1500, 1000) self.move( QApplication.desktop().screenGeometry().center() - self.rect().center()) self.show() # Main layout self.layout = QHBoxLayout() self.setLayout(self.layout) self.layout_left = QVBoxLayout() self.layout.addLayout(self.layout_left) self.layout_output = QVBoxLayout() self.layout.addLayout(self.layout_output) # LEFT: Options self.options = QWidget() self.layout_options = QVBoxLayout() self.options.setLayout(self.layout_options) self.options_scroll = QScrollArea() self.options_scroll.setWidget(self.options) self.options_scroll.setWidgetResizable(True) self.layout_left.addWidget(self.options_scroll) titles = odict([ ["Presets", odict([["preset", "Presets"]])], ["Cosmological Model", odict([ ["theory", "Theory code"], ["primordial", "Primordial perturbations"], ["geometry", "Geometry"], ["hubble", "Hubble parameter constraint"], ["matter", "Matter sector"], ["neutrinos", "Neutrinos and other extra matter"], ["dark_energy", "Lambda / Dark energy"], ["bbn", "BBN"], ["reionization", "Reionization history"]])], ["Data sets", odict([ ["like_cmb", "CMB experiments"], ["like_bao", "BAO experiments"], ["like_sn", "SN experiments"], ["like_H0", "Local H0 measurements"]])], ["Sampler", odict([["sampler", "Samplers"]])]]) self.combos = odict() for group, fields in titles.items(): group_box = QGroupBox(group) self.layout_options.addWidget(group_box) group_layout = QVBoxLayout(group_box) for a, desc in fields.items(): self.combos[a] = QComboBox() if len(fields) > 1: label = QLabel(desc) group_layout.addWidget(label) group_layout.addWidget(self.combos[a]) self.combos[a].addItems( [text(k, v) for k, v in getattr(input_database, a).items()]) # PLANCK NAMES CHECKBOX TEMPORARILY DISABLED # if a == "theory": # # Add Planck-naming checkbox # self.planck_names = QCheckBox( # "Keep common parameter names " # "(useful for fast CLASS/CAMB switching)") # group_layout.addWidget(self.planck_names) # Connect to refreshers -- needs to be after adding all elements for field, combo in self.combos.items(): if field == "preset": combo.currentIndexChanged.connect(self.refresh_preset) else: combo.currentIndexChanged.connect(self.refresh) # self.planck_names.stateChanged.connect(self.refresh_keep_preset) # RIGHT: Output + buttons self.display_tabs = QTabWidget() self.display = {} for k in ["yaml", "python", "citations"]: self.display[k] = QTextEdit() self.display[k].setLineWrapMode(QTextEdit.NoWrap) self.display[k].setFontFamily("mono") self.display[k].setCursorWidth(0) self.display[k].setReadOnly(True) self.display_tabs.addTab(self.display[k], k) self.layout_output.addWidget(self.display_tabs) # Buttons self.buttons = QHBoxLayout() self.save_button = QPushButton('Save', self) self.copy_button = QPushButton('Copy to clipboard', self) self.buttons.addWidget(self.save_button) self.buttons.addWidget(self.copy_button) self.save_button.released.connect(self.save_file) self.copy_button.released.connect(self.copy_clipb) self.layout_output.addLayout(self.buttons) self.save_dialog = QFileDialog() self.save_dialog.setFileMode(QFileDialog.AnyFile) self.save_dialog.setAcceptMode(QFileDialog.AcceptSave) def create_input(self): return create_input( get_comments=True, # planck_names=self.planck_names.isChecked(), **{field: list(getattr(input_database, field).keys())[combo.currentIndex()] for field, combo in self.combos.items() if field is not "preset"}) @Slot() def refresh_keep_preset(self): self.refresh_display(self.create_input()) @Slot() def refresh(self): self.combos["preset"].blockSignals(True) self.combos["preset"].setCurrentIndex(0) self.combos["preset"].blockSignals(False) self.refresh_display(self.create_input()) @Slot() def refresh_preset(self): preset = list(getattr(input_database, "preset").keys())[ self.combos["preset"].currentIndex()] info = create_input( get_comments=True, # planck_names=self.planck_names.isChecked(), preset=preset) self.refresh_display(info) # Update combo boxes to reflect the preset values, without triggering update for k, v in input_database.preset[preset].items(): if k in [input_database._desc]: continue self.combos[k].blockSignals(True) self.combos[k].setCurrentIndex( self.combos[k].findText( text(v, getattr(input_database, k).get(v)))) self.combos[k].blockSignals(False) def refresh_display(self, info): try: comments = info.pop(input_database._comment, None) comments_text = "\n# " + "\n# ".join(comments) except (TypeError, # No comments AttributeError): # Failed to generate info (returned str instead) comments_text = "" self.display["python"].setText( "from collections import OrderedDict\n\ninfo = " + pformat(info) + comments_text) self.display["yaml"].setText(yaml_dump(info) + comments_text) self.display["citations"].setText(prettyprint_citation(citation(info))) @Slot() def save_file(self): ftype = next(k for k, w in self.display.items() if w is self.display_tabs.currentWidget()) ffilter = {"yaml": "Yaml files (*.yaml *.yml)", "python": "(*.py)", "citations": "(*.txt)"}[ftype] fsuffix = {"yaml": ".yaml", "python": ".py", "citations": ".txt"}[ftype] fname, path = self.save_dialog.getSaveFileName( self.save_dialog, "Save input file", fsuffix, ffilter, os.getcwd()) if not fname.endswith(fsuffix): fname += fsuffix with open(fname, "w+") as f: f.write(self.display_tabs.currentWidget().toPlainText()) @Slot() def copy_clipb(self): self.clipboard.setText(self.display_tabs.currentWidget().toPlainText())