def python(script): """ Cleans a Python script. Essentially, this means changing leading spaces into tabs, to avoid indentation issues. Arguments: script -- A piece of Python script. Returns: A (msg, script) tuple, where the msg describes the changes to the text and script is the cleaned script. If no changes have been performed, msg is None. """ cleanLines = [] for l in script.split(u'\n'): indent = 0 while l.startswith(u' '): indent += 1 l = l[4:] cleanLines.append(u'\t'*indent + l) cleanScript = u'\n'.join(cleanLines) if cleanScript != script: msg = \ _(u'Convert space-based indentation to tab-based indentation?') else: msg = None return msg, cleanScript
def closeTab(self, index=None): """ desc: Closes a tab. keywords: index: A tab index (see [tabIndex]). """ index = self.tabIndex(index) if index is None: return self.removeTab(index) if self.count() == 0: self.addTab(_(u'Empty document'))
def paste(self): """ desc: Re-implements the paste method to allow modification of paste content. """ text = unicode(QtGui.QApplication.clipboard().text()) if hasattr(clean, self.lang().lower()): msg, cleanText = getattr(clean, self.lang().lower())(text) if msg != None: resp = QtGui.QMessageBox.question(self, _(u'Pasting content'), \ msg, QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) if resp == QtGui.QMessageBox.Yes: text = cleanText self.replaceSelectedText(text)
def paste(self): """ desc: Re-implements the paste method to allow modification of paste content. """ text = QtWidgets.QApplication.clipboard().text() text = self.newlines_re.sub('\n', text) if hasattr(clean, self.lang().lower()): msg, cleanText = getattr(clean, self.lang().lower())(text) if msg is not None: resp = QtWidgets.QMessageBox.question(self, _(u'Pasting content'), msg, QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No) if resp == QtWidgets.QMessageBox.Yes: text = cleanText self.replaceSelectedText(text)
def paste(self): """ desc: Re-implements the paste method to allow modification of paste content. """ text = QtWidgets.QApplication.clipboard().text() text = text.replace(os.linesep, u"\n") if hasattr(clean, self.lang().lower()): msg, cleanText = getattr(clean, self.lang().lower())(text) if msg is not None: resp = QtWidgets.QMessageBox.question( self, _(u"Pasting content"), msg, QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No ) if resp == QtWidgets.QMessageBox.Yes: text = cleanText self.replaceSelectedText(text)
def __init__(self, tabManager, msg=None, handlerButtonText=None, runButton=False): """ desc: Constructor. arguments: tabManager: desc: A tab manager. type: QTabManager keywords: msg: desc: An informative text message. type: [str, unicode, NoneType] handlerButtonText: desc: Text for a top-right button, which can be clicked to call the handler, or None for no button. type: [str, unicode, NoneType] runButton: desc: Indicates whether a run-selected-text button should be shown. type: bool """ super(QTabCornerWidget, self).__init__(tabManager) self.tabManager = tabManager # Run button if runButton: self.runButton = QtWidgets.QPushButton(QtGui.QIcon.fromTheme(u"system-run"), u"", self) self.runButton.setToolTip(_(u"Run selected text")) self.runButton.setCheckable(False) self.runButton.clicked.connect(self.tabManager.runSelectedText) # Preferences button self.prefsButton = QtWidgets.QPushButton(QtGui.QIcon.fromTheme(u"preferences-desktop"), u"", self) self.prefsButton.setCheckable(True) self.prefsButton.toggled.connect(self.tabManager.togglePrefs) # Find button self.findButton = QtWidgets.QPushButton(QtGui.QIcon.fromTheme(u"edit-find"), u"", self) self.findButton.setCheckable(True) self.findButton.toggled.connect(self.tabManager.toggleFind) # Language button (filled by update()) self.langButton = QtWidgets.QPushButton(self) self.langButton.setMenu(QLangMenu(self)) self.langShortcut = QtWidgets.QShortcut( QtGui.QKeySequence(u"Ctrl+Shift+L"), self.tabManager, context=QtCore.Qt.WidgetWithChildrenShortcut ) self.langShortcut.activated.connect(self.langButton.click) # Handler button if handlerButtonText is not None: self.handlerButton = QtWidgets.QPushButton(QtGui.QIcon.fromTheme(u"document-save"), handlerButtonText, self) self.handlerButton.clicked.connect(self.handlerButtonClicked) else: self.handlerButton = None # Editor status self.statusWidget = QEditorStatus(self) # Message if msg is not None: self.msgLabel = QtWidgets.QLabel(u"<small>%s</small>" % msg, parent=self) self.hBox = QtWidgets.QHBoxLayout(self) self.hBox.setSpacing(2) self.hBox.setContentsMargins(2, 2, 2, 2) if msg is not None: self.hBox.addWidget(self.msgLabel) self.hBox.addWidget(self.statusWidget) if runButton: self.hBox.addWidget(self.runButton) self.hBox.addWidget(self.prefsButton) self.hBox.addWidget(self.findButton) self.hBox.addWidget(self.langButton) if self.handlerButton is not None: self.hBox.addWidget(self.handlerButton) self.setLayout(self.hBox) # Set the tab order for keyboard navigation self.setTabOrder(self.prefsButton, self.findButton) self.setTabOrder(self.findButton, self.langButton) self.setTabOrder(self.langButton, self.handlerButton)
def __init__(self, tabManager, msg=None, handlerButtonText=None, runButton=False): """ desc: Constructor. arguments: tabManager: desc: A tab manager. type: QTabManager keywords: msg: desc: An informative text message. type: [str, unicode, NoneType] handlerButtonText: desc: Text for a top-right button, which can be clicked to call the handler, or None for no button. type: [str, unicode, NoneType] runButton: desc: Indicates whether a run-selected-text button should be shown. type: bool """ super(QTabCornerWidget, self).__init__(tabManager) self.tabManager = tabManager # Run button if runButton: self.runButton = QtWidgets.QPushButton( QtGui.QIcon.fromTheme(u'system-run'), u'', self) self.runButton.setToolTip(_(u'Run selected text')) self.runButton.setCheckable(False) self.runButton.clicked.connect(self.tabManager.runSelectedText) # Preferences button self.prefsButton = QtWidgets.QPushButton( QtGui.QIcon.fromTheme(u'preferences-desktop'), u'', self) self.prefsButton.setCheckable(True) self.prefsButton.toggled.connect(self.tabManager.togglePrefs) # Find button self.findButton = QtWidgets.QPushButton( QtGui.QIcon.fromTheme(u'edit-find'), u'', self) self.findButton.setCheckable(True) self.findButton.toggled.connect(self.tabManager.toggleFind) # Language button (filled by update()) self.langButton = QtWidgets.QPushButton(self) self.langButton.setMenu(QLangMenu(self)) self.langShortcut = QtWidgets.QShortcut( QtGui.QKeySequence(u'Ctrl+Shift+L'), self.tabManager, context=QtCore.Qt.WidgetWithChildrenShortcut) self.langShortcut.activated.connect(self.langButton.click) # Handler button if handlerButtonText is not None: self.handlerButton = QtWidgets.QPushButton( QtGui.QIcon.fromTheme(u'document-save'), handlerButtonText, self) self.handlerButton.clicked.connect(self.handlerButtonClicked) else: self.handlerButton = None # Editor status self.statusWidget = QEditorStatus(self) # Message if msg is not None: self.msgLabel = QtWidgets.QLabel(u'<small>%s</small>' % msg, parent= \ self) self.hBox = QtWidgets.QHBoxLayout(self) self.hBox.setSpacing(2) self.hBox.setContentsMargins(2, 2, 2, 2) if msg is not None: self.hBox.addWidget(self.msgLabel) self.hBox.addWidget(self.statusWidget) if runButton: self.hBox.addWidget(self.runButton) self.hBox.addWidget(self.prefsButton) self.hBox.addWidget(self.findButton) self.hBox.addWidget(self.langButton) if self.handlerButton is not None: self.hBox.addWidget(self.handlerButton) self.setLayout(self.hBox) # Set the tab order for keyboard navigation self.setTabOrder(self.prefsButton, self.findButton) self.setTabOrder(self.findButton, self.langButton) self.setTabOrder(self.langButton, self.handlerButton)