def __init__(self, name, parent): super(MagicHelper, self).__init__(name, parent) self.data = None class MinListWidget(QtGui.QListWidget): """Temp class to overide the default QListWidget size hint in order to make MagicHelper narrow """ def sizeHint(self): s = QtCore.QSize() s.setHeight(super(MinListWidget,self).sizeHint().height()) s.setWidth(self.sizeHintForColumn(0)) return s # construct content self.frame = QtGui.QFrame() self.search_label = QtGui.QLabel("Search:") self.search_line = QtGui.QLineEdit() self.search_class = QtGui.QComboBox() self.search_list = MinListWidget() self.paste_button = QtGui.QPushButton("Paste") self.run_button = QtGui.QPushButton("Run") # layout all the widgets main_layout = QtGui.QVBoxLayout() search_layout = QtGui.QHBoxLayout() search_layout.addWidget(self.search_label) search_layout.addWidget(self.search_line, 10) main_layout.addLayout(search_layout) main_layout.addWidget(self.search_class) main_layout.addWidget(self.search_list, 10) action_layout = QtGui.QHBoxLayout() action_layout.addWidget(self.paste_button) action_layout.addWidget(self.run_button) main_layout.addLayout(action_layout) self.frame.setLayout(main_layout) self.setWidget(self.frame) # connect all the relevant signals to handlers self.visibilityChanged[bool].connect( self._update_magic_helper ) self.search_class.activated[int].connect( self.class_selected ) self.search_line.textChanged[str].connect( self.search_changed ) self.search_list.itemDoubleClicked.connect( self.paste_requested ) self.paste_button.clicked[bool].connect( self.paste_requested ) self.run_button.clicked[bool].connect( self.run_requested )
def export(self): """ Displays a dialog for exporting HTML generated by Qt's rich text system. Returns ------- The name of the file that was saved, or None if no file was saved. """ parent = self.control.window() dialog = QtGui.QFileDialog(parent, 'Save as...') dialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) filters = [ 'HTML with PNG figures (*.html *.htm)', 'XHTML with inline SVG figures (*.xhtml *.xml)' ] dialog.setNameFilters(filters) if self.filename: dialog.selectFile(self.filename) root, ext = os.path.splitext(self.filename) if ext.lower() in ('.xml', '.xhtml'): dialog.selectNameFilter(filters[-1]) if dialog.exec_(): self.filename = dialog.selectedFiles()[0] choice = dialog.selectedNameFilter() html = py3compat.cast_unicode(self.control.document().toHtml()) # Configure the exporter. if choice.startswith('XHTML'): exporter = export_xhtml else: # If there are PNGs, decide how to export them. inline = self.inline_png if inline is None and IMG_RE.search(html): dialog = QtGui.QDialog(parent) dialog.setWindowTitle('Save as...') layout = QtGui.QVBoxLayout(dialog) msg = "Exporting HTML with PNGs" info = "Would you like inline PNGs (single large html " \ "file) or external image files?" checkbox = QtGui.QCheckBox("&Don't ask again") checkbox.setShortcut('D') ib = QtGui.QPushButton("&Inline") ib.setShortcut('I') eb = QtGui.QPushButton("&External") eb.setShortcut('E') box = QtGui.QMessageBox(QtGui.QMessageBox.Question, dialog.windowTitle(), msg) box.setInformativeText(info) box.addButton(ib, QtGui.QMessageBox.NoRole) box.addButton(eb, QtGui.QMessageBox.YesRole) layout.setSpacing(0) layout.addWidget(box) layout.addWidget(checkbox) dialog.setLayout(layout) dialog.show() reply = box.exec_() dialog.hide() inline = (reply == 0) if checkbox.checkState(): # Don't ask anymore; always use this choice. self.inline_png = inline exporter = lambda h, f, i: export_html(h, f, i, inline) # Perform the export! try: return exporter(html, self.filename, self.image_tag) except Exception as e: msg = "Error exporting HTML to %s\n" % self.filename + str(e) reply = QtGui.QMessageBox.warning(parent, 'Error', msg, QtGui.QMessageBox.Ok, QtGui.QMessageBox.Ok) return None